/**
 * 开票申请编辑选择对账单明细弹框页面JS
 *
 * Created by li.kui on 2020/8/14.
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    asNo: '#asNo',
    asdItemNo: '#asdItemNo',
    receiveCode: '#receiveCode',
    receiveRowNo: '#receiveRowNo',
    poCode: '#poCode',
    poItemNo: '#poItemNo',
    materialCode: '#materialCode',
    materialName: '#materialName',
    tempDelAsdId: '#tempDelAsdId',
    supplierCode: '#supplierCode',
    companyCode: '#companyCode',
    invoiceApplyId: '#invoiceApplyId',
    poCodeItemNoList:'#poCodeItemNoList',
    factoryCode: '#factoryCode',
    currencyCode: '#currencyCode',
    orderType: '#orderType',
    excludeAsdIds: '#excludeAsdIds',
    tempDelAsdIds: '#tempDelAsdIds',
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};


$(function () {
    loadAdvanceDetailGird();
});


/**
 * 加载对账单明细数据
 */
function loadAdvanceDetailGird() {
    var searchHeight = $(el.searchForm).outerHeight();

    $(el.tb).datagrid({
        loadMsg: '数据加载中, 请耐心等待......',
        height: el.bodyHeight - searchHeight,
        url: g.ctx + 'pc/finance/paymentAdvance/apply/detail/rest/provide/create/advance/apply',
        singleSelect: false,
        rownumbers: true,
        pagination: false,
        queryParams: {
            supplierCode: $(el.supplierCode).val(),
            companyCode: $(el.companyCode).val(),
            poCodeItemNoList:  poCodeItemNoList
        },
        frozenColumns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },{
                field: 'paId',
                title: '预付款id',
                width: 120,
                hidden: true
               },
                {
                    field: 'poCode',
                    title: '采购订单',
                    width: 120,
                    sortable: true
                },
                {
                    field: 'poItemNo',
                    title: '订单行号',
                    width: 120,
                    sortable: true
                }
            ]
        ],
        columns: [
            [
                {
                    field: 'applyPrice',
                    title: '预付款金额',
                    width: 120,
                    sortable: true,
                    formatter:function (value) {
                        if (!value) {
                            return '';
                        }
                        return  formatNumber(value, 4, 1).toString().replace(/(?:\.0*|(\.\d+?)0+)$/,'$1');
                    }
                },
                {
                    field: 'reversalMoney',
                    title: '已冲账金额',
                    width: 120,
                    sortable: true,
                    formatter:function (value) {
                        if (!value) {
                            return '0';
                        }
                        return  formatNumber(value, 4, 1).toString().replace(/(?:\.0*|(\.\d+?)0+)$/,'$1');
                    }
                },
                {
                    field: 'surplusReversalMoney',
                    title: '剩余可冲账金额',
                    width: 120,
                    sortable: true,
                    formatter:function (value) {
                        if (!value) {
                            return '';
                        }
                        return  formatNumber(value, 4, 1).toString().replace(/(?:\.0*|(\.\d+?)0+)$/,'$1');
                    }
                },
                {
                    field: 'currencyCode',
                    title: '币种',
                    width:100
                },
                {
                    field: 'applyNo',
                    title: '预付款凭证号',
                    width:100
                },
                {
                    field: 'applyItemNo',
                    title: '预付款凭证行号',
                    width:100
                },{
                field: 'paymentChangeNumber',
                title: '预付款变更单号',
                width:100
                },
                {
                    field: 'paymentChangeItemNo',
                    title: '预付款变更单行',
                    width:100
                },
                {
                    field: 'refundedTaxAmount',
                    title: '已退款含税金额',
                    width:100
                },{
                field: 'dataType',
                title: '预付款变更类型',
                hidden: true,
                width:100
            },
                {
                    field: 'remark',
                    title: '摘要',
                    width:140
                },
            ]
        ]
    });
}


/**
 * 确认选择
 *
 * @param callback 回调函数
 */
function confirmChose(callback) {
    var rows = $(el.tb).datagrid('getSelections');
    if(rows.length === 0){
        topWin.layer.alert('请选择预付款明细数据', {icon: 0});
        return false;
    }
    var selectList = [];
    for (var i = 0; i < rows.length; i++) {
        var head = {
            applyNo: rows[i].applyNo,
            applyItemNo: rows[i].applyItemNo
        }
        selectList.push(head);
    }
    callback(rows);
    // var loading = topWin.layer.msg('数据校验中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
    // $.ajax({
    //     url: g.ctx + 'fin/invoiced/apply/rest/checkAdvanceUse',
    //     type: 'POST',
    //     data: {selectJson: JSON.stringify(selectList),invoiceApplyId:$(el.invoiceApplyId).val()},
    //     success: function () {
    //         topWin.layer.close(loading);
    //         callback(rows);
    //     },
    //     error: function (data) {
    //         topWin.layer.close(loading);
    //         topWin.layer.alert(data.responseJSON.message, {icon: 2});
    //         return false;
    //     }
    // });

}
