<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: zwq
 * @LastEditTime: 2024-04-25 16:39:52
 * @Description:
-->
<template>
	<el-form
		:model="dataForm"
		:rules="dataRule"
		ref="dataForm"
		@keyup.enter.native="dataFormSubmit()"
		label-width="60px">
		<el-form-item
			v-if="!type"
			label-width="150px"
			label="选择到货区域"
		  class="drawer"
			prop="region">
			<el-select
				v-model="dataForm.region"
				style="width: 100%"
        @change="getOutArr"
        :popper-append-to-body="false"
				placeholder="请选择区域">
				<el-option
					v-for="item in regionArr"
					:key="item.id"
					:label="item.name"
					:value="item.id" />
			</el-select>
		</el-form-item>
		<el-form-item
			v-if="!type"
			label-width="150px"
			label="选择到货位置"
		  class="drawer"
			prop="targetId">
			<el-select
				v-model="dataForm.targetId"
        :disabled="outDisable"
				style="width: 100%"
        :popper-append-to-body="false"
				placeholder="请选择位置">
				<el-option
					v-for="item in potArr"
					:key="item.id"
					:label="item.lineEdgeLibraryCode"
					:value="item.id" />
			</el-select>
		</el-form-item>
		<div v-else style="display: flex; justify-content: space-around">
			<el-form-item label="排" prop="wareRow">
				<el-select
					v-model="dataForm.wareRow"
					style="width: 150px"
					placeholder="请选择排">
					<el-option
						v-for="item in potArr1"
						:key="item.id"
						:label="item.label"
						:value="item.id" />
				</el-select>
			</el-form-item>
			<el-form-item label="列" prop="wareColumn">
				<el-select
					v-model="dataForm.wareColumn"
					style="width: 150px"
					placeholder="请选择列">
					<el-option
						v-for="item in potArr2"
						:key="item.id"
						:label="item.label"
						:value="item.id" />
				</el-select>
			</el-form-item>
			<el-form-item label="层" prop="wareLayer">
				<el-select
					v-model="dataForm.wareLayer"
					style="width: 150px"
					placeholder="请选择层">
					<el-option
						v-for="item in potArr3"
						:key="item.id"
						:label="item.label"
						:value="item.id" />
				</el-select>
			</el-form-item>
		</div>
	</el-form>
</template>

<script>
import basicAdd from '../mixins/basic-add';
import {
	getWarehouseStorehousePage,
	moveStorehouse,
	outWarehouseStorehouse,
	getLineList,
	getWarehouseStorehouse,
} from '@/api/asrs/warehouseStorehouse';
import { getWarehouseStorehouseGoodsSpecificationPage } from '@/api/asrs/warehouseStorehouseGoodsSpecification';

const potArr3 = [
	{ label: '1层', id: 1 },
	{ label: '2层', id: 2 },
	{ label: '3层', id: 3 },
	{ label: '4层', id: 4 },
];
const regionArr = [
	{ name: '一次分切区域1线', id: 1 },
	{ name: '一次分拣区域', id: 2 },
	{ name: '二次分切区域', id: 3 },
	{ name: '二次分拣区域', id: 4 },
	{ name: '手动包装区域', id: 5 },
	{ name: '涂覆区域', id: 6 },
	{ name: '其他', id: 7 },
	{ name: '自动包装区域', id: 8 },
	{ name: '一次分切区域2线', id: 9 },
	{ name: '一次分切区域3线', id: 10 },
	{ name: '一次分切区域4线', id: 11 },
];
export default {
	mixins: [basicAdd],
	data() {
		return {
			urlOptions: {},
			warehouseId: null,
			dataForm: {
				sourceId: undefined,
				targetId: undefined,
        region: undefined,
				wareRow: undefined,
				wareColumn: undefined,
				wareLayer: undefined,
			},
			wareInfo: {
				list: [],
			},
			potArr: [],
			potArr1: [],
			potArr2: [],
			potArr3,
      regionArr,
			type: 0,
			isloading: false,
      outDisable: true, //
			dataRule: {
				region: [
					{ required: true, message: '区域不能为空', trigger: 'change' },
				],
				targetId: [
					{ required: true, message: '位置不能为空', trigger: 'change' },
				],
				wareRow: [{ required: true, message: '排不能为空', trigger: 'blur' }],
				wareColumn: [
					{ required: true, message: '列不能为空', trigger: 'blur' },
				],
				wareLayer: [{ required: true, message: '层不能为空', trigger: 'blur' }],
			},
		};
	},
	methods: {
		init(id, type, wId, stacker) {
			this.isloading = false;
			this.dataForm.sourceId = id || '';
			this.warehouseId = wId || null;
			this.dataForm.targetId = null;
      this.dataForm.region = null
			this.type = type;
      this.outDisable = true
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields();
				if (this.type) {
					this.potArr1.splice(0, this.potArr1.length);
					for (let i = 1; i <= 4; i++) {
						const obj = {
							id: i + (stacker - 1) * 4,
							label: i + (stacker - 1) * 4 + '排',
						};
						this.potArr1.push(obj);
					}
					this.potArr2.splice(0, this.potArr2.length);
					for (let i = 1; i <= 30; i++) {
						const obj = {
							id: i,
							label: i + '列',
						};
						this.potArr2.push(obj);
					}
					return;
				}
				getWarehouseStorehouse(id).then((response) => {
					this.wareInfo = response.data;
				});
				const par = {
					pageSize: 100,
					pageNo: 1,
					total: 1,
					warehouseStorehouseId: id,
				};
				getWarehouseStorehouseGoodsSpecificationPage(par).then((response) => {
					this.wareInfo.list = response.data.list;
				});
			});
		},
    getOutArr(val){
				getLineList({'region':val,'warehouseId':this.warehouseId}).then((response) => {
					this.potArr = response.data;
          this.outDisable = false
				});
    },
		// 表单提交
		dataFormSubmit() {
			if (!this.isloading) {
				this.$refs['dataForm'].validate((valid) => {
					if (!valid) {
						return false;
					}
					this.isloading = true;
					if (this.type) {
						const lQ = {
							warehouseId: this.warehouseId,
							wareRow: this.dataForm.wareRow,
							wareColumn: this.dataForm.wareColumn,
							wareLayer: this.dataForm.wareLayer,
							pageSize: 10,
							pageNo: 1,
						};
						getWarehouseStorehousePage(lQ).then((response) => {
							if (response.data.list) {
								this.dataForm.targetId = response.data.list[0].id;
								moveStorehouse(
									this.dataForm.sourceId,
									this.dataForm.targetId
								).then((response) => {
									this.$modal.msgSuccess('移库成功');
									this.visible = false;
									this.$emit('refreshDataList');
								});
							}
						});
						return;
					}
					const pos = this.potArr.find((item) => {
						return item.id === this.dataForm.targetId;
					});
					this.wareInfo.agvGroundCode = pos.agvGroundCode;
					this.wareInfo.lineEdgeLibraryCode = pos.lineEdgeLibraryCode;
					// const data = {
					// 	id: this.dataForm.sourceId,
					// 	agvGroundCode: position.agvGroundCode,
					// 	lineEdgeLibraryCode: position.lineEdgeLibraryCode,
					// };
					outWarehouseStorehouse(this.wareInfo).then((response) => {
						this.$modal.msgSuccess('出库成功');
						this.visible = false;
						this.$emit('refreshDataList');
					});
				});
			} else {
				this.$modal.msgWarning('请勿重复提交！');
			}
		},
	},
};
</script>

<style scoped>
.drawer >>> .el-select-dropdown__item {
  font-size: 20px;
}
</style>
