// import { getBuildingList, getRecommendArea } from '../api'
import { message } from 'ant-design-vue'
import moment from "moment";
import { handleShowProductionDate, handleShowExpiredDate, handleExpiredDays } from "../script/lineHandleLogic";

export let itemsColChangeModule = {

    methods: {

        warningExpiredDateTips(record, index) {
            // 判断拦截  移库入库4只提示不拦截
            if (record.batchExpiredDate && record.batchProductionDate) {
                let code = handleExpiredDays(record.batchExpiredDate, this.expiredDays)
                if (code == 1) {
                    message.error(`第${index + 1}行, 有效期至${record.batchExpiredDate}, 近效期${this.expiredDays.alert}, 警告`);
                }
                if (code == 2) {
                    let t = this.detailData.main.rcvType == '4' ? '警告' : '拦截';
                    message.error(`第${index + 1}行, 有效期至${record.batchExpiredDate}, 近效期${this.expiredDays.intercept}, ${t}`);
                }
            }
        },

         // 显示生产日期
         handleShowProductionDate(record, index, value) {
            if (value === '*') {
                record.batchProductionDate = moment().format('YYYY-MM-DD');
                return;
            }
            value = value.replace(/\D/g, '');
            record.showProductionDate = value;
            if (value.length != 8 && value.length != 6) {
                record.batchProductionDate = '';
                if (record.showExpiredDate && record.showExpiredDate.length == 6) record.batchExpiredDate = '';
                return;
            }
            let t = handleShowProductionDate(value);
            if (!t || t.indexOf('Invalid') >= 0) {
                message.warning('显示生产日期 格式有误，请修改！')
                return;
            }
            record.batchProductionDate = t;
            this.warningExpiredDateTips(record, index);

            // (生产日期+质保期天数（无值取1）) 、有效期至，取最大值。
            if (!record.showExpiredDate || record.showExpiredDate.length == 8) { return; }
            if (record.showExpiredDate == '*') {
                record.batchExpiredDate = '2099-01-01';
            } else {
                let a = new Date(t);
                a.setDate(a.getDate() + (+record.shelfLifeNum || 1));
                let b = handleShowExpiredDate(record.showExpiredDate);
                if (+(a) > +(new Date(b))) {
                    record.batchExpiredDate = moment(a).format('YYYY-MM-DD');
                } else {
                    record.batchExpiredDate = b;
                }

            }

        },


       
        // 显示有效期至
        handleShowExpiredDate(record, index, value) {
            if (value === '*') {
                record.batchExpiredDate = '2099-01-01';
                return;
            }
            value = value.replace(/\D/g, '');
            record.showExpiredDate = value;
            if (value.length != 6 && value.length != 8) {
                record.batchExpiredDate = '';
                return;
            }
            let t = handleShowExpiredDate(value, record.shelfLifeUnit, +record.shelfLifeNum);
            if (!t || t.indexOf('Invalid') >= 0) {
                message.warning('显示有效期至 格式有误，请修改！')
                return;
            }
            if (value.length == 8) {
                record.batchExpiredDate = t;
            } else {
                // (生产日期+质保期天数（无值取1）) 、有效期至，取最大值。
                if (!record.batchProductionDate) { return; }
                let a = new Date(record.batchProductionDate);
                a.setDate(a.getDate() + (+record.shelfLifeNum || 1));
                if (+(a) > +(new Date(t))) {
                    record.batchExpiredDate = moment(a).format('YYYY-MM-DD');
                } else {
                    record.batchExpiredDate = t;
                }
            }
            this.warningExpiredDateTips(record, index);
        },

        // 零散数
        handleOddPieceQty(record) {
            if (record.allowDecimal != 'Y') {
                record.oddPieceQty = parseInt(record.oddPieceQty + '');
            }
        },

        // 件数 零散数  数量关系
        handlePieceQty(record, dataSource) {
            let asnLineId = record.asnLineId.replace(/_cus_\d*/g, '');
            record.pieceQty = record.conversionRate * (+record.oddCaseQty || 0) + (+record.oddPieceQty || 0);
            let oItem = this.detailData.list.find(t => t.asnLineId.indexOf(asnLineId) >= 0);
            let cPieceQty = dataSource.reduce((accumulator, current) => {
                if(current.asnLineId.indexOf(asnLineId) >= 0) return accumulator + current.pieceQty
                else return accumulator
            }, 0);
            if (cPieceQty > oItem.pieceQty) {
                message.warning('输入后总数量为' + cPieceQty + ', 超出了原单总数量' + oItem.pieceQty + ', 请检查输入的数量');
            }
        },

        // 收货结论 为正常收货清空
        handleStockStatusTemperature(record) {
            if (record.stockStatus === '0') {
                record.rcvReason1 = '';
                record.rcvReason2 = '';
                record.rcvrEason3 = '';
            }
        },

    }

};