<template>
	<div class="device-category-edit-container">
		<el-dialog v-model="state.dialogVisible" width="600px" draggable title="编辑ivr分类">
			<el-form :model="state.ruleForm" label-width="100px" ref="ruleFormRef">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="ivr分类名称" prop="name" :rules="[{ required: true, message: '请输入分类名称', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.name" placeholder="请输入分类名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="24" :lg="24" :xl="24">
						<el-form-item label="备注" prop="remark">
							<el-input
								v-model="state.ruleForm.remark"
								type="textarea"
								:autosize="{ minRows: 4, maxRows: 6 }"
								placeholder="请输入备注"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="state.dialogVisible = false" class="default-button">取 消</el-button>
					<el-button type="primary" @click="onSubmit(ruleFormRef)">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="deviceCategoryEdit">
import { ref, reactive } from 'vue';
import {ElMessage, FormInstance} from 'element-plus';
import other from '/@/utils/other';
import { updateIvrCategory } from '/@/api/device/ivr';

// 定义接口来定义对象的类型
interface StateIvrCategory {
	ruleForm: {
		name: string;
		remark: string;
	};
	dialogVisible: boolean;
}

// 定义子组件向父组件传值/事件
const emit = defineEmits(['updateList', 'openDialog', 'closeDialog']);

// 定义变量内容
const ruleFormRef = ref<RefType>();
const state = reactive<StateIvrCategory>({
	ruleForm: {
		name: '',
		remark: '',
	},
	dialogVisible: false,
});
const openDialog = (row: any) => {
	ruleFormRef.value?.clearValidate();
	ruleFormRef.value?.resetFields();
	state.ruleForm = other.deepClone(row);
	state.dialogVisible = true;
	emit('openDialog');
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
	emit('closeDialog');
};
const onSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid: boolean) => {
    if (!valid) return;
    updateIvrCategory(state.ruleForm).then(() => {
      ElMessage.success('操作成功');
      emit('updateList');
      state.dialogVisible = false;
    });
  })
};
// 暴露变量
defineExpose({ closeDialog, openDialog });
</script>
