<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="物料MPN" prop="m_mpn">
				<el-input v-model="form.m_mpn" placeholder="请输入物料MPN" clearable></el-input>
			</el-form-item>
			<el-form-item label="物料名称" prop="m_name">
				<el-input v-model="form.m_name" placeholder="请输入物料名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="物料成本价" prop="m_price">
				<el-input v-model="form.m_price" placeholder="请输入物料成本价" clearable></el-input>
			</el-form-item>
			<el-form-item label="ERP实体ID" prop="f_id">
				<el-input v-model="form.f_id" placeholder="请输入ERP实体ID" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSave" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
			},
			visible: false,
			isSave: false,
			//表单数据
			form: {
				m_mpn: "",
				m_name:"",
				m_price: "",
				f_id: ""
			},
			//验证规则
			rules: {
				m_mpn: [
					{required: true, message: '请输入物料MPN'}
				],
				m_name: [
					{required: true, message: '请输入物料名称'}
				],
				m_price: [
					{required: true, message: '请输入物料成本价'}
				],
				f_id: [
					{required: true, message: '请输入ERP实体ID'}
				],
			},
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode='add'){
			this.mode = mode;
			this.visible = true;
			return this
		},

		//表单提交方法
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSave = true;
					const res = await this.$API.setup.material.add.post(this.form);
					this.isSave = false;
					if(res.code == 200){
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					}
				}
			})
		},
	}
}
</script>

<style lang="scss" scoped>

</style>
