<template>
	<lay-form :model="factoryInfo" style="max-width:96%; margin:30px 0;" @click.stop="stopClick">
		<lay-form-item label="工厂名称" prop="FName" required>
			<lay-input v-model="factoryInfo.FName"></lay-input>
		</lay-form-item>
		<lay-form-item label="工厂编号" prop="FCode" required>
			<lay-input v-model="factoryInfo.FCode"></lay-input>
		</lay-form-item>
		<lay-form-item label="工厂模式" prop="FType" required>
			<view required v-for="(sIdv,sIdx) in typeLists" key="sIdx">
				<lay-radio v-model="factoryInfo.FType" name="FState" :value="sIdv.FValue">{{ sIdv.FName }}</lay-radio>
			</view>
		</lay-form-item>
		<lay-form-item v-if="factoryInfo.FType == '1'" label="委托工厂" prop="FFactoryID" required>
			<lay-select v-model="factoryInfo.FFactoryID" required>
				<lay-select-option v-for="(fIdv,fIdx) in factoryLists" key="fIdx" :value="fIdv.FID+''"
					:label="fIdv.FName +' - '+ fIdv.FCode"></lay-select-option>
			</lay-select>
		</lay-form-item>
		<lay-form-item label="负责人" prop="FChargePerson">
			<lay-input v-model="factoryInfo.FChargePerson"></lay-input>
		</lay-form-item>
		<lay-form-item label="手机号" prop="FPhone">
			<lay-input v-model="factoryInfo.FPhone"></lay-input>
		</lay-form-item>
		<lay-form-item label="工厂状态" prop="FState" required>
			<view required v-for="(sIdv,sIdx) in stateLists" key="sIdx">
				<lay-radio v-model="factoryInfo.FState" name="FState" :value="sIdv.FValue">{{ sIdv.FName }}</lay-radio>
			</view>
		</lay-form-item>
		<lay-form-item>
			<lay-button type="normal" @click="submitClick">{{ factoryInfo.FID ? "修改" : "新增" }}</lay-button>
			<lay-button @click="cancelClick">取消</lay-button>
		</lay-form-item>
	</lay-form>
</template>

<script>
	import {
		ref
	} from 'vue';
	import {
		updateDataInfo,
		insertDataInfo
	} from "/src/api/api/common";
	import {
		getRoleList
	} from "/src/api/api/user";
	import {
		checkHasCode
	} from "/src/api/api/factory";

	export default {
		setup() {
			const factoryInfo = ref({});
			const typeLists = ref([]);
			const stateLists = ref([]);
			const factoryLists = ref([]);
			return {
				factoryInfo,
				typeLists,
				stateLists,
				factoryLists
			}
		},
		props: {
			factoryInfoObj: {
				type: Object,
				default: () => {},
			},
			typeList: {
				type: Array,
				default: () => [],
			},
			stateList: {
				type: Array,
				default: () => [],
			},
			factoryList: {
				type: Array,
				default: () => [],
			}
		},
		mounted() {
			this.typeLists = JSON.parse(JSON.stringify(this.typeList));
			this.stateLists = JSON.parse(JSON.stringify(this.stateList));

			let factoryInfo = JSON.parse(JSON.stringify(this.factoryInfoObj));
			factoryInfo.FName = factoryInfo.FName || " ";
			factoryInfo.FCode = factoryInfo.FCode || " ";
			factoryInfo.FType = (factoryInfo.FType || "1") + "";
			factoryInfo.FFactoryID = (factoryInfo.FFactoryID || " ") + "";
			factoryInfo.FState = (factoryInfo.FState || "1") + "";
			//排除自己
			let factoryList = JSON.parse(JSON.stringify(this.factoryList));
			factoryList = factoryList.filter(s => s.FType == "2" || s.FType == 2) || [];
			if (factoryInfo.FID && factoryInfo.FID > 0) {
				let obj = factoryList.find(s => s.FID == factoryInfo.FID);
				if (obj != null) factoryList.splice(factoryList.indexOf(obj), 1);
			}
			this.factoryLists = factoryList;
			this.factoryInfo = factoryInfo;
			console.log(this.factoryInfo)
		},
		methods: {
			stopClick() {

			},
			cancelClick() {
				this.$emit('cancelClick', false);
			},
			async submitClick() {
				this.factoryInfo.FName = this.factoryInfo.FName.trim();
				this.factoryInfo.FCode = this.factoryInfo.FCode.trim();
				this.factoryInfo.FFactoryID = this.factoryInfo.FFactoryID.trim();
				if (this.factoryInfo.FFactoryID == "-1") this.factoryInfo.FFactoryID = "";
				if (this.factoryInfo.FName == "" || this.factoryInfo.FCode == "") {
					return false;
				}
				if (this.factoryInfo.FType == "1" && this.factoryInfo.FFactoryID == "") {
					return false;
				}
				if (this.factoryInfo.FType != "1") this.factoryInfo.FFactoryID = "-1";
				let codeCount = await checkHasCode(this.factoryInfo.FID || -1, this.factoryInfo.FCode) || 0;
				if (codeCount > 0) {
					layer.msg("工厂编号重复", {
						time: 1000,
						icon: 3
					})
					return false;
				}
				let result = 0;
				if (this.factoryInfo.FID && this.factoryInfo.FID > 0) {
					result = await updateDataInfo(this.factoryInfo, "Factory") || 0;
				} else {
					this.factoryInfo.FAddUser = -1;
					this.factoryInfo.FEditUser = -1;
					result = await insertDataInfo(this.factoryInfo, "Factory") || 0;
				}
				if (result > 0) {
					layer.msg("操作成功", {
						time: 1500,
						icon: 1
					});
					this.$emit('cancelClick', true);
				} else {
					layer.msg("操作失败", {
						time: 1000,
						icon: 2
					})
				}
			}
		}
	}
</script>
