{% load seahub_tags group_avatar_tags i18n %}
$(function () {
    $.ajax({
        url:'{% url 'get_contacts' %}',
        cache: false,
        dataType: 'json',
        success: function(data) {
            var share_list = [];
            var contacts = data['contacts'], contact_email, group_name, group_name_py;   
            for (var i = 0, len = contacts.length; i < len; i++) {
                contact_email = contacts[i].email;
                share_list.push({value:contact_email, label:contact_email});
            }
            {% for group in request.user.joined_groups %}
            group_name = '{{ group.group_name }}';
            group_name_py = '{{ group.group_name|char2pinyin }}'
            share_list.push({value:group_name + group_name_py, label:group_name});
            {% endfor %}

            $(".repo-share-btn").click(function() {
                var form = $("#repo-share-form"),
                    hd = $('.hd', form),
                    btn_ct = $(this).parents('td'),
                    repo_id = btn_ct.data('id'),
                    repo_name = btn_ct.attr('data-name');
                var grp_options_ct = $('#share-grp-options');
                if (!$.trim(grp_options_ct.html())) {
                    var grp_options = '<ul class="option-list">';
                    {% for group in request.user.joined_groups %}
                    grp_options += '<li> <label class="checkbox-label"> <span class="checkbox"><input type="checkbox" name="grp" value="{{ group.group_name }}" class="checkbox-orig"/></span> {% grp_avatar group.id 20 %} <span class="checkbox-option">{{ group.group_name }}</span> </label> </li>';
                    {% endfor %}
                    grp_options += '</ul>';
                    grp_options_ct.html(grp_options);
                }
                var contact_options_ct = $('#share-contact-options');
                if (!$.trim(contact_options_ct.html())) {
                    var contact_options = '<ul class="option-list">';
                    for (var i = 0, len = contacts.length; i < len; i++) {
                        contact_email = contacts[i].email;
                        contact_options += ' <li> <label class="checkbox-label"> <span class="checkbox"><input type="checkbox" name="contact" value="' + contact_email + '" class="checkbox-orig" /></span>' + contacts[i].avatar + ' <span class="checkbox-option">' + contact_email + '</span> </label> </li>';
                    }
                    contact_options += '</ul>';
                    contact_options_ct.html(contact_options);
                }

                $('.checkbox-orig', form).unbind().click(function() {
                    $(this).parent().toggleClass('checkbox-checked');
                });
                $(".checkbox-label", form).unbind().hover(
                    function() {
                        $(this).addClass('hl');
                    },  
                    function() {
                        $(this).removeClass('hl');
                    }   
                );
                form.modal({appendTo: "#main", focus:false, containerCss:{"padding": 0}});
                $('[name="repo_id"]', form).val(repo_id);
                hd.html(hd.html().replace('%(lib_name)s', '<span class="op-target">' + repo_name + '</span>'));
                $('#repo-share-tabs').tabs();
                $('#simplemodal-container').css('height', 'auto');
                addAutocomplete('#email_or_group', '#repo-share-form', share_list);
            });
        }
    });
});

    //check before post
    $('#share-submit-btn').click(function() {
        var form = $("#repo-share-form"),
            cur_tab_id = $('.ui-tabs-selected a', form).attr('href'),
            post_data = '',
            input = $('[name="email_or_group"]', form);
        switch(cur_tab_id) {
            case '#share-enter':
                post_data = input.val();
                break;
            case '#share-grp-options':
            case '#share-contact-options':
                $(cur_tab_id + ' .checkbox-checked .checkbox-orig').each(function() {
                    post_data += $(this).val() + ',';
                });
                input.val(post_data);
        }
        if (!post_data) {
            apply_form_error(form.attr('id'), "{% trans "Please enter emails or groups, or select some." %}");
            return false;
        }
        form.submit();
        disable($(this));
    });


    {% include 'snippets/repo_del_js.html' %}

    $(".download-btn").click(function() {
        var repo_id = $(this).parents('td').data('id');
        window.open('{{ SITE_ROOT }}seafile_access_check/?repo_id=' + e(repo_id));
    });

function repoCreateSuccessCallback(data) {
    $.modal.close();
    var new_repo_item = $('#new-repo .repo-item').clone(true); 
    var tds = $('td', new_repo_item);
    var repo_id = data['repo_id'], repo_name = HTMLescape(data['repo_name']);
    if (data['repo_enc']) {
        $('img', $(tds[0])).attr('src', '{{MEDIA_URL}}img/sync-folder-encrypt-20.png');
    }
    $(tds[1]).html('<a href="{{ SITE_ROOT }}repo/' + repo_id + '/">' + repo_name + '</a>');
    $(tds[2]).text(data['repo_desc']);
    $(tds[4]).attr('data-id', repo_id).attr('data-name', repo_name);
    
    var my_own_repos = $('#my-own-repos');
    var empty_tip = $('.empty-tips', my_own_repos);
    if (empty_tip.length == 1) {
        var repo_table = $('#new-repo').clone().removeAttr('id').removeClass('hide');
        $('.repo-item', repo_table).replaceWith(new_repo_item);
        empty_tip.replaceWith(repo_table);
    } else {
        $('tr:first', my_own_repos).after(new_repo_item);
    }
}
{% include "snippets/sort_lib_js.html" %}
$(function() {
    $.ajax({
        url:'{% url 'my_shared_and_group_repos' %}',
        cache: false,
        dataType: 'json',
        success: function(data) {
            var shared_ct = $('#repos-shared-to-me');
            if (data.shared.return_shared_repos) {
                $('.loading-tip', shared_ct).hide();
                $('table', shared_ct).append(data.shared.html).removeClass('hide');
            } else {
                shared_ct.html(data.shared.html); 
            }
            $('#group-repos').html(data['group']);
            $('.unshare-btn').click(function() {
                location.href = $(this).data('url');
            });
            $("tr:gt(0)", $('#repos-shared-to-me, #group-repos').find('table')).unbind().hover(
                function() {
                    $(this).addClass('hl');
                    $(this).find('.op-icon').removeClass('vh');
                },  
                function() {
                    $(this).find('.op-icon').addClass('vh');
                    $(this).removeClass('hl');
                }   
            );
            $('#tabs').unbind().tabs({cookie:{expires:1}});
        }
    });
});
