<div id="cv-friend-table-header">
    <div class="panel-title">
        <a href="javascript:openDialog('添加友链','<?= url('new'); ?>','520px','330px')" class="easyui-linkbutton">+ 添加</a>
        <a id="cv-friend-table-tool-delete" href="javascript:deleteAllFriend();" class="easyui-linkbutton"><i class="sui-icon icon-tb-delete cv-f-14"></i> 删除全部</a>
        <a href="javascript:saveCoverFriendOrderForm();" class="easyui-linkbutton"><i class="sui-icon icon-tb-delete cv-f-14"></i> 排序</a>
    </div>
</div>
<form id="cv-friend-order-form" method="post" style="width: 100%;height: 100%;">
<table id="cv-friend-table" style="height: 100%;"></table>
</form>
    <script type="text/javascript">
    $(function () {
        var cvFriendTable = $('#cv-friend-table');
        cvFriendTable.datagrid({
            url:'<?= url('index'); ?>',
            columns:[[
                {field:'ck',checkbox:true,width:10},
                {field:'id',title:'ID',width:5,align:'center'},
                {field:'order',title:'权重',width:10,align:'center'},
                {field:'cover',title:'友链图片',width:10,align:'center'},
                {field:'title',title:'友链名称',width:50,align:'center'},
                {field:'url',title:'友链地址',width:50,align:'center'},
                {field:'actions',title:'可用操作',width:50,align:'center'}
//                {field:'author',title:'<?//= lang('Author'); ?>//',width:25,halign:'center'},
//                {field:'actions',title:'<?//= lang('Available operations'); ?>//',width:70,halign:'center'},
//                {field:'create_time',title:'<?//= lang('Create time'); ?>//',width:70,halign:'center'},
//                {field:'update_time',title:'<?//= lang('Update time'); ?>//',width:70,halign:'center'}
            ]],
            pagination: false,
            border:false,
            fitColumns: true,
            striped: true,
            header: '#cv-friend-table-header',
            onCheckAll: function () {
                $('#cv-friend-table-tool-delete').linkbutton('enable');
            },
            onUncheckAll: function () {
                $('#cv-friend-table-tool-delete').linkbutton('disable');
            },
            onLoadSuccess: function () {
                $('#cv-friend-table-tool-delete').linkbutton('disable');
            }
        });
    });

    function saveCoverFriendOrderForm(){
        $("#cv-friend-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){
                    reloadFriendListData();
                }
            }
        });
    }

    function reloadFriendListData() {
        $('#cv-friend-table').datagrid('reload');
    }

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

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