{% extends "admin_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" %}" />
<style type="text/css">
#left-panel { position:relative; }
</style>
{% endblock %}

{% block left_panel %}
<a class="go-back" title="{% trans "Back to user list" %}"  href="{% url 'sys_useradmin' %}"><span class="icon-chevron-left"></span></a>
<div class="side-info">
    <h3 class="hd">{% trans "Profile" %}</h3>
    {% avatar email 48 %}
    <dl>
        <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>

        <dt>{% trans "Self-introduction" %}</dt>
        <dd>{{ profile.intro }}</dd>
        {% endif %}

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

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

    <h3 class="hd">{% trans "Space Used" %}</h3>
    <p>{% trans "Used" %}: {{ space_usage|filesizeformat }} {% if space_quota > 0 %} / {{ space_quota|filesizeformat }} {% endif %}</p>

    {% if CALC_SHARE_USAGE %}
    <p>{% trans "Sharing" %}: {{ share_usage|filesizeformat }} {% if share_quota > 0 %} / {{ share_quota|filesizeformat }} {% endif %}</p>
    {% endif %}

    <a href="#" id="set-quota">{% trans "Set Quota" %}</a>
</div>
<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
    {% if CALC_SHARE_USAGE %}
    <br /><input type="text" name="share_quota" placeholder=" Share quota" /> MB<br />
    {% endif %}
    <p class="tip">{% trans "Tip: 0 means default limit" %}</p>
    <p class="error hide"></p>
    <input type="submit" value="{% trans "Submit" %}" class="submit" />
</form>
{% endblock %}

{% block right_panel %}
<div id="tabs" class="tab-tabs">
    <div class="hd ovhd">
        <ul class="tab-tabs-nav fleft">
            <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>
        </ul>
    </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 %}
                <td><a href="{% url 'view_common_lib_dir' repo.id '' %}">{{ repo.props.name }}</a></td>
                <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 %}
        <p>{% trans "None" %}</p>
        {% 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 %}
                <td><a href="{% url 'view_common_lib_dir' repo.id '' %}">{{ repo.props.repo_name }}</a></td>
                <td>{{ repo.props.user }}</td>
                <td>{{ repo.size|filesizeformat }}</td>
                <td>{{ repo.last_modify|translate_seahub_time }}</td>
            </tr>
            {% endfor %}
        </table>
        {% else %}
        <p>{% trans "None" %}</p>
        {% 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-icon-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" href="{% url 'sys_publink_remove' %}?t={{ link.token }}">{% trans "Remove"%}</a>
                    </td>
                {% else %}
                    <td class="alc"><img src="{{ MEDIA_URL }}img/folder-icon-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" href="{% url 'sys_upload_link_remove' %}?t={{ link.token }}">{% trans "Remove"%}</a>
                    </td>
                {% endif %}
            </tr>
            {% endfor %}
        </table>
        {% else %}
        <p>{% trans "None" %}</p>
        {% 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" %}

$('#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 };

    {% if CALC_SHARE_USAGE %}
    var share_quota = $('input[name="share_quota"]', form).val();
    if (!$.trim(share_quota)) {
        apply_form_error(form_id, "{% trans "Share Quota can't be empty" %}");
        return false;
    }
    data['share_quota'] = share_quota;
    {% endif %}

    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;
});
</script>
{% endblock %}
