<template>
	<lay-container style="height:450px;overflow:hidden">
		<view v-show="showSupplyInfo==4" class="note-no">
			不需要补充包材规格
		</view>
		<lay-form v-show="showSupplyInfo==2 || showSupplyInfo==3" :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"></lay-input>
				<lay-button :disabled="showSupplyInfo!=2" type="normal" @click="selectMaterial">选择
				</lay-button>
			</lay-form-item>
			<lay-form-item label="物料组" prop="FGroup" required>
				<lay-select v-model="editDataInfo.FGroup" v-if="seletList[1].length>0">
					<lay-select-option v-for="(rV,rI) in seletList[1]" key="rI" :value="rV.FValue+''" :label="rV.FName">
					</lay-select-option>
				</lay-select>
			</lay-form-item>
			<lay-form-item label="单位" prop="FUnit" required>
				<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>
				<p class="note3" v-if="editDataInfo.FUnit&&seletList[0].find(s=>s.FValue==editDataInfo.FUnit)!=null">
					{{ seletList[0].find(s=>s.FValue==editDataInfo.FUnit).FName }}
				</p>
			</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" v-show="showSupplyInfo==2">
			<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;
	}

	.note-no {
		font-weight: bold;
		text-align: center;
		width: 100%;
		display: block;
		line-height: 400px;
		font-size: 40px;
		color: #5FB878;
	}
	.note3 {
		display: inline-flex;
		line-height: 38px;
		margin-left: 12px;
	}

	.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 {
		dockMDMCode,
		getPackageChildList,
		noSupplyPackageChild
	} from "/src/api/api/package";
	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 showSupplyInfo = ref(0);
			return {
				editDataInfo,
				showEditBox,
				showSupplyInfo,
				seletList: ref([
					[],
					[]
				])
			}
		},
		props: {
			dataInfoObj: {
				type: Object,
				default: () => {},
			}
		},
		mounted() {
			this._getTypeList();
			this._getPackageSupplyList();
		},
		methods: {
			selectMaterial() {
				this.showEditBox = true;
			},
			async _getTypeList() {
				let result = await getDataCode({
					FType: '1,2'
				});
				this.seletList[0] = result.FType1 || [];
				this.seletList[1] = result.FType2 || [];
			},
			async _getPackageSupplyList() {
				let result = await getPackageChildList({
					FPageSize: 999,
					FTeamID: this.dataInfoObj.FTeamID
				});
				if (result.Data.Total > 0) this.editDataInfo = result.Data.List[0];
				if (this.dataInfoObj.FState != 2) {
					if (result.Data.Total == 0) this.showSupplyInfo = 1; //提示是否补充
					else this.showSupplyInfo = 2; //显示编辑
				} else {
					if (result.Data.Total > 0) this.showSupplyInfo = 3; //显示编辑，不能修改
					else this.showSupplyInfo = 4; //显示不要补充
				}
				if (this.showSupplyInfo == 1) this.showIsSupply();
			},
			//提示是否补充
			showIsSupply() {
				let $this = this;
				layer.confirm("请确认是否需要补充额外包材？", {
					title: "提示",
					btn: [{
							text: '需要',
							callback: function(id) {
								$this.showSupplyInfo = 2;
								$this.editDataInfo.FUnit = $this.editDataInfo.FUnit || "EA";
								layer.close(id);
							}
						},
						{
							text: '不需要',
							callback: function(id) {
								$this._noSupplyPackageChild();
								layer.close(id);
							}
						}
					]
				});
			},
			//不补充操作
			async _noSupplyPackageChild() {
				await noSupplyPackageChild(this.dataInfoObj);
				this.showSupplyInfo = 4;
				this.$emit('cancelClick', true);
			},
			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.FCode || "";
				this.editDataInfo.FMaterialID = data.FID || this.editDataInfo.FID || -1;
				this.editDataInfo.FGroup = data.FGroup || this.editDataInfo.FGroup || "";
				this.editDataInfo.FTeamID = this.dataInfoObj.FTeamID || data.FTeamID || -1;
				let idx = layer.load(2);
				let postData = JSON.parse(JSON.stringify(this.dataInfoObj));
				postData.FList = JSON.stringify([this.editDataInfo]);
				let result = await dockMDMCode(postData);
				if (result > 0) this._noSupplyPackageChild();
				layer.close(idx);
			},
			async submitClick() {
				if (!this.editDataInfo.FName) return this.editDataInfo.FName = "";
				if (!this.editDataInfo.FGroup) return this.editDataInfo.FGroup = "";
				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 btnName = "";
				let message = "系统检索到您使用的是新物料，请确认？";
				if (list.length > 0) {
					data = list[0];
					message = "系统检索到您使用的是旧物料，请确认？";
					btnName = "保存";
				}
				if (btnName == "") {
					btnName = this.editDataInfo.FID ? "提交MDM" : "保存并提交MDM";
				}
				layer.confirm(message, {
					title: "提示",
					btn: [{
						text: btnName,
						callback: function(id) {
							$this._submitData(id, data);
						}
					}, {
						text: '取消',
						callback: function(id) {
							layer.close(id);
						}
					}]
				});
			}
		}
	}
</script>
