<div id="cv-case-table-header">
    <div class="panel-title">
        <a href="javascript:archiveWindow('<?= url('new',['category_id'=>$category_id]); ?>','650px','520px')" class="easyui-linkbutton">+ 添加</a>
        <a id="cv-case-table-tool-delete" href="javascript:deleteAllCase();" class="easyui-linkbutton"><i class="sui-icon icon-tb-delete cv-f-14"></i> 删除全部</a>
        <a href="javascript:saveCoverCaseOrderForm();" class="easyui-linkbutton"><i class="sui-icon icon-tb-delete cv-f-14"></i> 排序</a>
    </div>
</div>
<form id="cv-case-order-form" method="post" style="width: 100%;height: 100%;">
<table id="cv-case-table" style="height: 100%;"></table>
</form>
<script type="text/javascript">
    $(function () {
        var cvCaseTable = $('#cv-case-table');
        var cvCasePageSize = parseInt((bodyHeight-137)/32);
        cvCaseTable.datagrid({
            url:'<?= url('index', ['category_id'=>$category_id]); ?>',
            columns:[[
                {field:'ck',checkbox:true,width:10},
                {field:'id',title:'ID',width:15,halign:'center'},
                {field:'order',title:'权重',width:25,halign:'center'},
                {field:'title',title:'标题',width:250,halign:'center'},
                {field:'actions',title:'可用操作',width:70,halign:'center'},
                {field:'create_time',title:'创建时间',width:70,halign:'center'},
                {field:'update_time',title:'更新时间',width:70,halign:'center'}
            ]],
            pagination: true,
            scrollbarSize:0,
            pageList: [cvCasePageSize],
            pageSize: cvCasePageSize,
            border:false,
            fitColumns: true,
            striped: true,
            header: '#cv-case-table-header',
            onCheckAll: function () {
                $('#cv-case-table-tool-delete').linkbutton('enable');
            },
            onUncheckAll: function () {
                $('#cv-case-table-tool-delete').linkbutton('disable');
            },
            onLoadSuccess: function () {
                $('#cv-case-table-tool-delete').linkbutton('disable');
            }
        });
        var cvCasePager = cvCaseTable.datagrid('getPager');
        cvCasePager.pagination({
            showPageList:false
        });
    });

    function reloadCaseListData() {
        $('#cv-case-table').datagrid('reload');
    }

    function deleteCaseData(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){
                        reloadCaseListData();
                    }
                }
            });
            layer.close(confirm);
        }, function(){
            layer.close(confirm);
        });
    }

    function deleteAllCase() {
        var confirm = layer.confirm('确认删除吗？删除后将无法恢复', {
            btn: ['删除','取消'],
            btnAlign: 'c',
            anim:4
        }, function(){
            var hasSelected = $('#cv-case-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){
                            reloadCaseListData();
                            layer.close(confirm);
                        }
                    }
                })
            }else {
                message('没有任何内容被选择')
            }
        }, function(){
            layer.close(confirm);
        });
    }

    function saveCoverCaseOrderForm(){
        $("#cv-case-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){
                    reloadCaseListData();
                }
            }
        });
    }
</script>
