<template>
  <el-dialog v-model="visible" :title="title" :on-close="fnClose">
    <el-form ref="formRef" :model="form" label-width="110px">
      <el-form-item label="标题" prop="biaoti">
        <el-input v-model="form.biaoti" maxlength="31" />
      </el-form-item>
      <el-form-item label="内容" prop="neirong">
        <el-input
          v-model="form.neirong"
          :autosize="{ minRows: 3 }"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="fnClose">关闭</el-button>
      <el-button type="primary" @click="fnSubmit">保存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { useVModels } from "@vueuse/core";
import { ref } from "vue";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { ElMessage } from "element-plus";
import { getCalendarAdd, getCalendarEdit } from "@/request/index.js";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
  title: {
    type: String,
    required: true,
    default: "",
  },
  type: {
    type: String,
    required: true,
    default: "",
  },
});
const emits = defineEmits(["update:visible", "update:form", "get-data"]);
const { visible, form } = useVModels(props, emits);
const formRef = ref(null);
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    props.type === "add"
      ? await getCalendarAdd({
          SCHEDULE_ID: form.value.SCHEDULE_ID,
          TITLE: form.value.biaoti,
          FHDESC: form.value.neirong,
          CDATA: form.value.noteDate,
        })
      : await getCalendarEdit({
          SCHEDULE_ID: form.value.SCHEDULE_ID,
          TITLE: form.value.biaoti,
          FHDESC: form.value.neirong,
          CDATA: form.value.noteDate,
        });
    ElMessage.success("保存成功");
    emits("get-data");
    fnClose();
  },
  { atBegin: true }
);
</script>

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