<template>
	<view style="margin: 10rpx;">
		<u-navbar back-text="返回" title="立体库转库" :background="background"></u-navbar>
				<u-form ref="testForm">
					<u-form-item class="item2">
						<p>物料:</p>
						<u-input v-model="materialSpecName"    border></u-input>
					</u-form-item>
					<u-form-item class="item2">
						<p>原库区:</p>
						<zxzUniDataSelect  @change="changeSlect" filterable v-model="AreaName" :localdata="Area"></zxzUniDataSelect>
					</u-form-item>
					<br />
					<u-form-item class="item2">
						<p>目标库区:</p>
						<zxzUniDataSelect  @change = "changeAimAreaSlect" filterable v-model="desAreaName" :localdata="Area"></zxzUniDataSelect>
					</u-form-item>
					<br />
					<u-form-item>
						<p>出货口:</p>
						<zxzUniDataSelect filterable v-model="outPort" :localdata="outPortList">
						</zxzUniDataSelect>
					</u-form-item>
					<checkbox-group @change="IFHaveBh">
						<label>
							<checkbox value="bh" />是否已备货
						</label>
					</checkbox-group>
					<br />
					<checkbox-group @change="IFUserAGV" v-if="AreaName != 'E'">
						<label>
							<checkbox value="cb" />是否使用AGV
						</label>
					</checkbox-group>
					<zb-table
					style="height: 350upx;"
					ref="zbTable"
					:show-header="true"
					:columns="column"
					:stripe="true"
					:fit="false" 
					:highlight="true"
					:border="true"
					:data="list_detail"
					:cell-style="cellStyleFn"
					@toggleAllSelection="toggleAllSelection"
					@toggleRowSelection="stockSelectionChange"
					></zb-table>
			<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>
			<!-- </u-popup>
		</view> -->
		<u-form ref="testForm" style="padding-bottom: 130rpx;" v-if="desAreaName != 'C' && desAreaName != '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 />
		<view class="tabbar_bottom">
			<uni-datetime-picker style="align-items: center;" type="date" :clear-icon="false" v-model="commitDate" @maskClick="maskClick" />
			
		<u-button class="btn-bottom" @click="FGStockIn" type="primary" :loading="loading">确定</u-button>
		</view>
	</view>
</template>

<script>
	import zxzUniDataSelect from "@/components/zxz-uni-data-select/zxz-uni-data-select.vue"
	export default {
		components: {
			zxzUniDataSelect
		},
		data() {
			return {
				list_detail: [],
				column:[ //列
					{
						type: 'selection',
						width: 40
					},
					{
						name: 'MATERIALSPECNAME',
						label: '料号'
					},
					{
						name: 'CHARGE',
						label: '批次',
						align: 'center',
						// fixed: true,
					},
					{
						name: 'DESC_CN',
						label: '品名'
					},

					{
						name: 'ERPLOCATION',
						label: '库位'
					},
					{
						name: 'LOCATIONNAME',
						label: '货架'
					}, {
						name: 'DESCRIPTION',
						label: '库区'
					},{
						name: 'TRAYNAME',
						label: '载具'
					},
					{
						name: 'SHELFNAME',
						label: '立体库货架'
					},
					{
						name: 'WIDTH',
						label: '宽幅(AGV)',
						sorter: true
					},
					{
						name: 'DIAMETER',
						label: '直径(AGV)',
						sorter: true
					},
					{
						name: 'DEVIATION',
						label: '偏移量(AGV)',
						sorter: true
					},
					{
						name: 'WEIGHT',
						label: '重量(AGV)',
						sorter: true
					},
					{
						name: 'PRODUCTLINE',
						label: '生产线(AGV)',
						sorter: true
					},
					{
						name: 'MATERIALQUANTITY',
						label: '系统数量',
						sorter: true
					},
				],
				MATERIALQUANTITY: null,
				items_In: [],
				items_En: [],
				commitDate : '',
				INITILATION: '', //起始点
				ENDING: '', //目的地
				AB: '', //AB轴
				ABS: [{
						text: "A轴",
						value: "A"
					},
					{
						text: "B轴",
						value: "B"
					}
				],
				DURABLENAMECode: '', //卷芯
				AreaName: '',
				Area: [],
				desAreaName:'',
				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: [],
				stockChoose:[],
				materialSpecName : "",
				outPort : "",
				outPortList : [],
			}
		},
		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.getTimeData();
			
			// let Things = 20
			// for (var i = 0; i < Things; i++) {
			// 	this.list_detail.push({aa:"1"}) 
			// }	
		},
		methods: {
			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);
			},
			outPortFn() {
				this.$MyRequest('/MaturationLibrary/getPort', {AREANAME:this.AREANAME}).then((res) => {
					let list = res.data.resultObj.map(item => {
							return {
								value:item.PORTNO,
								text: item.PORTNAME
							}	
					})
					this.outPortList = list
				})
			},
			changeSlect(e) {
				this.list_detail = []
				console.log('1',e)
				this.AREANAME = e.value
				if (this.AREANAME == this.desAreaName) {
					this.AREANAME == ""
					this.$showMessage("目标库区与发出库区不能相同")
					try {Vue.prototype.$playFail() } catch (e) {}
					return
					
				}
				this.outPortFn()
				this.getDetailQuery({AREANAME:this.AREANAME,SHIPREQUESTNAME:this.IFBh,MATERIALSPECNAME : this.materialSpecName})
			},
			changeAimAreaSlect(e) {
				console.log('1',e)
				this.desAreaName = e.value
				if (this.AREANAME == this.desAreaName) {
					this.desAreaName = ""
					this.$showMessage("目标库区与发出库区不能相同")
					try {Vue.prototype.$playFail() } catch (e) {}
					return
				}
			},
			IFHaveBh(e) {
				let value = e.detail.value[0] || ""
				console.log(value)
				this.IFBh = value
				this.list_detail = []
				this.getDetailQuery({AREANAME:this.AREANAME,SHIPREQUESTNAME:this.IFBh,MATERIALSPECNAME : this.materialSpecName})
			},
			getDetailQuery(data) {
				this.loading = true
				this.$MyRequest('/MaturationLibrary/getPreStockOutByAuto', data).then((res) => {
					console.log("aaa",res.data.resultObj)
					this.list_detail = res.data.resultObj
					this.loading = false
				})
			},
			toggleAllSelection(e,data) {
				console.log('调用了', data)
				this.stockChoose = data
			},
			//选择备货
			stockSelectionChange(e, selection) {
			  console.log('调用了', selection)
			  this.stockChoose = selection
			},
			cellStyleFn({row, column, rowIndex, columnIndex}) {
				// console.log(row, column, rowIndex, columnIndex)
				// return {backgroundColor: row.ISCHECK == 'Y' ? 'yellowgreen' : 'white'}
			},
			change(e) {
				this.ERPLOCATION = e.value;
				this.LOCATIONNAME = ""
				this.getLocationName();
			},
			GetArea(){
				this.$MyRequest('/api/storage/getArea2', {}).then(res => {
					if (res.data.success) {
						console.log(res.data.resultObj)
						this.Area = res.data.resultObj
						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)
				})
			},
			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 = []
				if (this.ERPLOCATION != '') {
					this.$MyRequest('/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 {
					this.$showMessage("请先选择仓库")
				}

			},
			/**
			 * 转库保存
			 */
			FGStockIn() {
				console.log(this.commitDate)
				console.log(this.AREANAME + "****" + this.desAreaName)
				if (!this.AGVshow && (this.AREANAME == this.desAreaName || this.AREANAME  == "" || this.desAreaName == "") ){
					this.$showMessage("目标库区与发出库区不能相同")
					try {Vue.prototype.$playFail() } catch (e) {}
					return
					
				}
				if (this.stockChoose.length > 0) {
					this.loading = true;
					let ISAGV = this.AGVshow ? "TRUE" : "FALSE"
					let MATERIALPACKINGNAMELIST = this.stockChoose.map(item => {
						if(this.AREANAME == this.desAreaName){
							if(item.ERPLOCATION == this.ERPLOCATION){
								if(item.LOCATIONNAME == this.LOCATIONNAME){
									this.$showMessage("不能选择同库区 同仓库 同库位 转移")
									return;
								}
							}
						}
						item.AREANAME = this.AREANAME
						item.FLAG = "MOVE"
						item.OUTDESTINATION = this.AREANAME
						item.INDESTINATION = this.desAreaName
						item.ISAGV = ISAGV
						item.INITILATION = this.INITILATION
						item.ENDING = this.ENDING
						item.AB = this.AB
						item.TRANSPORTCOMMANDTYPE = "MOVE"
						item.PORT= this.outPort
						return item;
					})
					this.$MyRequest('/MaturationLibrary/AutoStockOut', {
						SITENAME: "SDK",
						commitDate : this.commitDate,
						userId: this.userId,
						AREANAME: this.AREANAME,
						aimAreaName: this.desAreaName,
						outProt: '',
						erpLocation: this.ERPLOCATION,
						erpFactory: this.ERPFACTORY,
						locationName: this.LOCATIONNAME,
						barCode: this.MATERIALPACKINGNAME,
						durableName: '',
						shipRequestName: "",
						MATERIALPACKINGNAME: MATERIALPACKINGNAMELIST
					}).then(res => {
						if (res.data.success) {
							this.loading = false;
							this.resetValue();
							this.$showMessage(res.data.message)
							this.list_detail = [];
						} else {
							this.loading = false;
							this.$showMessage(res.data.message)
							this.list_detail = [];
						}
					}).catch(err => {
						this.loading = false;
						this.$showMessage(err)
					});
				} else {
					this.$showMessage("请扫描！")
				}
			},

			resetValue() {
				this.MATERIALPACKINGNAME = "";
				this.BoxListInfo = [];
				this.RECEIVEACTNO = "";
				this.MATERIALSPECNAME = "";
				this.PHASE = "";
				this.SPECNAME = "";
				this.DESC_CN = "";
				this.CHARGE = "";
			}
		}
	}
</script>
<style>
	.btn-bottom {
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
	}
</style>
<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>