<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="ORGANIZATION_DEPARTMENT">
          <el-input
            v-model="data.form.ORGANIZATION_DEPARTMENT"
            maxlength="255"
            placeholder="这里输入培训组织部门"
          />
        </el-form-item>
        <el-form-item label="培训日期" prop="TRAINING_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_TIME">
          <el-time-picker
            v-model="data.form.TRAINING_TIME"
            value-format="HH:mm:ss"
            format="HH:mm:ss"
            clearable
            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="TRAINING_METHODS">
          <el-input
            v-model="data.form.TRAINING_METHODS"
            maxlength="255"
            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="ASSESSMENT_METHOD">
          <el-input
            v-model="data.form.ASSESSMENT_METHOD"
            maxlength="255"
            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_TEACHER">
          <el-input
            v-model="data.form.TRAINING_TEACHER"
            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 {
  getArchivesTrainingSchedule,
  addArchivesTrainingSchedule,
  editArchivesTrainingSchedule,
} from "@/request/archives.js";

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

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

const data = reactive({
  form: {
    TRAININGSCHEDULE_ID: undefined,
    ORGANIZATION_DEPARTMENT: undefined,
    TRAINING_DATE: undefined,
    TRAINING_TIME: undefined,
    TRAINING_CONTENT: undefined,
    TRAINING_METHODS: undefined,
    TRAINING_OBJECT: undefined,
    ASSESSMENT_METHOD: undefined,
    PLACE: undefined,
    TRAINING_TEACHER: undefined,
    FUND_GUARANTEE: undefined,
    QUALITY_ASSESSMENT: undefined,
    STUDYTASK_ID: studyTaskId.value,
  },
});

const rules = {};

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

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

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

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