{% extends 'home_base.html' %}
{% load seahub_tags i18n %}
{% load url from future %}

{% block sub_title %}{% trans "Folders - Share" %} - {% endblock %}

{% block cur_share_folders %}tab-cur{% endblock %}

{% block right_panel %}
<h3 class="hd">{% trans "Folders" %}</h3>
{% if shared_folders %}
<table>
    <tr>
        <th width="4%"><!--icon--></th>
        <th width="33%">{% trans "Name"%}</th>
        <th width="30%">{% trans "Share To"%}</th>
        <th width="27%">{% trans "Permission"%}</th>
        <th width="6%"></th>
    </tr>
    {% for repo in shared_folders %}
    <tr data="{{repo.props.share_type}}">
        <td><img src="{{MEDIA_URL}}img/folder-icon-24.png" title="{% trans "Read-Write"%}" alt="{% trans "directory icon" %}" /></td>
        <td data="{{ repo.props.repo_id }}"><a href="{% url 'view_common_lib_dir' repo.origin_repo_id repo.origin_path|urlencode|strip_slash %}">{{ repo.props.repo_name }}</a></td>
        <td data="{{repo.props.user_info}}" title="{{ repo.props.user }}">{{ repo.props.user | email2nickname }}</td>
        <td>
            <div class="share-permission">
                <span class="share-permission-cur-value">{{ repo.share_permission }}</span>
                <img src="{{MEDIA_URL}}img/edit_12.png" alt="{% trans "Edit"%}" title="{% trans "Edit"%}" class="share-permission-edit-icon op-icon vh" />
            </div>
            <select name="permission" class="share-permission-select hide">
                <option value="{{ repo.props.permission }}" selected="selected">{{ repo.share_permission }}</option>
                {% if repo.props.permission == 'rw' %}
                <option value="r">{% trans "Read-Only"%}</option>
                {% else %}
                <option value="rw">{% trans "Read-Write"%}</option>
                {% endif %} 
            </select>
        </td>
        <td>
            {% if repo.props.share_type == 'group' %}
            <a href="{% url 'repo_remove_share' %}?repo_id={{ repo.props.repo_id }}&from={{ request.user.username|urlencode }}&gid={{ repo.props.group_id }}&permission={{ repo.props.permission }}" class="op-icon vh" title="{% trans "Unshare"%}"><img src="{{MEDIA_URL}}img/rm.png" alt="" /></a>
            {% endif %}
            {% if repo.props.share_type == 'personal' %}
            <a href="{% url 'repo_remove_share' %}?repo_id={{ repo.props.repo_id }}&from={{ request.user.username|urlencode }}&to={{ repo.props.user|urlencode }}&permission={{ repo.props.permission }}" class="op-icon vh" title="{% trans "Unshare"%}"><img src="{{MEDIA_URL}}img/rm.png" alt="" /></a>
            {% endif %}
            {% if repo.props.share_type == 'public' %}
            {% if not org %}
            <a href="{% url 'unsetinnerpub' repo.repo_id %}?permission={{ repo.props.permission }}" class="op-icon vh" title="{% trans "Unshare"%}"><img src="{{MEDIA_URL}}img/rm.png" alt="" /></a>
            {% else %}
            <a href="{{ SITE_ROOT }}organizations/{{ org.url_prefix }}/innerpubrepo/unset/{{ repo.props.repo_id }}" class="op-icon vh" title="{% trans "Unshare" %}"><img src="{{MEDIA_URL}}img/rm.png" alt="" /></a>
            {% endif %}
            {% endif %}
        </td>
    </tr>
    {% endfor %}
</table>
{% else %}
<div class="empty-tips">
    <h2 class="alc">{% trans "You have not shared any folder" %}</h2>
    <p>{% trans "You can share a single folder with a registered user if you don't want to share a whole library." %}</p>
</div>
{% endif %}
{% endblock %}

{% block extra_script %}{{block.super}}
<script type="text/javascript">
{% if shared_folders %}
$('.share-permission-edit-icon').click(function() {
    $(this).parent().addClass('hide');
    $(this).parent().next().removeClass('hide'); // show 'share-permission-select'
});
$('.share-permission-select').change(function() {
    var select = $(this),
        select_cont = $(this).parent(),
        user = select_cont.prev(),
        share_type = select_cont.parent().attr('data');
    var url = '{% url 'share_permission_admin' %}?share_type=' + share_type;

    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        cache: false,
        beforeSend: prepareCSRFToken,
        data: {
            repo_id: user.prev().attr('data'),
            email_or_group: user.attr('data'),
            permission: select.val()
        },
        success: function(data) {
            if (data['success']) {
                feedback("{% trans "Edit succeeded" %}", 'success');
                location.reload(true);
            }
        },
        error: function() {
            feedback("{% trans "Edit failed." %}", 'error');
            select.addClass('hide');
            select.prev().removeClass('hide');
        }   
    });
});
// select shows, but the user doesn't select a value, or doesn't change the permission, click other place to hide the select
$(document).click(function(e) {
    var target = e.target || event.srcElement;
    // target can't be edit-icon
    if (!$('.share-permission-edit-icon, .share-permission-select').is(target)) {
        $('.share-permission').removeClass('hide');
        $('.share-permission-select').addClass('hide');
    }
});
{% endif %}                     // shared_repos

</script>
{% endblock %}
