{% extends "layout.html" %}

{% from "macros/form_inputs.html" import
    input_csrf_token
    with context
    %}

{% from "macros/general.html" import
        display_subnav,
        set_account_status_img,
        set_account_activity_img,
        set_forwarding_address_img,
        set_alias_address_img,
        set_assigned_groups_img,
        set_user_admin_type_img,
        highlight_username_in_mail,
        display_progress_bar,
        display_filter_by_first_char,
        show_pages,
        display_remove_mailbox_days
        with context
        %}

{% from "macros/msg_handlers.html" import user_msg_handler with context %}

{% block title %}{{ _('Mail Users') }}{% endblock %}
{% block navlinks_domains %}class="active"{% endblock %}

{% block breadcrumb %}
    {% if all_are_admins %}
        {% set crumbs = [
            (ctx.homepath + '/domains', _('All domains')),
            (ctx.homepath + '/profile/domain/general/' + cur_domain, cur_domain),
            (ctx.homepath + '/users/' + cur_domain, _('Users')),
            (ctx.homepath + '/mls/' + cur_domain, _('Mailing Lists')),
            (ctx.homepath + '/aliases/' + cur_domain, _('Aliases')),
            ('active', ctx.homepath + '/admins/' + cur_domain, _('Admins')),
            ]
        %}
    {% else %}
        {% set crumbs = [
            (ctx.homepath + '/domains', _('All domains')),
            (ctx.homepath + '/profile/domain/general/' + cur_domain, cur_domain),
            ('active', ctx.homepath + '/users/' + cur_domain, _('Users')),
            (ctx.homepath + '/mls/' + cur_domain, _('Mailing Lists')),
            (ctx.homepath + '/aliases/' + cur_domain, _('Aliases')),
            ] %}

        {% if session.get('is_global_admin') %}
            {% set crumbs = crumbs + [(ctx.homepath + '/admins/' + cur_domain, _('Admins'))] %}
        {% endif %}
    {% endif %}

    {% if backend != "pgsql" %}
        {% set crumbs = crumbs + [(ctx.homepath + '/users/' + cur_domain + '/last_logins', _('Last Logins'))] %}
    {% endif %}

    {{ display_subnav(crumbs) }}
{% endblock %}

{% block main %}

{# Show system message #}
{{ user_msg_handler(msg) }}

{% if users is defined %}

    {% if users or (not users and first_char) %}
        {% if all_are_admins %}
            {{ display_filter_by_first_char(baseurl=ctx.homepath + '/admins/' + cur_domain,
                                            first_char=first_char,
                                            account_type='admin',
                                            disabled_only=disabled_only) }}
        {% else %}
            {{ display_filter_by_first_char(baseurl=ctx.homepath + '/users/' + cur_domain,
                                            available_chars=all_first_chars,
                                            first_char=first_char,
                                            account_type='user',
                                            disabled_only=disabled_only) }}
        {% endif %}
    {% endif %}

    {# List all users. #}
    <div class="content-box">
        <div class="box-body">
            <div class="box-header clear">
                <ul class="tabs clear">
                    <li><a href="{{ctx.homepath}}/create/user/{{cur_domain}}"><i class="fa fa-plus"></i> {{ _('User') }}</a>
                    <li><a href="{{ctx.homepath}}/create/ml/{{cur_domain}}"><i class="fa fa-plus"></i> {{ _('Mailing List') }}</a>
                    <li><a href="{{ctx.homepath}}/create/alias/{{cur_domain}}"><i class="fa fa-plus"></i> {{ _('Alias') }}</a>
                </ul>

                <h2>
                    {% if all_are_admins %}
                        {{ _('Admins under domain: %s') |format(cur_domain) }}
                    {% else %}
                        {{ _('Users under domain: %s') |format(cur_domain) }}
                    {% endif %}

                    {% if total is defined and users|length > 0 %}
                        ({{ (cur_page-1) * page_size_limit + 1 }}-{{ (cur_page-1) * page_size_limit + users|length}}/{{ total }})
                    {% endif %}

                    <a href="{{ctx.homepath}}/profile/domain/general/{{cur_domain}}"><i class="fa fa-cog fa-lg" title="{{ _('Edit domain profile') }}"></i></a>
                    {{ set_account_activity_img('sent', 'domain', cur_domain, float=false) }}
                </h2>
            </div>

            <div class="box-wrapper clear">
            <div id="user_list">
                <form name="form_users" id="account_list" method="post" action="{{ctx.homepath}}/users/{{cur_domain}}/page/{{ cur_page }}">

        {# Control whether we should redirect to /admins/[domain] or /usrs/[domain] #}
        {% if all_are_admins %}
            <input type="hidden" name="redirect_to_admin_list" value="yes">
        {% endif %}

    {{ input_csrf_token() }}
    <table class="style1">
        <thead>
        <tr>
            <th class="checkbox"><input type="checkbox" class="checkbox select-all" /></th>
            <th data-sort="string-ins"><a href="{{ctx.homepath}}/users/{{cur_domain}}?order_name=name&order_by={% if order_name == 'name' and not order_by_desc %}desc{% else %}asc{% endif %}">{{ _('Display Name') }}</a></th>
            <th data-sort="string-ins">{{ _('Mail Address') }}</th>
            <th data-sort="string-ins">{{ _('User/Employee ID') }}</th>
            {#
            <th data-sort="string-ins">{{ _('Job Title') }}</th>
            #}
            {% if all_are_admins %}
                <th data-sort="int">{{ _('Quota') }}</th>
            {% else %}
                <th data-sort="int"><a href="{{ctx.homepath}}/users/{{cur_domain}}?order_name=quota&order_by={% if order_name == 'quota' and order_by_desc %}asc{% else %}desc{% endif %}">{{ _('Quota') }}</a></th>
            {% endif %}

            {% if last_logins %}
                <th data-sort="int">
                    {{ _('Last Login') }}
                    {% if backend != "pgsql" %}
                        (<a href="{{ ctx.homepath }}/users/{{ cur_domain }}/last_logins">{{ _("All users") }}</a>)
                    {% endif %}
                </th>
            {% endif %}
        </tr>
        </thead>

        <tbody>
        {% if users %}
            {% for r in users %}
                {% set mail = r.username |e %}

                <tr>
                    <td class="checkbox">
                        <input type="checkbox"
                               name="mail"
                               class="checkbox"
                               value="{{ mail }}"
                               alt="{% if r.active %}active{% else %}disabled{% endif %}"
                               {% if session.get('is_global_admin') %}
                                   {% if mail == session.get('username') %}disabled="disabled"{% endif %}
                               {% else %}
                                   {# Normal admin is NOT allowed to enable/disable/delete itself and global admin accounts. #}
                                   {% if mail == session.get('username') or r.isglobaladmin == 1 %}disabled="disabled"{% endif %}
                               {% endif %}
                               />
                    </td>
                    <td class="vcenter">
                        <a href="{{ctx.homepath}}/profile/user/general/{{mail}}">
                            <i class="fa fa-cog fa-lg fr-space" title="{{ _('Edit account profile') }}{% if r.passwordlastchange %}<br/>{{ _('Password last change:') }} {{ r.passwordlastchange | utc_to_timezone(timezone=session.get('timezone')) }}{% endif %}"></i>
                        </a>
                        {{ set_account_activity_img('sent', 'user', mail) }}
                        {{ set_account_status_img(r.active) }}

                        {% if r.isglobaladmin %}
                            {{ set_user_admin_type_img('globaladmin') }}
                        {% elif r.isadmin %}
                            {{ set_user_admin_type_img('domainadmin') }}
                        {% endif %}

                        {{ set_assigned_groups_img(addresses=user_assigned_groups.get(mail, [])) }}
                        {{ set_forwarding_address_img(user=mail, addresses=user_forwardings.get(mail, [])) }}
                        {{ set_alias_address_img(addresses=user_alias_addresses.get(mail, [])) }}

                        <a href="{{ctx.homepath}}/profile/user/general/{{mail}}" title="{{ _('Edit account profile') }}">{% if r.name %}{{ r.name |cut_string |e }}{% else %}{{ mail.split('@', 1)[0] }}{% endif %}</a>
                    </td>
                    <td class="vcenter">{{ highlight_username_in_mail(mail) }}</td>
                    <td class="vcenter">{% if r.employeeid %}{{ r.employeeid |e }}{% endif %}</td>

                    {# mail quota #}
                    {% set usedQuotaBytes = used_quotas.get(mail, {}).get('bytes', 0) %}
                    {% set usedQuotaMessages = used_quotas.get(mail, {}).get('messages', 0) %}
                    {% if r.quota == 0 %}
                        {% if session.get('show_used_quota') %}
                            <td class="vcenter" data-sort-value="0">{{ usedQuotaBytes |file_size_format }} / {{ _('Unlimited') }}</td>
                        {% else %}
                            <td class="vcenter" data-sort-value="0">{{ _('Unlimited') }}</td>
                        {% endif %}
                    {% else %}
                        {% set percent = usedQuotaBytes |convert_to_percentage(r.quota |int * 1024 * 1024) %}
                        <td class="vcenter" data-sort-value="{{ r.quota }}">
                            <div>
                                <span title="{{ _('Edit quota setting') }}"><a href="{{ctx.homepath}}/profile/user/general/{{mail}}">{{ percent }}%</a></span>
                                <span class="color-grey">(<span title="{{ _('Stored') }}">{{ usedQuotaMessages }} {{_('Emails') }} / {{ usedQuotaBytes |file_size_format }}</span>) / <span title="{{ _('Allocated') }}">{{ r.quota |file_size_format(base_mb=True) }}</span></span>
                            </div>
                            {{ display_progress_bar(percent, show_zero=true, width='60%', style='thin') }}
                        </td>
                    {% endif %}

                    {% if last_logins %}
                        {% if mail in last_logins %}
                            {% set _times = last_logins.get(mail, {}) %}

                            {% set _imap = _times.get('imap') or 0 %}
                            {% set _pop3 = _times.get('pop3') or 0 %}
                            {% set _lda = _times.get('lda') or 0 %}

                            {# imap or pop3 login time #}
                            {% set _login_time = 0 %}
                            {% set _login_service = None %}

                            {% if (_imap > 0) and (_pop3 > 0) %}
                                {% if _imap > _pop3 %}
                                    {% set _login_time = _imap %}
                                    {% set _login_service = 'imap' %}
                                {% else %}
                                    {% set _login_time = _pop3 %}
                                    {% set _login_service = 'pop3' %}
                                {% endif %}
                            {% elif _imap > 0 %}
                                {% set _login_time = _imap %}
                                {% set _login_service = 'imap' %}
                            {% elif _pop3 > 0 %}
                                {% set _login_time = _pop3 %}
                                {% set _login_service = 'pop3' %}
                            {% endif %}

                            <td data-sort-value="{{ _login_time }}">
                                {% if _login_time %}
                                    <span title="{% if _imap %}{{ _('IMAP Login:') }} {{ _imap | epoch_seconds_to_gmt | utc_to_timezone(timezone=session.get('timezone')) }}<br/>{% endif %}{% if _pop3 %}{{ _('POP3 Login:') }} {{ _pop3 | epoch_seconds_to_gmt | utc_to_timezone(timezone=session.get('timezone')) }}<br/>{% endif %}{% if _lda %}{{ _('New Mail Delivered:') }} {{ _lda | epoch_seconds_to_gmt | utc_to_timezone(timezone=session.get('timezone')) }}{% endif %}">
                                        {{ _login_time | epoch_seconds_to_gmt | utc_to_timezone(timezone=session.get('timezone')) }}

                                        {% if _login_service %}
                                            {{ _login_service | upper }}
                                        {% endif %}
                                    </span>
                                {% endif %}
                            </td>
                        {% else %}
                            <td data-sort-value="0"></td>
                        {% endif %}
                    {% endif %}
                </tr>
            {% endfor %}
        {% else %}
            <tr>
                <td class="checkbox"></td>

                {% if all_are_admins %}
                    <td colspan="4">{{ _('No mail admin assigned.') }}</td>
                {% else %}
                    <td colspan="4">{{ _('No mail user available.') }}
                        {% if not (first_char or disabled_only) %}
                            <a href="{{ctx.homepath}}/create/user/{{ cur_domain }}">{{ _('Add one') }}</a>?
                        {% endif %}
                    </td>
                {% endif %}
            </tr>
        {% endif %}
        </tbody>
    </table>

    <input type="hidden" name="cur_page" value="{{cur_page}}" />

    {% if users %}
    <div class="tab-footer clear f1">
        <div class="fl">
            <select name="action" class="fl-space" id="account_list_actions" >
                <option>{{ _('Choose Action') }}</option>
                <option disabled>----</option>
                <option value="enable">{{ _('Enable') }}</option>
                <option value="disable">{{ _('Disable') }}</option>
                <option value="delete">{{ _('Delete') }}</option>

                <option disabled>----</option>
                <option value="markasadmin">{{ _('Mark as domain admin') }}</option>
                <option value="unmarkasadmin">{{ _('Unmark as domain admin') }}</option>

                {% if session.get('is_global_admin') %}
                    <option disabled>----</option>
                    <option value="markasglobaladmin">{{ _('Mark as global admin') }}</option>
                    <option value="unmarkasglobaladmin">{{ _('Unmark as global admin') }}</option>
                {% endif %}
            </select>

            <select name="keep_mailbox_days" class="fl-space" id="account_delete_date">
                {% for _day in days_to_keep_removed_mailbox %}
                    <option value="{{ _day }}">{{ display_remove_mailbox_days(_day) }}</option>
                {% endfor %}
            </select>

            <input type="submit" name="submit_users" value="{{ _('Apply') }}" class="button fl-space" />
        </div>

        {% if all_are_admins %}
            {% set baseurl = ctx.homepath + '/admins/' + cur_domain %}
        {% else %}
            {% set baseurl = ctx.homepath + '/users/' + cur_domain %}
        {% endif %}

        {% if disabled_only %}
            {% set baseurl = baseurl + '/disabled' %}
        {% endif %}

        {% if all_are_admins %}
            {{ show_pages(baseurl=baseurl,
                          total=total,
                          cur_page=cur_page) }}
        {% else %}
            {{ show_pages(baseurl=baseurl,
                          total=total,
                          cur_page=cur_page,
                          url_suffix=ctx.query) }}
        {% endif %}
    </div>
    {% endif %}
    </form>

</div>{# #user_list #}
    </div>{# .box-wrapper #}
</div>{# -- End id=user_list -- #}
</div>
{% endif %}
{% endblock main %}
