<div id="cv-productattr-table-header">
    <div class="panel-title">
        <a href="javascript:openDialog('添加产品属性','<?= url('new'); ?>','650px','520px')" class="easyui-linkbutton">+ 添加</a>
        <a id="cv-productattr-table-tool-delete" href="javascript:deleteAllProductAttr();" class="easyui-linkbutton"><i class="sui-icon icon-tb-delete cv-f-14"></i> 删除全部</a>
        <a href="javascript:saveProductAttrOrderForm();" class="easyui-linkbutton"><i class="sui-icon icon-tb-delete cv-f-14"></i> 排序</a>
    </div>
</div>
<form id="cv-productattr-order-form" method="post" style="width: 100%;height: 100%;">
<table id="cv-productattr-table" style="height: 100%;"></table>
</form>
<script type="text/javascript">
    $(function () {
        var cvProductAttrTable = $('#cv-productattr-table');
        cvProductAttrTable.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:'label',title:'属性名称',width:100,align:'center'},
                {field:'field',title:'属性标识',width:80,align:'center'},
                {field:'field_type',title:'属性类型',width:80,align:'center'},
                {field:'actions',title:'可用操作',width:100,halign:'center'}
            ]],
            pagination: false,
            border:false,
            fitColumns: true,
            striped: true,
            header: '#cv-productattr-table-header',
            onCheckAll: function () {
                $('#cv-productattr-table-tool-delete').linkbutton('enable');
            },
            onUncheckAll: function () {
                $('#cv-productattr-table-tool-delete').linkbutton('disable');
            },
            onLoadSuccess: function () {
                $('#cv-productattr-table-tool-delete').linkbutton('disable');
            }
        });
    });

    function reloadProductAttrListData() {
        $('#cv-productattr-table').datagrid('reload');
    }

    function saveProductAttrOrderForm(){
        $("#cv-productattr-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){
                    reloadProductAttrListData();
                }
            }
        });
    }

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

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

</script>
