<template>
	<lay-container :style="'height:420px'">
		<lay-form :model="editDataInfo" class="edit-info color999" :initValidate="false">
			<lay-form-item label="物料描述" prop="FName" required :initValidate="false">
				<lay-input v-model="editDataInfo.FName" style="width:calc(100% - 76px);margin-right:10px" disabled="true"></lay-input>
				<lay-button type="normal" @click="selectMaterial">选择
				</lay-button>
			</lay-form-item>
			<lay-form-item label="物料编号" prop="FCode" required :initValidate="false">
				<lay-input v-model="editDataInfo.FCode" style="width:calc(100% - 76px);margin-right:10px" disabled="true"></lay-input>
			</lay-form-item>
			<lay-form-item label="单位" prop="FUnit" required :initValidate="false">
				<lay-select v-model="editDataInfo.FUnit" v-if="seletList[0].length>0">
					<lay-select-option v-for="(rV,rI) in seletList[0]" key="rI" :value="rV.FValue+''"
						:label="rV.FValue">
					</lay-select-option>
				</lay-select>
			</lay-form-item>
			<lay-form-item label="包规对应尺寸" prop="FIsPackageSize">
				<lay-radio v-model="editDataInfo.FIsPackageSize" name="FIsPackageSize" :value="1">对应</lay-radio>
				<lay-radio v-model="editDataInfo.FIsPackageSize" name="FIsPackageSize" :value="0">不对应</lay-radio>
			</lay-form-item>
			<lay-form-item label="包材外形尺寸" prop="FOutSize">
				<lay-input v-model="editDataInfo.FOutSize" placeholder="长*宽*高(mm)"></lay-input>
			</lay-form-item>
			<lay-form-item label="包材重量(kg)" prop="FWeight">
				<lay-input v-model="editDataInfo.FWeight"></lay-input>
			</lay-form-item>
			<lay-form-item label="数量" prop="FCount">
				<lay-input v-model="editDataInfo.FCount"></lay-input>
			</lay-form-item>
		</lay-form>
		<view class="menuBtn">
			<lay-button type="danger" @click="submitClick">提交</lay-button>
			<lay-button @click="$emit('cancelClick')">取消</lay-button>
		</view>
		<lay-layer :area="['70%','80%']" v-model="showEditBox" title="选择包材">
			<MaterialSelect :dataInfoObj="editDataInfo" @selectClick="selectClick" @cancelClick="showEditBox=false">
			</MaterialSelect>
		</lay-layer>
	</lay-container>
</template>
<style scoped>
	.edit-info {
		width: 90%;
		padding-right: 30px;
		margin: 30px auto 10px;
	}

	.menuBtn {
		display: block;
		text-align: center;
		width: 96%;
		margin: 20px auto;
	}

	.empty-small2 {
		max-width: 96%;
		margin: 0 auto;
	}

	.empty-small2>.layui-btn {
		margin-bottom: 16px;
	}

	.note {
		color: #999;
		font-size: 12px;
		margin: 12px auto 2px;
		text-align: right;
	}
</style>
<script>
	import {
		ref,
		reactive
	} from 'vue';
	import {
		getPageList,
		getDataCode
	} from "/src/api/api/common";
	import MaterialSelect from "./MaterialSelect.vue";

	export default {
		components: {
			MaterialSelect
		},
		setup() {
			const editDataInfo = ref({});
			const showEditBox = ref(false);
			const initValidate = ref(false);
			return {
				editDataInfo,
				showEditBox,
				initValidate,
				seletList: ref([
					[],
					[]
				])
			}
		},
		props: {
			dataInfoObj: {
				type: Object,
				default: () => {},
			}
		},
		mounted() {
			this._getTypeList();
			if (this.dataInfoObj) {
				this.editDataInfo = JSON.parse(JSON.stringify(this.dataInfoObj));
			} else {
				this.editDataInfo.FUnit = "EA";
			}
		},
		methods: {
			selectMaterial() {
				this.showEditBox = true;
				this.editDataInfo.FName = "";
			},
			async _getTypeList() {
				let result = await getDataCode({
					FType: '1,2'
				});
				this.seletList[0] = result.FType1 || [];
				this.seletList[1] = result.FType2 || [];
			},
			selectClick(item) {
				this.editDataInfo.FMaterialID = item.FID;
				this.editDataInfo.FName = item.FName;
				this.editDataInfo.FCode = item.FCode || "";
				this.showEditBox = false;
			},
			async _submitData(layId, data) {
				layer.close(layId);
				this.editDataInfo.FCode = data.FCode || "";
				this.editDataInfo.FMaterialID = data.FID;
				this.editDataInfo.FGroup = data.FGroup || this.editDataInfo.FGroup || "";
				this.$emit('submitClick', this.editDataInfo);
			},
			async submitClick() {
				if (!this.editDataInfo.FName) return this.editDataInfo.FName = "";
				if (!this.editDataInfo.FUnit) return this.editDataInfo.FUnit = "";
				let result = await getPageList({
					FPageIndex: 1,
					FPageSize: 1,
					FType: 40,
					FName: this.editDataInfo.FName.trim() || ""
				}, "Material");
				let list = result.Data.List || [];
				let $this = this;
				let data = {};
				let message = "系统检索到您使用的是新物料，请确认？";
				if (list.length > 0) {
					data = list[0];
					message = "系统检索到您使用的是旧物料，请确认？";
				}
				layer.confirm(message, {
					title: "提示",
					btn: [{
						text: '确定',
						callback: function(id) {
							$this._submitData(id, data);
						}
					}, {
						text: '取消',
						callback: function(id) {
							layer.close(id);
						}
					}]
				});
			}
		}
	}
</script>
