{% extends "admin/header.html" %}

{% load admin_tags i18n lib_tags %}

{% block extra_css %}
  {{ block.super }}
  {% extra_static_content "domains" "css" user %}
{% endblock %}

{% block leftcol %}
<ul class="nav nav-sidebar">
  {% domains_menu "domains" user False %}
</ul>

{% get_extra_admin_content user "leftcol" selection %}
{% endblock %}

{% block apparea %}
  <h2>{{ object }} {% if not object.enabled %} <small>{% trans "(disabled)" %}</small>{% endif %}</h2>
  <hr>

  <div class="col-xs-12 col-sm-6">
    <div class="panel panel-info">
      <div class="panel-heading">
        <h3 class="panel-title">{% trans "Summary" %}</h3>
      </div>
      <table class="table">
        <tr>
          <th width="40%">{% trans "Creation date" %}</th>
          <td>{{ domain.creation }}</td>
        </tr>
        <tr>
          <th>{% trans "Last modification date" %}</th>
          <td>{{ domain.last_modification }}</td>
        </tr>
        <tr>
          <th>{% trans "Domain aliases" %}</th>
          <td>{{ domain.domainalias_count }}</td>
        </tr>
        <tr>
          <th><a href="{% url 'admin:identity_list' %}#list/?searchquery=@{{ object.name }}&idtfilter=account">{% trans "Mailboxes" %}</a></th>
          <td>{{ domain.mailbox_count }}</td>
        </tr>
        <tr>
          <th><a href="{% url 'admin:identity_list' %}#list/?searchquery=@{{ object.name }}&idtfilter=alias">{% trans "Mailbox aliases" %}</a></th>
          <td>{{ domain.mbalias_count }}</td>
        </tr>
        <tr>
          <th>{% trans "Quota" %}</a></th>
          <td>{{ domain.quota }} {% trans "MB" %}</td>
        </tr>
        {% if domain.quota %}
          <tr>
            <th>{% trans "Usage" %}</a></th>
            <td>
              <div class="progress">
                <div class="{% progress_color domain.used_quota_in_percent %}" style="width: {{ domain.used_quota_in_percent }}%" title="{{ domain.used_quota_in_percent }}% ({{ domain.used_quota }} {% trans "MB" %}) {% trans 'used' context 'quota' %}"></div>
                <div class="{% progress_color domain.allocated_quota_in_percent %}" style="width: {{ domain.allocated_quota_in_percent }}%" title="{{ domain.allocated_quota_in_percent }}% ({{ domain.allocated_quota }} {% trans "MB" %}) {% trans 'allocated' %}"></div>
              </div>
            </td>
          </tr>
        {% endif %}
        <tr>
          <th>{% trans "Default mailbox quota" %}</a></th>
          <td>{{ domain.default_mailbox_quota }} {% trans "MB" %}</td>
        </tr>
        {% if domain.message_limit != None %}
          <tr>
            <th>{% trans "Message sending limit" %}</a></th>
            <td>{{ domain.message_limit }} {% trans "per day" %}</td>
          </tr>
          <tr>
            <th>{% trans "Usage" %}</a></th>
            <td>
              <div class="progress">
                <div class="{% progress_color domain.sent_messages_in_percent %}" style="width: {{ domain.sent_messages_in_percent }}%" title="{{ domain.sent_messages_in_percent }}% ({{ domain.sent_messages }}) {% trans 'used' context 'message count' %}"></div>
              </div>
            </td>
          </tr>
        {% endif %}
      </table>
    </div>

    {% if object.domainalias_set.exists %}
    <div class="panel panel-info">
      <div class="panel-heading">
        <h3 class="panel-title">{% trans "Domain aliases" %}</h3>
      </div>
      <table class="table">
        {% for domainalias in object.domainalias_set.all %}
          <tr><td>{{ domainalias }}</td></tr>
        {% endfor %}
      </table>
    </div>
    {% endif %}

    {% for template in templates.left %}
      {% include template %}
    {% endfor %}
  </div>

  <div class="col-xs-12 col-sm-6">
    <div class="panel panel-info">
      <div class="panel-heading">
        <h3 class="panel-title">
          {% trans "DNS" %}
          <a href="{% url 'dnstools:domain_dns_configuration' domain.pk %}"
             data-toggle="ajaxmodal"><span class="fa fa-info-circle"></span></a>
        </h3>
      </div>
      <table class="table">
        {% if enable_mx_checks or enable_dnsbl_checks %}
          <tr>
            <th>{% trans "Status" %}</th>
            <td>
              {% include "admin/_domain_dnschecks_status.html" %}
            </td>
          </tr>
        {% endif %}
        <tr>
          <th>{% trans "DKIM key" %}</th>
          <td>
            {% if domain.dkim_public_key %}
              <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#domain_dkim_key">
                {% trans "Show key" %}
              </button>
              <a id="refresh_dkim_key" class="btn btn-default btn-xs" href="#" title="{% trans 'Generate new key' %}"><span class="fa fa-refresh"></span></a>
            {% else %}
              {% trans "Not generated" %}
            {% endif %}
          </td>
        </tr>
      </table>
    </div>
    <div class="panel panel-info">
      <div class="panel-heading"><h3 class="panel-title">{% trans "Administrators" %}</h3></div>
      <table id="domadmins" class="table">
        <thead>
          <tr><th>{% trans "Username" %}</th><th>{% trans "Name" %}</th><th></th></tr>
        </thead>
        <tbody>
          {% for da in domain.admins %}
            <tr>
              <td>{{ da.username }}</td>
              <td>{{ da.fullname }}</td>
              <td name="actions">
                {% if perms.admin.change_domain %}
                  <a name="removeperm" href="{% url 'admin:permission_remove' %}?domid={{ domain.id }}&daid={{ da.id }}" title="{% trans 'Remove this permission' %}"><span class="fa fa-trash"></span></a>
                {% endif %}
              </td>
            </tr>
          {% empty %}
            <tr><td colspan="3">{% trans "No domain administrator defined yet." %}</td></tr>
          {% endfor %}
        </tbody>
      </table>
    </div>

    {% for template in templates.right %}
      {% include template %}
    {% endfor %}
  </div>

  {% if domain.dkim_public_key %}
    <div class="modal fade" id="domain_dkim_key" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      {% include "admin/_domain_dkim_key.html" %}
    </div>
  {% endif %}
{% endblock %}

{% block extra_js %}
  {{ block.super }}
  <script type="text/javascript">
  $(document).ready(function () {
      $('#refresh_dkim_key').click(function (evt) {
          evt.preventDefault();
          var $this = $(this);
          $.ajax({
              url: '/api/v1/domains/{{ domain.pk }}/',
              method: 'PATCH',
              data: {
                  dkim_public_key: '',
                  dkim_private_key_path: ''
              }
          }).done(function (response) {
              window.location.reload();
          });
      });
  });
  </script>
{% endblock %}
