{% extends 'admin/master.html' %}

{% macro show_user(user) -%}
  <h4>User {{ user.email }} with ID {{ user.id }}.</h4>
  <table class="table">
    <thead>
      <tr>
        <th scope="col">User ID</th>
        <th scope="col">Email</th>
        <th scope="col">Paid</th>
        <th>Subscription</th>
        <th>Created At</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>{{ user.id }}</td>
        <td>{{ user.email }}</td>
        <td>{{ "yes" if user.is_paid() else No }}</td>
        <td>{{ user.get_active_subscription() }}</td>
        <td>{{ user.created_at }}</td>
      </tr>
    </tbody>
  </table>
{%- endmacro %}
{%  macro list_mailboxes(mbox_count, mboxes) %}
  <h4>
    {{ mbox_count }} Mailboxes found.
    {% if mbox_count>10 %}Showing only the first 10.{% endif %}
  </h4>
  <table class="table">
    <thead>
      <tr>
        <th>Mailbox ID</th>
        <th>Email</th>
        <th>Verified</th>
        <th>Created At</th>
      </tr>
    </thead>
    <tbody>
      {% for mailbox in mboxes %}

        <tr>
          <td>{{ mailbox.id }}</td>
          <td>{{ mailbox.email }}</td>
          <td>{{ "Yes" if mailbox.verified else "No" }}</td>
          <td>{{ mailbox.created_at }}</td>
        </tr>
      {% endfor %}
    </tbody>
  </table>
{% endmacro %}
{% macro list_alias(alias_count, aliases) %}
  <h4>
    {{ alias_count }} Aliases found.
    {% if alias_count>10 %}Showing only the first 10.{% endif %}
  </h4>
  <table class="table">
    <thead>
      <tr>
        <th>Alias ID</th>
        <th>Email</th>
        <th>Verified</th>
        <th>Created At</th>
      </tr>
    </thead>
    <tbody>
      {% for alias in aliases %}

        <tr>
          <td>{{ alias.id }}</td>
          <td>{{ alias.email }}</td>
          <td>{{ "Yes" if alias.verified else "No" }}</td>
          <td>
            {{ alias.created_at }}
          </td>
        </tr>
      {% endfor %}
    </tbody>
  </table>
{% endmacro %}
{% macro show_deleted_alias(deleted_alias) -%}
  <h4>
    Deleted Alias {{ deleted_alias.email }} with ID {{ deleted_alias.id }}.
  </h4>
  <table class="table">
    <thead>
      <tr>
        <th scope="col">
          Deleted Alias ID
        </th>
        <th scope="col">
          Email
        </th>
        <th scope="col">
          Deleted At
        </th>
        <th scope="col">
          Reason
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          {{ deleted_alias.id }}
        </td>
        <td>
          {{ deleted_alias.email }}
        </td>
        <td>
          {{ deleted_alias.created_at }}
        </td>
        <td>
          {{ deleted_alias.reason }}
        </td>
      </tr>
    </tbody>
  </table>
{%- endmacro %}
{% macro show_domain_deleted_alias(dom_deleted_alias) -%}
  <h4>
    Domain Deleted Alias {{ dom_deleted_alias.email }} with ID {{ dom_deleted_alias.id }} for domain {{ dom_deleted_alias.domain.domain }}
  </h4>
  <table class="table">
    <thead>
      <tr>
        <th scope="col">
          Deleted Alias ID
        </th>
        <th scope="col">
          Email
        </th>
        <th scope="col">
          Domain
        </th>
        <th scope="col">
          Domain ID
        </th>
        <th scope="col">
          Domain owner user ID
        </th>
        <th scope="col">
          Domain owner user email
        </th>
        <th scope="col">
          Deleted At
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          {{ dom_deleted_alias.id }}
        </td>
        <td>
          {{ dom_deleted_alias.email }}
        </td>
        <td>
          {{ dom_deleted_alias.domain.domain }}
        </td>
        <td>
          {{ dom_deleted_alias.domain.id }}
        </td>
        <td>
          {{ dom_deleted_alias.domain.user_id }}
        </td>
        <td>
          {{ dom_deleted_alias.created_at }}
        </td>
      </tr>
    </tbody>
  </table>
  {{ show_user(data.domain_deleted_alias.domain.user) }}
{%- endmacro %}
{% block body %}

  <div class="border border-dark border-2 mt-1 mb-2 p-3">
    <form method="post">
      <div class="form-group">
        <label for="email">
          Email to search:
        </label>
        <input type="text"
               class="form-control"
               name="email"
               value="{{ email or '' }}"/>
      </div>
      <button type="submit" class="btn btn-primary">
        Submit
      </button>
    </form>
  </div>
  {% if no_match %}

    <div class="border border-dark border-2 mt-1 mb-2 p-3 alert alert-warning"
         role="alert">
      No user, alias or mailbox found for {{ email }}
    </div>
  {% endif %}
  {% if data.alias %}

    <div class="border border-dark border-2 mt-1 mb-2 p-3">
      <h3 class="mb-3">
        Found Alias {{ data.alias.email }}
      </h3>
      {{ list_alias(1,[data.alias]) }}
      {{ show_user(data.alias.user) }}
      {{ list_mailboxes(helper.mailbox_count(data.alias.user), helper.mailbox_list(data.alias.user) ) }}
    </div>
  {% endif %}
  {% if data.user %}

    <div class="border border-dark border-2 mt-1 mb-2 p-3">
      <h3 class="mb-3">
        Found User {{ data.user.email }}
      </h3>
      {{ show_user(data.user) }}
      {{ list_mailboxes(helper.mailbox_count(data.user), helper.mailbox_list(data.user) ) }}
      {{ list_alias(helper.alias_count(data.user),helper.alias_list(data.user)) }}
    </div>
  {% endif %}
  {% if data.mailbox %}

    <div class="border border-dark mt-1 mb-2 p-3">
      <h3 class="mb-3">
        Found Mailbox {{ data.mailbox.email }}
      </h3>
      {{ list_mailboxes(1, [data.mailbox] ) }}
      {{ show_user(data.mailbox.user) }}
    </div>
  {% endif %}
  {% if data.deleted_alias %}

    <div class="border border-dark mt-1 mb-2 p-3">
      <h3 class="mb-3">
        Found DeletedAlias {{ data.deleted_alias.email }}
      </h3>
      {{ show_deleted_alias(data.deleted_alias) }}
    </div>
  {% endif %}
  {% if data.domain_deleted_alias %}

    <div class="border border-dark mt-1 mb-2 p-3">
      <h3 class="mb-3">
        Found DomainDeletedAlias {{ data.domain_deleted_alias.email }}
      </h3>
      {{ show_domain_deleted_alias(data.domain_deleted_alias) }}
    </div>
  {% endif %}
{% endblock %}
