<!-- 盘点 -->
<template>
    <view class="shpage pdpage">
        <!-- 标题栏 -->
        <view class="mianheade mianheade2" @click="goback()">
            <image src="../../static/img/n_baiback.png" class="blacBackico"></image>
            <text class="pagetitle">盘点</text>
        </view>
        <view :style="{ height: '100%' }">
            <!-- 固定内容-->
            <view class="sh_gdInfo" :style="{ 'pointer-events': wcpdTag ? 'none' : '' }">
                <view :class="warehouseDis ? 'item itembg2' : 'item'" :style="warehouseDis ? 'pointer-events:none' : ''">
                    <view class="it">
                        <text class="t1">
                            <text class="redtag">*</text>
                            仓库:
                        </text>
                        <w-select
                            class="wwselectit"
                            v-model="warehouseName"
                            defaultValue="请选择"
                            :list="warehouseList"
                            valueName="name"
                            keyName="name"
                            @change="ckchange"
                            ref="wselectck"
                        ></w-select>
                    </view>
                </view>
                <view class="item itembg2" style="pointer-events: none">
                    <view class="it itsp">
                        <text class="t1">
                            <text class="redtag">*</text>
                            盘点类型:
                        </text>
                        <w-select
                            class="wwselectit"
                            v-model="pdlxVal"
                            defaultValue="模糊搜索"
                            :list="pdSelectlist"
                            valueName="content"
                            keyName="content"
                            @change="pdlxchange"
                            :filterable="filterable"
                        ></w-select>
                    </view>
                </view>
                <view
                    class="item"
                    :class="{ itembg2: xhDisabled || (xlhStrlist && xlhStrlist.length > 0) || wcpdTag }"
                    :style="{ 'pointer-events': (xlhStrlist && xlhStrlist.length > 0) || wcpdTag ? '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'"
                            :disabled="xhDisabled"
                        />
                        <image src="../../static/img/smico.png" class="searchico" @click="scanImg('box')"></image>
                    </view>
                </view>
                <view class="item" :class="{ itembg2: boxstrList.length == 0 || wcpdTag }" :style="{ 'pointer-events': boxstrList.length == 0 || wcpdTag ? '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, '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: wcpdTag || xlhOrggType == 'xlh' || !xlhOrggType || !xlhOrggxhVal }"
                    :style="{ 'pointer-events': wcpdTag || xlhOrggType == 'xlh' || !xlhOrggType || !xlhOrggxhVal ? 'none' : '' }"
                >
                    <view class="it itsp">
                        <text class="t1">
                            <text class="redtag">*</text>
                            数量:
                        </text>
                        <input
                            v-model="xlhGgxNum"
                            class="inpt1"
                            type="number"
                            @focus="inputfocus($event, 'xlhGgxNum')"
                            @blur="inputblur($event, '')"
                            @confirm="inputConfirm($event, 'xlhGgxNum')"
                            :focus="focusInput == 'xlhGgxNum'"
                        />
                    </view>
                </view>
                <view style="margin-left: 20px; margin-top: 10px">
                    <u-checkbox-group v-model="checkboxValue1" placement="column" @change="checkboxChange">
                        <u-checkbox :labelSize="14" :customStyle="{ marginBottom: '8px' }" label="重新上架" name="重新上架" :disabled="warehouseName ? false : true"></u-checkbox>
                    </u-checkbox-group>
                </view>
            </view>
            <!-- 列表内容-->
            <view class="shlb cgsjrklb jscklb" :style="{ 'margin-top': heights.top - 5 + 'px', height: heights.body + 'px' }">
                <view class="item" v-for="(item, index) in dataList" :key="index" :style="{ 'pointer-events': wcpdTag ? 'none' : '' }">
                    <!-- 当点击完成盘点按钮时, 如果盘点数量小于数量则绿色标识-->
                    <view class="it" :class="{ redtxt: wcpdTag && parseInt(item.pdNum) > parseInt(item.qty), greentxt: wcpdTag && parseInt(item.pdNum) < parseInt(item.qty) }">
                        <view
                            class="txt titstr"
                            style="padding-bottom: 5px; margin-bottom: 5px; display: flex; flex-direction: row; align-items: center; justify-content: space-between"
                        >
                            <view style="width: 95%">{{ item.specifications }}</view>
                            <image
                                src="../../static/img/ic_remark.png"
                                class="searchico"
                                @click="openRrmark(index)"
                                v-if="parseInt(item.pdNum) < parseInt(item.qty) && item.remark"
                            ></image>
                            <image
                                src="../../static/img/Frame.png"
                                class="searchico"
                                @click="openRrmark(index)"
                                v-if="parseInt(item.pdNum) < parseInt(item.qty) && !item.remark"
                            ></image>
                        </view>
                        <view class="txtflexnum" style="height: 22px">
                            <text class="txt">数量: {{ item.qty }}</text>
                            <text class="txt">盘点数量: {{ item.pdNum }}</text>
                            <!-- 完成盘点之后如果仓库编码是hd或者gd子仓库可以修改-->
                            <view class="txt" @click="openStoclmod(item, index)" style="max-width: 60%; display: flex; align-items: center">
                                <view>子仓库:</view>
                                <view
                                    class="ckborder"
                                    style="height: 16px; max-width: 60%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis"
                                    :style="{ background: wcpdTag || warehouseCode == 'HD' || warehouseCode == 'GD' ? '#F3F6FA' : '#fff' }"
                                >
                                    {{ item.zckVal }}
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <!-- 不是以CTN开头的箱号需要二次确认 -->
        <u-modal :show="noCtnShow" :content="ctnContent" @confirm="ctnConfirm" :showCancelButton="true" @cancel="noCtnShow = false" ref="noCtnModel"></u-modal>
        <!--清空二次确认弹框-->
        <u-modal :show="qktkshow" :title="qktkTitle" :content="qktkContent" @confirm="qktkconfirm" :showCancelButton="true" @cancel="qktkshow = false" ref="model1"></u-modal>
        <u-modal
            ref="model2"
            v-preventReClick="100"
            :show="qrpdshow"
            @cancel="qrpdshow = false"
            title="确认盘点"
            content="确认进行盘盈/盘亏?"
            @confirm="tjDataFn"
            :showCancelButton="true"
        ></u-modal>
        <!--子仓库选择弹框选择 -->
        <u-popup :show="stockChildshow" @close="stockChildshow = false" @open="stockChildshow = true" mode="right">
            <view class="zckdiv">
                <view class="titleSelect">子仓库选择</view>
                <superwei-combox
                    :candidates="zistockselectList"
                    :isJSON="true"
                    keyName="name"
                    placeholder="请选择或输入"
                    v-model="stockChildVal"
                    @select="stockSelectChange"
                    @input="stockValWath"
                ></superwei-combox>
            </view>
            <view class="tkbts">
                <u-button type="default" :plain="true" size="small" text="取消" class="itw" @click="stockChildshow = false"></u-button>
                <u-button type="primary" text="确定" size="small" class="itw" @click="qrStock" style="border: none"></u-button>
            </view>
        </u-popup>
        <!--盘亏时候的备注弹框-->
        <u-modal :show="pkRmarkShow" title="备注" @confirm="Rmarkconfirm" :showCancelButton="true" @cancel="pkRmarkShow = false" style="margin-top: -30%">
            <u-textarea v-model.trim="itremake" count :maxlength="100" placeholder="请输入内容" style="width: 100%; border: 1px solid #ccc" height="100"></u-textarea>
        </u-modal>
        <!-- 新箱号需要输入仓位(子仓库) -->
        <u-modal :show="isShowSubWarehouse" title="重新上架" :showCancelButton="true" @cancel="subWarehouseCancel" @confirm="subWarehouseConfirm">
            <view style="display: flex; flex-direction: column">
                <view style="display: flex; flex-direction: row; align-items: center; border-bottom: 1px solid #e0e5eb">
                    <text style="width: 70px">仓位号</text>
                    <view style="display: flex; flex-direction: row; align-items: center; padding-left: 5px; background: #fff">
                        <input
                            type="text"
                            v-model.trim="subWarehouseValue"
                            :focus="focusInput == 'subWarehouse'"
                            clearable
                            @focus="inputfocus($event, 'subWarehouse')"
                            @blur="inputblur($event, '')"
                            @confirm="inputConfirm($event, 'subWarehouse')"
                        />
                        <image src="../../static/img/smico.png" class="searchico" @click="scanImg('subWarehouse')"></image>
                    </view>
                </view>
                <text style="font-size: 10px; color: red">{{ cwstrts }}</text>
            </view>
        </u-modal>
        <!--<u-modal :show="isShowSubWarehouse"  :showConfirmButton="true" :showCancelButton="true" confirmText="确认" cancelText="清空" @confirm="subWarehouseConfirm" @cancel="subWarehouseCancel">
			<view style="display: flex; flex-direction: column;">
					<view style="display: flex;justify-content: space-between;">
					
						<text>重新上架</text>
						<u-icon  size="21" name="close" @click="hanldeSubWarehouseGB"></u-icon>
					</view>
					<view class='subWarehouse'  >
						<text style="padding-right: 10rpx;"><text style="color:red">*</text>仓位 : </text>
						<w-select
							class="wwselectit"
							v-model.trim="subWarehouseValue"
							defaultValue="模糊搜索"
							:list="cwselectList"
							:focus="focusInput == 'subWarehouse'"
							@focus="inputfocus($event, 'subWarehouse')"
							valueName="name"
							keyName="name"
							@change="cwchange"
							:filterable="true"
							:showClose="true"
							ref="wselect1"
						></w-select>
						<image src="../../static/img/smico.png" class="searchico" @click="scanImg('subWarehouse')"></image>
					</view>
			</view>
			
		</u-modal> -->
        <!-- 底部按钮固定-->
        <view class="footbts">
            <view class="bt bt1" @click="qktkshow = true">重置</view>
            <view class="bt bt3" @click="wcPD">完成盘点</view>
            <view class="bt bt2" @click="makeSurePd">确认盘点</view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            heights: {
                top: 0,
                body: 0
            },
            isCheckBox: false,
            checkboxValue1: [], //重新上架
            cwselectList: [], //仓位列表
            subWarehouseValue: '', //仓位值
            isShowSubWarehouse: false, //仓位
            isNewWarehouse: false, //判断是否是新箱
            ctnContent: '箱号不为CTN,确认箱号无误?', //箱号不为CTN的时候,弹窗的内容
            noCtnShow: false, //不是以CTN开头的二次确认弹窗条件
            warehouseList: [], //仓库列表数据
            focusInput: '', //自动聚焦 根据不同的值聚焦不同的input框
            warehouseCode: '', // 如果仓库编码是hd或者gd且完成盘点之后就能修改子仓库
            warehouseName: '', //仓库名称
            warehouseDis: false, //仓库禁止条件
            pdlxVal: '箱号', // 盘点类型输入框的值
            xlhOrggxhVal: '', //序列号
            xlhStrlist: [], //序列号多个缓存
            xhNo: '', // 箱号
            boxstrList: [], // 缓存箱号集合
            xlhGgxNum: '', // 扫当前序列号的数量 一般是1，可手动修改
            dataList: [], //列表明细数据
            focusTag: '', //  判斷聚焦
            filterable: true,
            pdSelectlist: [
                {
                    id: 1,
                    content: '箱号'
                }
            ],
            xhDisabled: true, //箱号input禁止条件
            qktkshow: false, //清空二次确认弹框
            qktkTitle: '重置?',
            qktkContent: '是否确认重置页面数据？',
            qrpdshow: false, // 确认盘点弹框
            stockChildshow: false, // 子仓库选择弹框
            stockselecItem: {
                name: '',
                code: ''
            },
            stockChildCode: '', //子仓库选择的值code
            zistockselectList: [], //子仓库下拉数据
            stockChildVal: '',
            xlhOrggType: '', // 判断输入的是序列号还是规格型号
            itIndx: null, //如果扫描的是规格序列号，判断是在明细的那个位置
            cKIindex: null, //判断是点击的第几个子仓库的明细数据
            wcpdTag: false, // 判断是否完成盘点
            orgCode: '', // 组织编码
            dqboxNO: '', //用来是否保存当前输入箱号的值
            dqxlhOrggxh: '', //用来是否保存当前输入xlh的值
            firstNumTag: false, // 判断数量第一次是否有回车
            pkRmarkShow: false, // 盘亏时候必须要填写盘亏原因，否者不能完成盘点
            remarkIndex: null, //修改的是那个明细的备注
            itremake: '', // 明细盘亏备注
            choseBoxId: '', // 当前箱的箱id
           // subStockCode: null, // 当前箱的仓位code
            subStockCode: '', //仓位code
            old_SubStockCode: '', //老仓位code,由箱号携带出来
            APPdevice: uni.getStorageSync('devicePixelRatio'), // 缓存设备的像素比用来区分普通安卓normalAnroid还是pda
            requestStatus: false, // 请求状态识别变量
            cwstrts: '',
			twoData:[],
            scanTracker: {
                lastScanTime: 0,
                lastScanCode: null
            } // 源头处理扫码段时间内重复扫
        };
    },
    onReady() {
        this.heights = this.$util.setlistHeight('.sh_gdInfo', '.footbts');
    },
    onLoad(options) {
        //获取广播扫码监听
        this.$broadcastScan.init(this.getScancode);
        this.$broadcastScan.start();
        this.stopScanCode();
        this.startScanCode();
		if(!options.warehouseName) {
				 this.getStock();
		}else {
			this.warehouseCode=options.warehouseCode
			this.warehouseName=options.warehouseName
			this.xhNo = options.boxBillNo
			this.focusInput = 'xlhGgx';
			this.warehouseDis = true;
			this.boxstrList.push(this.xhNo)
			this.getBoxinfoHttp()
		}
    },
    onHide() {
        this.$broadcastScan.stop();
        this.stopScanCode();
    },
    onShow() {
        this.$broadcastScan.start();
        this.startScanCode();
    },
    onBackPress(e) {
        this.$util.appgoBack(e, 'index');
        return true;
    },
    watch: {
        xhNo(n) {
            if (!n) {
                this.dqboxNO = '';
            }
        },
        xlhOrggxhVal(n) {
            if (!n) {
                this.dqxlhOrggxh = '';
            }
        }
        // subWarehouseValue(n) {

        // 	if(n) {
        // 		this.getcwList(n);
        // 	}
        // },
    },
    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');
        },
        //获取仓库数据
        getStock() {
            this.$api.get('/SysConfig/GetUcStock').then((res) => {
                if (res.status == 200) {
                    this.warehouseList = res.data;
                }
            });
        },
        //仓库change事件
        ckchange(e) {
            this.xhDisabled = !e.name;
            this.focusInput = '';
            this.warehouseName = e.name;
            this.warehouseCode = e.code;
            this.orgCode = e.erpOrgCode;
            setTimeout(() => {
                this.focusInput = 'box';
            }, 300);
        },
        // 打开弹框去修改明细备注
        openRrmark(index) {
            this.itremake = this.dataList[index].remark;
            this.pkRmarkShow = true;
            this.remarkIndex = index;
        },
        // 明细盘亏时候的备注修改确认
        Rmarkconfirm() {
            this.pkRmarkShow = false;
            uni.showToast({
                title: '备注成功',
                icon: 'none',
                duration: 2000
            });
            this.dataList[this.remarkIndex].remark = this.itremake;
        },
        //确认子仓库选择
        qrStock() {
            this.stockChildCode = this.stockselecItem.code;
            this.dataList[this.cKIindex].erp_SubStockCode = this.stockselecItem.code;
            this.stockChildshow = false;
            this.dataList[this.cKIindex].zckVal = this.stockselecItem.name;
        },
        // 实时获取
        stockValWath(e) {
            this.$api
                .get('/SysConfig/GetErp_SubUcStock/' + this.warehouseCode + '/' + this.orgCode, {
                    name: e
                })
                .then((res) => {
                    if (res.status == 200) {
                        this.zistockselectList = res.data;
                    }
                });
        },
        // 明细子仓库更改下拉选择监听
        stockSelectChange(val) {
            this.stockselecItem = val;
        },
        // 更改子仓库
        openStoclmod(item, index) {
            this.cKIindex = index;
            this.stockChildshow = false;
            if (this.warehouseCode == 'HD' || this.warehouseCode == 'GD') {
                this.stockChildVal = item.zckVal;
                this.stockChildshow = true;
            }
        },
        // 完成盘点
        wcPD() {
            // 判断必填
            if (this.boxstrList.length == 0 || this.xlhStrlist.length == 0 || !this.firstNumTag) {
                uni.showToast({
                    title: '请先输入必填值',
                    icon: 'none',
                    duration: 1500
                });
                return;
            } else if (
                this.dataList.filter((it) => {
                    return parseInt(it.pdNum) < parseInt(it.qty) && !it.remark;
                }).length > 0
            ) {
                uni.showToast({
                    title: '请先输入备注内容',
                    icon: 'none',
                    duration: 1500
                });
                return;
            } else if (
                (this.warehouseCode == 'HD' || this.warehouseCode == 'GD') &&
                this.dataList.filter((it) => {
                    return !it.zckVal;
                }).length > 0
            ) {
                uni.showToast({
                    title: '请选择子仓库',
                    icon: 'none',
                    duration: 1500
                });
                return;
            }
            if (this.isNewWarehouse) {
                this.isShowSubWarehouse = true;
                this.focusInput = 'subWarehouse';
            } else {
                // 所有文本文本置灰不能在操作
                this.wcpdTag = true;
                //如果当前仓库编码是"HD""GD",对应子仓库值可以修改且必填
                return;
            }
        },
        // 确认盘点提交数据
        tjDataFn() {
            if (this.boxstrList.length == 0 || this.xlhStrlist.length == 0 || this.firstNumTag == false) {
                uni.showToast({
                    title: '请先输入必填值',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }
            let formData = [];
			
            this.dataList.forEach((it) => {
                formData.push({
                    boxId: this.choseBoxId,
                    materialNumber: it.materialNumber,
                    subStockCode: this.subStockCode,
                    erp_SubStockCode: it.erp_SubStockCode ? it.erp_SubStockCode : '',
                    beforeQty: it.qty,
                    afterQty: it.pdNum,
                    serialNumbers: it.itxlhList,
                    remark: it.remark,
                    stockCode: this.warehouseCode,
                    BoxBillNo: this.xhNo,
                    old_SubStockCode: it.old_SubStockCode ? it.old_SubStockCode : this.subStockCode,
					twoSerialNumbers:it.twoSerialNumbers ? it.twoSerialNumbers.filter(Boolean):[]
                });
            });
            console.log('提交數據', formData);
			
            // 节流
            if (this.requestStatus) {
                return false;
            }

            this.requestStatus = true;
            this.qrpdshow = false;
            this.$api.post('/TakeStock/Save', formData).then((res) => {
                if (res.status == 200) {
                    uni.showToast({
                        title: '成功',
                        icon: 'none',
                        duration: 2000
                    });
                    uni.navigateTo({
                        url: '/pages/Inventory/index'
                    });
                }
            });
            setTimeout(() => {
                this.requestStatus = false;
            }, 1500);
        },
        // 确认盘点-前提示
        makeSurePd() {
            if (!this.wcpdTag) {
                uni.showToast({
                    title: '请确认完成盘点',
                    icon: 'none',
                    duration: 2000
                });
                return;
            } else if (this.boxstrList.length == 0 || this.xlhStrlist.length == 0) {
                uni.showToast({
                    title: '请先输入必填值',
                    icon: 'none',
                    duration: 2000
                });
                return;
            } else {
                this.qrpdshow = true;
            }
        },
        // 重置
        qktkconfirm() {
            this.xlhStrlist = [];
			this.twoData=[]
            this.xlhOrggxhVal = null;
            this.xlhGgxNum = null;
            this.xhNo = null;
            this.dataList = [];
            this.qktkshow = false;
            this.boxstrList = [];
            this.cwselectList = [];
            // this.warehouseName =null
            // this.warehouseList=[]//仓库列表数据
            this.isNewWarehouse = false; //判断是否是新箱
            this.itIndx = null;
            this.wcpdTag = false;
            this.cKIindex = null;
            this.subStockCode = '';
            this.firstNumTag = false;
            this.warehouseDis = false;
            this.xhDisabled = false;
            this.isCheckBox = false;
            this.isShowSubWarehouse = false;
            this.checkboxValue1 = []; //重新上架
            uni.navigateTo({
                url: '/pages/Inventory/index'
            });
            setTimeout(() => {
                this.focusInput = 'box';
            }, 300);
        },
        // 普通安卓设备碘酒扫描图标扫描
        scanImg(type) {
            if (this.APPdevice == 'normalAnroid') {
                this.$util.doScanQrCode().then((res) => {
                    this.focusInput = type;
                    this.focusTag = type;
                    this.getScancode(res.result);
                });
            }
        },
        // 获取扫描的值
        getScancode(code) {
            // 有些PDA会自带换行符，trim函数处理下
            if (this.focusTag == 'box') {
                this.xhNo = '';
                this.xhNo = code.trim();
                this.getboxinfo();
            }
            if (this.focusTag == 'xlhGgx') {
                this.xlhOrggxhVal = '';
                this.xlhOrggxhVal = code.trim();
                this.getXlhOrggxhInfo();
            }
            //重新上架
            if (this.focusTag === 'subWarehouse') {
                this.subWarehouseValue = '';
                this.subWarehouseValue = code.trim();
                getcwList(this.subWarehouseValue);
            }
            console.log('获取扫描的值', this.focusTag, code);
        },
        // 扫描框数据重置，光标聚焦
        setSMinputxlh() {
            this.focusInput = '';
            setTimeout(() => {
                this.xlhOrggxhVal = '';
                this.dqxlhOrggxh = '';
                this.focusInput = 'xlhGgx';
                this.xlhGgxNum = null;
            }, 200);
        },
        // 盘点--扫码序列号获取相关信息
        getXlhOrggxhInfo() {
            if (this.boxstrList.length == 0) {
                this.setSMinputxlh();
                uni.showToast({
                    title: '请先扫码或输入箱号获取箱信息',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }

            if (!this.xlhOrggxhVal) {
                this.setSMinputxlh();
                uni.showToast({
                    title: '请输入或者扫描序列号/规格型号',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }
            if (this.xlhStrlist.includes(this.xlhOrggxhVal) || this.twoData.includes(this.xlhOrggxhVal)) {
                this.setSMinputxlh();
                uni.showToast({
                    title: '该序列号/规格型号已扫描',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }
            this.focusInput = '';
            //盘点 --扫码序列号、规格型号带出相关的明细数据
            this.$api
                .get('/SysConfig/GetMaterial', {
                    serialNumber: this.xlhOrggxhVal,
                    IsOps: true,
                    serialStatus: 0
                })
                .then((res) => {
                    if (res.status == 200) {
                        // 判断扫码的是个序列号还是规格型号
                        this.xlhOrggType = res.data.serialNumber ? 'xlh' : 'ggxh';
                        // 判断当前序列号或者规格型号存不存在明细里面
                        let findindx = this.dataList.findIndex((obj) => obj.specifications === res.data.specifications);
                        if (this.xlhOrggType == 'ggxh' && this.xlhStrlist.includes(res.data.specifications)) {
                            this.setSMinputxlh();
                            uni.showToast({
                                title: '该序列号/规格型号已扫描',
                                icon: 'none',
                                duration: 2000
                            });
                            return;
                        }
                        if (findindx !== -1) {
                            //如果是序列号
                            if (this.xlhOrggType == 'xlh') {
                                this.xlhGgxNum = 1;
                                this.firstNumTag = true;
                                this.dataList[findindx].pdNum = this.dataList[findindx].pdNum + 1;
                                this.dataList[findindx].itxlhList.push(res.data.serialNumber);
                                // 缓存当前扫描数据
                                this.xlhStrlist.push(res.data.serialNumber);
								//缓存2件装序列号
								if(res.data.isTwo==2) {
									this.dataList[findindx].twoSerialNumbers.push(res.data.twoSerialNumber);
									this.twoData.push(res.data.twoSerialNumber)
								}
                                uni.showToast({
                                    title: '获取成功',
                                    icon: 'none',
                                    duration: 2000
                                });
                            }
                            if (this.xlhOrggType == 'ggxh') {
                                this.xlhGgxNum = null;
                                this.itIndx = findindx;
                            }
                            //清空当前框数据 自动聚焦箱号
                            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;
                        } else {
                            this.dataList.push({
                                materialNumber: res.data.materialNumber,
                                materialName: res.data.materialName,
                                specifications: res.data.specifications,
                                subStockCode: res.data.subStockCode,
                                remark: '',
                                qty: 0,
                                old_SubStockCode: res.data.subStockCode,
                                pdNum: this.xlhOrggType == 'ggxh' ? 0 : this.xlhOrggType == 'xlh' ? 1 : 0,
                                serialNumbers: [],
                                itxlhList: [],
								twoSerialNumbers:[],
                            });
                            if (this.xlhOrggType == 'xlh') {
                                this.xlhGgxNum = 1;
                                this.firstNumTag = true;
                                this.dataList[this.dataList.length - 1].itxlhList.push(res.data.serialNumber);
                                this.xlhStrlist.push(res.data.serialNumber);
								//缓存2件装序列号
								if(res.data.isTwo==2) {
									this.dataList[this.dataList.length - 1].twoSerialNumbers.push(res.data.twoSerialNumber);
									this.twoData.push(res.data.twoSerialNumber)
								}
                            }
                            if (this.xlhOrggType == 'ggxh') {
                                this.xlhGgxNum = null;
                            }
                            this.itIndx = this.dataList && this.dataList.length > 0 ? this.dataList.length - 1 : 0;
                            //清空当前框数据 自动聚焦箱号
                            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.focusInput = this.xlhOrggType == 'ggxh' ? 'xlhGgxNum' : 'xlhGgx';
                        this.xhDisabled = true;
                    } else {
                        this.setSMinputxlh();
                    }
                });
        },
        // 扫描框数据重置，光标聚焦
        setSMinputbox() {
            this.focusInput = '';
            setTimeout(() => {
                this.xhNo = '';
                this.dqboxNO = '';
                this.focusInput = 'box';
                this.xlhOrggxhVal = '';
                this.dqxlhOrggxh = '';
                this.xlhGgxNum = '';
                // this.dataList=[]
            }, 200);
        },
        //获取箱号信息
        getBoxinfoHttp() {
            //SysConfig/GetBoxSynthesis
            //盘点 -扫码箱号带出相关的明细数据
            //'/Inventory/GetBoxInventoryDetails/' + this.xhNo
            this.$api
                .get('/SysConfig/GetBoxSynthesis', {
                    isReminder: false,
                    boxBillNo: this.xhNo
                })
                .then((res) => {
                    if (res.status == 200) {
                        // 当前箱的仓位和箱子id
                        this.choseBoxId = res.data.id; //boxId
                        //当不选择重新上架是使用的箱号带出来的,但是重新上架会重新进行赋值
                        this.subStockCode = res.data.subStockCode;
                        //无论是否重新上架,该仓位code都是由箱号带出不会改变
                        this.old_SubStockCode = res.data.subStockCode;
                        const { stockCode } = res.data;
                        //判断是否是新箱,如果是新箱是没有仓库的
                        this.isNewWarehouse = res.data.stock ? false : true;
                        //扫描的箱号已绑定仓位但是不属于所选仓库则提示"箱号不属于所选仓库" 数据不录入
                        if (stockCode && stockCode !== this.warehouseCode) {
                            uni.showToast({
                                title: '箱号不属于所选仓库',
                                icon: 'none',
                                duration: 2000
                            });
                            return;
                        }
                       
                        this.dataList = [];
                        res.data.details.forEach((obj) => {
                            if (obj.qty > 0 && res.data.subStockCode) {
                                this.dataList.push({
                                    ...obj,
                                    pdNum: 0,
                                    itxlhList: [],
                                    boxId: res.data.id, //boxId,
                                    subStockCode: res.data.subStockCode,
                                    remark: '',
                                    erpSubStockCode: null,
                                    old_SubStockCode: res.data.subStockCode,
									twoSerialNumbers:res.data.twoSerialNumbers ? res.data.twoSerialNumbers: []
									
                                });
                            }
                        });
						
                        // 如果查询的箱号正常返回数据，就缓存改箱号信息
                        this.boxstrList.push(this.xhNo);
                        //清空当前框数据 自动聚焦箱号
                        this.focusInput = 'xlhGgx';
                        this.xlhOrggxhVal = '';
                        this.xlhGgxNum = null;
                        uni.showToast({
                            title: '获取成功',
                            icon: 'none',
                            duration: 2000
                        });
                        this.warehouseDis = true;
                    } else {
                        this.setSMinputbox();
                    }
                });
        },
        // 盘点根据箱号扫码调出相关信息
        getboxinfo() {
            if (!this.xhNo) {
                this.setSMinputbox();
                uni.showToast({
                    title: '请扫描需要上架的箱号',
                    icon: 'none',
                    duration: 2000
                });
                return;
            }
            let xhNoClone = this.xhNo;
            xhNoClone = xhNoClone.substring(0, 3).toUpperCase();
            if (xhNoClone !== 'CTN') {
                this.noCtnShow = true;
                return;
            }
            this.getBoxinfoHttp();
        },
        //非CTN开头的箱号二次确认
        ctnConfirm() {
            this.noCtnShow = false;
            this.getBoxinfoHttp();
        },
        // 輸入失去焦點
        inputblur(val, type) {},
        // 输入聚焦
        inputfocus(val, type) {
            this.focusTag = type;
        },
        // 区分不同的扫描数据、输入数据做处理显示 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
        setSMxlhstr(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;
            }
        },
        getSubUcStockPrecisionByName() {
            console.log(this.subWarehouseValue, '=this.subWarehouseValue=');
            if (!this.subWarehouseValue) {
                this.cwstrts = '请输入或者扫描值';
                return;
            }
            this.$api
                .get('/SysConfig/GetSubUcStockPrecisionByName', {
                    name: this.subWarehouseValue
                })
                .then((res) => {
                    if (res.status == 200) {
                        this.isShowSubWarehouse = false;
                        this.subStockCode = res.data.code;
                        //如果重新上架,选择了仓位在完成盘点的时候就不需要弹窗了
                        this.isNewWarehouse = res.data.code ? false : true;
                        this.subWarehouseValue = '';
                        // this.focusInput = 'box';
						this.focusInput = '';
                        this.cwselectList = [];
                        if (this.subStockCode && !this.isCheckBox) {
                            // 所有文本文本置灰不能在操作
                            this.wcpdTag = true;
                        }
                    } else {
                        this.cwstrts = res.message;
                    }
                });
        },
        // 输入确认
        inputConfirm(val, type) {
            if (val) {
                if (this.focusTag == 'box') {
                    this.setSMstr(val.target.value);
                    this.getboxinfo();
                }
                if (this.focusTag == 'xlhGgx') {
                    // 输入完序列号点击确认的时候
                    //1.如果当前存在的序列号对应的规格型号不存在明细里面加一条明细数据，数量为0 盘点数量为1  盘点多次累计加1
                    this.setSMxlhstr(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 !== '') {
                        this.dataList[this.itIndx].pdNum = this.dataList[this.itIndx].pdNum + parseInt(this.xlhGgxNum);
                        this.xlhStrlist.push(this.xlhOrggxhVal);
                        uni.showToast({
                            title: '数量获取成功',
                            icon: 'none',
                            duration: 2000
                        });
                        this.xlhGgxNum = null;
                        this.xlhOrggxhVal = null;
                        this.itIndx = '';
                        this.focusInput = 'xlhGgx';
                        this.firstNumTag = true;
                    } else {
                        uni.showToast({
                            title: '请先输入数量在操作',
                            icon: 'none',
                            duration: 2000
                        });
                    }
                }
                if (type === 'subWarehouse') {
                    this.getSubUcStockPrecisionByName();
                }
            }
        },
        // 盘点类型下拉监听
        pdlxchange(e) {},
        //重新上架取消
        subWarehouseCancel() {
            this.subWarehouseValue = '';
            this.focusInput = '';
            this.subStockCode = '';
            this.cwselectList = [];
            this.isCheckBox = false;
            this.checkboxValue1 = []; //重新上架
            this.isShowSubWarehouse = false;
            setTimeout(() => {
                this.focusInput = 'subWarehouse';
            }, 300);
        },
        //重新上架确认弹窗
        subWarehouseConfirm(e) {
            this.getSubUcStockPrecisionByName();
        },
        //重新上架
        checkboxChange(n) {
            this.isCheckBox = n.length ? true : false;
            this.isShowSubWarehouse = n.length ? true : false;
        },
        goback() {
            uni.navigateTo({
                url: '/pages/index'
            });
        }
    }
};
</script>

<style lang="scss">
@import '@/static/public.scss';
.reset-cs {
    .u-modal__title {
        font-size: 12px;
    }
}
.mianheade2 {
    .pagetitle {
        margin-left: 39%;
    }
}

.zckdiv {
    padding: 16px;
    height: 435px !important;

    .titleSelect {
        font-size: 15px;
        margin-bottom: 16px;
        font-weight: bold;
        text-align: center;
        padding-top: 30px;
    }
}

.tkbts {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin: 0 5px;
    margin-top: 10px;
}

.itw {
    width: 48% !important;
}

.ckborder {
    border: 1px solid #d0d7de;
    padding: 2px 10px;
    margin-left: 5px;
    border-radius: 6px;
}
.subWarehouse {
    margin-top: 20rpx;
    display: flex;
    align-items: center;
    border: 1rpx solid #ccc;
    height: 26px;
    padding: 0 10rpx;
    border-radius: 6rpx;
    font-size: 14px;
}
</style>
