<template>
	<lay-form :model="objInfo" class="dataInfo">
		<lay-form-item label="分类姓名" prop="FName" required>
			<lay-input v-model="objInfo.FName"></lay-input>
		</lay-form-item>
		<lay-form-item label="分类状态" prop="FState" required>
			<lay-radio v-model="objInfo.FState" name="FState" value="1">有效</lay-radio>
			<lay-radio v-model="objInfo.FState" name="FState" value="0">无效</lay-radio>
		</lay-form-item>
		<lay-form-item>
			<lay-button type="normal" @click="submitClick">{{ objInfo.FID ? "修改" : "新增" }}</lay-button>
			<lay-button @click="cancelClick">取消</lay-button>
		</lay-form-item>
	</lay-form>
</template>
<style scoped>
	.dataInfo {
		max-width: 90%;
		margin: 30px auto;
		padding-right: 30px;
		overflow-y: auto;
	}
</style>
<script>
	import {
		ref
	} from 'vue';
	import {
		updateDataInfo,
		insertDataInfo
	} from "/src/api/api/common";

	export default {
		setup() {
			const objInfo = ref({});
			return {
				objInfo
			}
		},
		props: {
			objInfoObj: {
				type: Object,
				default: () => {},
			}
		},
		mounted() {
			let objInfo = JSON.parse(JSON.stringify(this.objInfoObj));
			if (!objInfo.FState) objInfo.FState = "1";
			else objInfo.FState = objInfo.FState == "有效" ? "1" : "0";
			delete objInfo["FDeleted"];
			delete objInfo["FAddDate"];
			delete objInfo["FDateOpt"];
			delete objInfo["FUserOpt"];
			objInfo.FName = objInfo.FName || " ";
			this.objInfo = objInfo;
		},
		methods: {
			cancelClick() {
				this.$emit('cancelClick', false);
			},
			async submitClick() {
				this.objInfo.FName = this.objInfo.FName.trim();
				if (this.objInfo.FName == "" ) return false;
				let result = 0;
				if (this.objInfo.FID && this.objInfo.FID > 0) {
					result = await updateDataInfo(this.objInfo, "MaterialType") || 0;
				} else {
					result = await insertDataInfo(this.objInfo, "MaterialType") || 0;
				}
				if (result > 0) {
					layer.msg("操作成功", {
						time: 1500,
						icon: 1
					});
					this.$emit('cancelClick', true);
				} else {
					layer.msg("操作失败", {
						time: 1000,
						icon: 2
					})
				}
			}
		}
	}
</script>
