<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" draggable destroy-on-close @closed="$emit('closed')">
		<el-scrollbar max-height="500" style="padding: 0 20px;">
			<el-form class="form" :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="right">
				<el-form-item label="BOM版本" prop="bom_version">
					<el-input v-model="form.bom_version" placeholder="请填写BOM版本" clearable></el-input>
				</el-form-item>
				<el-form-item label="BOM简称" prop="bom_abbreviation">
					<el-input v-model="form.bom_abbreviation" placeholder="请填写BOM简称" clearable></el-input>
				</el-form-item>
				<el-form-item label="BOM分类" prop="bom_classification">
					<el-input v-model="form.bom_classification" placeholder="请填写BOM分类" clearable></el-input>
				</el-form-item>
				<el-form-item label="BOM用途" prop="bom_purpose">
					<el-input v-model="form.bom_purpose" placeholder="请填写BOM用途" clearable></el-input>
				</el-form-item>
				<el-form-item label="父项物料编码" prop="parent_material_code">
					<el-input v-model="form.parent_material_code" placeholder="请填写父项物料编码" clearable></el-input>
				</el-form-item>
				<el-form-item label="父项物料名称" prop="parent_material_name">
					<el-input v-model="form.parent_material_name" placeholder="请填写父项物料民称" clearable></el-input>
				</el-form-item>
				<el-form-item label="父项物料单位" prop="parent_material_unit">
					<el-input v-model="form.parent_material_unit" placeholder="请填写父项物料单位" clearable></el-input>
				</el-form-item>
				<el-form-item label="子项物料编码" prop="child_material_code">
					<el-input v-model="form.child_material_code" placeholder="请填写子项物料编码" clearable></el-input>
				</el-form-item>
				<el-form-item label="子项物料名称" prop="child_material_name">
					<el-input v-model="form.child_material_name" placeholder="请填写子项物料民称" clearable></el-input>
				</el-form-item>
				<el-form-item label="子项物料单位" prop="child_material_unit">
					<el-input v-model="form.child_material_unit" placeholder="请填写子项物料单位" clearable></el-input>
				</el-form-item>
				<el-form-item label="项次" prop="item_sequence">
					<el-input v-model="form.item_sequence" placeholder="请填写项次" clearable></el-input>
				</el-form-item>
				<el-form-item label="子项类型" prop="child_material_type">
					<el-input v-model="form.child_material_type" placeholder="请填写子项类型" clearable></el-input>
				</el-form-item>
				<el-form-item label="用量分子" prop="usage_numerator">
					<el-input v-model="form.usage_numerator" placeholder="请填写用量分子" clearable></el-input>
				</el-form-item>
				<el-form-item label="用量分母" prop="usage_denominator">
					<el-input v-model="form.usage_denominator" placeholder="请填写用量分母" clearable></el-input>
				</el-form-item>
				<el-form-item label="创建组织编码" prop="create_org_code">
					<el-input v-model="form.create_org_code" placeholder="请填写创建组织编码" clearable></el-input>
				</el-form-item>
				<el-form-item label="创建组织名称" prop="create_org_name">
					<el-input v-model="form.create_org_name" placeholder="请填写创建组织名称" clearable></el-input>
				</el-form-item>
				<el-form-item label="使用组织编码" prop="use_org_code">
					<el-input v-model="form.use_org_code" placeholder="请填写使用组织编码" clearable></el-input>
				</el-form-item>
				<el-form-item label="使用组织名称" prop="use_org_name">
					<el-input v-model="form.use_org_name" placeholder="请填写使用组织名称" clearable></el-input>
				</el-form-item>
				<el-form-item label="单据状态" prop="document_status">
					<el-input v-model="form.document_status" placeholder="请填写单据状态" clearable></el-input>
				</el-form-item>
				<el-form-item label="禁用状态" prop="disable_status">
					<el-input v-model="form.disable_status" placeholder="请填写禁用状态" clearable></el-input>
				</el-form-item>
			</el-form>
		</el-scrollbar>
		<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: '新增BOM',
				edit: '编辑BOM',
				show: '查看'
			},
			visible: false,
			isSave: false,
			//表单数据
			form: {
				id:"",
				bom_version:"",
				bom_abbreviation:"",
				bom_classification:"",
				bom_purpose:"",
				parent_material_code:"",
				parent_material_name:"",
				parent_material_unit:"",
				child_material_code:"",
				child_material_name:"",
				child_material_unit:"",
				item_sequence:"",
				child_material_type:"",
				usage_numerator:"",
				usage_denominator:"",
				create_org_code:"",
				create_org_name:"",
				use_org_code:"",
				use_org_name:"",
				document_status:"",
				disable_status:""
			},
			//验证规则
			rules: {
				bom_version:[
					{required:true,message:"BOM版本不能为空"},
				],
				bom_abbreviation:[
					{required:true,message:"BOM简称不能为空"},
				],
				bom_classification:[
					{required:true,message:"BOM分类不能为空"},
				],
				bom_purpose:[
					{required:true,message:"BOM用途不能为空"},
				],
				parent_material_code:[
					{required:true,message:"父项物料编码不能为空"},
				],
				parent_material_name:[
					{required:true,message:"父项物料名称不能为空"},
				],
				parent_material_unit:[
					{required:true,message:"父项物料单位不能为空"},
				],
				child_material_code:[
					{required:true,message:"子项物料编码不能为空"},
				],
				child_material_name:[
					{required:true,message:"子项物料名称不能为空"},
				],
				child_material_unit:[
					{required:true,message:"子项物料单位不能为空"},
				],
				item_sequence:[
					{required:true,message:"项次不能为空"},
				],
				child_material_type:[
					{required:true,message:"子项类型不能为空"},
				],
				usage_numerator:[
					{required:true,message:"用量分子不能为空"},
				],
				usage_denominator:[
					{required:true,message:"用量分母不能为空"},
				],
				create_org_code:[
					{required:true,message:"创建组织编码不能为空"},
				],
				create_org_name:[
					{required:true,message:"创建组织名称不能为空"},
				],
				use_org_code:[
					{required:true,message:"使用组织编码不能为空"},
				],
				use_org_name:[
					{required:true,message:"使用组织名称不能为空"},
				]
			},
		}
	},
	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.bom.add.post(this.form);
					this.isSave = false;
					if(res.code == 200){
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					}
				}else{
					return false;
				}
			})
		},
		//表单注入数据
		setData(data){
			Object.assign(this.form,data);
			// this.form.id = data.id
		}
	}
}
</script>

<style lang="scss" scoped>
.form{
	.cardBoxForm{
		border-bottom: 1px solid #f7f7f7;
		.title{
			padding: 15px 0 8px 18px;
			font-weight: bold;
		}
	}
	.cardBoxForm:last-child{
		border-bottom: 0;
	}
}
</style>
