/**
 * 对账单采购审批页面中选择扣款信息弹框JS
 *
 * Created by li.kui on 2020/8/4.
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    userId: '#userId',
    supplierCode: '#supplierCode',
    companyCode: '#companyCode',
    currencyCode: '#currencyCode',
    supCodeOrName: '#supCodeOrName',
    statementNo: '#statementNo',
    createTimeStart: '#createTimeStart',
    createTimeEnd: '#createTimeEnd',
    status: '#status',
    orderType: '#orderType',
    oaProcessNo: '#oaProcessNo',
    tb: '#tb',
    addOrEditDialog: null,
    optConfirmDialog: null,
    forceDelDialog: null,
    lookDialog: null,
    bodyHeight: document.documentElement.clientHeight
};


$(function () {
    initNecessaryDatas('pc/sys/purchaseGroup/rest/listAll,pc/sys/company/rest/listAll,pc/sys/tax/rest/listAll')
    initPageComBoBox();

    initDataGrid();
});


/**
 * 初始化页面下拉框
 */
function initPageComBoBox() {
    // 初始化公司代码下拉框
}


/**
 * 初始化扣款数据列表
 */
function initDataGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);

    $(el.tb).datagrid({
        loadMsg: '数据加载中, 请耐心等待......',
        url: g.ctx + 'pc/finance/cost/account/statement/rest/account/or/apply/list?supplierCode='+$(el.supplierCode).val()+'&companyCode='+$(el.companyCode).val()+'&currencyCode='+$(el.currencyCode).val(),
        height: el.bodyHeight - searchHeight-20,
        rownumbers: true,
        singleSelect: false,
        pagination: false,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        queryParams: {
            status: 9
        },
        frozenColumns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'accountStatementNo',
                    title: '对账单号',
                    width: 140,
                    sortable: true
                },{
                field: 'accountStatementYear',
                title: '对账单年',
                width: 140

            },{
                field: 'accountStatementMonth',
                title: '对账单月',
                width: 140
            },
                {
                    field: 'invoiceNo',
                    title: '开票申请单号',
                    width: 140,
                    sortable: true
                },
                // {
                //     field: 'salesAgreementSignStatus',
                //     title: '购销合同文件状态',
                //     width: 140,
                //     formatter: function (value, row) {
                //         if (row.contractSupplementStatus === 1||row.contractSupplementStatus === 2){
                //             return '<span style="color: red;">' + (row.contractSupplementStatus === 1?'已上传未复核' : '已上传复核未通过')+ '</span>';
                //         }else {
                //             if (!value) {
                //                 return '<span style="color: red;">未上传</span>';
                //             }
                //             return '<span style="color: red;">已上传</span>';
                //         }
                //     }
                // },
            ]
        ],
        columns: [
            [
                {
                    field: 'supplierCode',
                    title: '供应商编码',
                    width: 90,
                    sortable: true
                },
                {
                    field: 'supplierName',
                    title: '供应商名称',
                    width: 270,
                    sortable: true
                },
                {
                    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: 'notTaxAmount',
                    title: '对账未税总金额',
                    width: 110,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'taxAmount',
                    title: '对账含税总金额',
                    width: 110,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'strikePriceAmount',
                    title: '对账冲账金额',
                    width: 110,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'actualUnpaidAmount',
                    title: '对账实际应付金额',
                    width: 110,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'applyAmount',
                    title: '可申请金额',
                    width: 110,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'alreadyApplyAmount',
                    title: '已申请金额',
                    width: 110,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'alreadyPaymentAmount',
                    title: '已付款金额',
                    width: 110,
                    sortable: true,
                    formatter: function (value) {
                        return formatNumber(value, 4, 1);
                    }
                },
                {
                    field: 'invoiceCompleteFlag',
                    title: '是否开票',
                    width: 100,
                    sortable: true,
                    formatter: function (value) {
                        if (Number(value) === 0) {
                            return '否';
                        }
                        return '<span style="color: green;">是</span>';
                    }
                },
                {
                    field: 'companyCode',
                    title: '采购公司',
                    width: 240,
                    formatter: function (value, row) {
                        return getNecessaryDataName('pc/sys/company/rest/listAll',value);
                    }
                },
                {
                    field: 'currencyCode',
                    title: '币种',
                    width: 70,
                    sortable: true
                },
                {
                    field: 'remark',
                    title: '备注',
                    width: 120,
                    sortable: true
                },
                {
                    field: 'taxCode',
                    title: '税率',
                    width: 100,
                    formatter: function (value) {
                        return getNecessaryDataName('pc/sys/tax/rest/listAll',value);
                    }
                },
                {
                    field: 'purchaseGroup',
                    title: '采购组',
                    width: 100,
                    formatter: function (value) {
                        return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll',value);
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 140,
                    sortable: true,
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        }
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                },
                {
                    field: 'updateTime',
                    title: '最后更新时间',
                    width: 140,
                    sortable: true,
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        }
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                }
            ]
        ],
    });
}


/**
 * 确认选择
 */
function confirmChose(callback) {
    callback($(el.tb).datagrid('getSelections'));
}


/**
 * 查询对账单数据
 */
function searchAsData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.statementNo = $.trim($(el.statementNo).val());
    queryParams.createTimeStart = $(el.createTimeStart).datebox('getValue');
    queryParams.createTimeEnd = $(el.createTimeEnd).datebox('getValue');
    $(el.tb).datagrid('load');
}

/**
 * 重置查询
 */
function resetAsSearch() {
    $(el.statementNo).val('');
    $(el.createTimeStart).datebox('setValue', '');
    $(el.createTimeEnd).datebox('setValue', '');
    searchAsData();
}