/**
 * 对账模块-开票申请单查看页面JS
 *
 * Created by li.kui on 2020/8/15.
 */
'use strict';
var el = {
    dataPanel: '#dataPanel',
    deductionAmountTotal: '#deductionAmountTotal',
    dataDiv: '#dataDiv',
    id: '#id',
    iaNo: '#invoiceApplyNo',
    orderType: '#orderType',
    tbFile: '#tbFile',
    supplierCode: '#supplierCode',
    companyCode: '#companyCode',
    factoryCode: '#factoryCode',
    currencyCode: '#currencyCode',
    dataTabs: '#dataTabs',
    tbDetails: '#tbDetails',
    tbDeduction: '#tbDeduction',
    tbRebate: '#tbRebate',
    tbIaApprove: '#tbIaApprove',
    userTableColumn: [],
    bodyHeight: document.documentElement.clientHeight,

    tbInvoice: '#tbInvoice',
    tbAdvance: '#tbAdvance',
    invoiceTaxTotalAmount : '#invoiceTaxTotalAmount',
    invoiceTotalAmount : '#invoiceTotalAmount',
    diffAmount1 : '#diffAmount1',
    diffAmount : '#diffAmount',
    allBudInnerMoney : '#allBudInnerMoney'
};


/**
 * 计算页面上相关金额
 * 发票含税金额 = 对账明细含税金额合计 - 含税扣款金额 + 含税返利金额
 */
function  calculationPageAmount() {
    // 需要处理统计行中 未税金额 != 含税金额 / (1 + 税率)
    var taxValue = new BigNumber(1), itSumAmount = new BigNumber(0), ntSumAmount = new BigNumber(0);
    var maxAmount = new BigNumber(0), maxAmountRowIndex = -1, maxAdtAmount = new BigNumber(0), maxAntAmount = new BigNumber(0);
    var rows = $(el.tbDetails).datagrid('getSelections');
    for (var n = 0; n < rows.length; n++) {
        var item = rows[n];
        ntSumAmount = ntSumAmount.plus(item.asNotTaxAmount);
        itSumAmount = itSumAmount.plus(item.asTaxAmount);
        taxValue = new BigNumber(item.asTaxValue);

        if(maxAmount.eq(0)){
            maxAmount = new BigNumber(item.asTaxAmount);
            maxAdtAmount = new BigNumber(item.asDiffTaxAmount);
            maxAntAmount = new BigNumber(item.asNotTaxAmount);
            maxAmountRowIndex = $(el.tbDetails).datagrid('getRowIndex', item);
        }
        if(!maxAmount.eq(0) && maxAmount.lt(item.asTaxAmount)){
            maxAmount = new BigNumber(item.asTaxAmount);
            maxAdtAmount = new BigNumber(item.asDiffTaxAmount);
            maxAntAmount = new BigNumber(item.asNotTaxAmount);
            maxAmountRowIndex = $(el.tbDetails).datagrid('getRowIndex', item);
        }
    }

    var detail = $(el.tbDetails).datagrid('getSelections'), rebate = "", deduction = "";
    var itAmount = new BigNumber(0), ntAmount = new BigNumber(0), statementNum = new BigNumber(0);
    for (var i = 0; i < detail.length; i++) {
        var item1 = detail[i];
        ntAmount = ntAmount.plus(item1.asNotTaxAmount);
        itAmount = itAmount.plus(item1.asTaxAmount);
        statementNum = statementNum.plus(item1.statementNum);
    }

    var advanceDetail = $(el.tbAdvance).datagrid('getRows');
    var allBudInnerMoney = new BigNumber(0);
    for (var i = 0; i < advanceDetail.length; i++) {
        var item2 = advanceDetail[i];
        allBudInnerMoney = allBudInnerMoney.plus(item2.alreadyAdvanceAmount);
    }

    if(0 !== rows.length){
        // 更新页脚行的值并刷新
        var footers = $(el.tbDetails).datagrid('getFooterRows');
        footers[0]['asNotTaxAmount'] = ntAmount.toFormat(2);
        footers[0]['asTaxAmount'] = itAmount.toFormat(2);
        footers[0]['asDiffTaxAmount'] = itAmount.minus(ntAmount).toFormat(2);
        footers[0]['statementNum'] = statementNum.toNumber();
        $(el.tbDetails).datagrid('reloadFooter');
    }else {
        // 更新页脚行并载入新数据
        $(el.tbDetails).datagrid('reloadFooter', [
            {
                asNotTaxAmount: 0,
                asTaxAmount: 0,
                asDiffTaxAmount: 0,
                statementNum: 0
            }
        ]);
    }

    // ntAmount = ntAmount.plus(rntAmount).plus(dntAmount);
    // itAmount = itAmount.plus(rtAmount).plus(dtAmount);
    $(el.invoiceNetAmount).textbox('setValue', ntAmount.toFormat(2));
    $(el.invoiceTaxAmount).textbox('setValue', itAmount.toFormat(2));
    // $(el.diffAmount).textbox('setValue', itAmount.minus(ntAmount).toFormat(2));
    // $(el.allBudInnerMoney).textbox('setValue', allBudInnerMoney.toFormat(2));
    // $(el.rebateAmount).numberbox('setValue', Number(rtAmount.toFixed(2)));
    // $(el.rntAmount).val(Number(rntAmount.toFixed(2)));
    // $(el.deductionAmount).numberbox('setValue', Number(dtAmount.toFixed(2)));
    // $(el.dntAmount).val(Number(dntAmount.toFixed(2)));
}

/**
 * 文档就绪
 */
$(function () {
    $(el.dataPanel).panel({
        collapsible: true,
        onCollapse: function () {
            console.log(11)
            $(el.dataTabs).tabs('select', 0);

            //对账明细
            $(el.tbDetails).datagrid('resize', {height: el.bodyHeight - 80});
            //扣款信息
            // $(el.tbDeduction).edatagrid('resize', {height: el.bodyHeight - 80});
            //审批信息
            $(el.tbApproveLog).datagrid('resize', {height: el.bodyHeight - 80});
            //关联发票
            $(el.tbInvoice).edatagrid('resize', {height: el.bodyHeight - 80});
            //预付款
            $(el.tbAdvance).edatagrid('resize', {height: el.bodyHeight - 80});
            //关联附件
            // $(el.tbFile).edatagrid('resize', {height: el.bodyHeight - 80});
        },
        onExpand: function () {
            console.log(22)
            $(el.dataTabs).tabs('select', 0);

            var dataDiv = $(el.dataDiv).outerHeight() + 90;
            //对账明细
            $(el.tbDetails).datagrid('resize', {height: el.bodyHeight - dataDiv});
            //扣款信息
            // $(el.tbDeduction).edatagrid('resize', {height: el.bodyHeight - dataDiv});
            //审批信息
            $(el.tbApproveLog).datagrid('resize', {height: el.bodyHeight - dataDiv});
            //关联发票
            $(el.tbInvoice).edatagrid('resize', {height: el.bodyHeight - dataDiv});
            //预付款
            $(el.tbAdvance).edatagrid('resize', {height: el.bodyHeight - dataDiv});
            //关联附件
            // $(el.tbFile).edatagrid('resize', {height: el.bodyHeight - dataDiv});
        }
    });

    initPageInput();
    // findUserTableColumn();

    initTbDetails();

    // initTbDeduction();

    // initTbRebate();

    initTbIaApprove();

    initTbAdvance();
});
/**
 * 初始化页面输入框
 */
function initPageInput() {
    // 初始化税率代码下拉框
    $('#taxCode').combobox({
        required: true,
        panelHeight: 300,
        panelWidth: 340,
        limitToList: 'true',
        valueField: 'taxCode',
        method: 'GET',
        textField: 'codeWithDesc',
        url: g.ctx + 'pc/sys/tax/rest/listAll',
        prompt: '--- 请选择 ---'
    });
    // 付款条件
    $('#paymentTermCode').combobox({
        required: true,
        limitToList: 'true',
        panelHeight: 'auto',
        panelWidth: 200,
        method: 'GET',
        valueField: 'code',
        textField: 'codeWithDesc',
        prompt: '--- 请选择 ---',
        url: g.ctx + 'pc/sys/dict/getDicByType?type=payment_method',
    });
    getExchangeRate($(el.currencyCode).textbox('getValue'));
}
/**
 * 初始化预付款明细表格
 */
function initTbAdvance() {
    var dataDiv = $(el.dataDiv).outerHeight() + 90, orderType = $(el.orderType).val();
    $(el.tbAdvance).datagrid({
        loadMsg: '数据加载中, 请耐心等待......',
        height: el.bodyHeight - dataDiv,
        singleSelect: false,
        rownumbers: true,
        showFooter: true,
        pagination: true,
        queryParams: {invoiceApplyId:$(el.id).val()},
        url: g.ctx + 'pc/finance/invoiced/apply/pa/rest/find/paa/findPaaDetailByInvoiceApplyId',
        frozenColumns: [
            [
                // {
                //     field: 'ck',
                //     checkbox: true
                // },
                {
                    field: 'zlsdh',
                    title: 'SC订单号',
                    width:140
                },
                {
                    field: 'oldPoCode',
                    title: '旧订单号',
                    width:120
                },
                {
                    field: 'poCode',
                    title: '采购订单',
                    width: 200,
                    sortable: true
                },
                {
                    field: 'poItemNo',
                    title: '订单行号',
                    width: 200,
                    sortable: true
                }
            ]
        ],
        columns: [
            [
                {
                    field: 'applyPrice',
                    title: '预付款金额',
                    width: 200,
                    sortable: true,
                    formatter:function (value) {
                        if (!value) {
                            return '';
                        }
                        return  formatNumber(value, 4, 1).toString().replace(/(?:\.0*|(\.\d+?)0+)$/,'$1');
                    }
                },
                {
                    field: 'strikePrice',
                    title: '本次冲账金额',
                    width: 200,
                    sortable: true,
                    formatter:function (value) {
                        if (!value) {
                            return '';
                        }
                        return  formatNumber(value, 4, 1).toString().replace(/(?:\.0*|(\.\d+?)0+)$/,'$1');
                    }
                },
                {
                    field: 'yearNum',
                    title: '年度',
                    width:100
                },
                {
                    field: 'currencyCode',
                    title: '币种',
                    width:100
                },
                {
                    field: 'applyNo',
                    title: '预付款凭证号',
                    width:140
                },
                {
                    field: 'applyItemNo',
                    title: '预付款凭证行号',
                    width:120
                },
                {
                    field: 'remark',
                    title: '摘要',
                    width:200
                },
            ]
            ]
    });
}

/**
 * 初始化开票申请明细表格
 */
function initTbDetails() {
    var dataDiv = $(el.dataDiv).outerHeight() + 90, orderType = $(el.orderType).val();

    $(el.tbDetails).datagrid({
        loadMsg: '数据加载中, 请耐心等待......',
        height: el.bodyHeight - dataDiv,
        url: g.ctx + 'pc/finance/invoiced/apply/detail/rest/find/by/apply/id',
        singleSelect: true,
        showFooter: true,
        rownumbers: true,
        // 定义从服务器对数据进行排序
        remoteSort: false,
        // 定义是否允许多列排序
        // multiSort: true,
        pagination: false,
        queryParams: {
            iaId: $(el.id).val(),
            // 后台是否计算分摊 0:否 1:是
            becShare: 0
        },
        columns: [
            [
                {
                    field: 'itemNo',
                    title: '行号',
                    width: 70,
                    sortable: true
                },
                // 开票申请相关
                {
                    field: 'statementNum',
                    title: '对账数量',
                    width: 70,
                    sortable: true,
                    formatter: function (value) {
                        if(Number(value) < 0){
                            return '<span style="color: red;">' + value + '</span>';
                        }
                        return value;
                    }
                },
                {
                    field: 'notTaxPrice',
                    title: '到票未税单价',
                    width: 90,
                    sortable: true
                },
                {
                    field: 'taxPrice',
                    title: '到票含税单价',
                    width: 90,
                    sortable: true
                },
                {
                    field: 'iaDiffPrice',
                    title: '到票含税单价差异',
                    width: 70,
                    sortable: true
                },
                {
                    field: 'taxCodeDesc',
                    title: '到票税率',
                    width: 160,
                    sortable: true
                },
                {
                    field: 'notTaxAmount',
                    title: '到票未税金额',
                    width: 95,
                    sortable: true
                },
                {
                    field: 'taxAmount',
                    title: '到票含税金额',
                    width: 95,
                    sortable: true
                },
                {
                    field: 'diffTaxAmount',
                    title: '到票税额',
                    width: 70,
                    sortable: true
                },
                {
                    field: 'iaDiffAmount',
                    title: '到票含税金额差异',
                    width: 70,
                    sortable: true
                },
                // 对账相关
                {
                    field: 'asNotTaxPrice',
                    title: '对账未税单价',
                    width: 65,
                    sortable: true
                },
                {
                    field: 'asNotTaxAmount',
                    title: '对账未税金额',
                    width: 95,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 2, 1);
                    }
                },
                {
                    field: 'asTaxCodeDesc',
                    title: '对账税率',
                    width: 160,
                    sortable: true
                },
                {
                    field: 'asTaxPrice',
                    title: '对账含税单价',
                    width: 65,
                    sortable: true
                },
                {
                    field: 'asTaxAmount',
                    title: '对账含税金额',
                    width: 95,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 2, 1);
                    }
                },
                {
                    field: 'asDiffTaxAmount',
                    title: '对账税额',
                    width: 70,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 2, 1);
                    }
                },
                {
                    field: 'asDiffPrice',
                    title: '对账差异含税单价',
                    width: 80,
                    sortable: true,
                    formatter: function (value) {
                        if(value === undefined){
                            return '';
                        }
                        return formatNumber(value, 2, 1);
                    }
                },
                {
                    field: 'asDiffAmount',
                    title: '对账差异含税金额',
                    width: 80,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 2, 1);
                    }
                },
                // 凭证和订单相关
                {
                    field: 'receiveCode',
                    title: '物料凭证',
                    width: 100,
                    sortable: true
                },
                {
                    field: 'receiveRowNo',
                    title: '凭证行号',
                    width: 70,
                    sortable: true
                },
                {
                    field: 'receiveDate',
                    title: '入库日期',
                    width: 100,
                    sortable: true
                },
                {
                    field: 'inOutFlag',
                    title: '出入库标识',
                    width: 85,
                    sortable: true
                },
                {
                    field: 'materialCode',
                    title: '物料编码',
                    width: 90,
                    sortable: true
                },
                {
                    field: 'materialName',
                    title: '物料名称',
                    width: 320,
                    sortable: true
                },
                {
                    field: 'specs',
                    title: '规格',
                    width: 120
                },
                {
                    field: 'materialUnit',
                    title: '单位',
                    width: 65,
                    sortable: true
                },
                {
                    field: 'zlsdh',
                    title: 'SC订单号',
                    width: 100,
                    sortable: true
                },
                {
                    field: 'poCode',
                    title: 'SAP订单号',
                    width: 100,
                    sortable: true,
                    formatter: function (value, row, index) {
                        return "<a href='/purchaseOrder/detailByCode?code=" + window.btoa(value) + "' target='_blank'>" + value + "</a>"
                    }
                },
                {
                    field: 'poItemNo',
                    title: '订单行号',
                    width: 70,
                    sortable: true
                },
                {
                    field: 'subjectTypeName',
                    title: '科目分配类别',
                    width: 65,
                    sortable: true
                },
                {
                    field: 'poTaxCodeDesc',
                    title: '订单税率',
                    width: 160,
                    sortable: true
                },
                {
                    field: 'poNotTaxPrice',
                    title: '订单未税单价',
                    width: 65,
                    sortable: true
                },
                {
                    field: 'poTaxPrice',
                    title: '订单含税单价',
                    width: 65,
                    sortable: true
                },
                {
                    field: 'asNtAmount',
                    title: '订单未税金额',
                    width: 90,
                    sortable: true
                },
                {
                    field: 'asItAmount',
                    title: '订单含税金额',
                    width: 90,
                    sortable: true
                },
                {
                    field: 'asDtAmount',
                    title: '订单税额',
                    width: 90,
                    sortable: true
                },
                {
                    field: 'paymentTermCodeName',
                    title: '付款条件',
                    width: 200,
                    sortable: true
                },
                {
                    field: 'asNo',
                    title: '对账单号',
                    width: 120,
                    sortable: true
                },
                {
                    field: 'asdItemNo',
                    title: '对账单行号',
                    width: 80,
                    sortable: true
                },
                {
                    field: 'contractNo',
                    title: '合同号',
                    width: 120
                }
            ]
        ],
        toolbar: [
            {
                text: '导出',
                iconCls: 'icon-download',
                handler: function () {
                    detailDataExport();
                }
            },
            // '-',
            // {
            //     text: '调整布局',
            //     iconCls: 'icon-man',
            //     handler: function () {
            //         adjustTableColumn();
            //     }
            // }
        ],
        onLoadSuccess: function () {
            // 默认全部选中
            $(el.tbDetails).datagrid('selectAll');
            calculationPageAmount();
        },
    });
}


/**
 * 初始化返利信息表格
 */
// function initTbRebate() {
//     var dataDiv = $(el.dataDiv).outerHeight() + 90;
//
//     $(el.tbRebate).datagrid({
//         loadMsg: '数据加载中, 请耐心等待......',
//         height: el.bodyHeight - dataDiv,
//         url: g.ctx + 'fin/invoiced/apply/detail/rest/find/rebate/by/apply/id',
//         singleSelect: false,
//         showFooter: true,
//         rownumbers: true,
//         // 定义从服务器对数据进行排序
//         remoteSort: false,
//         // 定义是否允许多列排序
//         // multiSort: true,
//         pagination: false,
//         queryParams: {iaId: $(el.id).val()},
//         frozenColumns: [
//             [
//                 {
//                     field: 'rebateId',
//                     title: '附件查看',
//                     width: 80,
//                     formatter: function (value) {
//                         if(value){
//                             return '<a href="javascript:void(0)" onclick="lookDrAttachment(this)" data-type="2" data-id="' + value + '">查看</a>';
//                         }
//                         return '';
//                     }
//                 },
//                 {
//                     field: 'asNo',
//                     title: '对账单号',
//                     width: 130,
//                     sortable: true
//                 },
//                 {
//                     field: 'rebateNo',
//                     title: '返利单号',
//                     width: 140,
//                     sortable: true
//                 },
//                 {
//                     field: 'rebateType',
//                     title: '返利类型',
//                     width: 120,
//                     sortable: true,
//                     formatter: function (value) {
//                         if(Number(value) === 1){
//                             return '购方对供方返利';
//                         }
//                         if(Number(value) === 2) {
//                             return '供方对购方返利';
//                         }
//                         return '';
//                     }
//                 },
//                 {
//                     field: 'rebateAmount',
//                     title: '含税返利金额(元)',
//                     width: 110,
//                     sortable: true,
//                     formatter: function (value) {
//                         if (!value) {
//                             return '';
//                         }
//                         return formatNumber(value, 2, 1);
//                     }
//                 },
//                 {
//                     field: 'taxCodeDesc',
//                     title: '税率',
//                     width: 160,
//                     sortable: true
//                 }
//             ]
//         ],
//         columns: [
//             [
//                 {
//                     field: 'rebateReason',
//                     title: '返利原因',
//                     width: 300
//                 },
//                 {
//                     field: 'rebateTime',
//                     title: '返利时间',
//                     width: 110,
//                     sortable: true,
//                     formatter: function (value) {
//                         if (value) {
//                             return moment(value).format("YYYY-MM-DD");
//                         }
//                         return '';
//                     }
//                 },
//                 {
//                     field: 'companyCode',
//                     title: '公司代码',
//                     width: 80
//                 },
//                 {
//                     field: 'companyName',
//                     title: '公司名称',
//                     width: 180
//                 },
//                 {
//                     field: 'supplierCode',
//                     title: '供应商编码',
//                     width: 90
//                 },
//                 {
//                     field: 'supplierName',
//                     title: '供应商名称',
//                     width: 270
//                 },
//                 {
//                     field: 'createTime',
//                     title: '创建时间',
//                     width: 140,
//                     sortable: true,
//                     formatter: function (value) {
//                         if (value) {
//                             return moment(value).format("YYYY-MM-DD HH:mm:ss");
//                         }
//                         return '';
//                     }
//                 }
//             ]
//         ]
//     });
// }


/**
 * 初始化扣款信息表格
 */
// function initTbDeduction() {
//     var dataDiv = $(el.dataDiv).outerHeight() + 90;
//
//     $(el.tbDeduction).datagrid({
//         loadMsg: '数据加载中, 请耐心等待......',
//         height: el.bodyHeight - dataDiv,
//         url: g.ctx + 'fin/invoiced/apply/detail/rest/find/deduction/by/apply/id',
//         singleSelect: false,
//         showFooter: true,
//         rownumbers: true,
//         // 定义从服务器对数据进行排序
//         remoteSort: false,
//         // 定义是否允许多列排序
//         // multiSort: true,
//         pagination: false,
//         queryParams: {iaId: $(el.id).val()},
//         frozenColumns: [
//             [
//                 {
//                     field: 'deductionId',
//                     title: '附件查看',
//                     width: 80,
//                     formatter: function (value) {
//                         if(value){
//                             return '<a href="javascript:void(0)" onclick="lookDrAttachment(this)" data-type="1" data-id="' + value + '">查看</a>';
//                         }
//                         return '';
//                     }
//                 },
//                 {
//                     field: 'asNo',
//                     title: '对账单号',
//                     width: 140,
//                     sortable: true
//                 },
//                 {
//                     field: 'deductionNo',
//                     title: '扣款编号',
//                     width: 140,
//                     sortable: true
//                 },
//                 {
//                     field: 'deductionTypeDesc',
//                     title: '扣款类型',
//                     width: 90,
//                     sortable: true
//                 },
//                 {
//                     field: 'deductionAmount',
//                     title: '含税扣款金额(元)',
//                     width: 110,
//                     sortable: true,
//                     formatter: function (value) {
//                         if (!value) {
//                             return '';
//                         }
//                         return formatNumber(value, 2, 1);
//                     }
//                 },
//                 {
//                     field: 'taxCodeDesc',
//                     title: '税率',
//                     width: 160,
//                     sortable: true
//                 }
//             ]
//         ],
//         columns: [
//             [
//                 {
//                     field: 'deductionReason',
//                     title: '扣款原因',
//                     width: 300
//                 },
//                 {
//                     field: 'deductionTime',
//                     title: '扣款时间',
//                     width: 90,
//                     sortable: true,
//                     formatter: function (value) {
//                         if (value) {
//                             return moment(value).format("YYYY-MM-DD");
//                         }
//                         return '';
//                     }
//                 },
//                 {
//                     field: 'associatedOaNo',
//                     title: 'OA流程单号',
//                     width: 120,
//                     sortable: true
//                 },
//                 {
//                     field: 'associatedNo',
//                     title: 'SRM业务单据',
//                     width: 140,
//                     formatter: function (value, row) {
//                         if(!row.associatedRowNo){
//                             return value;
//                         }else {
//                             return value + '|' + row.associatedRowNo;
//                         }
//                     }
//                 },
//                 {
//                     field: 'companyCode',
//                     title: '公司代码',
//                     width: 80
//                 },
//                 {
//                     field: 'companyName',
//                     title: '公司名称',
//                     width: 180
//                 },
//                 {
//                     field: 'supplierCode',
//                     title: '供应商编码',
//                     width: 90
//                 },
//                 {
//                     field: 'supplierName',
//                     title: '供应商名称',
//                     width: 270
//                 },
//                 {
//                     field: 'createTime',
//                     title: '创建时间',
//                     width: 140,
//                     sortable: true,
//                     formatter: function (value) {
//                         if (value) {
//                             return moment(value).format("YYYY-MM-DD HH:mm:ss");
//                         }
//                         return '';
//                     }
//                 }
//             ]
//         ]
//     });
// }


/**
 * 初始化审批信息表格
 */
function initTbIaApprove() {
    var dataDiv = $(el.dataDiv).outerHeight() + 90;

    $(el.tbIaApprove).datagrid({
        loadMsg: '数据加载中, 请耐心等待......',
        height: el.bodyHeight - dataDiv,
        url: g.ctx + 'pc/finance/invoiced/apply/detail/rest/find/ia/approve/log',
        singleSelect: false,
        rownumbers: true,
        // 定义从服务器对数据进行排序
        remoteSort: false,
        // 定义是否允许多列排序
        // multiSort: true,
        pagination: true,
        queryParams: {
            iaId: $(el.id).val()
        },
        columns: [
            [
                {
                    field: 'approveNode',
                    title: '审批节点描述',
                    width: 120,
                    formatter: function (value) {
                        if(Number(value) === 1){
                            return '对账确认';
                        }else {
                            return '财务节点';
                        }
                    }
                },
                {
                    field: 'approveResult',
                    title: '审批结果',
                    width: 120,
                    formatter: function (value) {
                        switch (Number(value)){
                            case 0:
                                return '<span style="color: red;">拒绝</span>';
                            case 1:
                                return '<span style="color: green;">同意</span>';
                            case 2:
                                return '<span style="color: orange;">采购撤销</span>';
                            default:
                                return '';
                        }
                    }
                },
                {
                    field: 'approveOpinion',
                    title: '审批意见',
                    width: 300
                },
                {
                    field: 'approveUserName',
                    title: '审批用户姓名',
                    width: 130
                },
                {
                    field: 'approveTime',
                    title: '审批时间',
                    width: 140,
                    sortable: true,
                    formatter: function (value) {
                        if (value) {
                            return moment(value).format("YYYY-MM-DD HH:mm:ss");
                        }
                        return '';
                    }
                }
            ]
        ]
    });
}


/**
 * 明细导出
 */
function detailDataExport() {
    var rows = $(el.tbDetails).datagrid('getRows');
    $(el.tbDetails).datagrid('toExcel', {
        filename: '开票申请数据.xls',
        rows: rows,
        worksheet: '开票申请明细'
    });
}


/**
 * 查看扣款或返利附件
 *
 * @param ele 当前操作DOM节点
 */
function lookDrAttachment(ele) {
    var drId = $(ele).data('id'), attType = $(ele).data('type');
    var url = g.ctx + 'fin/common/look/dr/attachment?drId=' + drId + '&attType=' + attType;
    var lookDrAttDialog = openDialog('lookDrAtt', url, {
        title: '附件查看',
        width: '35%',
        height: '45%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            lookDrAttDialog.dialog('destroy');
        },
        buttons: [
            {
                text: '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                    lookDrAttDialog.dialog('close');
                }
            }
        ]
    });
}


/**
 * 调整表格列布局
 */
function adjustTableColumn() {
    var url = g.ctx + 'fin/common/adjust/table/column?pageTable=3';
    el.adjustTableColumnDialog = openDialog('adjustTableColumn', url, {
        title: '调整表格列布局',
        width: '970px',
        height: '80%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.adjustTableColumnDialog.dialog('destroy');
        },
        buttons: [
            {
                text: '确定并保存设置',
                iconCls: 'icon-ok',
                handler: function () {
                    topWin.frames['adjustTableColumn'].confirmAndSaveSetUp(function (data) {
                        el.userTableColumn = [];
                        for(var i = 0; i < data.length; i++){
                            el.userTableColumn.push(data[i].columnField);
                        }

                        initTbDetails();
                        el.adjustTableColumnDialog.dialog('close');
                    });
                }
            },
            {
                text: '确定',
                iconCls: 'icon-ok',
                handler: function () {
                    topWin.frames['adjustTableColumn'].confirmSetUp(function (data) {
                        el.userTableColumn = [];
                        for(var i = 0; i < data.length; i++){
                            el.userTableColumn.push(data[i].columnField);
                        }

                        initTbDetails();
                        el.adjustTableColumnDialog.dialog('close');
                    });
                }
            },
            {
                text: '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                    el.adjustTableColumnDialog.dialog('close');
                }
            }
        ]
    });
}


/**
 * 获取当前用户表格列布局
 */
function findUserTableColumn() {
    $.ajax({
        type: 'POST',
        url: g.ctx + 'fin/table/column/rest/user/setting?pageTable=3&isAuthority=1',
        async: false,
        success: function (data) {
            for(var i = 0; i < data.length; i++){
                el.userTableColumn.push(data[i].columnField);
            }
        }
    });
}
