<template>
  <el-dialog v-model="visible" title="取消申请" width="600" :on-close="fnClose">
    <el-form ref="formRef" :rules="rules" :model="form" label-width="110px">
      <el-form-item label="取消原因" prop="REVIEW_DESC">
        <el-input
          v-model="form.REVIEW_DESC"
          type="textarea"
          :autosize="{ minRows: 3 }"
        />
      </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 { setLeaveCancel } from "@/request/off_duty_management.js";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
});
const emits = defineEmits(["update:visible", "update:form", "get-data"]);
const { visible, form } = useVModels(props, emits);
const rules = {
  REVIEW_DESC: [{ required: true, message: "请输入取消原因", trigger: "blur" }],
};
const formRef = ref(null);
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    await setLeaveCancel({
      ...form.value,
    });
    ElMessage.success("提交成功");
    fnClose();
    emits("get-data");
  },
  { atBegin: true }
);
</script>

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