
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 () {
                addContractPay();
            }
        },
        {
            text: '编辑',
            id: 'btn_edit',
            iconCls: 'icon-edit',
            handler: function () {
                editContractPay();
            }
        },
        {
            text: '查看',
            id: 'btn_look',
            iconCls: 'icon-ok',
            handler: function () {
                lookContractPay();
            }
        },
        {
            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-saveoa', iconCls: 'icon-save', handler: function () {
                let row = $(el.tb).datagrid('getSelected');
                if (!row) {
                    layer.msg("请选中一条记录");
                    return;
                }
                if(row.payState==1||row.payState==2){
                    layer.msg("该状态不可提交审批!");
                    return;
                }
                fromAuditPay(row.id, row.oaProcessId);
            }
        },
    ];

}


function loadGrid() {

    $(el.tb).datagrid({
        url: '/pc/contract/pay/rest/index/page',
        iconCls: 'icon-list',
        height: gridheight - 50,
        singleSelect: true,
        fitColumns: false,
        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 },
            { field: 'contractCode', title: '合同编码', align: 'left',width: 150 },
            { field: 'payState', title: '状态', align: 'left' ,width: 100,
                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: 'oaProcessId', title: 'OA流程单号', align: 'left',width: 240 ,
                formatter: function (value) {
                    return oaTrack(value,OaProcessType.CONTRACT_CONFIRM);
                }},
            {  field: 'payeeName', title: '收款单位', align: 'left', width: 200 },
            {  field: 'payOrgName', title: '付款单位名称', align: 'left', width: 200 },
            { field: 'createUserName', title: '申请人', align: 'left', width: 150 },
            { field: 'payTime', title: '预计付款日期', align: 'left',width: 150,
                formatter: function (value) {
                    if (!value) {
                        return;
                    }
                    return moment(new Date(value)).format('YYYY-MM-DD');
                }
            },
            { field: 'payeeAmount', title: '预付金额', align: 'left',width: 150  },

           /* { field: 'paymentProgress', title: '付款进度', align: 'left',width: 150,
                formatter: function (value) {
                    if (!value) {
                        return 0 ;
                    }
                    value = (value*100)+"%"
                    return value;
                }
            },*/
            { field: 'createTime', title: '申请日期', align: 'left', width: 150,
                formatter: function (value) {
                    if (!value) {
                        return;
                    }
                    return moment(new Date(value)).format('YYYY-MM-DD');
                }
            },
            { field: 'oaFinishTime', title: '流程结束日期', align: 'left', width: 150,
                formatter: function (value) {
                    if (!value) {
                        return;
                    }
                    return moment(new Date(value)).format('YYYY-MM-DD');
                }
            },
            { field: 'payCode', title: '预付合同编码', align: 'left',width: 150 },
            // { field: ' ', title: '操作', align: 'left',width: 100,
            //     formatter: function (value, row) {
            //         var html = "";
            //         html += '<a href="javascript:;" onclick="checkContractPay( '+row.id+' )">查看</a>';
            //         return html
            //     }
            // }
            ]],
        toolbar: selfData.toolbar,
    });
}


function addContractPay(){
    var addContractPayDialog = openDialog('addContractPay', g.ctx+'pc/contract/pay/page/formDeatil?id=&check=1', {
        title: '合同付款',
        width: '90%',
        height:'100%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            addContractPayDialog.dialog('destroy');
        },
        buttons: [
            {
                text:'保存',
                iconCls:'icon-save',
                handler:function() {
                    topWin.frames['addContractPay'].submitContractPayDetalsForm(function (data) {
                        addContractPayDialog.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            },
            {
                text:'关闭',
                iconCls:'icon-cancel',
                handler:function() {
                    addContractPayDialog.dialog('close');
                }
            }
        ]
    });
}


function editContractPay(){
    let row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    if(row.payState==1||row.payState==2){
        layer.msg("该状态不可编辑!");
        return;
    }
    setOperationLog("编辑按钮", "合同付款跟踪");

    var editContractPayDialog = openDialog('editContractPay', g.ctx+'pc/contract/pay/page/formDeatil?id='+ row.id +'&check=1', {
        title: '编辑',
        width: '90%',
        height:'100%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            editContractPayDialog.dialog('destroy');
        },
        buttons: [
            {
                text:'保存',
                iconCls:'icon-save',
                handler:function() {
                    topWin.frames['editContractPay'].submitContractPayDetalsForm(function (data) {
                        editContractPayDialog.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            },
            {
                text:'关闭',
                iconCls:'icon-cancel',
                handler:function() {
                    editContractPayDialog.dialog('close');
                }
            }
        ]
    });
}

function lookContractPay(){
    let row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    // if(row.payState==1||row.payState==2){
    //     layer.msg("该状态不可编辑!");
    //     return;
    // }
    setOperationLog("查看按钮", "合同主数据");

    var lookContractPayDialog = openDialog('lookContractPay', g.ctx+'pc/contract/pay/page/lookDeatil?id='+ row.id +'&check=1', {
        title: '编辑',
        width: '90%',
        height:'100%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            lookContractPayDialog.dialog('destroy');
        },
        buttons: [
            // {
            //     text:'保存',
            //     iconCls:'icon-save',
            //     handler:function() {
            //         topWin.frames['lookContractPay'].submitContractPayDetalsForm(function (data) {
            //             lookContractPayDialog.dialog('close');
            //             $('#tb').datagrid('reload');
            //         });
            //     }
            // },
            {
                text:'关闭',
                iconCls:'icon-cancel',
                handler:function() {
                    lookContractPayDialog.dialog('close');
                }
            }
        ]
    });
}

function checkContractPay( id ){

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


function delOrEditPay(id) {
    topWin.layer.confirm("确定删除吗", function (index) {
        $.ajax({
            url: g.ctx + 'pc/contract/pay/rest/remove',
            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 checkConPay(id){
    let row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }

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


function fromAuditPay(payId,oaApplyId){
    var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
    //检查是否存在对应的科目流程
    $.ajax({
        url: '/pc/contract/pay/rest/getPayBillByKm',
        data: {payId: payId,oaApplyId:oaApplyId},
        type: 'POST',
        success: function (data) {
            top.layer.close(loading);
            if (data.code == "S") {
                topWin.layer.alert('OA提交成功!', {icon: 1});
                $('#tb').datagrid('reload');
            } else {
                topWin.layer.alert('OA提交失败!', {icon: 2});
            }
        },
        error: function (data) {
            top.layer.close(loading);
            topWin.layer.alert("OA提交失败!", {icon: 2});
        }
    })

}


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

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