<template>
    <view>
        <uni-collapse>
            <uni-forms ref="form" :modelValue="formData">
                <!-- <uni-collapse-item title="明细标签入库" :open="true"> -->
                <uni-forms-item label="扫描标签" :labelWidth='90' name="purchaseLabelCode">
                    <uni-easyinput suffixIcon="scan" @iconClick="scanBar" @blur="getlabelCode" placeholder='请扫描标签'
                        v-model="purchaseLabelCode" type="text" :focus='true'/>
                </uni-forms-item>
                <!-- 标签明细 -->
                <uni-collapse-item :open="true">
                    <uni-card :is-shadow="false" is-full v-for="(item,index) in formData.wmsCommonInDetailLabelList"
                        :key="index" v-if="total>0">
                        <div><strong>标签码</strong>:{{item.labelCode}}</div>
                        <div><strong>供应商</strong>:{{item.supplierName}}</div>
                        <div><strong>物料编码</strong>:{{item.materialCode}}</div>
                        <div><strong>物料名称</strong>:{{item.materialName}}</div>
                        <div><strong>规格</strong>:{{item.specification}}</div>
                        <div><strong>批号</strong>:{{item.materialBatchNo}}</div>
                        <div><strong>数量</strong>:{{item.number}}</div>
                        <!-- <div><strong>状态</strong>:{{returnStatus(item.status)}}</div> -->
                        <div><strong>状态</strong>:{{dtlStatusFormat(item.status)}}</div>
                    </uni-card>
                </uni-collapse-item>

                <uni-forms-item label="上架扫码" :labelWidth='90' name="location" style="margin-top:10px">
                    <uni-easyinput suffixIcon="scan" @iconClick="scanLocation" @blur="getWarehouse"
                        placeholder='请扫描库位标签' v-model="locationCode" type="text" />
                </uni-forms-item>
                <uni-collapse-item :open="true">
                    <uni-card :is-shadow="false" is-full v-if='warehouseName'>
                        <div><strong>仓库</strong>:{{warehouseName}}</div>
                        <div><strong>库位</strong>:{{showLocationCode}}</div>
                    </uni-card>
                </uni-collapse-item>

                <u-button type="primary" @click="submitIn">确认入库</u-button>
            </uni-forms>
        </uni-collapse>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="取消" confirmText="确认" title="确认是否入库"  @confirm="dialogConfirm">
			<div>
				<div><strong>物料编码</strong>:{{alert.materialCode}}</div>
				<div><strong>数量</strong>:{{alert.number}}</div>
				<div><strong>库位</strong>:{{alert.showLocationCode}}</div>
			</div>
			</uni-popup-dialog>
		</uni-popup>
    </view>
</template>

<script>
    import {
        listQuality,
        getQuality,
        addIn,
        listReceive,
        getReceive,
        getListDetail,
        updateIn,
        listIn,
        getIn,
        getQualityDetail,
        getReceiveDetail,
        getDetailListsBySRM,
        getListDetailByBL,
        listSupplier,
        listPurchaseInDetailLabel,
        listWarehouse,
        labelInStock,
    } from "@/api/wms/purchase.js";
	import {
		getConfigKey,
	} from "@/api/wms/materRequisitiontask";
    export default {
        onLoad: function() {},
		created() {
			//获取当前客户
			getConfigKey('wms.warehouse.mode').then(res =>{
				this.getConfigKey.setkey(res.msg)
			})
		},
        mounted() {

        },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 总条数
                total: 0,
                //标识码
                purchaseLabelCode: null,
                //库位码
                locationCode: null,
                //仓库码
                showLocationCode: null,
                //仓库名
                warehouseName: null,
                formData: {
                    areaCode: null,
                    shelvesCode: null,
                    storageLocationCode: null,
                    whCode: null,
                    // 采购入库明细标签表格数据
                    wmsCommonInDetailLabelList: [],
                },
                //标签码查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 50,
                    labelCode: null,
                },
                //仓库查询参数
                queryParamsCK: {
                    pageNum: 1,
                    pageSize: 50,
                    orderByColumn: "id",
                    isAsc: "desc",
                    warehouseCode: null,
                },
				getConfigKey:new classLocationCode(),
				alert:{
					materialCode :null,
					number : null,
					showLocationCode: null
				}
            }
        },
        methods: {
            //明细状态
            dtlStatusFormat(status) {
                if (status == 1) {
                    return "入库完成";
                } else if (status == 0) {
                    return "等待入库";
                } else if (status == 2) {
                    return "入库中";
                } else {
                    return "等待入库";
                }
            },
            //扫描标签码
            scanBar() {
                const _this = this;
                uni.scanCode({
                    scanType: ['barCode', 'qrCode'],
                    success: function(res) {
                        _this.purchaseLabelCode = res.result;
                        _this.getlabelCode();
                    }
                });
            },
            getlabelCode() {
                this.queryParams.labelCode = this.purchaseLabelCode;
                this.getList()
            },
            //查询标签列表
            getList() {
                this.loading = true;
				//解析扫的条码信息，以，为间隔转换成数组，并取第二个数据为真实需要的信息
				this.purchaseLabelCode = this.queryParams.labelCode = this.queryParams.labelCode.split(',')[1]
                listPurchaseInDetailLabel(this.queryParams).then(res => {
                    this.formData.wmsCommonInDetailLabelList = [res.rows[0]];
                    this.total = res.total;
                    this.loading = false;
                    if (this.total == 0) {
                        // console.log("二维码有错");
                        this.$modal.msgError("标签二维码错误");
                        this.purchaseLabelCode = null
                    }
                });

            },
            //扫描库位码
            scanLocation() {
                const _this = this;
                uni.scanCode({
                    scanType: ['barCode', 'qrCode'],
                    success: function(res) {
                        _this.locationCode = res.result;
                        _this.getWarehouse();
                    }
                });
            },
            //仓库码
            async getWarehouse() {
                if (this.locationCode == null || this.locationCode == '') {
                    this.$modal.msgError("库位码为空");
                } else {
                    this.showLocationCode = this.locationCode
					
                    let parts = this.locationCode.split('-');
                    if (parts.length <1 || parts.length > 4) {
                        this.$modal.msgError("库位码格式有误");
                        this.locationCode = null
                        return
                    }
					parts = this.getConfigKey.getValue(this.locationCode).split('-');
					// this.formData.whCode = parts[0] || null;
					this.formData.whCode = await getParts(parts) || null;
					this.formData.areaCode = parts[1] || null;
					this.formData.shelvesCode = parts[2] || null;
					this.formData.storageLocationCode = parts.slice(3).join('-') || null;
                    this.queryParamsCK.warehouseCode = parts[0]
                    this.getlistWarehouse()
					
                }
				/**
				 * 根据扫的二维码的长度判断其第一位应该是什么值
				 * @name PZQ
				 * @param {Object} parts 二维码转换的数组
				 */
				function getParts(parts){
					if(parts.length == 4 ){
						return parts[0] || null;
					}else {
						return  '0' + parts[0].charAt(0) || null;
					}
				}
            },
            //查仓库名
            getlistWarehouse() {
                // this.formData.location
                this.loading = true;
                listWarehouse(this.queryParamsCK).then(res => {
                    this.warehouseName = res.rows[0].warehouseName
                    this.loading = false;
                });
            },
            //重置
            reset() {
                // 总条数
                this.total = 0,
                    //标识码
                    this.purchaseLabelCode = null,
                    //库位码
                    this.locationCode = null,
                    //仓库码
                    this.showLocationCode = null,
                    //仓库名
                    this.warehouseName = null,
                    this.formData = {
                        areaCode: null,
                        shelvesCode: null,
                        storageLocationCode: null,
                        whCode: null,
                        // 采购入库明细标签表格数据
                        wmsCommonInDetailLabelList: [],
                    }
                //标签码查询参数
                this.queryParams = {
                    pageNum: 1,
                    pageSize: 50,
                    labelCode: null,
                }
                //仓库查询参数
                this.queryParamsCK = {
                    pageNum: 1,
                    pageSize: 50,
                    orderByColumn: "id",
                    isAsc: "desc",
                    warehouseCode: null,
                }
            },
            //确认入库
            submitIn() {
				if(this.formData.wmsCommonInDetailLabelList == null || this.formData.whCode == []){
					this.$modal.msgError("请扫描标识码");
					return;
				}
                if(this.formData.whCode == null || this.formData.whCode == ''){
					this.$modal.msgError("请扫描库位码");
					return;
                }
				console.log(this.formData)
				const {wmsCommonInDetailLabelList} = this.formData
				const {materialCode,number } = wmsCommonInDetailLabelList[0]
				// showLocationCode
				// else 
                //弹框二次确认
				this.alert = {
					materialCode,
					number,
					showLocationCode:this.showLocationCode
				}
				this.$refs.alertDialog.open()
				return
                // uni.showModal({
                //     title: '提示',
                //     content: `确定要入库数量为${number}物料编码为${materialCode}到${this.showLocationCode}吗？`,
                //     success: res => {
                //         if (res.confirm) {
                //             labelInStock(this.formData).then(response => {
                //                 this.$modal.msgSuccess("入库成功");
                //                 this.reset()
                //             });
                //         } else if (res.cancel) {
                //             // console.log('用户点击取消');
                //             uni.showToast({
                //                 title: '取消入库',
                //                 icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                //                 duration: 2000 //持续时间为 2秒
                //             })
                //         }
                //     }
                // });

            },
			dialogConfirm(){
				labelInStock(this.formData).then(response => {
					this.$modal.msgSuccess("入库成功");
					this.reset()
				});
				this.$refs.alertDialog.close()
			}
        }
    }
	class classLocationCode{
		#key = null
		setkey(key){
			this.key = key
		}
		/**
		 * 给外部调用的方法
		 * @param {Object} value
		 */
		getValue(value){
			const obj = {
				"1":this.#getLocationCode,
			}
			if(this.key in obj){
				return obj[this.key](value)
			}
			return value
		}
		/**
		 * 判断上架扫码是否拼接
		 * @param {Object} value
		 */
		#getLocationCode(value){
			const number = value[0]
			return '0' + number + '-' + value
		}
	}
</script>

<style>

</style>