<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="备件类别" prop="category_name">
				<el-input v-model="form.category_name" placeholder="请输入备件类别" clearable></el-input>
			</el-form-item>
			<el-form-item label="故障率" prop="rate">
				<el-input v-model="form.rate" 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: "addSpare",
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			setMap:{
				regionList:[]
			},
			visible: false,
			isSave: false,
			//表单数据
			form: {
				id:"",
				category_name: "",
				rate:"",
			},
			//验证规则
			rules: {
				category_name: [
					{required: true, message: '请输入备件类别'}
				],
				rate: [
					{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.spare.add.post(this.form);
					this.isSave = false;
					if(res.code == 200){
						this.$emit('success', this.form, this.mode);
						this.visible = false;
						this.$message.success("操作成功")
					}
				}
			})
		},
		setData(data){
			// Object.assign(this.form, data);
			this.form.id = data.id;
			this.form.category_name = data.spare_parts_category;
			this.form.rate = data.failure_rate;
		}
	}
}
</script>

<style scoped>

</style>
