/**
 * 开票申请编辑选择对账单明细弹框页面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',
    factoryCode: '#factoryCode',
    currencyCode: '#currencyCode',
    orderType: '#orderType',
    excludeAsdIds: '#excludeAsdIds',
    tempDelAsdIds: '#tempDelAsdIds',
    paymentTermCode: '#paymentTermCode',
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};


$(function () {
    loadStatementDetailGird();
});


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

    $(el.tb).datagrid({
        loadMsg: '数据加载中, 请耐心等待......',
        height: el.bodyHeight - searchHeight,
        url: g.ctx + 'pc/finance/account/statement/detail/rest/provide/create/invoiced/apply',
        singleSelect: false,
        rownumbers: true,
        pagination: false,
        queryParams: {
            supplierCode: $(el.supplierCode).val(),
            companyCode: $(el.companyCode).val(),
            factoryCode: $(el.factoryCode).val(),
            currencyCode: $(el.currencyCode).val(),
            orderType: $(el.orderType).val(),
            excludeAsdIds: $(el.excludeAsdIds).val(),
            tempDelAsdIds: $(el.tempDelAsdIds).val(),
            paymentTermCode: $(el.paymentTermCode).val()
        },
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'receiveCode',
                    title: '入库凭证号',
                    width: 100
                },
                {
                    field: 'receiveRowNo',
                    title: '凭证行号',
                    width: 60
                },
                {
                    field: 'receiveDate',
                    title: '入库日期',
                    width: 100
                },
                {
                    field: 'inOutFlag',
                    title: '出入库标识',
                    width: 85
                },
                {
                    field: 'asQuantity',
                    title: '对账数量',
                    width: 90,
                    formatter: function (value) {
                        if(Number(value) < 0){
                            return '<span style="color: red;">' + value + '</span>';
                        }
                        return value;
                    }
                },
                {
                    field: 'asTaxCodeDesc',
                    title: '对账税率',
                    width: 160
                },
                {
                    field: 'asTaxPrice',
                    title: '对账含税单价',
                    width: 100
                },
                {
                    field: 'asTaxAmount',
                    title: '对账含税金额',
                    width: 100,
                    formatter: function (value) {
                        if(Number(value) < 0){
                            return '<span style="color: red;">' + formatNumber(value, 2, 1) + '</span>';
                        }
                        return formatNumber(value, 2, 1);
                    }
                },
                {
                    field: 'asNotTaxPrice',
                    title: '对账未税单价',
                    width: 100
                },
                {
                    field: 'asNotTaxAmount',
                    title: '对账未税金额',
                    width: 100,
                    formatter: function (value) {
                        if(Number(value) < 0){
                            return '<span style="color: red;">' + formatNumber(value, 2, 1) + '</span>';
                        }
                        return formatNumber(value, 2, 1);
                    }
                },
                {
                    field: 'asDiffTaxAmount',
                    title: '对账税额',
                    width: 100,
                    formatter: function (value) {
                        if(Number(value) < 0){
                            return '<span style="color: red;">' + formatNumber(value, 2, 1) + '</span>';
                        }
                        return formatNumber(value, 2, 1);
                    }
                },
                {
                    field: 'asNo',
                    title: '对账单号',
                    width: 140
                },
                {
                    field: 'asdItemNo',
                    title: '对账单行号',
                    width: 80
                },
                {
                    field: 'zlsdh',
                    title: 'SC订单号',
                    width: 100,
                    sortable: true
                },
                {
                    field: 'poCode',
                    title: 'SAP订单号',
                    width: 100
                },
                {
                    field: 'poItemNo',
                    title: '订单行号',
                    width: 70
                },
                {
                    field: 'materialCode',
                    title: '物料编码',
                    width: 90
                },
                {
                    field: 'materialName',
                    title: '物料名称',
                    width: 400
                },
                // 集采订单信息
                // {
                //     field: 'jcPoCode',
                //     title: '集采SAP订单号',
                //     width: 110
                // },
                // {
                //     field: 'jcPoItemNo',
                //     title: '行号',
                //     width: 70
                // },
                // {
                //     field: 'jcFactoryCode',
                //     title: '集采工厂',
                //     width: 80
                // }
            ]
        ]
    });
}


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

    var rvIds = [];
    for(var i = 0; i < rows.length; i++){
        rvIds.push(rows[i].receiveId);
    }

    // 校验选中的行是否保存
    var loading = topWin.layer.msg('校验选中的行是否保存, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
    $.ajax({
        type: 'POST',
        url: g.ctx + 'pc/finance/receipt/voucher/rest/check/write/off',
        data: {
            supplierCode: $(el.supplierCode).val(),
            rvIds: JSON.stringify(rvIds),
            receiveType: $(el.orderType).val()
        },
        success: function () {
            topWin.layer.close(loading);
            callback(rows);
        },
        error: function (data) {
            topWin.layer.close(loading);
            var message = "需要选择以下凭证: </br>";
            topWin.layer.alert(message + data.responseJSON.message, {icon: 2});
        }
    });
}


/**
 * 数据查询
 */
function searchStatementItemData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.asNo = $.trim($(el.asNo).val());
    queryParams.asdItemNo = $.trim($(el.asdItemNo).val());
    queryParams.receiveCode = $.trim($(el.receiveCode).val());
    queryParams.receiveRowNo = $.trim($(el.receiveRowNo).val());
    queryParams.poCode = $.trim($(el.poCode).val());
    queryParams.poItemNo = $.trim($(el.poItemNo).val());
    queryParams.materialCode = $.trim($(el.materialCode).val());
    queryParams.materialName = $.trim($(el.materialName).val());

    queryParams.supplierCode = $.trim($(el.supplierCode).val());
    queryParams.companyCode = $.trim($(el.companyCode).val());
    queryParams.factoryCode = $.trim($(el.factoryCode).val());
    queryParams.currencyCode = $.trim($(el.currencyCode).val());
    queryParams.orderType = $.trim($(el.orderType).val());
    queryParams.excludeAsdIds = $(el.excludeAsdIds).val();


    $(el.tb).datagrid('load');
}


/**
 * 清空查询条件
 */
function resetStatementItemSearch() {
    $(el.asNo).val('');
    $(el.asdItemNo).val('');
    $(el.receiveCode).val('');
    $(el.receiveRowNo).val('');
    $(el.poCode).val('');
    $(el.poItemNo).val('');
    $(el.materialCode).val('');
    $(el.materialName).val('');

    searchStatementItemData();
}