<template>
  <div>
    <el-dialog
      v-if="visible"
      :model-value="visible"
      :title="type === 'add' ? '新增' : '修改'"
      :before-close="fnClose"
    >
      <el-form
        ref="formRef"
        :model="data.form"
        :rules="rules"
        label-width="150px"
      >
        <el-form-item label="培训时间" prop="DATE">
          <el-date-picker
            v-model="data.form.TRAINING_DATE"
            value-format="YYYY-MM-DD"
            format="YYYY-MM-DD"
            type="date"
            clearable
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="培训对象" prop="TRAINING_OBJECT">
          <el-input
            v-model="data.form.TRAINING_OBJECT"
            maxlength="255"
            placeholder="这里输入培训对象"
          />
        </el-form-item>
        <el-form-item label="培训类型" prop="TRAINING_TYPE">
          <el-input
            v-model="data.form.TRAINING_TYPE"
            maxlength="255"
            placeholder="这里输入培训类型"
          />
        </el-form-item>
        <el-form-item label="培训内容" prop="TRAINING_CONTENT">
          <el-input
            v-model="data.form.TRAINING_CONTENT"
            maxlength="255"
            placeholder="这里输入培训内容"
          />
        </el-form-item>
        <el-form-item label="参加人数" prop="PERSON_NUMBER">
          <el-input
            v-model="data.form.PERSON_NUMBER"
            maxlength="11"
            placeholder="这里输入参加人数"
          />
        </el-form-item>
        <el-form-item label="培训学时" prop="CLASS_HOURS">
          <el-input
            v-model="data.form.CLASS_HOURS"
            maxlength="11"
            placeholder="这里输入培训学时"
          />
        </el-form-item>
        <el-form-item label="培训地点" prop="TRAINING_PLACE">
          <el-input
            v-model="data.form.TRAINING_PLACE"
            maxlength="255"
            placeholder="这里输入培训地点"
          />
        </el-form-item>
        <el-form-item label="培训教师" prop="TRAINING_TEACHER">
          <el-input
            v-model="data.form.TRAINING_TEACHER"
            maxlength="255"
            placeholder="这里输入培训教师"
          />
        </el-form-item>
        <el-form-item label="考核方式" prop="ASSESSMENT_METHOD">
          <el-input
            v-model="data.form.ASSESSMENT_METHOD"
            maxlength="255"
            placeholder="这里输入考核方式"
          />
        </el-form-item>
        <el-form-item label="汇总考核情况" prop="ASSESSMENT">
          <el-input
            v-model="data.form.ASSESSMENT"
            maxlength="255"
            placeholder="这里输入汇总考核情况"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="fnClose">取消</el-button>
        <el-button type="primary" @click="fnSubmit"> 确定 </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { useVModels } from "@vueuse/core";
import {
  getArchivesEdumanager,
  addArchivesEdumanager,
  editArchivesEdumanager,
} from "@/request/archives.js";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  },
  year: {
    type: String,
    required: true,
  },
  type: {
    type: String,
    required: true,
  },
  ARCHIVESEDUMANAGER_ID: {
    type: String,
    required: false,
    default: "",
  },
});
const { visible, year, type, ARCHIVESEDUMANAGER_ID } = useVModels(props);

const emits = defineEmits(["update:visible", "getData"]);

const data = reactive({
  form: {
    ARCHIVESEDUMANAGER_ID: undefined,
    TRAINING_DATE: undefined,
    TRAINING_OBJECT: undefined,
    TRAINING_TYPE: undefined,
    TRAINING_CONTENT: undefined,
    PERSON_NUMBER: undefined,
    CLASS_HOURS: undefined,
    TRAINING_PLACE: undefined,
    TRAINING_TEACHER: undefined,
    ASSESSMENT_METHOD: undefined,
    ASSESSMENT: undefined,
    YEAR: year.value,
  },
});

const rules = {
  PERSON_NUMBER: [
    {
      type: "number",
      message: "请输入有效数字",
      trigger: "blur",
      transform: (value) => Number(value),
    },
  ],
  CLASS_HOURS: [
    {
      type: "number",
      message: "请输入有效数字",
      trigger: "blur",
      transform: (value) => Number(value),
    },
  ],
};

const formRef = ref(null);

const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    const params = Object.assign({}, data.form);
    let respData;
    if (type.value === "add") {
      respData = await addArchivesEdumanager(params);
    } else if (type.value === "edit") {
      respData = await editArchivesEdumanager(params);
    }
    if (respData && respData.result === "success") {
      emits("getData");
      fnClose();
    }
  },
  { atBegin: true }
);

const fnClose = () => {
  emits("update:visible", false);
};

const fnGetData = async () => {
  if (
    ARCHIVESEDUMANAGER_ID &&
    ARCHIVESEDUMANAGER_ID.value &&
    ARCHIVESEDUMANAGER_ID.value.length > 0
  ) {
    const respData = await getArchivesEdumanager({
      ARCHIVESEDUMANAGER_ID: ARCHIVESEDUMANAGER_ID.value,
    });
    if (respData && respData.pd) {
      data.form = respData.pd;
    }
  }
};
await fnGetData();
</script>

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