<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="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-item label="离岗原因" prop="DESCR">
        <el-input
          v-model="form.DESCR"
          type="textarea"
          :autosize="{ minRows: 3 }"
        />
      </el-form-item>
      <el-form-item label="审批人" prop="REVIEW_USER_ID">
        <el-select v-model="form.REVIEW_USER_ID">
          <el-option
            v-for="item in reviewUserList"
            :key="item.USER_ID"
            :value="item.USER_ID"
            :label="item.NAME"
          />
        </el-select>
      </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 {
  getApprovedByList,
  setLeaveEdit,
} from "@/request/off_duty_management.js";
import { useUserStore } from "@/pinia/user.js";

const userStore = useUserStore();
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" },
  ],
  DESCR: [{ required: true, message: "请输入离岗原因", trigger: "blur" }],
  REVIEW_USER_ID: [
    { required: true, message: "请选择审批人", trigger: "change" },
  ],
};
const reviewUserList = ref([]);
const formRef = ref(null);
const fnGetReviewUserList = async () => {
  const resData = await getApprovedByList({
    CORPINFO_ID: userStore.getUserInfo.CORPINFO_ID,
    DEPARTMENT_ID: userStore.getUserInfo.DEPARTMENT_ID,
    USER_ID: userStore.getUserInfo.USER_ID,
  });
  reviewUserList.value = resData.userList;
};
fnGetReviewUserList();
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    await setLeaveEdit({
      ...form.value,
      STARTTIME: form.value.offdutyDates[0],
      ENDTIME: form.value.offdutyDates[1],
    });
    ElMessage.success("提交成功");
    fnClose();
    emits("get-data");
  },
  { atBegin: true }
);
</script>

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