<template>
	<div class="system-blacklist-add-container">
		<el-dialog v-model="state.dialogVisible" width="500px" draggable :title="dialogTitle">
			<el-form :model="state.ruleForm" ref="evaluationKeyRef" label-width="80px" label-position="left">
				<el-row :gutter="10">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="评价名称" prop="satisName" :rules="[{ required: true, message: '请输入评价名称', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.satisName" placeholder="请输入评价名称" clearable></el-input>
						</el-form-item>
					</el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="评价值" prop="satisVal" :rules="[{ required: true, message: '请输入评价值', trigger: 'blur' }]">
              <el-input v-model="state.ruleForm.satisVal" placeholder="请输入评价值" clearable></el-input>
            </el-form-item>
          </el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="closeDialog" class="default-button">取 消</el-button>
					<el-button type="primary" @click="onSubmit(evaluationKeyRef)">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="EvaluationKeyAdd">
import {nextTick, reactive, ref} from 'vue';
import {ElMessage, FormInstance} from 'element-plus';
import {satisAdd,satisEdit} from '/@/api/public/wex';
import other from "/@/utils/other";

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

// 定义变量内容
const dialogTitle = ref<string>('新增评价按键');
const state = reactive<any>({
	ruleForm: {
    satisName: '',  // 评价名称
    satisVal: '', // 评价值
	},
	dialogVisible: false, // 弹窗显示隐藏
});
// 打开弹窗
const evaluationKeyRef = ref<RefType>();
const openDialog = async (row?:any) => {
	state.dialogVisible = true;
	emit('openDialog');
  await nextTick(() => {
    evaluationKeyRef.value?.resetFields();
    evaluationKeyRef.value?.clearValidate();
    if(row && row.id) {
      dialogTitle.value = '编辑评价按键';
      state.ruleForm = other.deepClone(row);
    }else{
      dialogTitle.value = '新增评价按键';
      state.ruleForm = {
        satisName: '',  // 评价名称
        satisVal: '', // 评价值
      };
    }
  });
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
	emit('closeDialog');
};
// 新增保存
const onSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid: boolean) => {
    if(!valid) return;
    const operation = state.ruleForm.id ? satisEdit : satisAdd; // 判断是新增还是编辑
    operation(state.ruleForm).then(() => {
        ElMessage.success('操作成功');
        emit('updateList');
        state.dialogVisible = false;
      });
	});
};
//暴漏变量和方法
defineExpose({ closeDialog, openDialog });
</script>
