<!-- 出库回退上架 -->
<template>
    <view class="shpage ckhtsjpage =">
        <!-- 标题栏 -->
        <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 itembg2">
                <view class="it">
                    <text class="t1">仓库:</text>
                    <text class="t2">{{ warehouseName }}</text>
                </view>
            </view>
            <view class="item" :class="{ itembg2: boxstrList.length > 0 }" :style="{ 'pointer-events': boxstrList.length > 0 ? 'none' : '' }">
                <view class="it itsp">
                    <text class="t1">
                        <text class="redtag">*</text>
                        仓位号:
                    </text>
                    <w-select
                        class="wwselectit"
                        v-model="cwNum"
                        defaultValue="模糊搜索"
                        :list="cwselectList"
                        valueName="name"
                        keyName="name"
                        @change="cwchange"
                        :filterable="filterable"
                        ref="wselect1"
                        :focus="focusInput == 'cw'"
                        :showClose="true"
                        @focus="inputfocus($event, 'cw')"
                    ></w-select>
                    <image src="../../static/img/smico.png" class="searchico" @click="scanImg('cw')"></image>
                </view>
            </view>
            <view
                class="item"
                :class="{ itembg2: boxstrList.length > 0 || xlhstrList.length > 0 }"
                :style="{ 'pointer-events': boxstrList.length > 0 || xlhstrList.length ? 'none' : '' }"
            >
                <view class="it itsp">
                    <text class="t1">
                        <text class="redtag">*</text>
                        回退方式:
                    </text>
                    <w-select class="wwselectit" v-model="htfsType" defaultValue="模糊搜索" :list="htfslist" valueName="name" keyName="name" @change="htfsChange"></w-select>
                </view>
            </view>
            <view
                class="item"
                :class="{ itembg2: !cwcode || xlhstrList.length > 0 || (firstNumTag && htfsType == '按产品回退') || (ggxhhclist.length > 0 && htfsType == '按产品回退') }"
                :style="{
                    'pointer-events':
                        !cwcode || xlhstrList.length > 0 || (firstNumTag && htfsType == '按产品回退') || (ggxhhclist.length > 0 && htfsType == '按产品回退') ? 'none' : ''
                }"
            >
                <view class="it itsp">
                    <text class="t1">
                        <text class="redtag">*</text>
                        箱号:
                    </text>
                    <input
                        v-model.trim="xhNo"
                        class="inpt1"
                        type="text"
                        @focus="inputfocus($event, 'box')"
                        @blur="inputblur($event, '')"
                        @confirm="inputConfirm($event, 'box')"
                        :focus="focusInput == 'box'"
                    />
                    <image src="../../static/img/smico.png" class="searchico" @click="scanImg('box')"></image>
                </view>
            </view>
            <view
                v-if="htfsType == '按箱回退' || htfsType == ''"
                class="item"
                :class="{ itembg2: !cwcode || boxstrList.length == 0 || htfsType == '按箱回退' }"
                :style="{ 'pointer-events': !cwcode || boxstrList.length == 0 || htfsType == '按箱回退' ? 'none' : '' }"
            >
                <view class="it itsp">
                    <text class="t1">序列号:</text>
                    <input
                        v-model.trim="xlhVal"
                        class="inpt1"
                        type="text"
                        @focus="inputfocus($event, 'xlh')"
                        @blur="inputblur($event, '')"
                        @confirm="inputConfirm($event, 'xlh')"
                        :focus="focusInput == 'xlh'"
                    />
                    <image src="../../static/img/smico.png" class="searchico" @click="scanImg('xlh')"></image>
                </view>
            </view>
            <view
                v-if="htfsType == '按产品回退'"
                class="item"
                :class="{ itembg2: !cwcode || boxstrList.length == 0 }"
                :style="{ 'pointer-events': !cwcode || boxstrList.length == 0 ? 'none' : '' }"
            >
                <view class="it itsp">
                    <text class="t1">序列号/规格型号:</text>
                    <input
                        v-model.trim="xlhOrggxhVal"
                        class="inpt1"
                        type="text"
                        @focus="inputfocus($event, 'xlhGgx')"
                        @blur="inputblur($event, '')"
                        @confirm="inputConfirm($event, 'xlhGgx')"
                        :focus="focusInput == 'xlhGgx'"
                    />
                    <image src="../../static/img/smico.png" class="searchico" @click="scanImg('xlhGgx')"></image>
                </view>
            </view>
            <view
                class="item"
                :class="{ itembg2: !cwcode || boxstrList.length == 0 || htfsType == '按箱回退' || (htfsType == '按产品回退' && xlhOrggType == 'xlh') }"
                :style="{ 'pointer-events': !cwcode || boxstrList.length == 0 || htfsType == '按箱回退' || (htfsType == '按产品回退' && xlhOrggType == 'xlh') ? 'none' : '' }"
            >
                <view class="it itsp">
                    <text class="t1">
                        <text class="redtag">*</text>
                        上架数量:
                    </text>
                    <input
                        v-model.trim="sjNum"
                        class="inpt1"
                        type="number"
                        @focus="inputfocus($event, 'xlhGgxNum')"
                        @blur="inputblur($event, '')"
                        @confirm="inputConfirm($event, 'xlhGgxNum')"
                        :focus="focusInput == 'xlhGgxNum'"
                        :disabled="htfsType == '按箱回退' || (htfsType == '按产品回退' && xlhOrggType == 'xlh')"
                    />
                </view>
            </view>
            <view class="item item2">
                <view class="it2 it50">
                    <text class="t1">{{ ysjTotalNum }}</text>
                    <text class="t2">已上架总数量</text>
                </view>
                <view class="it2 it50" @click="xhmodelOpen">
                    <text class="t1 rednum">{{ boxstrList.length }}</text>
                    <text class="t2">已扫描箱数</text>
                </view>
            </view>
        </view>
        <!-- 列表内容-->
        <view class="shlb cgsjrklb f_cgsjrklb" :style="{ 'margin-top': heights.top + 'px', height: heights.body + 'px' }">
            <view class="item" v-for="(item, index) in dataList" :key="index">
                <view class="it" style="border: none">
                    <text class="txt titstr">{{ item.boxBillNo }}</text>
                    <view class="cssj_it ckhtit" v-for="(it, idx) in item.details" :key="idx + 'a'">
                        <view class="tlinb">
                            <text class="txt ntxtcolor">{{ it.specifications }}</text>
                            <!-- <text class="txt" v-if="htfsType =='按箱回退'">{{it.materialNumber}}</text> -->
                            <view class="nwemxflex" v-if="htfsType == '按箱回退'">
                                <text class="txt">{{ it.materialNumber }}</text>
                                <view class="fgline"></view>
                                <text class="txt">数量 {{ it.wuToatal }}</text>
                            </view>
                            <text class="txt" v-if="htfsType == '按产品回退'" style="margin-bottom: 6px;">{{ it.materialNumber }}</text>
                        </view>
                        <view class="txtNum nslbzflex">
                            <view class="n-sl-bz">
                                <view class="tinput">
                                    <text>上架数量</text>
                                    <u-input
                                        v-model="it.qty"
                                        border="surround"
                                        clearable
                                        class="inpt"
                                        type="number"
                                        :disabled="(it.itxlhlist && it.itxlhlist.length > 0) || htfsType == '按箱回退'"
                                        @focus="inputfocus($event, 'sjNum')"
                                        @blur="inputblur($event, '')"
                                        @confirm="inputConfirm($event, 'sjNum')"
                                        @input="sjNumInput($event, 'sjNum', index, idx)"
                                    ></u-input>
                                </view>
                                <view class="tinput" @click="openRemarTk(it.remark, index, idx)">
                                    <text class="ntxtcolor nbtxt">备注</text>
                                    <u-input v-model="it.remark" border="surround" clearable class="inpt" type="text" style="pointer-events: none"></u-input>
                                </view>
                            </view>
                        </view>
                        <!-- <view style="display: flex; margin-top: 8px; margin-bottom: 5px">
                            <text class="uni-title uni-common-pl" style="width: 100rpx; font-size: 12px; color: #717275">备注 :</text>
                            <textarea
                                v-model="it.remark"
                                auto-height
                                :maxlength="100"
                                style="background-color: #fff; font-size: 12px; border: 1px solid #ccc; padding: 5px 10px; border-radius: 6px"
                            />
                        </view> -->
                    </view>
                </view>
            </view>
        </view>
        <!-- 已扫描箱号-->
        <u-modal :show="xhlistshow" title="已扫描箱号" @confirm="xhlistshow = false">
            <view style="display: flex; flex-direction: column; overflow-y: auto; max-height: 60vh; overflow-y: auto">
                <view v-for="(item, index) in boxstrList">{{ item }}</view>
            </view>
        </u-modal>
        <!-- 底部按钮固定-->
        <view class="footbts">
            <view class="bt bt1" @click="qktkshow = true">清空上架数量</view>
            <view class="bt bt2" @click="wcNxetFn">完成/下一单</view>
        </view>
        <!--清空二次确认弹框-->
        <u-modal :show="qktkshow" :title="qktkTitle" :content="qktkContent" @confirm="qktkconfirm" :showCancelButton="true" @cancel="qktkshow = false"></u-modal>
        <!--备注弹框-->
        <u-modal :show="remarkshow" title="备注" @confirm="remarkconfirm" :showCancelButton="true" @cancel="remarkshow = false">
            <textarea v-model="dqremarkVal" auto-height :maxlength="100" class="remaktinpt" placeholder="请输入备注信息" />
        </u-modal>
    </view>
</template>

<script>
export default {
    data() {
        return {
            heights: {
                top: 0,
                body: 0
            },
            xhlistshow: false, // 箱号查看弹框
            erpOrgCode: '',
            focusInput: 'cw', // 自动聚焦
            focusTag: '', //  判斷聚焦
            warehouseName: '', // 仓库名称
            warehouseCode: '', // 仓库编码
            cwNum: '', // 仓位的值
            cwselectList: [], //仓位下拉集合
            cwcode: '', // 仓位code
            xhNo: '', // 箱号的值
            htkey: '', //回退方式id
            htfsType: '', // 回退方式
            htfslist: [
                {
                    id: 1,
                    name: '按箱回退'
                },
                {
                    id: 2,
                    name: '按产品回退'
                }
            ], // 回退下拉选择
            dqboxId: '', // 扫当前箱子的id
            boxstrList: [], // 缓存的箱号信息（多个）
            xlhVal: '', // 序列号的值
            xlhOrggxhVal: '', // 序列号or规格型号
            xlhstrList: [], // 缓存的序列號信息（多个）
            sjNum: '', // 上架数量
            ysjTotalNum: 0, // 已上架总数量
            filterable: true,
            dataList: [], // 明细列表数据
            qktkshow: false, //清空二次确认弹框
            dqBoxInfo: {}, // 扫描当前箱的物料信息
            qktkTitle: '确定清空',
            qktkContent: '是否清空当前页面已经扫描的箱号信息/序列号信息和统计数值?',
            dqboxNO: '', //用来是否保存当前输入箱号的值
            dqboxXlh: '', //用来是否保存当前输入xlh的值
            APPdevice: uni.getStorageSync('devicePixelRatio'), // 缓存设备的像素比用来区分普通安卓normalAnroid还是pda
            requestStatus: false, // 请求状态识别变量
            firstNumTag: false, // 判断数量第一次是否有回车
            xlhOrggType: '', // 判断当前扫描的是序列号还是规格型号
            dqxlhOrggxh: '', //用来是否保存当前输入xlh的值
            itIndx: null, //如果扫描的是规格序列号，判断是在明细的那个位置
            dqggxhItem: {}, // 按照产品回退，如果是规格型号（新增），缓存当前的要新增的规格的对象
            ggxhhclist: [], // 规格型号缓存
            addmxitem: [], // 明细添加数据
            requestStatus: false, // 请求状态识别变量
            remarkshow: false, // 当前备注弹框
            dqremarkVal: '', //当前弹框显示备注内容
			twoData:[],
            scanTracker: {
                lastScanTime: 0,
                lastScanCode: null
            } // 源头处理扫码段时间内重复扫
        };
    },
    watch: {
        xhNo(n) {
            if (!n) {
                this.dqboxNO = '';
            }
        },
        xlhVal(n) {
            if (!n) {
                this.dqboxXlh = '';
            }
        },
        // 仓位下拉数据
        cwNum(n, o) {
            if (!n) {
                this.cwselectList = [];
                this.cwcode = 0;
                this.warehouseCode = '';
                this.warehouseName = '';
                this.erpOrgCode = '';
            } else {
                this.getcwList(n);
            }
        }
    },
    onReady() {
        this.heights = this.$util.setlistHeight('.sh_gdInfo', '.footbts');
    },
    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: {
        // 开启广播
        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');
        },
        // 打开当前明细行备注弹框
        openRemarTk(remarkStr, index, indexs) {
            this.remarkshow = true;
            this.dqremarkVal = remarkStr;
            this.remarkidx = [index, indexs];
        },
        // 确认修改弹框备注
        remarkconfirm() {
            this.dataList[this.remarkidx[0]].details[this.remarkidx[1]].remark = this.dqremarkVal;
            this.remarkshow = false;
        },
        // 打开查看箱号
        xhmodelOpen() {
            if (this.boxstrList.length > 0) {
                this.xhlistshow = true;
            }
        },
        // 回退方式下拉选择
        htfsChange(e) {
            this.htkey = e.id;
            this.qktkconfirm();
        },
        //明细上架数量输入实时监听
        sjNumInput(val, type, index, idx) {
            if (val) {
                this.ysjTotalNum = this.$util.sumNestedObjectValues(this.dataList, 'details', 'qty');
                this.sjNum = this.$util.sumNestedObjectValues([this.dataList[index]], 'details', 'qty');
            }
        },
        // 完成
        wcNxetFn() {
            // 判断必填
            if (this.boxstrList.length == 0 || !this.ysjTotalNum || (this.xlhOrggType == 'ggxh' && this.xlhOrggxhVal && !this.firstNumTag)) {
                uni.showToast({
                    title: '请先输入必填值',
                    icon: 'none',
                    duration: 1500
                });
                return;
            }
            let formdata = {
                method: this.htkey,
                orgCode: this.erpOrgCode,
                stockCode: this.warehouseCode,
                subStockCode: this.cwcode,
                details: []
            };

            this.dataList.forEach((it) => {
                if (it.details && it.details.length > 0) {
                    it.details.forEach((k) => {
                        formdata.details.push({
                            boxId: k.boxId,
                            serialNumberBoxId: k.serialNumberBoxId,
                            materialNumber: k.materialNumber,
                            qty: parseInt(k.qty ? k.qty : 0),
                            serialNumbers: k.itxlhlist,
                            remark: k.remark
                        });
                    });
                }
            });
            console.log('出庫上架提交数据', this.dataList, formdata);
            // 节流
            if (this.requestStatus) {
                return false;
            }
            this.requestStatus = true;
            this.$api.post('/BackRecord/OnShelf', formdata).then((res) => {
                if (res.status == 200) {
                    uni.showToast({
                        title: '成功',
                        icon: 'none',
                        duration: 2000
                    });
                    setTimeout(() => {
                        uni.navigateTo({
                            url: '/pages/otherUnderwear/outStockUp'
                        });
                    }, 200);
                    // this.$router.go(0)
                }
            });
            setTimeout(() => {
                this.requestStatus = false;
            }, 1500);
        },
        // 清空二次确认
        qktkconfirm() {
            this.boxstrList = [];
            this.dataList = [];
            this.xlhstrList = [];
			this.twoData=[];
            this.xlhVal = '';
            this.xhNo = '';
            this.sjNum = '';
            this.qktkshow = false;
            this.ysjTotalNum = 0;
            this.xlhOrggType = '';
            this.xlhOrggxhVal = '';
            this.dqxlhOrggxh = '';
            this.xlhstrList = [];
			this.twoData=[]
            this.firstNumTag = false;
            this.itIndx = '';
            this.dqggxhItem = {};
            this.ggxhhclist = [];
        },
        // 获取仓位下拉数据
        getcwList(val) {
            if (!val) return;
            this.$api
                .get('/SysConfig/GetSubUcStockByName', {
                    name: val
                })
                .then((res) => {
                    if (res.status == 200) {
                        this.cwselectList = res.data;
                        if (this.cwselectList && this.cwselectList.length == 1) {
                            this.cwNum = this.cwselectList[0].name;
                            this.cwchange(this.cwselectList[0]);
                            this.$refs.wselect1.optionsShow = false;
                            this.$refs.wselect1.isShow = false;
                        }
                    }
                });
        },
        // 仓位下拉选择
        cwchange(e) {
            this.cwcode = e.code;
            this.warehouseCode = e.stockCode;
            this.warehouseName = e.stockName;
            this.erpOrgCode = e.erpOrgCode;
        },
        // 普通安卓设备碘酒扫描图标扫描
        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 == 'box') {
                // 有些PDA会自带换行符，trim函数处理下
                this.xhNo = '';
                this.xhNo = code.trim();
                this.getboxinfo();
            }
            // 序列号
            if (this.focusTag == 'xlh') {
                this.xlhVal = '';
                this.xlhVal = code.trim();
                this.getXlhInfo();
                //根据序列号去更新对应的明细规格型号的上架数量的值 扫描一个数量加一，重复扫描要提示
            }
            // 序列号
            if (this.focusTag == 'cw') {
                this.cwNum = '';
                this.cwNum = code.trim();
                this.$refs.wselect1.inputData = code.trim();
                this.getcwList();
                //根据序列号去更新对应的明细规格型号的上架数量的值 扫描一个数量加一，重复扫描要提示
            }
            // 序列号/规格型号
            if (this.focusTag == 'xlhGgx') {
                this.xlhOrggxhVal = '';
                this.xlhOrggxhVal = code.trim();
                this.getXlhOrggxhInfo();
                //根据序列号去更新对应的明细规格型号的上架数量的值 扫描一个数量加一，重复扫描要提示
            }
        },
        // 輸入失去焦點
        inputblur(val, type) {
            this.focusTag = type;
        },
        // 输入聚焦
        inputfocus(val, type) {
            this.focusTag = type;
            if (type == 'cw') {
                this.focusInput = 'cw';
            }
        },
        // 区分不同的扫描数据、输入数据做处理显示 box
        setSMstr(str) {
            let lth = str.length - this.dqboxNO.length;
            if (lth > 1) {
                // 扫描的值
                this.xhNo = str.substring(this.dqboxNO.length);
            } else {
                this.dqboxNO = str;
                this.xhNo = str;
            }
        },
        // 区分不同的扫描数据、输入数据做处理显示 xlh
        setSMxlhggxhstr(str) {
            let lth = str.length - this.dqxlhOrggxh.length;
            if (lth > 1) {
                // 扫描的值
                this.xlhOrggxhVal = str.substring(this.dqxlhOrggxh.length);
            } else {
                this.dqxlhOrggxh = str;
                this.xlhOrggxhVal = str;
            }
        },
        // 区分不同的扫描数据、输入数据做处理显示 xlh
        setSMxlhstr(str) {
            let lth = str.length - this.dqboxXlh.length;
            if (lth > 1) {
                // 扫描的值
                this.xlhVal = str.substring(this.dqboxXlh.length);
            } else {
                this.dqboxXlh = str;
                this.xlhVal = str;
            }
        },
        // 输入确认
        inputConfirm(val, type) {
            if (val) {
                if (this.focusTag == 'box') {
                    this.setSMstr(val.target.value);
                    this.getboxinfo();
                }
                if (this.focusTag == 'xlh') {
                    this.setSMxlhstr(val.target.value);
                    this.getXlhInfo();
                }
                if (this.focusTag == 'xlhGgx') {
                    // 输入完序列号、规格型号点击确认的时候
                    //1.如果当前存在的序列号、规格型号对应的规格型号不存在明细里面加一条明细数据，
                    this.setSMxlhggxhstr(val.target.value);
                    this.getXlhOrggxhInfo();
                }
                if (this.focusTag == 'xlhGgxNum') {
                    this.xlhGgxNum = val.target.value;
                    if (this.xlhGgxNum !== null && this.xlhGgxNum !== '' && this.xlhOrggType == 'ggxh' && this.itIndx !== '') {
                        if (this.dataList.length == 0) {
                            this.dataList = [];
                            this.dqBoxInfo.details = [];
                            this.dqBoxInfo.details.push(this.addmxitem);
                            this.dataList = [this.dqBoxInfo].concat(this.dataList);
                            this.dataList[0].details[this.itIndx].qty = parseInt(this.sjNum);
                        } else {
                            // 先判断这个规格是否存在当前明细里面，如果存在直接数量累加，不存在就直接新增
                            if (this.dqggxhItem.specifications) {
                                this.dataList[0].details.push(this.dqggxhItem);
                                this.itIndx = this.dataList[0].details.length - 1;
                                this.dataList[0].details[this.itIndx].qty = parseInt(this.sjNum);
                            } else {
                                this.dataList[0].details[this.itIndx].qty = this.dataList[0].details[this.itIndx].qty + parseInt(this.sjNum);
                            }
                        }
                        uni.showToast({
                            title: '数量获取成功',
                            icon: 'none',
                            duration: 2000
                        });
                        this.sjNum = null;
                        this.xlhOrggxhVal = null;
                        this.itIndx = '';
                        this.focusInput = 'xlhGgx';
                        this.firstNumTag = true;
                        this.ysjTotalNum = this.$util.sumNestedObjectValues(this.dataList, 'details', 'qty');
                        if (this.xlhOrggType == 'ggxh') {
                            this.sjNum = null;
                            this.focusInput = '';
                            setTimeout(() => {
                                this.focusInput = 'xlhGgx';
                            }, 300);
                        }
                    } else {
                        uni.showToast({
                            title: '请先输入数量在操作',
                            icon: 'none',
                            duration: 2000
                        });
                    }
                }
            }
        },
        //回退方式是按照产品--扫码序列号或者规格型号获取相关信息
        getXlhOrggxhInfo() {
            if (this.boxstrList.length == 0) {
                this.setSMinputxlhorggxh();
                uni.showToast({
                    title: '请先扫码或输入箱号获取箱信息',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }

            if (!this.xlhOrggxhVal) {
                this.setSMinputxlhorggxh();
                uni.showToast({
                    title: '请输入或者扫描序列号/规格型号',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }
            if (this.xlhstrList.includes(this.xlhOrggxhVal) || this.twoData.includes(this.xlhOrggxhVal)) {
                this.setSMinputxlhorggxh();
                uni.showToast({
                    title: '该序列号/规格型号已扫描',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }
            this.focusInput = '';
            // console.log('扫描序列号规格型号', this.xlhOrggxhVal, this.dqxlhOrggxh)
            //盘点 --扫码序列号、规格型号带出相关的明细数据
            this.$api
                .get('/SysConfig/GetMaterial', {
                    serialNumber: this.xlhOrggxhVal,
                    IsOps: true,
                    serialStatus: 2
                })
                .then((res) => {
                    if (res.status == 200) {
                        // 判断扫码的是个序列号还是规格型号
                        this.xlhOrggType = res.data.serialNumber ? 'xlh' : 'ggxh';
                        if (this.xlhOrggType == 'xlh' && res.data.isBoxInventory && !res.data.isOldOps) {
                            this.setSMinputxlhorggxh();
                            uni.showToast({
                                title: '序列号已上架入库',
                                icon: 'none',
                                duration: 2000
                            });
                            return;
                        }
                        // 判断当前序列号或者规格型号对应的规格是否存在在明细里面
                        let findindx = this.dataList.length == 0 ? -1 : this.dataList[0].details.findIndex((obj) => obj.specifications === res.data.specifications);
                        // 如果不存在就新增一条明细，如果存在就累计数量。
                        if (findindx !== -1) {
                            //如果是序列号
                            if (this.xlhOrggType == 'xlh') {
                                this.sjNum = 1;
                                this.dataList[0].details[findindx].qty = this.dataList[0].details[findindx].qty + 1;
                                this.dataList[0].details[findindx].itxlhlist.push(res.data.serialNumber);
                                // 缓存当前扫描数据
                                this.xlhstrList.push(res.data.serialNumber);
								if(res.data.isTwo==2) {
									this.twoData.push(res.data.twoSerialNumber)
								}
                                uni.showToast({
                                    title: '获取成功',
                                    icon: 'none',
                                    duration: 2000
                                });
                            }
                            if (this.xlhOrggType == 'ggxh') {
                                this.sjNum = null;
                                this.itIndx = findindx;
                                this.dqggxhItem = {};
                                this.focusInput = '';
                            }
                            //清空当前框数据 自动聚焦箱号
                            this.dqxlhOrggxh =
                                this.xlhOrggType == 'xlh'
                                    ? res.data.serialNumber
                                    : this.xlhOrggType == 'ggxh'
                                    ? this.xlhOrggxhVal == res.data.specifications
                                        ? res.data.specifications
                                        : res.data.materialNumber
                                    : null;
                            this.xlhOrggxhVal =
                                this.xlhOrggType == 'xlh'
                                    ? res.data.serialNumber
                                    : this.xlhOrggType == 'ggxh'
                                    ? this.xlhOrggxhVal == res.data.specifications
                                        ? res.data.specifications
                                        : res.data.materialNumber
                                    : null;
                            //计算已上架总数量
                            this.ysjTotalNum = this.$util.sumNestedObjectValues(this.dataList, 'details', 'qty');
                        } else {
                            if (this.dataList.length == 0) {
                                this.dqBoxInfo.details = [];
                                this.addmxitem = {};
                                this.addmxitem = {
                                    qty: 1,
                                    itxlhlist: [],
                                    boxId: this.dqboxId,
                                    serialNumberBoxId: this.dqboxId,
                                    stockCode: this.warehouseCode,
                                    subStockCode: this.cwcode,
                                    materialNumber: res.data.materialNumber,
                                    materialNumber: res.data.materialNumber,
                                    specifications: res.data.specifications,
                                    materialName: res.data.materialName,
                                    remark: ''
                                };
                                if (this.xlhOrggType == 'xlh') {
                                    this.dqBoxInfo.details = [];
                                    this.dqBoxInfo.details.push(this.addmxitem);
                                    this.dataList = [this.dqBoxInfo].concat(this.dataList);
                                }
                                this.itIndx = 0;
                            } else {
                                if (this.xlhOrggType == 'xlh') {
                                    this.dataList[0].details.push({
                                        qty: 1,
                                        itxlhlist: [],
                                        boxId: this.dqboxId,
                                        serialNumberBoxId: this.dqboxId,
                                        stockCode: this.warehouseCode,
                                        subStockCode: this.cwcode,
                                        materialNumber: res.data.materialNumber,
                                        materialNumber: res.data.materialNumber,
                                        specifications: res.data.specifications,
                                        materialName: res.data.materialName,
                                        remark: ''
                                    });
                                    this.itIndx = this.dataList[0].details.length - 1;
                                }
                                if (this.xlhOrggType == 'ggxh') {
                                    this.dqggxhItem = {};
                                    this.dqggxhItem = {
                                        qty: 0,
                                        itxlhlist: [],
                                        boxId: this.dqboxId,
                                        serialNumberBoxId: this.dqboxId,
                                        stockCode: this.warehouseCode,
                                        subStockCode: this.cwcode,
                                        materialNumber: res.data.materialNumber,
                                        materialNumber: res.data.materialNumber,
                                        specifications: res.data.specifications,
                                        materialName: res.data.materialName,
                                        remark: ''
                                    };
                                    this.itIndx = this.dataList[0].details.length;
                                }
                            }
                            if (this.xlhOrggType == 'xlh') {
                                this.sjNum = 1;
                                this.dataList[0].details[this.itIndx].itxlhlist.push(res.data.serialNumber);
                                this.xlhstrList.push(res.data.serialNumber);
								//缓存2件装序列号
								if(res.data.isTwo==2) {
									this.twoData.push(res.data.twoSerialNumber)
								}
                            }
                            if (this.xlhOrggType == 'ggxh') {
                                this.sjNum = null;
                            }
                            //清空当前框数据 自动聚焦箱号
                            this.dqxlhOrggxh =
                                this.xlhOrggType == 'xlh'
                                    ? res.data.serialNumber
                                    : this.xlhOrggType == 'ggxh'
                                    ? this.xlhOrggxhVal == res.data.specifications
                                        ? res.data.specifications
                                        : res.data.materialNumber
                                    : null;
                            this.xlhOrggxhVal =
                                this.xlhOrggType == 'xlh'
                                    ? res.data.serialNumber
                                    : this.xlhOrggType == 'ggxh'
                                    ? this.xlhOrggxhVal == res.data.specifications
                                        ? res.data.specifications
                                        : res.data.materialNumber
                                    : null;
                        }
                        setTimeout(() => {
                            this.focusInput = this.xlhOrggType == 'ggxh' ? 'xlhGgxNum' : 'xlhGgx';
                        }, 300);

                        //计算已上架总数量
                        this.ysjTotalNum = this.$util.sumNestedObjectValues(this.dataList, 'details', 'qty');
                        this.ggxhhclist.push(res.data.specifications);
                    } else {
                        this.setSMinputxlhorggxh();
                    }
                });
        },
        // 扫描框数据重置，光标聚焦
        setSMinputbox() {
            this.focusInput = '';
            setTimeout(() => {
                this.xhNo = '';
                this.dqboxNO = '';
                this.focusInput = 'box';
                this.xlhVal = '';
                this.dqboxXlh = '';
                this.xlhOrggxhVal = '';
                this.dqxlhOrggxh = '';
                this.sjNum = null;
            }, 200);
        },
        // 根据箱号获取箱信息
        getboxinfo() {
            if (!this.htfsType) {
                this.setSMinputbox();
                uni.showToast({
                    title: '请先选择回退方式',
                    icon: 'none',
                    duration: 1500
                });
                return;
            }
            if (!this.xhNo) {
                this.setSMinputbox();
                uni.showToast({
                    title: '请扫描或输入箱号',
                    icon: 'none',
                    duration: 1500
                });
                return;
            }
            if (this.boxstrList.includes(this.xhNo)) {
                this.setSMinputbox();
                uni.showToast({
                    title: '该箱号已扫描',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }
            this.focusInput = '';
            let pathurl = this.htfsType == '按箱回退' ? '/SysConfig/GetBox_OutStock' : '/SysConfig/GetBoxSynthesis';
            this.$api
                .get(pathurl, {
                    boxBillNo: this.xhNo
                })
                .then((res) => {
                    if (res.status == 200) {
                        if (res.data !== null) {
                            if (this.htfsType == '按产品回退' && this.cwcode !== res.data.subStockCode && res.data.subStockCode) {
                                uni.showToast({
                                    title: '请扫描对应仓位内的箱号',
                                    icon: 'none',
                                    duration: 2000
                                });
                                this.xhNo = '';
                                return;
                            }
                            if (this.htfsType == '按箱回退') {
                                // 缓存当前箱的物料信息
                                this.dqBoxInfo = JSON.parse(JSON.stringify(res.data));
                                res.data.details.forEach((it) => {
                                    it.wuToatal = JSON.parse(JSON.stringify(it.qty));
                                    it.itxlhlist = this.htfsType == '按箱回退' ? it.serialNumbers : [];
                                    it.boxId = res.data.id;
                                    it.serialNumberBoxId = res.data.id;
                                    it.stockCode = this.warehouseCode;
                                    it.subStockCode = this.cwcode;
                                    it.remark = '';
                                });
                                this.dataList = [res.data].concat(this.dataList);
                                this.dqboxId = res.data.id;
                                // 缓存箱号
                                this.boxstrList.push(res.data.boxBillNo);
                                // 上架数量
                                this.sjNum = res.data.totalQty;
                                // 计算已上架总数量
                                this.ysjTotalNum = this.$util.sumNestedObjectValues(this.dataList, 'details', 'qty');
                                //清空当前框数据 自动聚焦箱号
                                this.focusInput = 'box';
                                this.xhNo = res.data.boxBillNo;
                                this.dqboxNO = res.data.boxBillNo;
                            }
                            if (this.htfsType == '按产品回退') {
                                this.focusInput = 'xlhGgx';
                                // 缓存当前箱的物料信息
                                this.dqBoxInfo = JSON.parse(JSON.stringify(res.data));
                                this.dqboxId = res.data.id;
                                // 缓存箱号
                                this.boxstrList.push(res.data.boxBillNo);
                                // 计算已上架总数量
                                this.ysjTotalNum = this.$util.sumNestedObjectValues(this.dataList, 'details', 'qty');
                                // 记录当前箱号和箱号的值
                                this.xhNo = res.data.boxBillNo;
                                this.dqboxNO = res.data.boxBillNo;
                            }
                            uni.showToast({
                                title: '获取成功',
                                icon: 'none',
                                duration: 2000
                            });
                        }
                    } else {
                        this.setSMinputbox();
                    }
                });
        },
        // 扫描序列号框数据重置，光标聚焦
        setSMinputxlh() {
            this.focusInput = '';
            setTimeout(() => {
                this.xlhVal = '';
                this.dqboxXlh = '';
                this.focusInput = 'xlh';
                this.sjNum = null;
            }, 200);
        },
        // 扫描序列号或者规格型号框数据重置，光标聚焦
        setSMinputxlhorggxh() {
            this.focusInput = '';
            setTimeout(() => {
                this.xlhOrggxhVal = '';
                this.dqxlhOrggxh = '';
                this.focusInput = 'xlhGgx';
            }, 200);
        },
        // 根据序列号获取箱信息
        getXlhInfo() {
            if (!this.xlhVal) {
                this.setSMinputxlh();
                uni.showToast({
                    title: '该扫描或输入序列号',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }
            if (this.xlhstrList.includes(this.xlhVal) || this.twoData.includes(this.xlhOrggxhVal)) {
                this.setSMinputxlh();
                uni.showToast({
                    title: '该序列号已扫描',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }
            //根据序列号或规格型号搜索物料信息
            this.$api
                .get('/SysConfig/GetMaterial', {
                    serialNumber: this.xlhVal,
                    IsOps: true,
                    serialStatus: 2
                })
                .then((res) => {
                    if (res.status == 200) {
                        if (!res.data.serialNumber) {
                            this.setSMinputxlh();
                            uni.showToast({
                                title: '扫描的不是序列号',
                                icon: 'none',
                                duration: 2000
                            });
                            return;
                        }
                        if (res.data.isBoxInventory && !res.data.isOldOps) {
                            this.setSMinputxlh();
                            uni.showToast({
                                title: '序列号已上架入库',
                                icon: 'none',
                                duration: 2000
                            });
                            return;
                        }
                        // 提取列表的物料id
                        let materialCodelist = [];
                        materialCodelist = this.dqBoxInfo.details.flatMap((obj) => obj.serialNumbers);
                        // 1.判断当前序列号如果与当前箱信息的对应的物料物料信息匹配 上架数量加一
                        // 2.如果当前序列号不属于当前箱，就在当前箱的明细里面添加一条对应的物料信息
                        let indexobj = null;
                        indexobj = this.$util.findMaterialCodePosition(this.dataList, res.data.materialNumber);
                        // 找到这条物料对应的明细的规格物料上架数量加一，切不可在编辑
                        if (indexobj || indexobj == 0) {
                            // 如果是第一次扫，就清空上架数量，切锁住输入框不能在输入
                            if (this.dataList[indexobj.objectIndex].details[indexobj.detailIndex].itxlhlist.length == 0) {
                                this.dataList[indexobj.objectIndex].details[indexobj.detailIndex].qty = 0;
                            }
                            ++this.dataList[indexobj.objectIndex].details[indexobj.detailIndex].qty;
                            this.dataList[indexobj.objectIndex].details[indexobj.detailIndex].itxlhlist.push(this.xlhVal);
                        } else {
                            const indexsh = this.dataList.findIndex((obj) => obj.boxBillNo === res.data.boxBillNo);
                            console.log(12, indexsh);
                            if (indexsh !== -1) {
                                const shwlidindex = this.dataList[indexsh].details.findIndex((obj) => obj.materialNumber === res.data.materialNumber);
                                console.log(13, shwlidindex, this.dataList[indexsh].details);
                                if (shwlidindex !== -1) {
                                    this.dataList[indexsh].details[shwlidindex].itxlhlist.push(res.data.serialNumber);
                                    this.dataList[indexsh].details[shwlidindex].serialNumbers.push(res.data.serialNumber);
                                    ++this.dataList[indexsh].details[shwlidindex].qty;
                                } else {
                                    this.dataList[indexsh].details.push(this.SetSHitem(res));
                                }
                            }
                        }
                        // 计算已上架总数量
                        this.ysjTotalNum = this.$util.sumNestedObjectValues(this.dataList, 'details', 'qty');
                        // 缓存该序列号
                        this.xlhstrList.push(res.data.serialNumber);
						//缓存2件装序列号
						if(res.data.isTwo==2) {
							this.twoData.push(res.data.twoSerialNumber)
						}
						
						
                        // 上架数量默认为1
                        this.sjNum = 1;
                        //清空当前框数据 自动聚焦箱号
                        this.focusInput = 'xlh';
                        this.xlhVal = res.data.serialNumber;
                        this.dqboxXlh = res.data.serialNumber;
                        uni.showToast({
                            title: '获取成功',
                            icon: 'none',
                            duration: 2000
                        });
                    } else {
                        this.setSMinputxlh();
                    }
                });
        },
        // 设置散货明细数据
        SetSHitem(res) {
            let obj = {
                boxId: this.dqboxId,
                serialNumberBoxId: res.data.boxId,
                itxlhlist: [res.data.serialNumber],
                materialName: res.data.materialName,
                materialNumber: res.data.materialNumber,
                qty: 1,
                serialNumbers: [res.data.serialNumber],
                specifications: res.data.specifications,
                stockCode: this.warehouseCode,
                subStockCode: this.cwcode,
                wuToatal: 0
            };
            return obj;
        },
        goback() {
            uni.navigateTo({
                url: '/pages/otherUnderwear/index'
            });
        }
    }
};
</script>

<style lang="scss">
@import '@/static/public.scss';

.mianheade2 {
    .pagetitle {
        margin-left: 26%;
    }
}
</style>
