<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" draggable destroy-on-close @closed="$emit('closed')">
		<el-form class="form" :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="ERP编号" prop="erp_pn">
				<el-input type="text" v-model="form.erp_pn" placeholder="请填写ERP编号"  clearable></el-input>
			</el-form-item>
			<el-form-item label="供应商" prop="vendor">
				<el-input type="text" v-model="form.vendor" placeholder="请填写供应商" clearable></el-input>
			</el-form-item>
			<el-form-item label="名称" prop="name">
				<el-input type="text" v-model="form.name" placeholder="请填写名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="制造商型号" prop="mpn">
				<el-input type="text" v-model="form.mpn" placeholder="请填写制造商型号" clearable></el-input>
			</el-form-item>
			<el-form-item label="规格" prop="specification">
				<el-input type="textarea" v-model="form.specification" :row="2" placeholder="请填写规格" 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 {
	name: "infoSave",
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增备件信息',
				edit: '编辑备件信息',
				show: '查看'
			},
			visible: false,
			isSave: false,
			//表单数据
			form: {
				id:"",
				erp_pn:"",
				vendor:"",
				name:"",
				mpn:"",
				specification: "",
			},
			//验证规则
			rules: {
				erp_pn: [
					{required: true, message: 'ERP编号不能为空'}
				],
				vendor: [
					{required: true, message: '供应商不能为空'}
				],
				name:[
					{required: true, message: '名称不能为空'}
				],
				mpn: [
					{required: true, message: '制造商型号不能为空'}
				],
				specification: [
					{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.system.spareInfo.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;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			let params = {
				id: data.id
			}
			const res = await this.$API.system.spareInfo.info.post(params);
			if (res.code == 200) {
				// this.form = res.data;
				this.form.id= res.data.id;
				this.form.erp_pn= res.data.erp_pn;
				this.form.vendor= res.data.vendor;
				this.form.name= res.data.name;
				this.form.mpn= res.data.mpn;
				this.form.specification= res.data.specification;
			}
		}
	}
}
</script>

<style scoped lang="scss">
.form{
	::v-deep .input{width: 100%;}
}
</style>
