{% extends "sysadmin/base.html" %}
{% load i18n avatar_tags seahub_tags %}
{% load i18n staticfiles %}
{% load url from future %}

{% block extra_style %}
<link rel="stylesheet" type="text/css" href="{% static "css/select2-3.5.2.css" %}" />
<link rel="stylesheet" type="text/css" href="{% static "css/select2.custom.css" %}" />
{% endblock %}


{% block right_panel %}

<p class="path-bar">
    <a class="normal" href="{{ SITE_ROOT }}sys/useradmin/">Users</a>
    <span class="path-split">/</span>
    {{ email }}
</p>

<div id="tabs" class="tab-tabs">
    <div class="hd header-bar ovhd">
        <ul class="tab-tabs-nav fleft">
            <li class="tab"><a href="#profile" class="a">{% trans "Profile" %}</a></li>
            <li class="tab"><a href="#owned" class="a">{% trans "Owned Libs" %}</a></li>
            <li class="tab"><a href="#shared" class="a">{% trans "Shared Libs" %}</a></li>
            <li class="tab"><a href="#shared-links" class="a">{% trans "Shared Links" %}</a></li>
            <li class="tab"><a href="#user-admin-groups" class="a">{% trans "Groups" %}</a></li>
        </ul>
    </div>

    <div id="profile">
        <dl>
            <dt>{% trans "Avatar" %}</dt>
            <dd>{% avatar email 48 %}</dd>

            <dt>{% trans "Email" %}</dt>
            <dd>{{ email }}</dd>

            {% if org_name %}
            <dt>{% trans "Organization" %}</dt>
            <dd>{{ org_name }}</dd>
            {%  endif %}

            {% if profile %}
            <dt>{% trans "Name" context "true name" %}</dt>
            <dd>{{ profile.nickname }}</dd>
            {% endif %}

            {% if d_profile %}
            <dt>{% trans "Department" %}</dt>
            <dd>{{ d_profile.department }}</dd>

            <dt>{% trans "Telephone" %}</dt>
            <dd>{{ d_profile.telephone }}</dd>
            {% endif %}

            <dt>{% trans "Space Used" %}</dt>
            <dd>{{ space_usage|seahub_filesizeformat }} {% if space_quota > 0 %} / {{ space_quota|seahub_filesizeformat }} {% endif %}  <a href="#" class="sf-btn-link" style="margin-left:20px;" id="set-quota">{% trans "Set Quota" %}</a></dd>
        </dl>

        <form id="set-quota-form" method="post" class="hide">{% csrf_token %}
            <h3>{% trans "Set user storage limit" %}</h3>
            <input type="hidden" name="email" value="{{ email }}" />
            <input type="text" name="space_quota" /> MB
            <p class="tip">{% trans "Tip: 0 means default limit" %}</p>
            <p class="error hide"></p>
            <input type="submit" value="{% trans "Submit" %}" class="submit" />
        </form>
    </div>

    <div id="owned">
        {% if owned_repos %}
        <table class="repo-list">
            <tr>
                <th width="4%"><!--icon--></th>
                <th width="35%">{% trans "Name" %}</th>
                <th width="16%">{% trans "Size"%}</th>
                <th width="25%">{% trans "Last Update"%}</th>
                <th width="20%">{% trans "Operations" %}</th>
            </tr>

            {% for repo in owned_repos %}
            <tr>
                {% if repo.encrypted %}
                <td><img src="{{MEDIA_URL}}img/sync-folder-encrypt-20.png" title="{% trans "Encrypted"%}" alt="{% trans "library icon" %}" /></td>
                {% else %}
                <td><img src="{{MEDIA_URL}}img/sync-folder-20.png?t=1387267140" title="{% trans "Read-Write" %}" alt="{% trans "library icon" %}" /></td>
                {% endif %}

                {% if not repo.name %}
                    <td>Broken ({{repo.id}})</td>
                {% else %}
                    {% if repo.encrypted %}
                        <td>{{ repo.name }}</td>
                    {% elif enable_sys_admin_view_repo %}
                        <td><a href="{% url 'sys_admin_repo' repo.id %}">{{ repo.name }}</a></td>
                    {% else %}
                        <td>{{ repo.name }}</td>
                    {% endif %}
                {% endif %}

                <td>{{ repo.size|filesizeformat }}</td>
                <td>{{ repo.last_modify|translate_seahub_time }}</td>
                <td data-id="{{ repo.props.id }}" data-name="{{repo.name}}">
                    <div>
                        <a href="#" data-url="{% url "sys_repo_delete" repo.id %}" data-target="{{ repo.name }}" class="repo-delete-btn op vh">{% trans "Delete" %}</a>
                        <a href="#" class="repo-transfer-btn op vh">{% trans "Transfer" %}</a>
                    </div>
                </td>
            </tr>
            {% endfor %}
        </table>
        {% else %}
        <div class="empty-tips">
            <h2 class="alc">{% trans "This user has not created any libraries" %}</h2>
        </div>
        {% endif %}
    </div>

    <div id="shared">
        {% if in_repos %}
        <table>
            <tr>
                <th width="4%"><!--icon--></th>
                <th width="30%">{% trans "Name" %}</th>
                <th width="26%">{% trans "Share From" %}</th>
                <th width="15%">{% trans "Size"%}</th>
                <th width="25%">{% trans "Last Update"%}</th>
            </tr>

            {% for repo in in_repos %}
            <tr>
                {% if repo.encrypted %}
                <td><img src="{{MEDIA_URL}}img/sync-folder-encrypt-20.png" title="{% trans "Encrypted"%}" alt="{% trans "library icon" %}" /></td>
                {% elif repo.permission == 'r' %}
                <td><img src="{{MEDIA_URL}}img/folder-no-write-20.png" title="{% trans "Read-Only"%}" alt="{% trans "library icon" %}" /></td>
                {% else %}
                <td><img src="{{MEDIA_URL}}img/sync-folder-20.png?t=1387267140" title="{% trans "Read-Write" %}" alt="{% trans "library icon" %}" /></td>
                {% endif %}
                {% if enable_sys_admin_view_repo %}
                <td><a href="{% url 'sys_admin_repo' repo.id %}">{{ repo.name }}</a></td>
                {% else %}
                <td>{{ repo.name }}</td>
                {% endif %}
                <td>{{ repo.props.user }}</td>
                <td>{{ repo.size|filesizeformat }}</td>
                <td>{{ repo.last_modify|translate_seahub_time }}</td>
            </tr>
            {% endfor %}
        </table>
        {% else %}
        <div class="empty-tips">
            <h2 class="alc">{% trans "This user has no shared libraries" %}</h2>
        </div>
        {% endif %}
    </div>

    <div id="shared-links">
        {% if user_shared_links%}
        <table class="sharelink-list">
            <tr>
                <th width="5%"><!--icon--></th>
                <th width="30%">{% trans "Name"%}</th>
                <th width="20%">{% trans "Size"%}</th>
                <th width="20%">{% trans "Type"%}</th>
                <th width="10%">{% trans "Visits"%}</th>
                <th width="15%">{% trans "Operations"%}</th>
            </tr>
            {% for link in user_shared_links %}
            <tr>
                {% if link.is_download %}
                    {% if link.is_file_share_link %}
                    <td class="alc"><img src="{{ MEDIA_URL }}img/file/{{ link.filename|file_icon_filter }}" alt="{% trans "File"%}" /></td>
                    <td>{{ link.filename }}</td>
                    <td>{{ link.file_size|filesizeformat}}</td>
                    {% else %}
                    <td class="alc"><img src="{{ MEDIA_URL }}img/folder-24.png" alt="{% trans "Directory icon"%}" /></td>
                    <td>{{ link.filename }}</td>
                    <td>{{ link.dir_size|filesizeformat}}</td>
                    {% endif %}
                    <td>{% trans "Download" %}</td>
                    <td>{{ link.view_cnt }}</td>
                    <td>
                        <a class="op vh rm-link" href="#" data-url="{% url 'sys_publink_remove' %}" data-token="{{ link.token }}">{% trans "Remove"%}</a>
                    </td>
                {% else %}
                    <td class="alc"><img src="{{ MEDIA_URL }}img/folder-24.png" alt="{% trans "Directory icon"%}" /></td>
                    <td>{{ link.dir_name }}</td>
                    <td>--</td>
                    <td>{% trans "Upload" %}</td>
                    <td>{{ link.view_cnt }}</td>
                    <td>
                        <a class="op vh rm-link" href="#" data-url="{% url 'sys_upload_link_remove' %}" data-token="{{ link.token }}">{% trans "Remove"%}</a>
                    </td>
                {% endif %}
            </tr>
            {% endfor %}
        </table>
        {% else %}
        <div class="empty-tips">
            <h2 class="alc">{% trans "This user has not created any shared links" %}</h2>
        </div>
        {% endif %}
    </div>

    <div id="user-admin-groups">
        {% if personal_groups %}
        <table>
            <tr>
                <th width="30%">{% trans "Name" %}</th>
                <th width="30%">{% trans "Role" %}</th>
                <th width="25%">{% trans "Create At" %}</th>
                <th width="15%">{% trans "Operations" %}</th>
            </tr>
            {% for group in personal_groups %}
                <tr>
                    <td><a href="{% url 'sys_admin_group_info' group.id %}">{{ group.group_name }}</a></td>
                    <td>{{ group.role }}</td>
                    <td>{{ group.timestamp|tsstr_sec }}</td>
                    <td><a href="#" data-url="{% url 'group_remove' group.id %}" data-target="{{ group.group_name }}" class="rm-grp op vh">{% trans "Delete" %}</a></td>
                </tr>
            {% endfor %}
        </table>
        {% else %}
        <div class="empty-tips">
            <h2 class="alc">{% trans "This user has not created or joined any groups" %}</h2>
        </div>
        {% endif %}
    </div>
</div>

{% include "sysadmin/repo_transfer_form.html" %}
{% endblock %}


{% block extra_script %}
<script type="text/javascript" src="{% static "scripts/lib/select2-3.5.2.js" %}"></script>
<script type="text/javascript">
{% include "sysadmin/repoadmin_js.html" %}

$('.rm-link').click(function() {
    var _this = $(this);
    $.ajax({
        url: _this.attr('data-url'),
        type: 'POST',
        data: {'t': _this.attr('data-token')},
        cache: false,
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        success: function() {
            _this.closest('tr').remove();
        },
        error: ajaxErrorHandler
    });
    return false;
});

$('#set-quota').click(function() {
    $("#set-quota-form").modal({appendTo: "#main"});
    return false;
});

$('#set-quota-form .submit').click(function() {
    var form = $('#set-quota-form'),
        form_id = form.attr('id'),
        space_quota = $('input[name="space_quota"]', form).val();

    if (!$.trim(space_quota)) {
        apply_form_error(form_id, "{% trans "Space Quota can't be empty" %}");
        return false;
    }

    data = { 'email': $('input[name="email"]', form).val(), 'space_quota': space_quota };

    var sb_btn = $(this);
    disable(sb_btn);
    $.ajax({
        url: '{% url 'user_set_quota' email %}',
        type: 'POST',
        dataType: 'json',
        cache: false,
        beforeSend: prepareCSRFToken,
        data: data,
        success: function(data) {
            location.reload(true);
        },
        error: function(xhr, textStatus, errorThrown) {
            if (xhr.responseText) {
                apply_form_error(form_id, $.parseJSON(xhr.responseText).error);
            } else {
                apply_form_error(form_id, "{% trans "Failed. Please check the network." %}");
            }
            enable(sb_btn);
        }
    });
    return false;
});
addConfirmTo($('.rm-grp'), {
    'title': "{% trans "Delete Group" %}",
    'con': "{% trans "Are you sure you want to delete %s ?" %}",
    'post': true
});
</script>
{% endblock %}
