var el = {
    searchForm: '#searchForm',
    tb: '#tb',
    purchaseGroup: '#purchaseGroup',
    paymentWay: '#paymentWay',
    status: '#status',
    postStatus: '#postStatus',
    accountStatementYear: '#accountStatementYear',
    accountStatementMonth: '#accountStatementMonth',
    type: '#type',
    company_matching_add: '#company_matching_add',
    company_matching_edit: '#company_matching_edit',
    company_matching_search: '#company_matching_search',
    company_matching_push_confirm: '#company_matching_push_confirm',
    btn_remove: '#btn_remove',
    defaultColumns: [],
    bodyHeight: document.documentElement.clientHeight
};
$(function () {
    initDict('unit,currency,payment_way,payment_method')
    initNecessaryDatas('pc/sys/purchaseGroup/rest/listAll,pc/sys/company/rest/listAll');
    initCombo();

    el.defaultColumns = [
        {
            field: 'status',
            title: '数据状态',
            width: 115,
            sortable: true,
            exportEnum:[
                {code:1,codeWithDesc:'暂存对账单'},
                {code:2,codeWithDesc:'待采购确认'},
                {code:3,codeWithDesc:'采购拒绝'},
                {code:4,codeWithDesc:'待推送OA审批'},
                {code:5,codeWithDesc:'OA审批中'},
                {code:6,codeWithDesc:'OA审批不通过'},
                {code:7,codeWithDesc:'待财务确认'},
                {code:8,codeWithDesc:'财务拒绝对账单'},
                {code:9,codeWithDesc:'对账单已确认'},
            ],
            formatter: function (value) {
                switch (value) {
                    case 1:
                        return '暂存对账单';
                    case 2:
                        return '<span style="color: blue;">待采购确认</span>';
                    case 3:
                        return '<span style="color: red;">采购拒绝</span>';
                    case 4:
                        return '<span style="color: orange;">待推送OA审批</span>';
                    case 5:
                        return '<span style="color: blue;">OA审批中</span>';
                    case 6:
                        return '<span style="color: red;">OA审批不通过</span>';
                    case 7:
                        return '<span style="color: orange;">待财务确认</span>';
                    case 8:
                        return '<span style="color: red;">财务拒绝对账单</span>';
                    case 9:
                        return '<span style="color: green;">对账单已确认</span>';
                    default:
                        return '';
                }
            }
        },
        {
            field: 'signFileStatus',
            title: '签章文件状态',
            width: 115,
            sortable: true,
            exportEnum:[
                {code:0,codeWithDesc:'待上传签章文件'},
                {code:1,codeWithDesc:'已上传签章文件'}
            ],
            formatter: function (value,row) {
                if (value===1){
                    return '<span style="color: green;">已上传签章文件</span>';
                }else {
                    return '<span style="color: grey;">待上传签章文件</span>';
                }
            }
        },
        {
            field: 'supplierCode',
            title: '供应商编码',
            width: 90,
            sortable: true
        },
        {
            field: 'supplierName',
            title: '供应商名称',
            width: 270,
            sortable: true
        },
        {
            field: 'taxAmount',
            title: '对账合计金额',
            width: 110,
            sortable: true,
            formatter: function (value) {
                if (!value) {
                    return '';
                }
                return formatNumber(value, 2, 1);
            }
        },
        {
            field: 'currencyCode',
            title: '币种',
            width: 70,
            sortable: true
        },
        {
            field: 'invoiceCompleteFlag',
            title: '是否开票',
            width: 100,
            sortable: true,
            exportEnum:[
                {code:0,codeWithDesc:'否'},
                {code:1,codeWithDesc:'是'}
            ],
            formatter: function (value) {
                if (Number(value) === 0) {
                    return '否';
                }
                return '<span style="color: green;">是</span>';
            }
        },
        {
            field: 'paymentWay',
            title: '付款方式',
            width: 100,
            sortable: true,
            exportEnum:getDictList('payment_way'),
            formatter: function (value) {
                return getDicName('payment_way',value);
            }
        },
        {
            field: 'paymentExpireDate',
            title: '付款到期日',
            width: 120,
            sortable: true,
            exportDateFormatter:'date',
            formatter: function (value, row) {
                if (!value) {
                    return '';
                }
                return '<span style="color: red;">' + moment(value).format('YYYY-MM-DD') + '</span>';
            }
        },
        {
            field: 'companyCode',
            title: '采购公司',
            width: 240,
            sortable: true,
            exportEnum:getNecessaryDataList('pc/sys/company/rest/listAll'),
            formatter: function (value) {
                return getNecessaryDataName('pc/sys/company/rest/listAll',value);
            }
        },
        {
            field: 'signStatus',
            title: '签署状态',
            width: 115,
            sortable: true,
            exportEnum:[
                {code:0,codeWithDesc:'草稿'},
                {code:1,codeWithDesc:'签署中'},
                {code:2,codeWithDesc:'完成'},
                {code:3,codeWithDesc:'已过期'},
                {code:4,codeWithDesc:'已拒签'},
                {code:5,codeWithDesc:'已作废'},
                {code:6,codeWithDesc:'作废中'}
            ],
            formatter: function (value) {
                switch (value) {
                    case 0:
                        return '<span style="color: grey;">草稿</span>';
                    case 1:
                        return '<span style="color: blue;">签署中</span>';
                    case 2:
                        return '<span style="color: green;">完成</span>';
                    case 3:
                        return '<span style="color: orange;">已过期</span>';
                    case 4:
                        return '<span style="color: red;">已拒签</span>';
                    case 5:
                        return '<span style="color: dimgrey;">已作废</span>';
                    case 6:
                        return '<span style="color: blue;">作废中</span>';
                    default:
                        return '';
                }
            }
        },
        {
            field: 'signOrder',
            title: '签章顺序',
            width: '10%' ,
            exportEnum:[
                {code:1,codeWithDesc:'乙方先签章'},
                {code:2,codeWithDesc:'甲方先签章'},
                {code:3,codeWithDesc:'单签甲方'}
            ],
            formatter: function (value) {
                if (value==="1"){
                    return '乙方先签章';
                }else if (value==="2"){
                    return '甲方先签章';

                }else if (value==="3"){
                    return '单签甲方';
                }else {
                    return '';
                }
            }
        },
        {
            field: 'accountStatementNo',
            title: '对账单号',
            width: 140,
            sortable: true
        },
        {
            field: 'accountStatementYear',
            title: '对账单年份',
            width: 100,
            sortable: true
        },
        {
            field: 'accountStatementMonth',
            title: '对账单月份',
            width: 100,
            sortable: true
        } ,
        // {
        //     field: 'invoicedCode',
        //     title: 'F-02凭证号',
        //     width: 100,
        //     sortable: true,
        // },
        {
            field: 'invoiceNo',
            title: '到票发票号',
            width: 100,
            sortable: true,
        },
        {
            field: 'purchaseGroup',
            title: '采购组',
            width: 100,
            exportEnum:getNecessaryDataList('pc/sys/purchaseGroup/rest/listAll'),
            formatter: function (value) {
                return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll',value);
            }
        },
        // {
        //     field: 'voucherType',
        //     title: '凭证类型',
        //     width: 100,
        //     exportEnum:[
        //         {code:1,codeWithDesc:'材料'},
        //         {code:2,codeWithDesc:'寄售'},
        //         {code:3,codeWithDesc:'其他'}
        //     ],
        //     formatter: function (value) {
        //         if (value===1){
        //             return '材料';
        //         }else if (value===2){
        //             return '寄售';
        //         }else {
        //             return '其他';
        //         }
        //     }
        // },
        // {
        //     field: 'taxCode',
        //     title: '税率',
        //     width: 100,
        //     exportEnum:getNecessaryDataList('pc/sys/tax/rest/listAll'),
        //     formatter: function (value) {
        //         return getNecessaryDataName('pc/sys/tax/rest/listAll',value);
        //     }
        // },
        {
            field: 'paymentTermCode',
            title: '付款条件',
            width: 100,
            exportEnum:getDictList('payment_method'),
            formatter: function (value) {
                return getDicName('payment_method',value);
            }
        },
        // {
        //     field: 'createUserName',
        //     title: '创建人',
        //     width: 100,
        //     sortable: true
        // },
        {
            field: 'createTime',
            title: '创建时间',
            width: 130,
            sortable: true,
            exportDateFormatter:'dateTime',
            formatter: function (value) {
                if (!value) {
                    return '';
                }
                return moment(value).format('YYYY-MM-DD HH:mm');
            }
        },
        {
            field: 'updateTime',
            title: '最后更新时间',
            width: 140,
            sortable: true,
            exportDateFormatter:'dateTime',
            formatter: function (value) {
                if (!value) {
                    return '';
                }
                return moment(value).format('YYYY-MM-DD HH:mm');
            }
        },
        {
            field: 'deleteFlag',
            title: '删除标记',
            width: 80,
            sortable: true,
            exportEnum:[
                {code:0,codeWithDesc:'未删除'},
                {code:1,codeWithDesc:'已删除'}
            ],
            formatter:function (value) {
                if(value === 0){
                    return "未删除"
                }else{
                    return "<span style='color: red;'>已删除</span>"
                }
            }
        },
        {
            field: 'paymentStatus',
            title: '付款状态',
            width: 80
        },
        {
            field: 'paymentRemark',
            title: '付款备注',
            width: 120,
            editor: {type: 'textbox', options: {editable: true}}
        },
        {
            field: 'remark',
            title: '备注',
            width: 120,
            sortable: true
        }
    ];
    //供应商不能看到的字段
    if (Number($(el.isSupplier).val())===1){
        el.defaultColumns = el.defaultColumns.filter(obj => obj.field !== 'paymentExpireDate');
    }
    //从后台查询动态列
    let column = getRemoteColumnSort(columnSortCodeEnum.COMPANY_ACCOUNT_STATEMENT_SUP,el.defaultColumns);
    loadGrid(column);
});

function initCombo(){
    $(el.status).combobox({
        valueField: 'value',
        textField: 'label',
        prompt: '--- 请选择 ---',
        data: [
            // 对账单数据状态 1:暂存对账单 2:待确认 3:采购拒绝对账单 4:待推送OA审批 5:OA审批中 6:OA审批不通过 7:待财务确认对账单 8:财务驳回对账单 9:对账单已确认
            {label: '暂存对账单', value: 1},
            {label: '待采购确认', value: 2},
            {label: '采购拒绝', value: 3},
            // {label: '待推送OA审批', value: 4},
            // {label: 'OA审批中', value: 5},
            // {label: 'OA审批不通过', value: 6},
            {label: '待财务确认', value: 7},
            {label: '财务拒绝对账单', value: 8},
            {label: '对账已确认', value: 9}
        ],
        editable: false,
        panelHeight: 'auto'
    });
    $(el.postStatus).combobox({
        editable: false,
        panelHeight: 'auto',
        valueField: 'value',
        textField: 'label',
        prompt: '--- 请选择 ---',
        data: [
            {label: '未过账', value: 1},
            {label: '预制失败', value: 3},
            {label: '预制成功', value: 4},
            {label: '过账成功', value: 5}
        ],
    });

    $(el.accountStatementYear).combobox({
        required: false,
        valueField: 'code',
        textField: 'name',
        limitToList: 'true',
        data: [
            {'name': moment().format('YYYY'), 'code': moment().format('YYYY')},
            {'name': moment().add(-12, 'month').format('YYYY'), 'code': moment().add(-12, 'month').format('YYYY')},
            {'name': moment().add(-24, 'month').format('YYYY'), 'code': moment().add(-24, 'month').format('YYYY')},
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
    });
    $(el.accountStatementMonth).combobox({
        required: false,
        valueField: 'code',
        textField: 'name',
        limitToList: 'true',
        data: [
            {'name': '01', 'code': '01'},
            {'name': '02', 'code': '02'},
            {'name': '03', 'code': '03'},
            {'name': '04', 'code': '04'},
            {'name': '05', 'code': '05'},
            {'name': '06', 'code': '06'},
            {'name': '07', 'code': '07'},
            {'name': '08', 'code': '08'},
            {'name': '09', 'code': '09'},
            {'name': '10', 'code': '10'},
            {'name': '11', 'code': '11'},
            {'name': '12', 'code': '12'},
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
    });

    $(el.purchaseGroup).combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        method: 'POST',
        prompt: '--- 请选择 ---',
        data: getNecessaryDataList('pc/sys/purchaseGroup/rest/listAll')
    });
    // 付款方式
    $(el.paymentWay).combobox({
        required: false,
        panelHeight: '200',
        valueField: 'code',
        textField: 'codeWithDesc',
        prompt: '--- 请选择 ---',
        method: 'GET',
        url: g.ctx + 'pc/sys/dict/getDicByType?type=payment_way'
    });
}


function loadGrid(column) {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight - 20, 37, 40);
    $(el.tb).datagrid({
        height: el.bodyHeight - searchHeight,
        url: g.ctx + 'pc/finance/company/account/statement/rest/list',
        singleSelect: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        loadMsg: '数据加载中......',
        rownumbers: true,
        frozenColumns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
            ]
        ],
        columns: [column],
        toolbar: [
            {
                text: '查看',
                iconCls: 'icon-search',
                id:'btn_look',
                handler: function () {
                    detail(2);
                }
            },
            "-",
            {
                text: '新增',
                iconCls: 'icon-add',
                id:'btn_add',
                handler: function () {
                    detail(0);
                }
            },
            {
                text: '编辑',
                iconCls: 'icon-edit',
                id:'btn_edit',
                handler: function () {
                    detail(1);
                }
            },
            "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                id: 'btn_del',
                handler: function () {
                    remove();
                }
            },
            '-',
            {
                text: '提交确认',
                iconCls: 'icon-ok',
                id: 'btn_submit',
                handler: function () {
                    submitConfirm();
                }
            },
            '-',
            {
                text: '打印',
                iconCls: 'icon-download',
                id: 'btn_print_download',
                handler: function () {
                    printInvoice();
                }
            },
            '-',
            {
                text: '线下盖章附件上传',
                iconCls: 'icon-redo',
                id: 'btn-file-sign',
                handler: function () {
                    let row = $(el.tb).datagrid('getSelected');
                    if (!row) {
                        layer.msg("请选中一条记录");
                        return;
                    }
                    //如果为发票已确认状态，不可发起线下盖章附件上传。
                    var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
                    $.ajax({
                        url: g.ctx + 'pc/finance/invoiced/apply/rest/checkApply?accountStatementNo=' + row.accountStatementNo + '&type=1'+'&accountStatementId='+row.id,
                        type: 'GET',
                        processData: false,
                        contentType: false,
                        success: function () {
                            $("#fileUpload .filebox-label").click();
                        },
                        complete: function(data) {
                            top.layer.close(loading);
                        },
                        error: function (data) {
                            parent.layer.alert("该对账单为发票已确认状态，不可发起线下盖章附件上传操作!",{icon: 2, shadeClose: true, title: '温馨提示'});
                        }
                    });
                }
            },
            '-',
            {
                text: '电子印章签署',
                iconCls: 'icon-sign',
                id: 'btn-sign',
                handler: function () {
                    sendSign();
                }
            },
            '-',
            {
                text: '查看签署文件',
                iconCls: 'icon-find',
                id: 'btn-sign-look',
                handler: function () {
                    signFileLook();
                }
            },
            '-',
            {
                text: '列表导出',
                iconCls: 'icon-redo',
                id: 'btn_list_export',
                handler: function () {
                    openExportDialog('pc/finance/company/account/statement/rest/sup/export','tb',searchParams());
                }
            },"-",
            {
                text: '列设置',
                iconCls: 'icon-anchor',
                id: 'btn_column_sort',
                handler: function () {
                    openSortDialog({
                        //唯一编码
                        code: columnSortCodeEnum.COMPANY_ACCOUNT_STATEMENT_SUP,
                        //初始列（每次新增或删除列需要手动删除sys_column_sort表中对应code数据）
                        columns: el.defaultColumns,
                        callBack : function (newColumns) {
                            loadGrid(newColumns);
                        },
                    });
                }
            }
        ],
        onBeforeLoad:function () {
            displayButtonByUserRole('company_account_statement_list');
        },
        onCheck: function (index, row) {
            // 对账单数据状态 1:暂存对账单 2:待确认 3:采购拒绝对账单 4:待推送OA审批 5:OA审批中 6:OA审批不通过 7:待财务确认对账单 8:财务驳回对账单 9:对账单已确认
            var status = row.status;
            var paymentWay = row.paymentWay;
            var signFileStatus = row.signFileStatus;
            var signStatus = row.signStatus;
            // 按钮 0:查看 1:新增 2:编辑 3:删除 4:提交审批 5:导出
            var btn = $('div.datagrid-toolbar a');

            switch (Number(status)){
                case 1:
                    btn.eq(2).linkbutton('enable');
                    btn.eq(3).linkbutton('enable');
                    btn.eq(4).linkbutton('enable');
                    btn.eq(5).linkbutton('disable');
                    btn.eq(6).linkbutton('disable');
                    btn.eq(7).linkbutton('disable');
                    btn.eq(11).linkbutton('disable');
                    btn.eq(12).linkbutton('disable');
                    break;
                case 2:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');
                    btn.eq(6).linkbutton('disable');
                    btn.eq(7).linkbutton('disable');
                    btn.eq(11).linkbutton('disable');
                    btn.eq(12).linkbutton('disable');
                    break;
                case 3:
                    btn.eq(2).linkbutton('enable');
                    btn.eq(3).linkbutton('enable');
                    btn.eq(4).linkbutton('enable');
                    btn.eq(5).linkbutton('disable');
                    btn.eq(6).linkbutton('disable');
                    btn.eq(7).linkbutton('disable');
                    btn.eq(11).linkbutton('disable');
                    btn.eq(12).linkbutton('disable');
                    break;
                case 4:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');
                    btn.eq(6).linkbutton('disable');
                    btn.eq(7).linkbutton('disable');
                    btn.eq(11).linkbutton('disable');
                    btn.eq(12).linkbutton('disable');
                    break;
                case 5:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');
                    btn.eq(6).linkbutton('disable');
                    btn.eq(7).linkbutton('disable');
                    btn.eq(11).linkbutton('disable');
                    btn.eq(12).linkbutton('disable');
                    break;
                case 6:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');
                    btn.eq(6).linkbutton('disable');
                    btn.eq(7).linkbutton('disable');
                    btn.eq(11).linkbutton('disable');
                    btn.eq(12).linkbutton('disable');
                    break;
                case 7:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');
                    btn.eq(6).linkbutton('disable');
                    btn.eq(7).linkbutton('disable');
                    btn.eq(11).linkbutton('disable');
                    btn.eq(12).linkbutton('disable');
                    break;
                case 8:
                    btn.eq(2).linkbutton('enable');
                    btn.eq(3).linkbutton('enable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');
                    btn.eq(6).linkbutton('disable');
                    btn.eq(7).linkbutton('disable');
                    btn.eq(11).linkbutton('disable');
                    btn.eq(12).linkbutton('disable');
                    break;
                case 9:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('enable');
                    btn.eq(6).linkbutton('enable');
                    btn.eq(7).linkbutton('enable');
                    //购销合同签署
                    // if(paymentWay == "C"){
                    btn.eq(11).linkbutton('enable');
                    btn.eq(12).linkbutton('enable');
                    // }
                    break;
                default:
                    break;
            }

            // switch (Number(signFileStatus)){
            //     case 0:
            //         btn.eq(6).linkbutton('disable');
            //         btn.eq(7).linkbutton('disable');
            //         break;
            //     case 1:
            //         btn.eq(6).linkbutton('enable');
            //         btn.eq(7).linkbutton('disable');
            //         break;
            //     case 2:
            //         btn.eq(6).linkbutton('disable');
            //         btn.eq(7).linkbutton('enable');
            //         break;
            // }

            // switch (Number(signStatus)){
            //     case 1:
            //         btn.eq(7).linkbutton('disable');
            //         break;
            //     case 3:
            //         btn.eq(7).linkbutton('enable');
            //         break;
            //     case 4:
            //         btn.eq(7).linkbutton('enable');
            //         break;
            // }
            if (row.deleteFlag!==0){
                btn.eq(4).linkbutton('disable');
            }
        },
        onClickRow: function (index, row) {
            // 对账单数据状态 1:暂存对账单 2:待确认 3:采购拒绝对账单 4:待推送OA审批 5:OA审批中 6:OA审批不通过 7:待财务确认对账单 8:财务驳回对账单 9:对账单已确认
            var status = row.status;
            var signFileStatus = row.signFileStatus;
            var signStatus = row.signStatus;
            // 按钮 0:查看 1:新增 2:编辑 3:删除 4:提交审批 5:导出
            var btn = $('div.datagrid-toolbar a');
            switch (Number(status)){
                case 1:
                    btn.eq(2).linkbutton('enable');
                    btn.eq(3).linkbutton('enable');
                    btn.eq(4).linkbutton('enable');
                    btn.eq(5).linkbutton('disable');

                    break;
                case 2:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');

                    break;
                case 3:
                    btn.eq(2).linkbutton('enable');
                    btn.eq(3).linkbutton('enable');
                    btn.eq(4).linkbutton('enable');
                    btn.eq(5).linkbutton('disable');

                    break;
                case 4:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');

                    break;
                case 5:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');

                    break;
                case 6:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');

                    break;
                case 7:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');

                    break;
                case 8:
                    btn.eq(2).linkbutton('enable');
                    btn.eq(3).linkbutton('enable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('disable');

                    break;
                case 9:
                    btn.eq(2).linkbutton('disable');
                    btn.eq(3).linkbutton('disable');
                    btn.eq(4).linkbutton('disable');
                    btn.eq(5).linkbutton('enable');

                    break;
                default:
                    break;
            }

            // switch (Number(signFileStatus)){
            //     case 0:
            //         btn.eq(6).linkbutton('disable');
            //         btn.eq(7).linkbutton('disable');
            //         break;
            //     case 1:
            //         btn.eq(6).linkbutton('enable');
            //         btn.eq(7).linkbutton('disable');
            //         break;
            //     case 2:
            //         btn.eq(6).linkbutton('disable');
            //         btn.eq(7).linkbutton('enable');
            //         break;
            // }
            //
            // switch (Number(signStatus)){
            //     case 1:
            //         btn.eq(7).linkbutton('disable');
            //         break;
            //     case 3:
            //         btn.eq(7).linkbutton('enable');
            //         break;
            //     case 4:
            //         btn.eq(7).linkbutton('enable');
            //         break;
            // }
            if (row.deleteFlag!==0){
                btn.eq(4).linkbutton('disable');
            }
        }
    });
}


/**
 * 删除
 */
function remove(){
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert('请选择一条对账单进行操作', {icon: 0});
        return false;
    }
    var ids = [];
    if(Number(row.status) === 1 || Number(row.status) === 3 || Number(row.status) === 8){
        ids.push(row.id);
    }else {
        topWin.layer.alert('对账单号: ' + row.accountStatementNo + ', 不可以删除', {icon: 0});
        return false;
    }
    topWin.layer.confirm('您确定要删除选中的对账单吗?', {icon: 3}, function (index) {
        topWin.layer.close(index);

        var loading = topWin.layer.msg('数据提交中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
        $.ajax({
            url: g.ctx + 'pc/finance/company/account/statement/rest/del',
            type: 'POST',
            data: {ids: JSON.stringify(ids),type:0},
            success: function () {
                topWin.layer.close(loading);
                topWin.layer.msg('删除成功', {icon: 1});
                $(el.tb).datagrid('reload');
            },
            error: function (data) {
                topWin.layer.close(loading);
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    });
}


/**
 * 列表上提交确认
 */
function submitConfirm() {
    var row = $(el.tb).datagrid('getSelected'), ids = [];
    if (!row) {
        topWin.layer.alert('请选择您需要提交的对账单进行操作', {icon: 0});
        return false;
    }

    if(Number(row.status) === 1 || Number(row.status) === 3){
        ids.push(row.id);
    }else {
        topWin.layer.alert('只有暂存或者采购拒绝的对账单才能提交确认', {icon: 0});
        return false;
    }

    topWin.layer.confirm('您确定要提交选中的对账单吗?', {icon: 3}, function (index) {
        topWin.layer.close(index);

        var loading = topWin.layer.msg('数据提交中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
        $.ajax({
            url: g.ctx + 'pc/finance/company/account/statement/rest/submit/confirm',
            type: 'POST',
            data: {ids: JSON.stringify(ids)},
            success: function () {
                topWin.layer.close(loading);
                topWin.layer.msg('提交成功', {icon: 1});
                $(el.tb).datagrid('reload');
            },
            error: function (data) {
                topWin.layer.close(loading);
                topWin.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}

//打印
function printInvoice() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert("请选择数据", {icon: 0});
        return false;
    }
    if(row.deleteFlag === 1){ layer.msg("删除的单据不可打印"); return; }

    $.ajax({
        type: 'GET',
        url: g.ctx + 'pc/finance/company/account/statement/print?id=' + row.id,
        success: function (data) {
            $('#printOrder').print({
                globalStyles: false,
                mediaPrint: true,
                iframe: true,
                prepend: data,
                deferred: $.Deferred().done(function () {
                    // $.ajax({
                    //     type: 'POST',
                    //     url: g.ctx + 'pc/finance/invoiced/apply/rest/update/print?id=' + row.id,
                    //     success: function (data) {
                    //         $('#tb').datagrid('reload');
                    //     }
                    // });
                })
            });
        }
    });
}


$("#fileInput").filebox({
    buttonText: 'Choose', buttonAlign: 'left', onChange: function () {
        let row = $(el.tb).datagrid("getSelected");
        let addFiles = [];
        let files = $("#fileInput").filebox('files'), formData = new FormData()
        for (let r = 0; r < files.length; r++) {
            addFiles.push(files[r]);
        }
        for (let n = 0; n < addFiles.length; n++) {
            formData.append('files', addFiles[n]);
        }
        if (addFiles.length > 0) {
            fileUploadAjaxWithCall(formData, function (rows) {
                const postData = []
                $.each(rows, function (idx, itm) {
                    postData.push({
                        fileId: itm.id, accountStatementId: row.id
                    })
                })
                formData.append("signFile",postData[0].fileId)
                formData.append("id",postData[0].accountStatementId)
                var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
                $.ajax({
                    url: g.ctx + 'pc/finance/company/account/statement/rest/saveSignFile',
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function () {
                        topWin.layer.msg('上传成功', {icon: 1});
                        $(el.tb).datagrid('reload');
                    },
                    complete: function(data) {
                        top.layer.close(loading);
                    },
                    error: function (data) {
                        parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
                    }
                });
            }, true)
            $("#fileInput").filebox('clear')
        }
    }
});

function sendSign() {
    let row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    //电子印章签署 先根据对账单号 查询申请单中，是否存在且状态为发票已确认状态 select * from fin_invoiced_apply fia  where  account_statement_no = '24000091'
    //  and status = 9 如果存在则提示该单已经为发票已确认状态，不可发起电子印章签署操作。
    var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
    $.ajax({
        url: g.ctx + 'pc/finance/company/invoiced/apply/rest/checkApply?accountStatementNo=' + row.accountStatementNo + '&type=1'+'&accountStatementId='+row.id,
        type: 'GET',
        processData: false,
        contentType: false,
        success: function (result) {
            openSignDialog('esignDialog',{
                //业务类型编码
                title:'费用对账单'+row.accountStatementNo,
                //业务类型编码
                businessTypeCode:eSignBusinessTypeCodeEnum.COMPANY_ACCOUNT_STATEMENT,
                //业务唯一id
                businessId:row.id,
                //甲方
                firstParty:row.companyCode,
                //甲方默认用户id
                firstPartyUserId:row.financeConfirmUserId,
                //甲方名称
                firstPartyName:getNecessaryDataName('pc/sys/company/rest/listAll',row.companyCode),
                //乙方
                secondParty:row.supplierCode,
                //乙方名称
                secondPartyName:row.supplierName,
                sumSignFlag:1,
                summaryFileId : "",
                callBack : function () {
                    $(el.tb).datagrid('reload');
                },
            });
        },
        complete: function(data) {
            top.layer.close(loading);
        },
        error: function (data) {
            parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
        }
    });
}

function signFileLook() {
    let row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    openSignLookDialog('esignDialog',row.id,eSignBusinessTypeCodeEnum.COMPANY_ACCOUNT_STATEMENT);
}

/**
 * 提交
 */
function pushConfirm() {
    var cbox = $(el.tb).datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }

    var id = cbox.id;

    parent.layer.confirm('是否确认提交？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        var url= g.ctx + 'pc/finance/company/matching/rest/pushConfirm';
        $.ajax({
            url: url,
            method: "post",
            data: {id: id},
            success: function (data) {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}

