<template>
  <el-dialog
    v-model="visible"
    title="离岗时间调整"
    :on-close="fnClose"
    width="600"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="110px">
      <el-form-item label="离岗申请时间" prop="offdutyDates">
        <el-date-picker
          v-model="form.offdutyDates"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
          format="YYYY-MM-DD"
        />
      </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 { ElMessage } from "element-plus";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { setLeaveEdit } 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 = {
  offdutyDates: [
    { required: true, message: "请选择离岗申请时间", trigger: "change" },
  ],
};
const formRef = ref(null);
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    await setLeaveEdit({
      OFFDUTY_ID: form.value.OFFDUTY_ID,
      STARTTIME: form.value.offdutyDates[0],
      ENDTIME: form.value.offdutyDates[1],
    });
    fnClose();
    ElMessage.success("提交成功");
    emits("get-data");
  },
  { atBegin: true }
);
</script>

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