<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="START_DATE">
          <el-date-picker
            v-model="data.form.START_DATE"
            value-format="YYYY-MM-DD"
            format="YYYY-MM-DD"
            type="date"
            clearable
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="结束时间" prop="END_DATE">
          <el-date-picker
            v-model="data.form.END_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_MATERIALS">
          <el-input
            v-model="data.form.TRAINING_MATERIALS"
            maxlength="255"
            placeholder="这里输入培训教材"
          />
        </el-form-item>
        <el-form-item label="培训方式" prop="TRAINING_METHODS">
          <el-input
            v-model="data.form.TRAINING_METHODS"
            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="CLASS_HOURS">
          <el-input
            v-model="data.form.CLASS_HOURS"
            maxlength="11"
            placeholder="这里输入学时"
          />
        </el-form-item>
        <el-form-item label="地点" prop="PLACE">
          <el-input
            v-model="data.form.PLACE"
            maxlength="255"
            placeholder="这里输入地点"
          />
        </el-form-item>
        <el-form-item label="培训教师" prop="TRAINING_TEACHERS">
          <el-input
            v-model="data.form.TRAINING_TEACHERS"
            maxlength="255"
            placeholder="这里输入培训教师"
          />
        </el-form-item>
        <el-form-item label="经费保障" prop="FUND_GUARANTEE">
          <el-input
            v-model="data.form.FUND_GUARANTEE"
            maxlength="255"
            placeholder="这里输入经费保障"
          />
        </el-form-item>
        <el-form-item label="质量评估" prop="QUALITY_ASSESSMENT">
          <el-input
            v-model="data.form.QUALITY_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 {
  getArchivesEduplan,
  addArchivesEduplan,
  editArchivesEduplan,
} from "@/request/archives.js";

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

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

const data = reactive({
  form: {
    ARCHIVESEDUPLAN_ID: undefined,
    START_DATE: undefined,
    END_DATE: undefined,
    TRAINING_OBJECT: undefined,
    TRAINING_MATERIALS: undefined,
    TRAINING_METHODS: undefined,
    ASSESSMENT_METHOD: undefined,
    CLASS_HOURS: undefined,
    PLACE: undefined,
    TRAINING_TEACHERS: undefined,
    FUND_GUARANTEE: undefined,
    QUALITY_ASSESSMENT: undefined,
    YEAR: year.value,
  },
});

const rules = {
  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 addArchivesEduplan(params);
    } else if (type.value === "edit") {
      respData = await editArchivesEduplan(params);
    }
    if (respData && respData.result === "success") {
      emits("getData");
      fnClose();
    }
  },
  { atBegin: true }
);

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

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

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