<template>
	<view style="margin: 10rpx;">
		<u-navbar back-text="返回" title="立体库入库" :background="background"></u-navbar>
		<!-- <view>
			<u-popup v-model="show" width="50%"> -->
		<u-form ref="testForm" labelWidth="200">
			<u-form-item class="item2">
				<p>选择库区:</p>
				<zxzUniDataSelect filterable v-model="AreaName" :localdata="Area"></zxzUniDataSelect>
			</u-form-item>
			<br />
			<u-form-item class="item2" v-if="!AGVshow">
				<p>绑定卷芯:</p>
				<u-input placeholder="请绑定卷芯" :border="true" v-model="DURABLENAMECode" />
			</u-form-item>
			<br />
			<checkbox-group @change="IFUserAGV" v-if="AreaName != 'E'">
				<label>
					<checkbox value="cb" />是否使用AGV
				</label>
			</checkbox-group>
			<u-form-item label="管芯宽幅" v-if="AGVshow">
				<uni-easyinput v-model="WIDTH" type="number" style="max-width: 250rpx;max-height: 50rpx;">
				</uni-easyinput>
			</u-form-item>
			<u-form-item label="直径" v-if="AGVshow">
				<uni-easyinput v-model="DIAMETER" type="number" style="max-width: 250rpx;max-height: 50rpx;">
				</uni-easyinput>
			</u-form-item>
			<u-form-item label="偏移量" v-if="AGVshow">
				<uni-easyinput v-model="DEVIATION" type="number" style="max-width: 250rpx;max-height: 50rpx;">
				</uni-easyinput>
			</u-form-item>
			<u-form-item label="重量" v-if="AGVshow">
				<uni-easyinput v-model="WEIGHT" type="number" style="max-width: 250rpx;max-height: 50rpx;">
				</uni-easyinput>
			</u-form-item>
			<u-form-item label="产线" v-if="AGVshow">
				<zxzUniDataSelect filterable v-model="PRODUCTLINE" :localdata="PRODUCTLINES"></zxzUniDataSelect>
			</u-form-item>
			<u-form-item label="起始点" v-if="AGVshow">
				<zxzUniDataSelect filterable v-model="INITILATION" :localdata="items_In"></zxzUniDataSelect>
			</u-form-item>
			<u-form-item label="目的点" v-if="AGVshow">
				<zxzUniDataSelect filterable v-model="ENDING" :localdata="items_En"></zxzUniDataSelect>
			</u-form-item>
			<u-form-item label="A/B轴" v-if="AGVshow">
				<zxzUniDataSelect filterable v-model="AB" :localdata="ABS"></zxzUniDataSelect>
				<!-- 	<select v-model="AB">
					<option value="A">A轴</option>
					<option value="B">B轴</option>
				</select> -->
			</u-form-item>

			<u-form-item label="温度" v-if="AreaName == 'C'">
				<zxzUniDataSelect filterable v-model="TEMPERATURE" :localdata="TEMPERATURES"></zxzUniDataSelect>
				<!-- <select v-model="TEMPERATURE">
					<option value="high">高温</option>
					<option value="middle">中温</option>
					<option value="low">低温</option>
				</select> -->
			</u-form-item>
			<u-form-item label="熟化小时" v-if="AreaName == 'C'">
				<uni-easyinput v-model="BAKINGDAYS" type="number" style="max-width: 250rpx;max-height: 50rpx;">
				</uni-easyinput>
			</u-form-item>
		</u-form>
		<!-- </u-popup>
		</view> -->
		<u-form ref="testForm" v-if="AreaName != 'C' && AreaName != 'B'">
			<u-form-item :border-bottom="false">
				<label>选择仓库:</label>&nbsp;&nbsp;&nbsp;&nbsp;
				<zxzUniDataSelect filterable v-model="ERPLOCATION" :localdata="items" @change="change">
				</zxzUniDataSelect>
			</u-form-item>
			<u-form-item :border-bottom="false">
				<label>选择库位:</label>&nbsp;&nbsp;&nbsp;&nbsp;
				<zxzUniDataSelect filterable v-model="LOCATIONNAME" :localdata="items_Location"></zxzUniDataSelect>
			</u-form-item>
		</u-form>
		<br />
		<u-form-item :border-bottom="false">
			<p>扫描条码:</p>
			<u-input :border="true" v-model="MATERIALPACKINGNAME" placeholder="请扫描条码" :focus="focusBarCode"
				@confirm="ScanFGBarCode" /> <u-button @click="handleScan()" type="primary" style="margin-left:10rpx;width:130rpx;height:70rpx">扫码</u-button>
		</u-form-item>
		<br />
		<uni-datetime-picker style="align-items: center;" type="date" :clear-icon="false" v-model="commitDate" @maskClick="maskClick" />
		<u-button @click="FGStockIn" type="primary" :loading="loading">确定</u-button>
		<br />
		<u-card style="border: 1rpx solid #0081ff" :show-head="false" :full="true"
			:body-style="{ height: '100rpx' }">
			<view slot="body">
				<u-row>
					<u-col span="6">
						<view>累计选择支数：{{ sumJs || '0' }}</view>
					</u-col>
				</u-row>
			</view>
		</u-card>
		<zb-table style="height: 450upx;" ref="zbTable" :show-header="true" :columns="column" :stripe="true" :fit="true"
			:highlight="true" :border="true" :data="BoxListInfo" @dele="del"></zb-table>
		<br />
	</view>
</template>

<script>
	import zxzUniDataSelect from "@/components/zxz-uni-data-select/zxz-uni-data-select.vue"
	import {
		toScanCode
	} from '@/components/QRCode/scan.js'
	export default {
		components: {
			zxzUniDataSelect
		},
		data() {
			return {
				column: [ //列
					{
						name: 'index',
						label: '序号'
					},
					{
						name: 'DURABLENAME',
						label: '载具'
					},
					{
						name: 'MATERIALPACKINGNAME',
						label: '标签',
						align: 'center',
					},

					{
						name: 'operation',
						type: 'operation',
						label: '操作',
						renders: [{
							name: '删除',
							size: 'mini',
							type: 'warn',
							func: "dele"
						}, ]
					},
				],
				commitDate : '',
				MATERIALQUANTITY: null,
				items_In: [],
				items_En: [],
				INITILATION: '', //起始点
				ENDING: '', //目的地
				AB: '', //AB轴
				DURABLENAMECode: '', //卷芯
				AreaName: '',
				Area: [],
				value: '',
				show: false,
				AGVshow: false,
				orgObject: {
					ERPFACTORY: '',
					DESCRIPTION: ''
				}, // 组织对象
				datamodel: '',
				LocationName: '', //库位信息
				LOCATIONNAME: '',
				ERPFACTORY: JSON.parse(uni.getStorageSync('orgObject')).ERPFACTORY, //组织
				ERPLOCATION: '', //库位
				tabH: 800, //表格的宽度
				MATERIALSPECNAME: '',
				MATERIALPACKINGNAME: '', //条码
				MATERIALPACKINGNAME2: '', //条码
				CHARGE: '', //批次
				DESC_CN: '', //品名
				SPECNAME: '', //规格
				RECEIVEACTNO: '', //入库单号
				UNIT: '', //单位
				PHASE: '', //阶段
				QTY: '', //数量
				background: {
					backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
				}, //返回按钮的样式
				colors: ['#007aff', '#4cd964', '#dd524d'],
				styleType: 'button',
				current: 0,
				BoxListInfo: [], //后端传回来的绑定表格的数据
				colorIndex: 0,
				activeColor: '#007aff',
				loading: false,
				scanData: [], //已扫描箱
				barCode: "", //条码
				focusBarCode: false, //焦点是否被选中
				focusShipRequest: true, //默认焦点在单号
				siteName: "",
				userId: "",
				items: [],
				items_Location: [],
				PRODUCTLINES: [],
				stockChoose: [],
				ABS: [{
						text: "A轴",
						value: "A"
					},
					{
						text: "B轴",
						value: "B"
					}
				],
				TEMPERATURES: [{
						text: "高温",
						value: "high"
					},
					{
						text: "中温",
						value: "middle"
					},
					{
						text: "低温",
						value: "low"
					}
				],
				WIDTH: "", //	宽幅(AGV使用)
				DIAMETER: "", //	直径(AGV使用)
				DEVIATION: "", //	偏移(AGV使用)
				WEIGHT: "", //	重量(AGV使用)
				PRODUCTLINE: "", //	生产线(AGV使用)
				TEMPERATURE: '', //烤箱温度
				BAKINGDAYS: '' //熟化小时
			}
		},
		onUnload() {
			uni.$off('rifdscan')
		},
		onShow() {
			let that = this
			// uni.$off('rifdscan')
			uni.$on('rifdscan', (res) => {
				console.log('扫码结果：', res);
				that.DURABLENAMECode = res.rifd
			})
		},
		computed: {
			sumJs() {
				// 计算累计选择支数  
				return this.BoxListInfo.reduce((total, item) => total + 1, 0);
			},
		},
		mounted() {
			this.GetArea()
			this.tabH = this.$GetRemainingHeight(4, 0); //定义表格的高度
			let that = this
			uni.getStorage({
				key: "sitename",
				success(res) {
					that.siteName = res.data
				}
			})
			uni.getStorage({
				key: "userid",
				success(res) {
					that.userId = res.data
				}
			})

			this.orgObject = JSON.parse(uni.getStorageSync('orgObject'))
			this.getERPLocation();
			this.GetLiberaryBaeIn()
			this.GetLiberaryBaeEn()
			this.GetPRODUCTLINES()
			this.getTimeData();
			// this.getLocationName();
		},
		methods: {
			handleScan() {
				toScanCode({
					onlyFromCamera: true // 只允许通过相机扫码
				}).then(res => {
					this.MATERIALPACKINGNAME = res
					console.log(res)
				})
			},
			getTimeData(){
				var year=new Date().getFullYear();//年
				if (year< 1900) year = year + 1900;
				var month = new Date().getMonth() + 1;//月
				if (month < 10) month = '0' + month;
				var day = new Date().getDate();//日
				if (day < 10) day = '0' + day;
				const calendar1 = year+'-'+month+'-'+day
				this.commitDate = calendar1
			},
			maskClick(e) {
				console.log('maskClick事件:', e);
			},
			del(row, index) {
				this.BoxListInfo.splice(index, 1)
				this.$showMessage("删除成功")
			},
			change(e) {
				this.ERPLOCATION = e.value;
				this.LOCATIONNAME = ""
				this.getLocationName();
			},
			GetArea() {
				this.$MyRequest('/api/storage/getArea', {}).then(res => {
					if (res.data.success) {
						console.log(res.data.resultObj)
						for (let i = 0; i < res.data.resultObj.length; i++) {
							let _Arr = res.data.resultObj[i];
							this.Area.push({
								text: _Arr.label,
								value: _Arr.value
							});
						}
						console.log(this.Area)
					} else {
						this.$showMessage(res.data.message)
					}

				}).catch(err => {
					this.$showMessage(err)
				})
			},
			confirm(e) {
				console.log(e[0].value)
				this.AreaName = e[0].value
			},
			IFUserAGV(e) {
				let value = e.detail.value[0]
				console.log(value)
				if (value == 'cb') {
					this.AGVshow = true
				} else {
					this.AGVshow = false
				}
			},
			//获取焦点的公共方法
			getfocus(nextfocus) {
				this.initfocus()
				this[nextfocus]
				this.$nextTick(() => { //vue是数据全部修改后才进行的视图更新，哪nextTick函数就是为了拿到最新的数据，还不懂的可以去百度
					this[nextfocus] = true
				})
			},
			GetLiberaryBaeIn() {
				//发送请求
				this.$MyRequest('/api/ApiQuery', {
					queryId: "GetLibraryBaseIn",
					version: "00001",
					params: {}
				}).then(res => {
					for (let i = 0; i < res.data.data.length; i++) {
						var _Arr = res.data.data[i];
						this.items_In.push({
							text: _Arr.LABEL,
							value: _Arr.VALUE
						});
					}
				}).catch(err => {
					this.$showMessage(err)
				})
			},
			//产线获取
			GetPRODUCTLINES() {
				//发送请求
				this.$MyRequest('/api/ApiQuery', {
					queryId: "GetProductLine",
					version: "00001",
					params: {}
				}).then(res => {
					for (let i = 0; i < res.data.data.length; i++) {
						var _Arr = res.data.data[i];
						this.PRODUCTLINES.push({
							text: _Arr.LABEL,
							value: _Arr.VALUE
						});
					}
				}).catch(err => {
					this.$showMessage(err)
				})
			},
			GetLiberaryBaeEn() {
				//发送请求
				this.$MyRequest('/api/ApiQuery', {
					queryId: "GetLibraryBaseIn",
					version: "00002",
					params: {}
				}).then(res => {
					for (let i = 0; i < res.data.data.length; i++) {
						var _Arr = res.data.data[i];
						this.items_En.push({
							text: _Arr.LABEL,
							value: _Arr.VALUE
						});
					}
				}).catch(err => {
					this.$showMessage(err)
				})
			},
			getERPLocation() {
				this.items = [];
				this.$MyRequest('/api/storage/getERPLocation', {
					ERPFACTORYNAME: this.ERPFACTORY
				}).then(res => {
					if (res.data.success) {
						for (let i = 0; i < res.data.resultObj.length; i++) {
							var _Arr = res.data.resultObj[i];
							this.items.push({
								text: _Arr.DESCRIPTION,
								value: _Arr.ERPLOCATIONNAME
							});
						}
					} else {
						this.$showMessage(res.data.message)
					}
				});
			},
			getLocationName() {
				this.items_Location = []
				//let ERPFACTORY = JSON.parse(uni.getStorageSync('orgObject')).ERPFACTORY;
				if (this.ERPLOCATION != '') {
				    this.$MyRequest('/api/storage/getLocationNameForERPLocation', {
				        ERPLocation: this.ERPLOCATION
				    }).then(res => {
				        if (res.data.success) {
				            for (let i = 0; i < res.data.resultObj.length; i++) {
				                var _Arr = res.data.resultObj[i];
				                this.items_Location.push({
				                    text: _Arr.DESCRIPTION,
				                    value: _Arr.STORAGENAME
				                });
								
				            }
				        } else {
				            this.$showMessage(res.data.message)
						}
						this.focusBarCode = true
						this.focusShipRequest = false
					}).catch(err => {
					        this.$showMessage(err)
					    });
					} else {
					    try {
					        Vue.prototype.$playFail()
					    } catch (e) {}
					    this.$showMessage("请先选择仓库")
					}
				/* 
				this.$MyRequest('/api/storage/getLocationNameForERPLocation', {
					ERPLocation: ERPFACTORY
				}).then(res => {
					if (res.data.success) {
						for (let i = 0; i < res.data.resultObj.length; i++) {
							var _Arr = res.data.resultObj[i];
							this.items_Location.push({
								text: _Arr.DESCRIPTION,
								value: _Arr.STORAGENAME
							});
				
						}
					} else {
						this.$showMessage(res.data.message)
					}
					this.focusBarCode = true
					this.focusShipRequest = false
				}).catch(err => {
					this.$showMessage(err)
				});
 */
			},

			ScanFGBarCode() {
				this.focusBarCode = false;
				if (this.BoxListInfo.some(box => box.MATERIALPACKINGNAME === this.MATERIALPACKINGNAME)) {
					this.$showMessage("该标签已扫描，请不要重复扫描！");
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
					this.$nextTick(() => {
						this.MATERIALPACKINGNAME = ""
						this.focusBarCode = true
						this.focusShipRequest = false
					})
					return;
				}
				// if ((this.LOCATIONNAME == null || this.LOCATIONNAME == "") && (this.AreaName != "C" && this.AreaName !=
				// 		"B")) {
				// 	this.$showMessage("请选择货位！");
				// 	try {
				// 		Vue.prototype.$playFail()
				// 	} catch (e) {}
				// 	this.$nextTick(() => {
				// 		this.MATERIALPACKINGNAME = ""
				// 		this.focusBarCode = true
				// 		this.focusShipRequest = false
				// 	})
				// 	return;
				// }
				// if ((this.ERPLOCATION == null || this.ERPLOCATION == "") && (this.AreaName != "C" && this.AreaName !=
				// 	"B")) {
				// 	this.$showMessage("请选择仓库！");
				// 	try {
				// 		Vue.prototype.$playFail()
				// 	} catch (e) {}
				// 	this.$nextTick(() => {
				// 		this.MATERIALPACKINGNAME = ""
				// 		this.focusBarCode = true
				// 		this.focusShipRequest = false
				// 	})
				// 	return;
				// }
				this.$MyRequest('/MaturationLibrary/getFGInfo', {
					BARCODE: this.MATERIALPACKINGNAME,
					durableName: this.DURABLENAMECode
				}).then(res => {
					if (res.data.success) {
						var _Arr = res.data.resultObj;
						this.BoxListInfo.push(...res.data.resultObj);
						this.$showMessage("扫描成功")
						this.$nextTick(() => {
							this.MATERIALPACKINGNAME = ""
							this.focusBarCode = true
							this.focusShipRequest = false
						})
						try {Vue.prototype.$playSuccess() } catch (e) {}
					} else {
						try {
							Vue.prototype.$playFail()
						} catch (e) {}
						this.$showMessage(res.data.message)
						this.$nextTick(() => {
							this.MATERIALPACKINGNAME = ""
							this.focusBarCode = true
							this.focusShipRequest = false
						})
					}
				}).catch(err => {
					this.$showMessage(err)
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
					this.$nextTick(() => {
						this.MATERIALPACKINGNAME = ""
						this.focusBarCode = true
						this.focusShipRequest = false
					})
				});
			},
			/**
			 * 入库审核
			 */
			FGStockIn() {
				console.log(this.commitDate)
				if (!this.AGVshow && this.DURABLENAMECode == "") {
					this.$showMessage("请扫描管芯")
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
					return;
				}
				if (this.AGVshow && this.DURABLENAMECode != "") {
					this.$showMessage("AGV搬运不能扫入管芯")
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
					return;
				}
				if (this.AGVshow) {
					if (this.INITILATION == "" || this.ENDING == "" || this.AB == ""
					|| this.WIDTH == "" || this.DIAMETER == "" || this.DEVIATION == "" || this.DEVIATION == ""
					|| this.WEIGHT == "" || this.PRODUCTLINE == "") {
						this.$showMessage("AGV信息必填")
						try {
							Vue.prototype.$playFail()
						} catch (e) {}
						return
					}
					if (this.INITILATION == this.ENDING) {
						this.$showMessage("起始点和目的点不能一样")
						try {
							Vue.prototype.$playFail()
						} catch (e) {}
						this.INITILATION = ""
						this.ENDING = ""
						return;
					}
				} else {
					this.INITILATION = ""
					this.ENDING = ""
				}
				if (this.AreaName == "C") {
					if (this.TEMPERATURE == "" || this.BAKINGDAYS == "") {
						this.$showMessage("熟化库熟化温度和时间不能为空")
						try {
							Vue.prototype.$playFail()
						} catch (e) {}
						return;
					}
				} else {
					this.TEMPERATURE = ""
					this.BAKINGDAYS = ""
				}
				if (this.BoxListInfo.length > 0) {
					this.loading = true;
					let ISAGV = this.AGVshow ? "TRUE" : "FALSE"
					let MATERIALPACKINGNAMELIST = this.BoxListInfo.map(item => {
						item.AREANAME = this.AreaName
						item.FLAG = "IN"
						item.OUTDESTINATION = this.AreaName
						item.ISAGV = ISAGV
						if (this.AGVshow) {
							item.INITILATION = this.INITILATION
							item.ENDING = this.ENDING
							item.AB = this.AB
							item.durableName = ''
							item.WIDTH = this.WIDTH //	宽幅(AGV使用)
							item.DIAMETER = this.DIAMETER //	直径(AGV使用)
							item.DEVIATION = this.DEVIATION //	偏移(AGV使用)
							item.WEIGHT = this.WEIGHT //	重量(AGV使用)
							item.PRODUCTLINE = this.PRODUCTLINE //	生产线(AGV使用)
						} 
						item.TRANSPORTCOMMANDTYPE = "IN"
						// item.TRAYNAME = ""
						item.INDESTINATION = this.AreaName
						return item;
					})
					this.$MyRequest('/MaturationLibrary/AutoStockIn', {
						SITENAME: "SDK",
						userId: this.userId,
						AREANAME: this.AreaName,
						aimAreaName: "",
						outProt: '',
						erpLocation: this.ERPLOCATION,
						erpFactory: this.ERPFACTORY,
						locationName: this.LOCATIONNAME,
						barCode: "",
						durableName: "",
						shipRequestName: "",
						WIDTH: this.WIDTH, //	宽幅(AGV使用)
						DIAMETER: this.DIAMETER, //	直径(AGV使用)
						DEVIATION: this.DEVIATION, //	偏移(AGV使用)
						WEIGHT: this.WEIGHT, //	重量(AGV使用)
						PRODUCTLINE: this.PRODUCTLINE, //	生产线(AGV使用)
						TEMPERATURE: this.TEMPERATURE, //烤箱温度
						BAKINGDAYS: this.BAKINGDAYS, //熟化小时
						MATERIALPACKINGNAME: MATERIALPACKINGNAMELIST,
						commitDate : this.commitDate,
					}).then(res => {
						if (res.data.success) {
							this.loading = false;
							this.resetValue();
							this.$showMessage(res.data.message)
							this.BoxListInfo = [];
						} else {
							this.loading = false;
							this.$showMessage(res.data.message)
							this.BoxListInfo = [];
						}
					}).catch(err => {
						this.loading = false;
						try {
							Vue.prototype.$playFail()
						} catch (e) {}
						this.$showMessage(err)
					});
				} else {
					this.$showMessage("请扫描！")
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
				}
			},
			resetValue() {
				this.MATERIALPACKINGNAME = "";
				this.BoxListInfo = [];
				this.RECEIVEACTNO = "";
				this.MATERIALSPECNAME = "";
				this.PHASE = "";
				this.SPECNAME = "";
				this.DESC_CN = "";
				this.CHARGE = "";
			}
		}
	}
</script>

<style lang="scss">
	switch {
		color: #FFCC33;
	}

	/* 基础样式 */
	.picker {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		width: 100%;
		/* 根据需要调整宽度 */
	}

	.row {
		display: flex;
		justify-content: space-between;
		margin-bottom: 10px;
		/* 行与行之间的间距 */
	}

	.col {
		flex: 1;
		/* 使得每列占据相等的空间 */
		display: flex;
		flex-direction: column;
		align-items: flex-start;
	}

	label {
		// font-weight: bold;
		/* 标签加粗 */
		margin-bottom: 5px;
		/* 标签和值之间的间距 */
	}

	.value {
		/* 值的样式，可以根据需要调整 */
	}
</style>
