/**
 * Created by zhang on 2018/7/12.
 */

$(document).ready(function() {
    $('#add').on('show.bs.modal', function () {
        $('#compilationSels').empty();
        mainAjax.getCompilationList();
    });

    // 保存按钮
    $("#addTemplate").click(function() {
        let name = $('#name').val();
        if(name==''){
            $("#nameError").show();
            return;
        }else {
            $("#addTemplate").addClass("disabled");//防止重复提交
            $("#addTemplateForm").submit();
        }
    });

    $("#rename").click(function() {
        let libID = $("#libID").val();
        let name = $('#renameText').val();
        if(libID!=''){
            if(name ==''){
                $("#renameError").show();
                return;
            }else {
                CommonAjax.post("/billsTemplate/saveLib",{query:{ID:libID},data:{name:name}},function (data) {
                    $("#"+libID).children("a").text(data.name);
                    $("#edit").modal('hide');
                });
            }
        }
    });

    $("#delete").click(function() {
        let libID = $("#libID_del").val();
        let delCount = parseInt($("#delCount").val());
        delCount = delCount+1;
        $("#delCount").val(delCount)
        if(delCount == 3){
           if(libID!=""){
               CommonAjax.post("/billsTemplate/deleteLibByID",{ID:libID},function (data) {
                   if(data.ok){
                       $("#"+libID).parent(".libTr").remove();
                   }
                   $("#del").modal('hide');
               });
           }
        }
    });

    // 锁定、解锁
    $('body').on('click', '.lock', function () {
        lockUtil.handleLockClick($(this));
    });

    //复制库
    $('#copyTemplate').click(function () {
        const libID = $('#libID').val();
        const name = $('#copyName').val();
        if (!name) {
            $('#copyNameError').show();
            return;
        }
        $.bootstrapLoading.start();
        CommonAjax.post('/billsTemplate/copyLib', {libID, name}, function () {
            $.bootstrapLoading.end();
            window.location.reload();
        }, function () {
            $.bootstrapLoading.end();
        });
    });

    // 排序更改
    $('.sort-table').on('click', function () {
        const sortBy = $(this).data('sort');
        const sortAsc = $(this).attr('data-status');
        $(this).removeClass('fa-sort fa-sort-amount-asc fa-sort-amount-desc').addClass('fa-sort-amount-' + sortAsc);
        $(this).attr('data-status', sortAsc === 'asc' ? 'desc' : 'asc');
        let sortList = templateLibs;
        if (sortBy === 'time') {
            $('i[data-sort="name"]').removeClass('fa-sort fa-sort-amount-asc fa-sort-amount-desc').addClass('fa-sort');
            sortList = _.orderBy(templateLibs, ['createDate'], [sortAsc]);
        } else {
            $('i[data-sort="time"]').removeClass('fa-sort fa-sort-amount-asc fa-sort-amount-desc').addClass('fa-sort');
            sortList = _.orderBy(templateLibs, ['name'], [sortAsc]);
        }
        let html = '';
        for (const lib of sortList) {
            html += '<tr class="libTr">\n' +
                '                                <td id="' + lib.ID + '"><a href="/billsTemplate/editTemplate/' + lib.ID + '?locked=true">' + lib.name + '</a></td>\n' +
                '                                <td>' + lib.compilationName + '</td>\n' +
                '                                <td>' + moment(lib.createDate).format('YYYY-MM-DD') + '</td>\n' +
                '                                <td>\n' +
                '                                    <a class="lock-btn-control disabled" href="javascript:void(0);" onclick="getTemplateLib(\'' + lib.ID + '\')" title="编辑"><i class="fa fa-pencil-square-o"></i></a>\n' +
                '                                    <a class="text-danger lock-btn-control disabled" href="javascript:void(0);" onclick="showDeleteModal(\'' + lib.ID + '\')" title="删除"><i class="fa fa-remove"></i></a>\n' +
                '                                    <a class="lock" data-locked="true" href="javascript:void(0);" title="解锁"><i class="fa fa-unlock-alt"></i></a>\n' +
                '                                </td>\n' +
                '                                <td>\n' +
                '                                    <a class="btn btn-secondary btn-sm copy-data lock-btn-control disabled" href="javascript:void(0);" onclick="showCopyModal(\'' + lib.ID + '\')" title="复制数据"><i class="fa fa-clone"></i>复制</a>\n' +
                '                                </td>\n' +
                '                            </tr>'
        }
        $('#showArea').html(html);
    });
});

function getTemplateLib (ID) {
    CommonAjax.post("/billsTemplate/getLibByID",{libID:ID},function (data) {
        $("#renameText").val(data.name);
        $("#libID").val(ID);
        $("#edit").modal({show:true});
    });
}

function showDeleteModal(ID){
    $("#libID_del").val(ID);
    $("#delCount").val(0);
    $("#del").modal({show:true});
}

function showCopyModal(ID) {
    $('#libID').val(ID);
    $('#copy').modal('show');
}
