<div id="cv-article-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-article-table-tool-delete" href="javascript:deleteAllArticle();" class="easyui-linkbutton"><i class="sui-icon icon-tb-delete cv-f-14"></i> 删除全部</a>
        <a href="javascript:saveCoverArticleOrderForm();" class="easyui-linkbutton"><i class="sui-icon icon-tb-delete cv-f-14"></i> 排序</a>
    </div>
</div>
<form id="cv-article-order-form" method="post" style="width: 100%;height: 100%;">
<table id="cv-article-table" style="height: 100%;"></table>
</form>
<script type="text/javascript">
    $(function () {
        var cvArticleTable = $('#cv-article-table');
        var cvArticlePageSize = parseInt((bodyHeight-137)/32);
        cvArticleTable.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:'author',title:'作者',width:25,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: [cvArticlePageSize],
            pageSize: cvArticlePageSize,
            border:false,
            fitColumns: true,
            striped: true,
            header: '#cv-article-table-header',
            onCheckAll: function () {
                $('#cv-article-table-tool-delete').linkbutton('enable');
            },
            onUncheckAll: function () {
                $('#cv-article-table-tool-delete').linkbutton('disable');
            },
            onLoadSuccess: function () {
                $('#cv-article-table-tool-delete').linkbutton('disable');
            }
        });
        var cvArticlePager = cvArticleTable.datagrid('getPager');
        cvArticlePager.pagination({
            showPageList:false
        });
    });

    function reloadArtilceListData() {
        $('#cv-article-table').datagrid('reload');
    }

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

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

    function saveCoverArticleOrderForm(){
        $("#cv-article-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){
                    reloadArtilceListData();
                }
            }
        });
    }
</script>
