/**
 * 开票申请编辑选择关联发票明细弹框页面JS
 *
 * Created by heqiaohui on 2021/3/29.
 */
'use strict';
var el = {
    companyCode: '#companyCode',
    companyName: '#companyName',
    supplierCode: '#supplierCode',
    supplierName: '#supplierName',

    tb: '#tb',
    editIndex: undefined,
    bodyHeight: document.documentElement.clientHeight
};


$(function () {
    loadInvoiceDetailGird();
});


/**
 * 加载关联发票明细数据
 */
function loadInvoiceDetailGird() {
    var searchHeight = $(el.searchForm).outerHeight();

    $(el.tb).datagrid({
        loadMsg: '数据加载中, 请耐心等待......',
        height: el.bodyHeight - searchHeight,
        url: g.ctx + 'fin/invoiced/leshui/rest/find/invoice',
        singleSelect: false,
        rownumbers: true,
        pagination: false,
        showFooter: true,
        queryParams: {
            supplierCode: $(el.supplierCode).val(),
            supplierName: $(el.supplierName).val(),
            companyCode: $(el.companyCode).val(),
            companyName: $(el.companyName).val()
        },
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '行id',
                    hidden: true
                },
                {
                    field: 'invoiceId',
                    title: '发票id',
                    hidden: true
                },
                {
                    field: 'invoiceCode',
                    title: '发票编号',
                    width: 100
                },
                {
                    field: 'invoiceNumber',
                    title: '发票号',
                    width: 100
                },
                {
                    field: 'billingDate',
                    title: '发票日期',
                    width: 100,
                    formatter: function (value) {
                        if (value) {
                            return moment(value).format("YYYY-MM-DD");
                        }
                        return '';
                    }
                },
                {
                    field: 'taxRate',
                    title: '税率',
                    width: 100
                },
                {
                    field: 'amount',
                    title: '含税金额',
                    width: 85
                },
                {
                    field: 'amountExcludeTax',
                    title: '无税金额',
                    width: 85
                },
                {
                    field: 'tax',
                    title: '税额',
                    width: 100
                },
                {
                    field: 'buyerTaxNumber',
                    title: '公司代码',
                    width: 160
                },
                {
                    field: 'buyerTaxName',
                    title: '公司名称',
                    width: 180
                },
                // {
                //     field: 'asNotTaxPrice',
                //     title: '供应商编码',
                //     width: 100
                // },
                {
                    field: 'sellerTaxName',
                    title: '供应商名称',
                    width: 180
                },
                {
                    field: 'invoiceStatusFlag',
                    title: '状态',
                    width: 100,
                    formatter: function (value) {
                        if (value == '0') {
                            return '<span style="color: green;">已生效票据</span>';
                        }
                        if (value == '1') {
                            return '<span style="color: red;">已关联单据</span>';
                        }
                        if (value == '2') {
                            return '<span style="color: red;">已经过账</span>';
                        }
                        if (value == '3') {
                            return '<span style="color: red;">已冲销</span>';
                        }
                        if (value == '10') {
                            return '<span style="color: red;">初始数据，未核对</span>';
                        }
                        if (value == '11') {
                            return '<span style="color: red;">未价税分离</span>';
                        }
                        return '';
                    }
                },
                {
                    field: 'isCheck',
                    title: '是否已经查验',
                    hidden: true
                },
                {
                    field: 'checkResult',
                    title: '查验结果',
                    width: 100
                }

            ]
        ],
        toolbar: el.tbToolbar,
        onBeforeLoad: function() {
            $(el.tb).datagrid('reloadFooter',
                [
                    {
                        invoiceCode: '合计',
                        amountExcludeTax: 0,
                        amount: 0,
                        tax:0
                    }
                ]
            );
        },
        onCheck: function (rowIndex, rowData) {
            el.editIndex = rowIndex;
            updateRowData(rowData,1);
        },
        onUncheck: function (rowIndex, rowData) {
            el.editIndex = rowIndex;
            updateRowData(rowData,2);
        },
    });
}



/**
 * 更新本次申请金额
 *
 * @param data 本次申请金额
 */
var amountExcludeTaxTotal = new BigNumber(0), amountTotal = new BigNumber(0), tax = new BigNumber(0);
function updateRowData(data,flag) {
    if(flag==1){
        amountExcludeTaxTotal = amountExcludeTaxTotal.plus(data.amountExcludeTax);
        amountTotal = amountTotal.plus(data.amount);
        tax = tax.plus(data.tax);
    }

    if(flag==2){
        amountExcludeTaxTotal = amountExcludeTaxTotal.minus(data.amountExcludeTax);
        amountTotal = amountTotal.minus(data.amount);
        tax = tax.minus(data.tax);
    }

    // 更新页脚行的值并刷新
    var footers = $(el.tb).datagrid('getFooterRows');
    footers[0]['amountExcludeTax'] = amountExcludeTaxTotal.toFormat(2);
    footers[0]['amount'] = amountTotal.toFormat(2);
    footers[0]['tax'] = tax.toFormat(2);
    $(el.tb).datagrid('reloadFooter');
}

/**
 * 确认选择
 *
 * @param callback 回调函数
 */
function confirmChose(callback) {
    var rows = $(el.tb).datagrid('getSelections');
    if(rows.length === 0){
        topWin.layer.alert('请选择关联发票数据', {icon: 0});
        return false;
    }

    callback(rows);
}

