<template>
  <el-dialog
    v-model="dialogVisible"
    title="审核退回"
    :width="600"
    center
    align-center
    :close-on-click-modal="false"
    @closed="closed"
  >
    <el-form :model="formData" ref="formRef" label-width="100px" :rules="formRules">
      <el-form-item label="处理人：">
        <span>{{ username }}</span>
      </el-form-item>
      <el-form-item label="退回至：" prop="annotationBy">
        <span>{{ annotationName }}</span>
      </el-form-item>
      <el-form-item label="退回原因：" prop="refuseReason">
        <el-input v-model="formData.refuseReason" type="textarea" rows="4" :maxlength="200"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" class="ml30" v-throttle="handleSubmit">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, defineExpose, nextTick, computed } from "vue";
import { FormInstance, FormRules, ElMessage } from "element-plus";
import { useUserStore } from "@/stores/modules/user";
import { auditProcess } from "@/api/modules/annotation/audit";
import { AnnoAudit } from "@/api/interface";
import { AnnoStatusEnum } from "@/enums";

const userStore = useUserStore();
const username = computed(() => userStore.userInfo.name);
//* 接收注入
const dialogVisible = ref(false);
//* data
//* ref
const annotationName = ref("");
const formRef = ref<FormInstance>();
const openPromise = ref<any>({});
interface FormData extends AnnoAudit.ReqAuditProcessParams {
  annotationName: string;
}
const formData = ref<AnnoAudit.ReqAuditProcessParams>(getFormDefaultValue());
//* 方法

// 校验规则
let formRules: FormRules = {
  refuseReason: [{ required: true, message: "退回原因不能为空", trigger: "blur" }]
};

function getFormDefaultValue(): AnnoAudit.ReqAuditProcessParams {
  return {
    articleIds: [] as number[],
    auditStatus: AnnoStatusEnum.REJECTED,
    refuseReason: undefined
  };
}

function closed() {
  formData.value = getFormDefaultValue();
  nextTick(formRef.value?.clearValidate);
}
// 取消
function handleCancel() {
  dialogVisible.value = false;
}
//提交
function handleSubmit() {
  formRef.value?.validate(async valid => {
    if (!valid) return;
    await auditProcess(formData.value);
    ElMessage.success("操作成功");
    openPromise.value.resolve();
    dialogVisible.value = false;
  });
}

function open(params: FormData) {
  formData.value.articleIds = params.articleIds;
  annotationName.value = params.annotationName;
  dialogVisible.value = true;
  return new Promise(resolve => {
    openPromise.value.resolve = resolve;
  });
}
defineExpose({
  open
});
</script>

<style lang="scss"></style>
