<template>
	<el-dialog title="检查指导项表单" v-model="visible" width="500px" append-to-body draggable>
		<div class="form-container">
			<el-form ref="formRef" class="master-container" :rules="rules" v-loading="loading" :model="form" label-width="80px">
				<el-form-item label="检测标准" prop="standard" required>
					<el-input v-model.trim="form.standard" placeholder="请输入检测标准" />
				</el-form-item>
				<el-form-item label="检测设备" prop="equipment">
					<el-input v-model.trim="form.equipment" placeholder="请输入检测设备" />
				</el-form-item>
			</el-form>
		</div>
		<template #footer>
			<el-button type="primary" icon="Check" @click="handleSave">确 定</el-button>
			<el-button type="default" icon="Close" @click="handleCancel">取 消</el-button>
		</template>
	</el-dialog>
</template>
<script setup>
import { getInfo, save } from '@/api/business/inspectionInstruction'
const { proxy } = getCurrentInstance()
const emit = defineEmits(['handleSaveSuccess'])

/** 表单抽屉 页变量 */
const loading = ref(false)
const visible = ref(false)
const form = ref({})
const categoryFrom = ref({})
const rules = {
	standard: [{ required: true, message: '检测标准不能为空', trigger: 'blur' }],
}

/***********************  方法区  ****************************/
/** 打开对话框 */
const open = (data) => {
	reset()
	visible.value = true
	loading.value = true
	form.value.categoryId = data.categoryId
	getInfo(data.id || 0).then((res) => {
		if (res.data) {
			form.value = res.data
		}
		loading.value = false
	})
}

/** 表单重置 */
const reset = () => {
	form.value = {
		id: null,
		type: 2,
		categoryId: '0',
		standard: '',
		equipment: '',
	}
	proxy.resetForm('formRef')
}

/** 提交按钮 */
const handleSave = () => {
	proxy.$refs['formRef'].validate((valid) => {
		if (valid) {
			save(form.value).then((res) => {
				if (res.code === 601) {
					proxy.$modal.msgWarn(res.msg)
					return
				}
				if (res.code === 200) {
					proxy.$modal.msgSuccess('操作成功')
					emit('handleSaveSuccess')
				}
				visible.value = false
			})
		}
	})
}

/** 取消按钮 */
const handleCancel = () => {
	visible.value = false
	reset()
}

/** 暴露给父组件的方法 */
defineExpose({
	open,
})
</script>
