<div id="cv-kefu-table-header">
    <div class="panel-title">
        <a href="javascript:openDialog('添加客服','<?= url('new'); ?>','520px','330px')" class="easyui-linkbutton">+ 添加</a>
        <a id="cv-kefu-table-tool-delete" href="javascript:deleteAllKefu();" class="easyui-linkbutton"><i class="sui-icon icon-tb-delete cv-f-14"></i> 删除全部</a>
        <a href="javascript:saveKefuOrderForm();" class="easyui-linkbutton"><i class="sui-icon icon-tb-delete cv-f-14"></i> 排序</a>
    </div>
</div>
<form id="cv-kefu-order-form" method="post" style="width: 100%;height: 100%;">
    <table id="cv-kefu-table" style="height: 100%;"></table>
</form>
<script type="text/javascript">
    $(function () {
        var cvKefuTable = $('#cv-kefu-table');
        cvKefuTable.datagrid({
            url:'<?= url('index'); ?>',
            columns:[[
                {field:'ck',checkbox:true,width:10},
                {field:'id',title:'ID',width:15,align:'center'},
                {field:'order',title:'权重',width:20,align:'center'},
                {field:'symbol',title:'客服名称',width:100,align:'center'},
                {field:'account',title:'客服账号',width:80,align:'center'},
                {field:'kf_type',title:'客服类型',width:80,align:'center'},
                {field:'actions',title:'可用操作',width:100,halign:'center'}
            ]],
            pagination: false,
            border:false,
            fitColumns: true,
            striped: true,
            header: '#cv-kefu-table-header',
            onCheckAll: function () {
                $('#cv-kefu-table-tool-delete').linkbutton('enable');
            },
            onUncheckAll: function () {
                $('#cv-kefu-table-tool-delete').linkbutton('disable');
            },
            onLoadSuccess: function () {
                $('#cv-kefu-table-tool-delete').linkbutton('disable');
            }
        });
    });

    function reloadKefuListData() {
        $('#cv-kefu-table').datagrid('reload');
    }

    function saveKefuOrderForm(){
        $("#cv-kefu-order-form").form('submit',{
            url: '<?= url('order'); ?>',
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
                var response = JSON.parse(result);
                message(response.message);
                if (response.success){
                    reloadKefuListData();
                }
            }
        });
    }

    function deleteKefuData(url){
        var confirm = layer.confirm('确认删除吗？删除后将无法恢复', {
            btn: ['删除','取消'],
            btnAlign: 'c',
            anim:4
        }, function(){
            $.ajax({
                url: url,
                type:'POST',
                success: function (response) {
                    message(response.message);
                    if (response.success){
                        reloadKefuListData();
                    }
                }
            });
            layer.close(confirm);
        }, function(){
            layer.close(confirm);
        });
    }

    function deleteAllKefu() {
        var confirm = layer.confirm('确认删除吗？删除后将无法恢复', {
            btn: ['删除','取消'],
            btnAlign: 'c',
            anim:4
        }, function(){
            var hasSelected = $('#cv-kefu-table').datagrid('getSelections');
            var hasSelectedLength = hasSelected.length;
            if(hasSelectedLength>0){
                var rows = [];
                for(var i=0;i<hasSelectedLength;i++){
                    rows.push(hasSelected[i].id);
                }
                $.ajax({
                    url: '<?= url('deleteall'); ?>',
                    data: {'rows':rows},
                    type:'POST',
                    success: function (response) {
                        message(response.message);
                        if (response.success){
                            reloadKefuListData();
                            layer.close(confirm);
                        }
                    }
                })
            }else {
                message('没有任何内容被选择')
            }
        }, function(){
            layer.close(confirm);
        });
    }

</script>
