<!-- 快速改箱/装箱 -->
<template>
    <view class="shpage gx_zxpage">
        <!-- 标题栏 -->
        <view class="mianheade mianheade2" @click="goback()">
            <image src="../../static/img/n_baiback.png" class="blacBackico"></image>
            <text class="pagetitle">快速改箱/装箱</text>
        </view>
        <!-- 固定内容-->
        <view class="sh_gdInfo">
            <view class="item" :class="{ itembg2: (MbNoTag && YyboxId) || boxMbNo }" :style="{ 'pointer-events': (MbNoTag && YyboxId) || boxMbNo ? 'none' : '' }">
                <view class="it itsp">
                    <text class="t1">原有箱号:</text>
                    <input
                        v-model.trim="boxYyNo"
                        class="inpt1"
                        type="text"
                        @focus="inputfocus($event, 'YyboxNo')"
                        @blur="inputblur($event, '')"
                        @confirm="inputConfirm($event, 'YyboxNo')"
                        :focus="focusInput == 'YyboxNo'"
                        :class="{ inpt2: (MbNoTag && YyboxId) || MbBoxId }"
                    />
                    <image src="../../static/img/smico.png" class="searchico" @click="scanImg('YyboxNo')"></image>
                </view>
            </view>
            <view class="item" :class="{ itembg2: xlhOrggStrlist.length > 0 }" :style="{ 'pointer-events': xlhOrggStrlist.length > 0 ? 'none' : '' }">
                <view class="it itsp">
                    <text class="t1">
                        <text class="redtag">*</text>
                        目标箱号:
                    </text>
                    <input
                        v-model.trim="boxMbNo"
                        class="inpt1"
                        type="text"
                        @focus="inputfocus($event, 'MbboxNo')"
                        @blur="inputblur($event, 'MbboxNo')"
                        @confirm="inputConfirm($event, 'MbboxNo')"
                        :focus="focusInput == 'MbboxNo'"
                    />
                    <image src="../../static/img/smico.png" class="searchico" @click="scanImg('MbboxNo')"></image>
                </view>
            </view>
            <view class="item" :class="{ itembg2: MbBoxId === '' || !boxMbNo }" :style="{ 'pointer-events': MbBoxId === '' || !boxMbNo ? 'none' : '' }">
                <view class="it itsp">
                    <text class="t1">
                        <text class="redtag">*</text>
                        序列号/规格型号:
                    </text>
                    <input
                        v-model.trim="xlhOrggxhVal"
                        class="inpt1"
                        type="text"
                        @focus="inputfocus($event, 'xlhOrggxh')"
                        @blur="inputblur($event, '')"
                        @confirm="inputConfirm($event, 'xlhOrggxh')"
                        :focus="focusInput == 'xlhggxh'"
                    />
                    <image src="../../static/img/smico.png" class="searchico" @click="scanImg('xlhOrggxh')"></image>
                </view>
            </view>
            <view
                class="item"
                :class="{ itembg2: xlhOrggType == 'xlh' || !xlhOrggType || !xlhOrggxhVal }"
                :style="{ 'pointer-events': xlhOrggType == 'xlh' || !xlhOrggType || !xlhOrggxhVal ? 'none' : '' }"
            >
                <view class="it itsp">
                    <text class="t1">
                        <text class="redtag">*</text>
                        数量:
                    </text>
                    <input
                        v-model="dqNum"
                        class="inpt1"
                        type="number"
                        pattern="[1-9]\d*"
                        inputmode="numeric"
                        min="1"
                        @focus="inputfocus($event, 'dqNum')"
                        @confirm="inputConfirm($event, 'dqNum')"
                        @blur="inputblur($event, '')"
                        :disabled="xlhOrggType == 'xlh'"
                        :focus="focusInput == 'dqNum'"
                    />
                </view>
            </view>
        </view>
        <!-- 重新上架 -->
        <view class="cxsjdiv" :style="{ 'margin-top': heights.top + 5 + 'px' }">
            <view style="display: flex; flex-direction: row; align-items: center; margin-left: 4.2%" v-if="!subStockCode && boxMbNo" @click="scsjFn">
                <view class="" style="border: 1px solid rgb(200, 201, 204); border-radius: 3px; width: 16px; height: 16px"></view>
                <view style="font-size: 15px; padding-left: 5px">重新上架</view>
            </view>
            <u-checkbox-group v-else class="gxcheckbox" v-model="cxsjCheck" placement="column" :disabled="!subStockCode">
                <u-checkbox
                    :customStyle="{ marginBottom: '8px' }"
                    v-for="(item, index) in checkboxList"
                    :key="index"
                    :label="item.name"
                    :name="item.name"
                    class="checkboxlabel"
                ></u-checkbox>
            </u-checkbox-group>
        </view>
        <!-- 列表内容-->
        <view class="shlb cgsjrklb f_cgsjrklb" :style="{ height: heights.body - 30 + 'px' }">
            <view class="item" v-for="(item, index) in dataList" :key="index">
                <view class="it" style="border: none">
                    <text class="txt titstr">{{ item.specifications }}</text>
                    <view class="cssj_it">
                        <view class="txtNum">
                            <view>{{ item.materialNumber }}</view>
                            <view class="txt">{{ item.qty }}</view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <!-- 底部按钮固定-->
        <view class="footbts" style="z-index: 2">
            <view class="bt bt1" @click="qkfn()">清空改箱</view>
            <view class="bt bt2" @click="qrGx()">确认改箱</view>
        </view>
        <!--重新上架弹框 、清空二次确认-->
        <u-modal :show="tkShow" :title="title" :content="content" :showCancelButton="true" @cancel="tkShow = false" @confirm="confirm">
            <view style="display: flex; flex-direction: column">
                <view style="display: flex; flex-direction: row; align-items: center; border-bottom: 1px solid #e0e5eb" v-if="title == '重新上架'">
                    <text style="width: 70px">仓位号</text>
                    <view style="display: flex; flex-direction: row; align-items: center; padding-left: 5px; background: #fff">
                        <input
                            v-model.trim="cwVal"
                            :focus="focusInput == 'cwh'"
                            clearable
                            @focus="inputfocus($event, 'cwh')"
                            @blur="inputblur($event, '')"
                            @confirm="inputConfirm($event, 'cwh')"
                        />
                        <image src="../../static/img/smico.png" class="searchico" @click="scanImg('cwh')"></image>
                    </view>
                </view>
                <text style="font-size: 10px; color: red">{{ cwstrts }}</text>
            </view>
        </u-modal>
        <u-modal :show="yzxMakeSureShow" title="此箱已装箱,确认再次装箱?" :showCancelButton="true" @cancel="ysxCancel" @confirm="yzxMakeSure" :negative-top="200">
            <view style="display: flex; flex-direction: column; justify-content: end">
                <view v-for="(item, index) in yzxMakeSurelist" :key="index" style="padding-bottom: 4px">{{ item.materialNumber }}({{ item.qty }})</view>
            </view>
        </u-modal>
        <!-- 不是以CTN开头的箱号需要二次确认 -->
        <u-modal :show="noCtnShow" :content="ctnContent" @confirm="ctnConfirm" :showCancelButton="true" @cancel="noCtnShow = false" ref="noCtnModel" :negative-top="200"></u-modal>
        <!-- 当序列号已上架入库 -->
        <u-modal :show="xlhShow" :content="xlhContent" @confirm="xlhConfirm" :showCancelButton="true" @cancel="xlhCancel" ref="noCtnModel1" :negative-top="200"></u-modal>
    </view>
</template>

<script>
export default {
    data() {
        return {
            heights: {
                top: 0,
                body: 0
            },
            booleacz: null,
            xlhggxhData: {}, //存储请求过来的序列号数据
            xlhShow: false,
            xlhContent: '',
            ctnContent: '箱号不为CTN,确认箱号无误?', //箱号不为CTN的时候,弹窗的内容
            noCtnShow: false, //不是以CTN开头的二次确认弹窗条件
            focusInput: 'YyboxNo', // 自动聚焦
            focusTag: '', //  判斷聚焦
            boxYyNo: '', // 原有箱号
            YyNoTag: false, // 是否是正常的原有箱子
            YyboxId: '', // 原箱子ID
            YyboxsubStockCode: '', //原有想是否存在仓位(用来判断是否上架)
            boxMbNo: '', // 目标箱号
            MbBoxId: '', // 目标箱号ID
            stockCode: '', // 目标箱的仓库编码
            subStockCode: '', // 目标箱的仓位id
            MbNoTag: false, // 是否是正常的目标箱号
            xlhOrggxhVal: '', // 序列号/格型号
            xlhOrggTag: false, // 是否是正常的目标箱号
            xlhOrggType: '', // 判断当前是规格型号还是序列号
            xlhOrggStrlist: [], // 序列號規格吸納好存在值的集合
            dqNum: '', // top数量
            dqNumTag: '', // 如果是序列号数量加一不可编辑==‘xlh’，如果是ggxh就可以输入正整数
            cxsjCheck: [], // 重新上架
            dataList: [], // 明细数据
            checkboxList: [
                {
                    name: '重新上架'
                }
            ],
            title: '重新上架', // 弹框标题
            content: '', //弹框内容
            tkShow: false, // 重新上架、清空二次确认弹框
            cwVal: '', // 仓位号
            itData: {}, // 缓存当前规格型号的接口数据，确定数量之后在添加到明细
            cwstrts: '', // 仓位错误提示
            dqYYboxNO: '', //用来是否保存当前输入原有箱号的值
            dqMBboxNO: '', //用来是否保存当前输入目标箱号的值
            dqxlhOrggxhNO: '', //用来是否保存当前输入序列号或者规格型号的值
            yyMXlist: [], // 缓存原有箱号明细数据
            useGGXH: '', // 当前输入物料编码或者是规格型号查的规格型号
            APPdevice: uni.getStorageSync('devicePixelRatio'), // 缓存设备的像素比用来区分普通安卓normalAnroid还是pda
            entGGXH: '', // 当前搜索序列号规格型号物料的规格型号
            requestStatus: false, // 请求状态识别变量
            scanTracker: {
                lastScanTime: 0,
                lastScanCode: null
            }, // 源头处理扫码段时间内重复扫
            yzxMakeSurelist: [], // 已装箱再次确认装箱弹框信息
            yzxMakeSureShow: false, //已装箱再次确认装箱弹框显示
            yzxMakeSureZCdata: {}, ///已装箱再次确认装箱确认时处理的数据源
            confirmParams: [], //提交給後臺的數據
            xlhObj: {}, //请求后台的数据
            // qtyClone:0,
            qtys: [],
			twoData:[],
            boxInfo: {} //装箱信息
        };
    },
    watch: {
        boxYyNo(n) {
            if (!n) {
                this.dqYYboxNO = '';
            }
        },
        boxMbNo(n) {
            if (!n) {
                this.dqMBboxNO = '';
            }
        },
        xlhOrggxhVal(n) {
            if (!n) {
                this.dqxlhOrggxhNO = '';
            }
        }
    },
    onReady() {
        this.heights = this.$util.setlistHeight('.sh_gdInfo', '.footbts');
		console.log(this.heights,'=this.heights=')
    },
    onLoad() {
        //获取广播扫码监听
        this.$broadcastScan.init(this.getScancode);
        this.$broadcastScan.start();
        this.stopScanCode();
        this.startScanCode();
    },
    onHide() {
        this.$broadcastScan.stop();
        this.stopScanCode();
    },
    onShow() {
        this.$broadcastScan.start();
        this.startScanCode();
    },
    onBackPress(e) {
        this.$util.appgoBack(e, 'otherUnderwearIndex');
        return true;
    },
    methods: {
        ysxCancel() {
            this.yzxMakeSureShow = false;
            this.boxMbNo = '';
            this.focusInput = '';
            this.dqMBboxNO = '';
            setTimeout(() => {
                this.focusInput = 'MbboxNo';
            }, 200);
        },
        // 开启广播
        startScanCode() {
            uni.$on('xwscan', (res) => {
                const now = Date.now();
                const timeSinceLastScan = now - this.scanTracker.lastScanTime;
                // 如果扫描的箱码和上次相同，并且时间间隔小于500ms，则不作处理
                if (res.code === this.scanTracker.lastScanCode && timeSinceLastScan < 4000) {
                    return;
                } else {
                    // 更新扫描跟踪信息
                    this.scanTracker.lastScanTime = now;
                    this.scanTracker.lastScanCode = res.code;
                    this.getScancode(res.code);
                }
            });
        },
        // 关闭广播
        stopScanCode() {
            uni.$off('xwscan');
        },
        // 重新上架提示
        scsjFn() {
            uni.showToast({
                title: '箱不存在仓位号',
                icon: 'none',
                duration: 1500
            });
            return;
        },
        // 已装箱，确认再次装箱
        yzxMakeSure(data) {
            this.setMbboxData(this.yzxMakeSureZCdata);
            this.yzxMakeSureShow = false;
        },
        // 弹框确认
        confirm() {
            this.cwstrts = '';
            if (this.title == '重新上架') {
                if (!this.cwVal) {
                    this.cwstrts = '请输入或者扫描值';
                    return;
                }
                // 获取仓位号id
                this.$api.get('/SysConfig/GetSubUcStockPrecisionByName', {
                        name: this.cwVal
                    })
                    .then((res) => {
                        if (res.status == 200) {
                            this.tkShow = false;
                            this.makeSureGX(res.data.code);
                        } else {
                            this.cwstrts = res.message;
                        }
                    });
            }
            if (this.title == '清空提示') {
                this.boxMbNo = '';
                this.boxYyNo = '';
                this.MbBoxId = '';
                this.YyboxId = '';
                this.xlhOrggxhVal = '';
                this.dataList = [];
                this.dqNum = null;
                this.dqNumTag = '';
                this.xlhOrggStrlist = [];
				this.twoData=[]
				this.confirmParams=[]
                this.MbNoTag = false;
                this.YyNoTag = false;
                this.xlhOrggTag = false;
                this.xlhOrggType = '';
                this.tkShow = false;
                this.stockCode = '';
                this.subStockCode = 0;
                this.dqYYboxNO = '';
                this.dqMBboxNO = '';
                this.dqxlhOrggxhNO = '';
                this.cxsjCheck = [];
                // this.qtyClone = 0
                this.qtys = [];
            }
        },
        // 确认改箱 调取接口
        makeSureGX(cwcode) {
            let isQty = this.confirmParams.some((item) => {
                return !item.qty && !item.serialNumber;
            });
            //数量必须
            if (isQty) {
                uni.showToast({
                    title: '数量不能为空 !',
                    icon: 'none',
                    duration: 1500
                });
                return;
            }
            const groupedBySrcBoxId = this.confirmParams.reduce((acc, obj, idx) => {
                if (!acc[obj.boxId]) {
                    acc[obj.boxId] = [];
                }
                acc[obj.boxId].push({
                    materialNumber: obj.materialNumber,
                    qty: obj.serialNumber ? 1 : obj.qty, //如归是序列号默认为1,如果是规格型号取用户输入的值
                    srcBoxId: obj.boxId,
                    serialNumber: obj.serialNumber,
					twoSerialNumber:obj.twoSerialNumber,
                    serialNumbers: [],
					twoSerialNumbers:[],
                });
                return acc;
            }, {});
			let srcBoxId =null
            const result = Object.values(groupedBySrcBoxId).map((group) => {
                return group.reduce((acc, obj) => {
                    const existing = acc.find((item) => item.materialNumber === obj.materialNumber);
                    if (existing) {
                        if (obj.serialNumber) {
                            existing.qty += obj.qty;
                            existing.serialNumbers.push(obj.serialNumber);
						
                        }
						if(obj.twoSerialNumber) {
							existing.twoSerialNumbers.push(obj.twoSerialNumber)
							console.log(existing,'=existing=')
						}
                       
                    } else {
						let twoSerialNumbers = []
						if(obj.twoSerialNumber) {
							twoSerialNumbers.push(obj.twoSerialNumber)
						}
                        acc.push({
                            materialNumber: obj.materialNumber,
                            qty: obj.qty,
                            serialNumbers: obj.serialNumber ? [obj.serialNumber] : [],
                            // srcBoxId: obj.srcBoxId,
							twoSerialNumbers:twoSerialNumbers,
                        });
						srcBoxId = obj.srcBoxId
                    }
                    return acc;
                }, []);
            });
			
			console.log()
            let params = [];
            let length = result.length;
            for (let i = 0; i < length; i++) {
                let obj = {
                    destBoxBillNo: this.boxMbNo,
                    srcBoxId: this.YyboxId ? this.YyboxId : srcBoxId,
                    destBoxId: this.MbBoxId ? this.MbBoxId : 0,
                    subStockCode: cwcode ? cwcode : this.subStockCode ? this.subStockCode : 0, //目标箱仓位或重新上架选择仓位
                    details: result[i],
                };
                params.push(obj);
            }
			
			
            // 节流
            if (this.requestStatus) {
                return false;
            }
			this.requestStatus = true;
			console.log(params,'=params=')
            this.$api.post('/ChangeBoxRecord/Save', params).then((res) => {
                if (res.status == 200) {
                    uni.showToast({
                        title: '成功',
                        icon: 'none',
                        duration: 1500
                    });
                    setTimeout(() => {
                        uni.navigateTo({
                            url: '/pages/otherUnderwear/fastSetbox'
                        });
                    }, 200);
                    // this.$router.go(0)
                }
            });
            setTimeout(() => {
                this.requestStatus = false;
            }, 2000);
        },
        // 清空改箱
        qkfn() {
            this.title = '清空提示';
            this.content = '是否确认清空当前页面已经扫描的箱号和目标箱号以及产品信息';
            this.tkShow = true;
        },
        // 普通安卓设备碘酒扫描图标扫描
        scanImg(type) {
            if (this.APPdevice == 'normalAnroid') {
                this.$util.doScanQrCode().then((res) => {
                    this.focusInput = type;
                    this.focusTag = type;
                    this.getScancode(res.result);
                });
            }
        },

        // 获取扫描的值

        getScancode(code) {
            if (this.focusTag == 'YyboxNo' || this.focusTag == 'MbboxNo') {
                // 有些PDA会自带换行符，trim函数处理下
                if (this.focusTag == 'YyboxNo') {
                    this.boxYyNo = null;
                    this.boxYyNo = code.trim();
                }
                if (this.focusTag == 'MbboxNo') {
                    if (this.boxYyNo && this.boxYyNo === this.boxMbNo) {
                        uni.showToast({
                            title: '该箱号已扫描',
                            icon: 'none',
                            duration: 1500
                        });
                        return;
                    }
                    this.boxMbNo = null;
                    this.boxMbNo = code.trim();
                }
                this.getboxinfo();
            }
            if (this.focusTag == 'xlhOrggxh') {
                // 有些PDA会自带换行符，trim函数处理下
                this.xlhOrggxhVal = '';
                this.xlhOrggxhVal = code.trim();
                this.getXlhOrggxhInfo();
            }
            if (this.focusTag == 'cwh') {
                // 有些PDA会自带换行符，trim函数处理下
                this.cwVal = '';
                this.cwVal = code.trim();
            }
        },
        // 确认改箱按钮点击
        qrGx() {
            if (!this.boxMbNo || this.xlhOrggStrlist.length == 0) {
                uni.showToast({
                    title: '请先输入必填值',
                    icon: 'none',
                    duration: 1500
                });
                return;
            }
            if (this.cxsjCheck && this.cxsjCheck.length > 0) {
                this.title = '重新上架';
                this.cwhFocused = true;
                this.tkShow = true;
                this.focusTag = 'cwh';
                this.focusInput = '';
                setTimeout(() => {
                    this.focusInput = 'cwh';
                }, 200);
            } else {
                this.makeSureGX();
            }
        },
        // 輸入失去焦點
        inputblur(val, type) {},
        // 输入聚焦
        inputfocus(val, type) {
            this.focusTag = type;
            if (type == 'box') {
            }
        },
        // 区分不同的扫描数据、输入数据做处理显示 原有箱子
        setYYboxNostr(str) {
            let lth = str.length - this.dqYYboxNO.length;
            if (lth > 1) {
                // 扫描的值
                this.boxYyNo = str.substring(this.dqYYboxNO.length);
            } else {
                this.dqYYboxNO = str;
                this.boxYyNo = str;
            }
        },
        // 区分不同的扫描数据、输入数据做处理显示 目标箱子
        setMBboxNostr(str) {
            let lth = str.length - this.dqMBboxNO.length;
            if (lth > 1) {
                // 扫描的值
                this.boxMbNo = str.substring(this.dqMBboxNO.length);
            } else {
                this.dqMBboxNO = str;
                this.boxMbNo = str;
            }
        },
        // 区分不同的扫描数据、输入数据做处理显示 序列号或规格型号
        setXlhOrGgxhstr(str) {
            let lth = str.length - this.dqxlhOrggxhNO.length;
            if (lth > 1) {
                // 扫描的值
                this.xlhOrggxhVal = str.substring(this.dqxlhOrggxhNO.length);
            } else {
                this.dqxlhOrggxhNO = str;
                this.xlhOrggxhVal = str;
            }
        },

        // 区分不同的扫描数据、输入数据做处理显示 序列号或规格型号

        setcwhstr(str) {
            let lth = str.length - this.dqcwh.length;
            if (lth > 1) {
                // 扫描的值
                this.cwVal = str.substring(this.dqcwh.length);
            } else {
                this.dqcwh = str;
                this.cwVal = str;
            }
        },
        // 输入确认
        inputConfirm(val, type) {
            if (val) {
                if (this.focusTag == 'MbboxNo' || this.focusTag == 'YyboxNo') {
                    if (this.focusTag == 'YyboxNo') {
                        this.setYYboxNostr(val.target.value);
                    }
                    if (this.focusTag == 'MbboxNo') {
                        if (this.boxYyNo && this.boxYyNo === this.boxMbNo) {
                            uni.showToast({
                                title: '该箱号已扫描',
                                icon: 'none',
                                duration: 1500
                            });
                            return;
                        }
                        this.setMBboxNostr(val.target.value);
                    }
                    this.getboxinfo();
                }

                if (this.focusTag == 'xlhOrggxh') {
                    // this.xlhOrggxhVal = val.target.value
                    this.setXlhOrGgxhstr(val.target.value);
                    this.getXlhOrggxhInfo();
                }
				// 8.6这里的逻辑只是针对扫描的是序列号/规格型号 这个框扫描的是规格型号 才会有数量填写，但是如果是序列号的话直接默认数量是1，是没有数量填写这个改概念的
                // 这里补充下
				if (this.focusTag == 'dqNum') {
                    // 当前规格型号的数量不能超过原来箱子改规格的数量
                    let yyboxggNum =
                        this.yyMXlist && this.yyMXlist.length > 0
                            ? this.yyMXlist.filter((it) => {
                                  return it.specifications == this.entGGXH;
                              }) : [];
                    if (this.YyboxId && parseInt(this.dqNum ? this.dqNum : 0) > yyboxggNum[0].qty) {
                        this.focusInput = '';
                        setTimeout(() => {
                            this.focusInput = 'dqNum';
                        }, 200);
                        this.dqNum = null;
                        this.dqxlhOrggxhNO = this.xlhOrggxhVal;
                        uni.showToast({
                            title: '数量大于原箱商品可改数量 ',
                            icon: 'none',
                            duration: 1500
                        });
                        return;
                    }
                    if (this.dqNum && this.dqNum > 0) {
                        if (this.xlhOrggType == 'ggxh') {
                            let booleacz = this.dataList.some((obj) => obj.specifications === this.itData.specifications);
                            if (booleacz) {
                                //如果存在判断该规格在列表第几条，且修改对应数量
                                let czindex = this.dataList.findIndex((obj) => obj.specifications === this.itData.specifications);
                                if (czindex !== -1) {
                                    this.dataList[czindex].qty = parseInt(this.dataList[czindex].qty) + parseInt(this.dqNum);
                                }
                            } else {
                                this.dataList.push({
                                    itxlhlist: [],
                                    specifications: this.itData.specifications,
                                    materialNumber: this.itData.materialNumber,
                                    qty: this.dqNum,
                                    srcBoxId: this.itData.boxId,
                                });
                            }
							
                            //將規格型號和数量组装成一个对象,然后循环进this.confirmParams相对应的规格型号中,防止数量取错值
                            let dataListObj = {};
                            let length = this.dataList.length;
                            for (let i = 0; i < length; i++) {
                                let item = this.dataList[i];
                                dataListObj[item.specifications] = Number(item.qty);
                            }
                            let length1 = this.confirmParams.length;
                            //将dataListObj存储的规格型号数量提取出来,在提交的时候赋值
                            for (let i = 0; i < length1; i++) {
                                this.confirmParams[i].qty = dataListObj[this.confirmParams[i].specifications];
                            }
							
							
                            this.xlhOrggxhVal = null;
                            this.dqxlhOrggxhNO = null;
                            this.dqNum = null;
                            this.focusInput = '';
                            setTimeout(() => {
                                this.focusInput = 'xlhggxh';
                            }, 200);

                            uni.showToast({
                                title: '设置成功',
                                icon: 'none',
                                duration: 1500
                            });
                        }
                    } else {
                        uni.showToast({
                            title: '请输入该规格型号的数量在确认',
                            icon: 'none',
                            duration: 1500
                        });
                    }
                }
                if (this.focusTag == 'cwh') {
                    setcwhstr(val.target.value);
                }
            }
        },
        // 扫描框数据重置，光标聚焦 扫箱
        setSMinputbox() {
            this.focusInput = '';
            if (this.focusTag == 'YyboxNo') {
                setTimeout(() => {
                    this.boxYyNo = '';
                    this.focusInput = 'YyboxNo';
                    this.dqYYboxNO = '';
                }, 200);
            }
            if (this.focusTag == 'MbboxNo') {
                setTimeout(() => {
                    this.boxMbNo = '';
                    this.focusInput = 'MbboxNo';
                    this.dqMBboxNO = '';
                }, 200);
            }
        },
        // 目标箱扫描数据处理
        setMbboxData(res) {
            this.MbBoxId = res.data.id;
            this.stockCode = res.data.stockCode;
            this.subStockCode = res.data.subStockCode;
            if (!this.YyboxId) {
                this.boxYyNo = '';
            }
            if (!this.subStockCode) {
                this.cxsjCheck = [];
            }
            this.MbNoTag = true;
            this.dqMBboxNO = res.data.boxBillNo;
            this.focusInput = '';
            setTimeout(() => {
                this.focusInput = 'xlhggxh';
                this.focusTag = 'xlhggxh';
            }, 300);
            uni.showToast({
                title: '获取成功',
                icon: 'none',
                duration: 1500
            });
        },
        //再次装箱二次确认(此箱已装箱,确认再次装箱)
        isZCzx(res) {},
        //抽离箱号信息请求
        getBoxinfoHttp(params) {
            this.$api.get('/SysConfig/GetBoxSynthesis', params).then((res) => {
                if (res.status == 200 && res.data !== null) {
                    this.boxInfo = res;
                    if (this.focusTag == 'YyboxNo') {
                        this.YyboxId = res.data.id;
                        this.YyboxsubStockCode = res.data.subStockCode;
                        this.YyNoTag = true;
                        this.dqYYboxNO = res.data.boxBillNo;
                        this.focusInput = '';
                        setTimeout(() => {
                            this.focusInput = 'MbboxNo';
                            this.focusTag = 'MbboxNo';
                        }, 300);

                        this.yyMXlist = res.data.details;
                        uni.showToast({
                            title: '获取成功',
                            icon: 'none',
                            duration: 1500
                        });
                    }
                    if (this.focusTag == 'MbboxNo') {
                        if (this.boxYyNo && this.boxYyNo === this.boxMbNo) {
                            uni.showToast({
                                title: '该箱号已扫描',
                                icon: 'none',
                                duration: 1500
                            });
                            return;
                        }
                        let regex = /^\s*CTN/;
                        if (!res.data.stockCode && !this.boxYyNo && !regex.test(this.boxMbNo)) {
                            this.noCtnShow = true;
                            return;
                        }
                        if ((!this.YyboxId && res.data.subStockCode && res.data.id > 0) || (this.YyboxId > 0 && !this.YyboxsubStockCode && res.data.subStockCode)) {
                            uni.showToast({
                                title: '箱已上架不支持再次装箱',
                                icon: 'none',
                                duration: 2500
                            });
                            return;
                        }
                        if (res.data.id > 0 && !res.data.subStockCode) {
                            this.yzxMakeSurelist = res.data.details;
                            this.yzxMakeSureZCdata = res;
                            this.yzxMakeSureShow = true;
                        } else {
                            this.setMbboxData(res);
                        }
                    }
                } else {
                    this.setSMinputbox();
                }
            });
        },
        // 获取扫码或者输入的箱子信息
        getboxinfo() {
            let boxNo = '';
            if (this.focusTag == 'YyboxNo') {
                boxNo = this.boxYyNo;
            }
            if (this.focusTag == 'MbboxNo') {
                boxNo = this.boxMbNo;
            }
            if (!boxNo) {
                this.setSMinputbox();
                uni.showToast({
                    title: '请扫描或输入值',
                    icon: 'none',
                    duration: 1500
                });
                return;
            }
            this.focusInput = '';
            // 原型请求不会拦截，目标会拦截
            let tjdata = {
                boxBillNo: boxNo
            };
            let mbtjdata = {
                isReminder: 'false',
                boxBillNo: boxNo
            };
            let params = this.focusTag == 'MbboxNo' ? mbtjdata : tjdata;
            this.getBoxinfoHttp(params);
        },
        // 扫描框数据重置，光标聚焦 扫箱
        setSMinputxlhOrggxh() {
            this.focusInput = '';
            setTimeout(() => {
                this.xlhOrggxhVal = '';
                this.focusInput = 'xlhggxh';
                this.dqxlhOrggxhNO = '';
            }, 200);
        },
        //添加序列号/规格型号信息
        addXlhOrGgxh(res) {
            if (this.booleacz) {
                //如果存在判断该序列号在列表第几条，且修改对应数量
                let czindex = this.dataList.findIndex((obj) => obj.specifications === res.data.specifications);
                if (czindex !== -1) {
                    this.dataList[czindex].qty = parseInt(this.dataList[czindex].qty) + 1;
                    this.dataList[czindex].itxlhlist.push(this.xlhOrggType == 'xlh' && res.data.serialNumber ? res.data.serialNumber : '');
                }
            } else {
                this.dataList.push({
                    itxlhlist: [this.xlhOrggxhVal],
                    specifications: res.data.specifications,
                    materialNumber: res.data.materialNumber,
                    qty: 1,
                    srcBoxId: res.data.boxId,
					twoSerialNumber:res.data.twoSerialNumber ? res.data.twoSerialNumber: ''
                });
            }
            //数量默认为1
            this.focusInput = 'xlhggxh';
            this.setSMinputxlhOrggxh();
            this.xlhOrggStrlist.push(this.xlhOrggType == 'xlh' ? res.data.serialNumber : res.data.specifications);	
			this.confirmParams.push(res.data);
            uni.showToast({
                title: '设置成功',
                icon: 'none',
                duration: 1500
            });
        },
        // 获取规格型号和序列号
        getXlhOrggxhInfo() {
            if (!this.xlhOrggxhVal) {
                this.setSMinputxlhOrggxh();
                uni.showToast({
                    title: '请扫描或输入值',
                    icon: 'none',
                    duration: 1500
                });
                return;
            }
            if (this.xlhOrggStrlist.includes(this.xlhOrggxhVal) || this.twoData.includes(this.xlhOrggxhVal)) {
                this.setSMinputxlhOrggxh();
                uni.showToast({
                    title: '该序列号/格型号已扫描',
                    icon: 'none',
                    duration: 1500
                });
                return;
            }
            this.focusInput = '';
            this.$api
                .get('/SysConfig/GetMaterial', {
                    serialNumber: this.xlhOrggxhVal,
                    IsOps: true,
                    serialStatus: 0
                })
                .then((res) => {
                    if (res.status == 200) {
                        //这里还是用一个全局变量存储
                        this.xlhggxhData = res;
                        this.useGGXH = res.data.specifications;
						this.entGGXH = res.data.specifications;
                        // 明细添加，如果该序列号对应的规格型号不存在，就明细填加一条，如果存在就数量加一（booleacz 这个是判断规格型号）
                        this.booleacz = this.dataList.some((obj) => obj.specifications === res.data.specifications);
                        // 判断扫码的是个序列号还是规格型号
                        this.xlhOrggType = res.data.serialNumber ? 'xlh' : 'ggxh';
                        //如果是序列号
                        if (this.xlhOrggType == 'ggxh' && this.xlhOrggStrlist.includes(res.data.specifications)) {
                            this.setSMinputxlhOrggxh();
                            uni.showToast({
                                title: '该序列号/格型号已扫描',
                                icon: 'none',
                                duration: 1500
                            });
                            return;
                        }
						//缓存2件装序列号
						if(res.data.isTwo==2 ) {
							this.twoData.push(res.data.twoSerialNumber)
						}
                        if (this.xlhOrggType == 'xlh') {
                            let xlhyyitlist = [];
                            this.yyMXlist.forEach((it) => {
                                if (it.materialNumber == res.data.materialNumber) {
                                    xlhyyitlist = it.serialNumbers;
                                }
                            });
						
                            /*
							原箱号有值，序列号=激活、激活且非冻结、非激活且非冻结=扫描成功，
							原箱号没有值，序列号=非激活且非冻结、激活且冻结=扫描成功
							*/
                            //原箱号有值，序列号=激活且冻结=扫描提示：序列号未上架入库
                            if (this.YyboxId && res.data.isActivate && res.data.isDelete) {
                                this.setSMinputxlhOrggxh();
                                uni.showToast({
                                    title: '序列号未上架入库',
                                    icon: 'none',
                                    duration: 1500
                                });
                                return;
                            }
                            //原箱号没有值，序列号=激活且非冻结=扫描提示：序列号已上架入库
                            if (!this.YyboxId && res.data.isActivate && !res.data.isDelete) {
                                this.xlhShow = true;
                                this.xlhContent = `序列号${res.data.serialNumber}存在箱号${res.data.boxBillNo},确定改箱?`;
                                return;
                            }
                            //原箱号有值  如果老序列号有绑定箱id 要判断这个箱子是否和原箱一致 如果不一致提示序列号已绑定箱
                            //原箱号有值  老序列号没箱id 正常操作
                            if (this.YyboxId && res.data.isOldOps && res.data.boxId > 0 && res.data.boxId !== this.YyboxId) {
                                this.setSMinputxlhOrggxh();
                                uni.showToast({
                                    title: '序列号已绑定箱',
                                    icon: 'none',
                                    duration: 1500
                                });
                                return;
                            }
                            if (xlhyyitlist.includes(this.xlhOrggxhVal)) {
                                if (res.data.isBoxInventory && !this.YyboxId) {
                                    this.xlhShow = true;
                                    this.xlhContent = `序列号${res.data.serialNumber}存在箱号${res.data.boxBillNo},确定改箱?`;
                                    return;
                                }
                            } else {
                                if (res.data.isBoxInventor && !this.YyboxId) {
                                    this.xlhShow = true;
                                    this.xlhContent = `序列号${res.data.serialNumber}存在箱号${res.data.boxBillNo},确定改箱?`;
                                    return;
                                }
                                //当原有箱号有值时如果序列号不存在原有箱号的商品数据中
                                if (res.data.boxId !== 0 && this.YyboxId && res.data.boxId !== this.YyboxId) {
                                    this.setSMinputxlhOrggxh();
                                    uni.showToast({
                                        title: '序列号已绑定箱',
                                        icon: 'none',
                                        duration: 1500
                                    });
                                    return;
                                }
                                // 当原有箱号没有值, 扫成功的序列号存在其它箱(其它箱未上架, 不存在箱库存), 则提示: 序列号已绑定箱
                                if (res.data.boxId !== 0 && !res.data.isBoxInventor && !this.YyboxId && res.data.boxId) {
                                    this.xlhShow = true;
                                    this.xlhContent = `序列号${res.data.serialNumber}存在箱号${res.data.boxBillNo},确定改箱?`;
                                    return;
                                }
                                let xlhindex = this.yyMXlist.findIndex((it) => {
                                    return it.materialNumber == res.data.materialNumber;
                                });
                                if ((this.YyboxId && res.data.isOldOps && xlhindex == -1) || (!res.data.isOldOps && this.YyboxId && !res.data.isBoxInventor)) {
                                    this.setSMinputxlhOrggxh();
                                    uni.showToast({
                                        title: '该序列号不在原有箱号内',
                                        icon: 'none',
                                        duration: 1500
                                    });
                                    return;
                                }
                            }
                            this.addXlhOrGgxh(res);
                        }
                        if (this.xlhOrggType == 'ggxh') {
                            // 缓存扫描或者输入过的虚了序列号或者规格型号 ,如果返回的serialNumber有值，表示当前输入的是序列号
                            if (this.YyboxId && !this.yyMXlist.some((obj) => obj.specifications === res.data.specifications)) {
                                this.setSMinputxlhOrggxh();
                                uni.showToast({
                                    title: '该规格型号不在原有箱号内',
                                    icon: 'none',
                                    duration: 1500
                                });
                                return;
                            }
                            this.itData = res.data;
                            this.dqNum = null;
                            this.focusInput = 'dqNum';
                            this.xlhOrggStrlist.push(this.xlhOrggType == 'xlh' ? res.data.serialNumber : res.data.specifications);
							this.confirmParams.push(res.data);
							//缓存2件装序列号
							// if(res.data.isTwo==2 && this.xlhOrggType == 'xlh') {
							// 	this.twoData.push(res.data.twoSerialNumber)
							// 	console.log(this.twoData,'=this.twoData=')
							// }
                        }
                        this.focusInput = this.xlhOrggType == 'ggxh' ? 'dqNum' : 'xlhggxh';
						
                    } else {
                        this.setSMinputxlhOrggxh();
                    }
                });
        },
        goback() {
            uni.navigateTo({
                url: '/pages/otherUnderwear/index'
            });
        },
        //序列号已上架入库的弹窗确认
        xlhConfirm() {
            this.xlhShow = false;
            this.addXlhOrGgxh(this.xlhggxhData);
        },
        xlhCancel() {
            this.setSMinputxlhOrggxh();
            this.xlhShow = false;
            this.confirmParams.pop();
        },
        //非CTN开头的箱号二次确认
        ctnConfirm() {
            this.noCtnShow = false;
            this.focusInput = '';
            if (this.boxInfo.data.id > 0 && !this.boxInfo.data.subStockCode) {
                this.yzxMakeSurelist = this.boxInfo.data.details;
                this.yzxMakeSureZCdata = this.boxInfo;
                this.yzxMakeSureShow = true;
            } else {
                this.setMbboxData(this.boxInfo);
            }
        }
    }
};
</script>
<style lang="scss">
@import '@/static/public.scss';
.mianheade2 {
    .pagetitle {
        margin-left: 28%;
    }
}
</style>