
let el = { tb: '#tb',  title: '#title' };

/*let selfData = {
    toolbar: []
};*/

$(function () {

    //initColumns();

    loadGrid();

});

/**
 * 初始化表格列
 */
/*
function initColumns() {

    selfData.toolbar = [
        {
            text: '新增',
            id: 'btn_add',
            iconCls: 'icon-add',
            handler: function () {
                addShoppingPay();
            }
        },
        {
            text: '编辑',
            id: 'btn_edit',
            iconCls: 'icon-edit',
            handler: function () {
                editShoppingPay();
            }
        },
        {
            text: '删除',
            id: 'btn_remove',
            iconCls: 'icon-remove',
            handler: function () {
                let row = $(el.tb).datagrid('getSelected');
                if (!row) {
                    layer.msg("请选中一条记录");
                    return;
                }
                if(row.payState==1||row.payState==2||row.payState==4){
                    layer.msg("该状态不可删除!");
                    return;
                }
                delOrEditPay(row.id);
            }
        },
        {
            text: '查看', id: 'btn_detail', iconCls: 'icon-search', handler: function () {
                let row = $(el.tb).datagrid('getSelected');
                if (!row) {
                    layer.msg("请选中一条记录");
                    return;
                }
                checkShoppingPay( row.id );
            }
        },
    ];

}
*/


function loadGrid() {

    $(el.tb).datagrid({
        url: '/pc/mall/shoppingPay/rest/list',
        iconCls: 'icon-list',
        height: gridheight - 50,
        singleSelect: true,
        fitColumns: true,
        pagination: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        nowrap: true,
        striped: true,
        page: 1,
        pageSize: 10,
        columns: [[
            { field: 'ck', checkbox: true},
            { field: 'payTitle', title: '标题', align: 'left',width: 200 ,sortable:true},
            { field: 'payState', title: '状态', align: 'left' , width: 100,sortable:true,
                formatter: function (value, row) {
                    // 状态 0 待提交  1审批中 2 通过 3 驳回
                    switch (value) {
                        case 0:
                            return '<span style="color:#ffcb52;">待提交</span>';
                        case 1:
                            return '<span style="color:blue;">审批中</span>';
                        case 2:
                            return '<span style="color:green;">通过</span>';
                        case 3:
                            return '<span style="color:red;">驳回</span>';
                        case 4:
                            return '<span style="color:#494646;">废弃</span>';
                        default:
                            return '--';
                    }
                    return "";
                }
            },
            { field: 'payCode', title: '付款流程编码', align: 'left',width: 150 , sortable:true},
            { field: 'oaProcessId', title: 'OA流程单号', align: 'left',width: 200 ,sortable:true,
                formatter: function (value) {
                    return oaTrack(value,OaProcessType.CONTRACT_CONFIRM);
                }},
            { field: 'createUserName', title: '申请人', align: 'left', width: 150,sortable:true },
            { field: 'payTime', title: '预计付款日期', align: 'left',width: 150,sortable:true,
                formatter: function (value) {
                    if (!value) {
                        return;
                    }
                    return moment(new Date(value)).format('YYYY-MM-DD');
                }
            },
            //{ field: 'cartCode', title: '商城订单号', align: 'left',width: 150 },
            { field: 'createTime', title: '申请日期', align: 'left', width: 150,sortable:true,
                formatter: function (value) {
                    if (!value) {
                        return;
                    }
                    return moment(new Date(value)).format('YYYY-MM-DD HH:mm:ss');
                }
            },
           /* { field: ' ', title: '操作', align: 'left',width: 100,
                formatter: function (value, row) {
                    var html = "";
                    html += '<a href="javascript:;" onclick="checkShoppingPay( '+row.id+' )">查看</a>';
                    return html
                }
            }*/]],
        toolbar:[
            {
                text: '新增',
                id: 'btn_add',
                iconCls: 'icon-add',
                handler: function () {
                    addShoppingPay();
                }
            },
            {
                text: '编辑',
                id: 'btn_edit',
                iconCls: 'icon-edit',
                handler: function () {
                    editShoppingPay();
                    setOperationLog("编辑按钮", "商城付款");

                }
            },
            {
                text: '删除',
                id: 'btn_remove',
                iconCls: 'icon-remove',
                handler: function () {
                    let row = $(el.tb).datagrid('getSelected');
                    if (!row) {
                        layer.msg("请选中一条记录");
                        return;
                    }
                    if(row.payState==1||row.payState==2||row.payState==4){
                        layer.msg("该状态不可删除!");
                        return;
                    }
                    delOrEditPay(row.id);
                }
            },
            {
                text: '查看', id: 'btn_detail', iconCls: 'icon-search', handler: function () {
                    let row = $(el.tb).datagrid('getSelected');
                    if (!row) {
                        layer.msg("请选中一条记录");
                        return;
                    }
                    checkShoppingPay( row.id );
                    setOperationLog("查看按钮", "商城付款");

                }
            },
            {
                text: '付款明细导出',
                hide: true,
                id: 'btn_export_detail',
                iconCls: 'icon-remove',
                handler: function () {
                    detailExport();
                }
            },
        ],
        onLoadSuccess: function () {
            displayButtonByUserRole('shoppingPay');
            $(el.tb).datagrid('clearSelections');
        }
    });
}

/**
*明细导出
*/
function detailExport() {
    xhrPostExport('pc/mall/shoppingPay/rest/detail/export', getQuery());

}

/**
 * 获取查询条件
 */
function getQuery() {
    var queryParams = {};
    queryParams.title = $.trim($("#title").val());
    queryParams.oaProcessId = $.trim($("#oaProcessId").val());
    queryParams.createUserName = $.trim($("#createUserName").val());
    return queryParams;
}


function addShoppingPay(){
    var addShoppingDialog = openDialog('addShoppingPay', g.ctx+'pc/mall/shoppingPay/page/formDeatil?id=&check=1', {
        title: '新增',
        width: '90%',
        height:'100%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            addShoppingDialog.dialog('destroy');
        },
        buttons: [
            {
                text:'提交',
                iconCls:'icon-save',
                handler:function() {
                    topWin.frames['addShoppingPay'].submitShoppingPayDetalsForm(function (data) {
                        addShoppingDialog.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            },
            {
                text:'关闭',
                iconCls:'icon-cancel',
                handler:function() {
                    addShoppingDialog.dialog('close');
                }
            }
        ]
    });
}


function editShoppingPay(){
    let row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    if(row.payState==1||row.payState==2){
        layer.msg("该状态不可编辑!");
        return;
    }

    var editShoppingPayDialog = openDialog('editShoppingPay', g.ctx+'pc/mall/shoppingPay/page/formDeatil?id='+ row.id +'&check=2', {
        title: '编辑',
        width: '90%',
        height:'100%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            editShoppingPayDialog.dialog('destroy');
        },
        buttons: [
            {
                text:'提交',
                iconCls:'icon-save',
                handler:function() {
                    topWin.frames['editShoppingPay'].submitShoppingPayDetalsForm(function (data) {
                        editShoppingPayDialog.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            },
            {
                text:'关闭',
                iconCls:'icon-cancel',
                handler:function() {
                    editShoppingPayDialog.dialog('close');
                }
            }
        ]
    });
}


function checkShoppingPay( id ){

    var checkShoppingPayDialog = openDialog('checkShoppingPay', g.ctx+'pc/mall/shoppingPay/page/formDeatil?id='+ id +'&check=0', {
        title: '查看',
        width: '90%',
        height:'100%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            checkShoppingPayDialog.dialog('destroy');
        },
        buttons: [
            {
                text:'关闭',
                iconCls:'icon-cancel',
                handler:function() {
                    checkShoppingPayDialog.dialog('close');
                }
            }
        ]
    });
}


function delOrEditPay(id) {
    topWin.layer.confirm("确定删除吗", function (index) {
        $.ajax({
            url: g.ctx + 'pc/mall/shoppingPay/rest/payRemove',
            type: 'POST',
            data: {id: id},
            success: function (data) {
                topWin.layer.alert("删除成功", {icon: 1}, function (idx) {
                    $(el.tb).datagrid("reload")
                    topWin.layer.close(index);
                    topWin.layer.close(idx);
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        })
    })
}





/**
 * 查询
 */
function searchGrid() {
    $(el.tb).datagrid('options').queryParams = $("#searchForm").serializeJson();
    $(el.tb).datagrid('load');
}

/**
 * 重置筛选条件
 */
function resetGrid() {
    $(el.title).textbox('setValue', '');
    searchGrid();
}