<script setup lang="ts">
import { reactive, ref, onMounted, computed, watch } from "vue";
import { useQuasar } from "quasar";

import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import { useLeaveStore } from "@/modules/05_leave/store";

import type { OrdinationForm } from "@/modules/05_leave/interface/request/AddAbsence";
import type {
  DocumentLists,
  DataOptions,
} from "@/modules/05_leave/interface/index/main";

/** Use */
const $q = useQuasar();
const dataStore = useLeaveStore();
const mixin = useCounterMixin();
const { date2Thai, messageError, convertDateToAPI } = mixin;
const edit = ref<boolean>(true);
const leaveId = ref<string>("");
/** รับ props มาจากหน้าหลัก */
const props = defineProps({
  data: {
    type: Object,
    default: null,
  },
  onSubmit: {
    type: Function,
    default: () => "",
  },
  onConfirm: {
    type: Function,
    default: () => "",
  },
  clickDelete: {
    type: Function,
    default: () => "",
  },
});

/** ตัวแปร ref สำหรับแสดง validate */
const ordainDayLocationNumberRef = ref<object | null>(null);
const leaveWroteRef = ref<object | null>(null);
const leavebirthDateRef = ref<object | null>(null);
const leavegovernmentDateRef = ref<object | null>(null);
const leaveTotalRef = ref<object | null>(null);
const leaveEndDateRef = ref<object | null>(null);
const ordainDayOrdinationRef = ref<object | null>(null);
const leaveStartDateRef = ref<object | null>(null);
const ordainDayLocationAddressRef = ref<object | null>(null);
const ordainDayBuddhistLentAddressRef = ref<object | null>(null);
const ordainDayLocationNameRef = ref<object | null>(null);
const ordainDayBuddhistLentNameRef = ref<object | null>(null);
const leaveDocumentRef = ref<object | null>(null);

/** ข้อมูล v-model ของฟอร์ม */
const formDataOrdination = reactive<any>({
  type: dataStore.typeId,
  leaveWrote: "",
  leavegovernmentDate: null,
  leavebirthDate: null,
  leaveStartDate: null,
  leaveEndDate: null,
  leaveTotal: 0,
  ordainDayOrdination: null,
  ordainDayLocationName: "",
  ordainDayLocationNumber: null,
  ordainDayLocationAddress: "",
  ordainDayBuddhistLentName: "",
  ordainDayBuddhistLentAddress: "",
  ordainDayStatus: true,
  leaveDocument: [],
  leaveDetail: "",
  leaveRange: "ALL",
  leaveRangeEnd: "ALL",
});

/** maping ref เข้าตัวแปรเพื่อเตรียมตรวจสอบ */
const formRef: OrdinationForm = {
  leaveWrote: leaveWroteRef,
  leavegovernmentDate: leavegovernmentDateRef,
  leavebirthDate: leavebirthDateRef,
  leaveStartDate: leaveStartDateRef,
  leaveEndDate: leaveEndDateRef,
  leaveTotal: leaveTotalRef,
  ordainDayOrdination: ordainDayOrdinationRef,
  ordainDayLocationName: ordainDayLocationNameRef,
  ordainDayLocationNumber: ordainDayLocationNumberRef,
  ordainDayLocationAddress: ordainDayLocationAddressRef,
  ordainDayBuddhistLentName: ordainDayBuddhistLentNameRef,
  ordainDayBuddhistLentAddress: ordainDayBuddhistLentAddressRef,
  leaveDocument: leaveDocumentRef,
};

/** ส่วนของการประกาศและเลือกไฟล์เอกสารประกอบ */
const fileDocDataUpload = ref<File[]>([]);
const fileUploadDoc = async (files: File[]) => {
  files.forEach((file: File) => {
    fileDocDataUpload.value.push(file);
  });
};

/** ฟังก์ชั่นตรวจสอบความถูกต้องก่อน บันทึก */
function onValidate() {
  const hasError = [];
  for (const key in formRef) {
    if (Object.prototype.hasOwnProperty.call(formRef, key)) {
      const property = formRef[key];
      if (property.value && typeof property.value.validate === "function") {
        const isValid = property.value.validate();
        hasError.push(isValid);
      }
    }
  }

  if (hasError.every((result) => result === true)) {
    const formData = new FormData();

    if (formDataOrdination.leaveDocument) {
      formDataOrdination.leaveDocument.forEach((file: File) => {
        formData.append("leaveDocument", file);
      });
    }
    // formData.append("leaveDocument", formDataOrdination.leaveDocument);
    formData.append("type", formDataOrdination.type);
    formData.append(
      "leaveStartDate",
      convertDateToAPI(formDataOrdination.leaveStartDate) ?? ""
    );
    formData.append(
      "leaveEndDate",
      convertDateToAPI(formDataOrdination.leaveEndDate) ?? ""
    );
    formData.append(
      "ordainDayOrdination",
      convertDateToAPI(formDataOrdination.ordainDayOrdination) ?? ""
    );
    formData.append(
      "ordainDayLocationName",
      formDataOrdination.ordainDayLocationName
    );
    formData.append(
      "ordainDayLocationNumber",
      formDataOrdination.ordainDayLocationNumber
    );
    formData.append(
      "ordainDayLocationAddress",
      formDataOrdination.ordainDayLocationAddress
    );
    formData.append(
      "ordainDayBuddhistLentName",
      formDataOrdination.ordainDayBuddhistLentName
    );
    formData.append(
      "ordainDayBuddhistLentAddress",
      formDataOrdination.ordainDayBuddhistLentAddress
    );
    formData.append("ordainDayStatus", formDataOrdination.ordainDayStatus);
    formData.append("leaveWrote", formDataOrdination.leaveWrote);
    formData.append("leaveDetail", formDataOrdination.leaveDetail);
    formData.append("leaveTotal", formDataOrdination.leaveTotal);
    formData.append("leaveRange", formDataOrdination.leaveRange);
    formData.append("leaveRangeEnd", formDataOrdination.leaveRangeEnd);
    formData.append("hajj", "false");
    props.onSubmit(formData, isLeave.value);
  }
}

const isLeave = ref<boolean>(true);
const reasonLeave = ref<string>("");

const checkDate = computed(() => {
  if (
    convertDateToAPI(formDataOrdination.leaveEndDate) ==
    convertDateToAPI(formDataOrdination.leaveStartDate)
  ) {
    return true;
  } else {
    return false;
  }
});

const isLoadCheck = ref<boolean>(false);

/** check ว่าลาได้ไหม จาก api*/
async function fetchCheck() {
  if (formDataOrdination.leaveStartDate && formDataOrdination.leaveEndDate) {
    isLoadCheck.value = true;
    await http
      .post(config.API.leaveCheck(), {
        type: dataStore.typeId ?? null,
        StartLeaveDate:
          convertDateToAPI(formDataOrdination.leaveStartDate) ?? null,
        EndLeaveDate: convertDateToAPI(formDataOrdination.leaveEndDate) ?? null,
        leaveRange: formDataOrdination.leaveRange,
        leaveRangeEnd: checkDate.value
          ? formDataOrdination.leaveRange
          : formDataOrdination.leaveRangeEnd,
      })
      .then((res) => {
        const data = res.data.result;
        formDataOrdination.leaveTotal = data.totalDate;
        reasonLeave.value =
          data.message != ""
            ? data.message
            : formDataOrdination.leaveTotal > 0
            ? "จำนวนวันลาเกินที่กำหนด"
            : "ช่วงวันลาที่ระบุไม่ถูกต้อง";
        isLeave.value =
          formDataOrdination.leaveTotal > 0 ? data.isLeave : false;
      })
      .catch((e) => {
        messageError($q, e);
      })
      .finally(() => {
        isLoadCheck.value = false;
      });
  }
}

/** แจ้งเมื่อวันลาไม่ถูกต้อง */
const dateEndInputStyle = computed(() => {
  return !isLeave.value ? "input-alert" : "";
});

/** ตรวจสอบว่ามีการส่งข้อมูลเข้ามาที่ฟอร์มไหม เมื่อมีการส่งจะ map ข้อมูลเข้า v-model ของฟอร์ม */
const leaveDocumentList = ref<DocumentLists[]>([]);
const statusCheck = ref<string>("");

watch(
  () => props.data,
  async () => {
    if (props.data) {
      // totalCheck.value = null;
      formDataOrdination.leaveWrote = props.data.leaveWrote;
      formDataOrdination.leaveStartDate = props.data.leaveStartDate;
      formDataOrdination.leaveEndDate = props.data.leaveEndDate;
      formDataOrdination.leaveTotal = props.data.leaveTotal;
      formDataOrdination.ordainDayOrdination = props.data.ordainDayOrdination;
      formDataOrdination.ordainDayLocationName =
        props.data.ordainDayLocationName;
      formDataOrdination.ordainDayLocationAddress =
        props.data.ordainDayLocationAddress;
      formDataOrdination.ordainDayBuddhistLentName =
        props.data.ordainDayBuddhistLentName;
      formDataOrdination.ordainDayBuddhistLentAddress =
        props.data.ordainDayBuddhistLentAddress;
      formDataOrdination.ordainDayStatus = props.data.ordainDayStatus;
      formDataOrdination.leaveDetail = props.data.leaveDetail;
      leaveDocumentList.value = props.data.leaveDocument;
      formDataOrdination.ordainDayLocationNumber =
        props.data.ordainDayLocationNumber;
      formDataOrdination.leaveRange = props.data.leaveRange;
      formDataOrdination.leaveRangeEnd = props.data.leaveRangeEnd;
      statusCheck.value = props.data.status;
      formDataOrdination.leaveDocument = [];
    }
  },
  { deep: true }
);

/** Hook */
onMounted(async () => {
  if (props.data) {
    // totalCheck.value = null;
    formDataOrdination.leaveWrote = props.data.leaveWrote;
    formDataOrdination.leaveStartDate = props.data.leaveStartDate;
    formDataOrdination.leaveEndDate = props.data.leaveEndDate;
    formDataOrdination.leaveTotal = props.data.leaveTotal;
    formDataOrdination.ordainDayOrdination = props.data.ordainDayOrdination;
    formDataOrdination.ordainDayLocationName = props.data.ordainDayLocationName;

    formDataOrdination.ordainDayLocationAddress =
      props.data.ordainDayLocationAddress;
    formDataOrdination.ordainDayBuddhistLentName =
      props.data.ordainDayBuddhistLentName;
    formDataOrdination.ordainDayBuddhistLentAddress =
      props.data.ordainDayBuddhistLentAddress;
    formDataOrdination.ordainDayStatus = props.data.ordainDayStatus;
    formDataOrdination.leaveDetail = props.data.leaveDetail;

    formDataOrdination.ordainDayStatus = props.data.ordainDayStatus;
    formDataOrdination.ordainDayLocationNumber =
      props.data.ordainDayLocationNumber;
    formDataOrdination.leaveRange = props.data.leaveRange;
    formDataOrdination.leaveRangeEnd = props.data.leaveRangeEnd;

    formDataOrdination.ordainDayLocationNumber =
      props.data.ordainDayLocationNumber;
    formDataOrdination.ordainDayStatus = props.data.ordainDayStatus;

    leaveDocumentList.value = props.data.leaveDocument;
    statusCheck.value = props.data.status;
    leaveId.value = props.data.id;
  }
});
</script>

<!-- ฟอร์ม ลาอุปสมบท -->
<template>
  <div style="display: flex; align-items: center">
    <q-icon name="mdi-numeric-3-circle" size="20px" color="primary" />
    <div class="q-pl-sm text-weight-bold text-dark">กรอกข้อมูล</div>
  </div>
  <form @submit.prevent="onValidate" class="full-width">
    <q-card bordered class="q-pa-md bg-grey-1">
      <div class="row q-pa-sm q-col-gutter-sm">
        <div class="col-12 inputgreen">
          <q-input
            ref="leaveWroteRef"
            dense
            outlined
            bg-color="white"
            hide-bottom-space
            v-model="formDataOrdination.leaveWrote"
            label="เขียนที่"
            :readonly="!edit || statusCheck === 'NEW'"
            :rules="[(val:string) => !!val || `${'เขียนที่'}`]"
          />
        </div>

        <div class="col-12">
          <div class="row q-col-gutter-sm">
            <div class="col-12 col-md-3 col-sm-6 inputgreen">
              <datepicker
                v-model="formDataOrdination.leaveStartDate"
                :locale="'th'"
                autoApply
                hide-bottom-space
                borderless
                :enableTimePicker="false"
                week-start="0"
                :readonly="!edit || statusCheck === 'NEW'"
                @update:model-value="
                  (formDataOrdination.leaveEndDate = null), fetchCheck()
                "
              >
                <template #year="{ year }">
                  {{ year + 543 }}
                </template>
                <template #year-overlay-value="{ value }">
                  {{ parseInt(value + 543) }}
                </template>
                <template #trigger>
                  <q-input
                    outlined
                    dense
                    ref="leaveStartDateRef"
                    for="leaveStartDateRef"
                    hide-bottom-space
                    bg-color="white"
                    :readonly="!edit || statusCheck === 'NEW'"
                    class="full-width datepicker"
                    :model-value="
                      formDataOrdination.leaveStartDate != null
                        ? date2Thai(formDataOrdination.leaveStartDate)
                        : null
                    "
                    :label="`${'วันที่เริ่มต้น'}`"
                    :rules="[(val:string) => !!val || `${'กรุณาเลือกวันที่เริ่มต้น'}`]"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        style="color: var(--q-primary)"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>

            <div class="col-12 col-md-3 col-sm-6 inputgreen">
              <q-select
                outlined
                dense
                bg-color="white"
                v-model="formDataOrdination.leaveRange"
                :options="dataStore.rangeOptions"
                emit-value
                map-options
                option-value="id"
                option-label="name"
                @update:model-value="fetchCheck()"
              >
                <template v-slot:prepend>
                  <q-icon
                    name="mdi-clock-outline"
                    style="color: var(--q-primary)"
                  />
                </template>
              </q-select>
            </div>
          </div>
        </div>

        <div class="col-12">
          <div class="row q-col-gutter-sm">
            <div class="col-12 col-md-3 col-sm-6 inputgreen">
              <datepicker
                v-model="formDataOrdination.leaveEndDate"
                :locale="'th'"
                autoApply
                hide-bottom-space
                @update:model-value="fetchCheck()"
                borderless
                :enableTimePicker="false"
                week-start="0"
                :min-date="formDataOrdination.leaveStartDate"
              >
                <template #year="{ year }">
                  {{ year + 543 }}
                </template>
                <template #year-overlay-value="{ value }">
                  {{ parseInt(value + 543) }}
                </template>
                <template #trigger>
                  <q-input
                    outlined
                    dense
                    ref="leaveEndDateRef"
                    for="leaveEndDateRef"
                    hide-bottom-space
                    bg-color="white"
                    class="full-width datepicker"
                    :model-value="
                      formDataOrdination.leaveEndDate != null
                        ? date2Thai(formDataOrdination.leaveEndDate)
                        : null
                    "
                    :label="`${'วันที่สิ้นสุด'}`"
                    :rules="[(val:string) => !!val || `${'กรุณาเลือกวันที่สิ้นสุด'}`]"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        style="color: var(--q-primary)"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>

            <div class="col-12 col-md-3 col-sm-6 inputgreen">
              <q-select
                v-if="!checkDate"
                outlined
                dense
                bg-color="white"
                v-model="formDataOrdination.leaveRangeEnd"
                :options="dataStore.rangeOptions.filter((items:DataOptions)=> items.id !== 'AFTERNOON')"
                emit-value
                map-options
                option-value="id"
                option-label="name"
                @update:model-value="fetchCheck()"
              >
                <template v-slot:prepend>
                  <q-icon
                    name="mdi-clock-outline"
                    style="color: var(--q-primary)"
                  />
                </template>
              </q-select>
            </div>
          </div>
        </div>

        <div class="full-width">
          <div class="q-col-gutter-sm row">
            <div class="col-12 col-md-3 col-sm-6">
              <q-input
                :bottom-slots="!isLeave ? true : false"
                :color="!isLeave ? 'red' : 'black'"
                :bg-color="!isLeave ? 'red-2' : 'white'"
                :border-color="!isLeave ? 'red' : 'gray'"
                :input-class="!isLeave ? dateEndInputStyle : ''"
                dense
                outlined
                ref="leaveTotalRef"
                for="leaveTotalRef"
                v-model="formDataOrdination.leaveTotal"
                label="จำนวนวันที่ลา (วัน)"
                readonly
                :loading="isLoadCheck"
              >
                <template v-slot:hint>
                  <span style="color: red">
                    {{ !isLeave ? reasonLeave : "" }}
                  </span>
                </template>
              </q-input>
            </div>

            <div class="col-12 col-md-3 col-sm-6">
              <datepicker
                v-model="formDataOrdination.leavegovernmentDate"
                :locale="'th'"
                autoApply
                borderless
                :enableTimePicker="false"
                week-start="0"
                readonly
              >
                <template #year="{ year }">
                  {{ year + 543 }}
                </template>
                <template #year-overlay-value="{ value }">
                  {{ parseInt(value + 543) }}
                </template>
                <template #trigger>
                  <q-input
                    outlined
                    ref="leavegovernmentDateRef"
                    dense
                    bg-color="white"
                    hide-bottom-space
                    readonly
                    class="full-width"
                    :model-value="
                      dataStore.dateAppoint != null
                        ? date2Thai(dataStore.dateAppoint)
                        : null
                    "
                    :label="`${'วันที่เข้ารับราชการ'}`"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        style="color: var(--black)"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>

            <div class="col-12 col-md-3 col-sm-6">
              <datepicker
                v-model="formDataOrdination.leavebirthDate"
                :locale="'th'"
                autoApply
                borderless
                readonly
                :enableTimePicker="false"
                week-start="0"
              >
                <template #year="{ year }">
                  {{ year + 543 }}
                </template>
                <template #year-overlay-value="{ value }">
                  {{ parseInt(value + 543) }}
                </template>
                <template #trigger>
                  <q-input
                    outlined
                    ref="leavebirthDateRef"
                    dense
                    bg-color="white"
                    hide-bottom-space
                    readonly
                    class="full-width"
                    :model-value="
                      dataStore.birthDate != null
                        ? date2Thai(dataStore.birthDate)
                        : null
                    "
                    :label="`${'วันเดือนปีเกิด'}`"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        style="color: var(--black)"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>
          </div>
        </div>

        <div class="q-pl-sm text-weight-bold text-dark col-12">
          เคยบวชหรือไม่
        </div>
        <div class="col-12">
          <q-radio
            v-model="formDataOrdination.ordainDayStatus"
            :disable="!edit || statusCheck === 'NEW'"
            :val="true"
            label="เคยบวช"
            checked-icon="task_alt "
            hide-bottom-space
          />
          <q-radio
            v-model="formDataOrdination.ordainDayStatus"
            :disable="!edit || statusCheck === 'NEW'"
            :val="false"
            label="ไม่เคยบวช"
            checked-icon="task_alt"
            hide-bottom-space
          />
        </div>

        <div class="text-weight-bold text-dark col-12">สถานที่บวช</div>
        <div class="col-12 col-md-4 col-sm-6 inputgreen">
          <datepicker
            v-model="formDataOrdination.ordainDayOrdination"
            :locale="'th'"
            autoApply
            full-width
            borderless
            :readonly="!edit || statusCheck === 'NEW'"
            hide-bottom-space
            :enableTimePicker="false"
            week-start="0"
          >
            <template #year="{ year }">
              {{ year + 543 }}
            </template>
            <template #year-overlay-value="{ value }">
              {{ parseInt(value + 543) }}
            </template>
            <template #trigger>
              <q-input
                outlined
                ref="ordainDayOrdinationRef"
                dense
                bg-color="white"
                hide-bottom-space
                :readonly="!edit || statusCheck === 'NEW'"
                class="full-width datepicker"
                :model-value="
                  formDataOrdination.ordainDayOrdination != null
                    ? date2Thai(formDataOrdination.ordainDayOrdination)
                    : null
                "
                :label="`${'วันอุปสมบท'}`"
                :rules="[(val:string) => !!val || `${'กรุณาเลือกวันอุปสมบท'}`]"
              >
                <template v-slot:prepend>
                  <q-icon
                    name="event"
                    class="cursor-pointer"
                    style="color: var(--q-primary)"
                  >
                  </q-icon>
                </template>
              </q-input>
            </template>
          </datepicker>
        </div>

        <div class="col-12 col-md-4 col-sm-6 inputgreen">
          <q-input
            dense
            hide-bottom-space
            ref="ordainDayLocationNameRef"
            bg-color="white"
            outlined
            full-width
            :readonly="!edit || statusCheck === 'NEW'"
            v-model="formDataOrdination.ordainDayLocationName"
            label="ชื่อวัด"
            :rules="[(val:string) => !!val || `${'กรุณากรอกชื่อวัด'}`]"
          />
        </div>

        <div class="col-12 col-md-4 col-sm-12 inputgreen">
          <q-input
            ref="ordainDayLocationNumberRef"
            dense
            full-width
            outlined
            v-model="formDataOrdination.ordainDayLocationNumber"
            bg-color="white"
            mask="(###)-###-####"
            lazy-rules
            unmasked-value
            :readonly="!edit || statusCheck === 'NEW'"
            hide-bottom-space
            label="หมายเลขโทรศัพท์"
            :rules="[(val:string) => !!val || `${'กรุณากรอกหมายเลขโทรศัพท์'}`]"
          />
        </div>

        <div class="col-12 inputgreen">
          <q-input
            dense
            ref="ordainDayLocationAddressRef"
            bg-color="white"
            outlined
            hide-bottom-space
            v-model="formDataOrdination.ordainDayLocationAddress"
            label="ที่อยู่"
            :readonly="!edit || statusCheck === 'NEW'"
            :rules="[(val:string) => !!val || `${'กรุณากรอกที่อยู่'}`]"
            type="textarea"
          />
        </div>

        <div class="q-pl-sm text-weight-bold text-dark col-12">
          สถานที่จำพรรษา
        </div>

        <div class="col-12 col-md-6 col-sm-12 inputgreen">
          <q-input
            dense
            ref="ordainDayBuddhistLentNameRef"
            bg-color="white"
            hide-bottom-space
            outlined
            :readonly="!edit || statusCheck === 'NEW'"
            v-model="formDataOrdination.ordainDayBuddhistLentName"
            label="ชื่อวัด"
            :rules="[(val:string) => !!val || `${'กรุณากรอกชื่อวัด'}`]"
          />
        </div>

        <div class="col-12 inputgreen">
          <q-input
            dense
            ref="ordainDayBuddhistLentAddressRef"
            bg-color="white"
            outlined
            :readonly="!edit || statusCheck === 'NEW'"
            v-model="formDataOrdination.ordainDayBuddhistLentAddress"
            label="ที่อยู่"
            :rules="[(val:string) => !!val || `${'กรุณากรอกที่อยู่'}`]"
            type="textarea"
            hide-bottom-space
          />
        </div>

        <div class="col-12 inputgreen">
          <q-input
            type="textarea"
            dense
            bg-color="white"
            outlined
            ref="leaveDetailRef"
            for="leaveDetailRef"
            v-model="formDataOrdination.leaveDetail"
            label="สาเหตุการลา"
            :readonly="!edit || statusCheck === 'NEW'"
          />
        </div>

        <div class="full-width" v-if="statusCheck != 'NEW'">
          <div class="q-col-gutter-sm row">
            <!-- multiple -->
            <div class="col-12 q-pl-sm col-12">
              <q-file
                ref="leaveDocumentRef"
                v-model="formDataOrdination.leaveDocument"
                @added="fileUploadDoc"
                dense
                label="เอกสารประกอบ"
                multiple
                outlined
                bg-color="white"
                use-chips
              >
                <template v-slot:prepend>
                  <q-icon name="attach_file" color="primary" />
                </template>
              </q-file>
            </div>
          </div>
        </div>
        <div class="col-12" v-if="data">
          <div class="col-12 col-md-12 col-sm-12">
            <q-card>
              <q-list
                separator
                v-if="leaveDocumentList && leaveDocumentList.length > 0"
              >
                <q-item
                  v-for="(document, index) in leaveDocumentList"
                  :key="index"
                  class="q-my-xs"
                >
                  <q-item-section>
                    <q-item-label class="row items-baseline">
                      <div class="col">
                        {{ "เอกสารแนบที่ " + (index + 1) }}
                      </div>
                      <div>
                        <q-btn
                          :href="document.path"
                          target="_blank"
                          outline
                          flat
                          dense
                          color="blue"
                          icon="mdi-download"
                          size="12px"
                          class="q-mr-md"
                        >
                          <q-tooltip>ดาวน์โหลดไฟล์</q-tooltip>
                        </q-btn>
                      </div>
                      <div>
                        <q-btn
                          @click="clickDelete(leaveId, document.docId)"
                          target="_blank"
                          outline
                          color="red"
                          flat
                          dense
                          icon="delete"
                          size="12px"
                        >
                          <q-tooltip>ลบไฟล์</q-tooltip>
                        </q-btn>
                      </div>
                    </q-item-label>
                  </q-item-section>
                </q-item>
              </q-list>
            </q-card>
          </div>
        </div>
      </div>
    </q-card>

    <q-separator class="q-mt-sm" />

    <div class="row col-12 q-pt-md">
      <q-space />
      <!-- <div v-if="totalCheck == 0" class="text-red q-mr-sm">**จำนวนวันลาของท่านไม่ถูกต้อง หรือวันที่ยื่นลาของท่านตรงกับวันหยุด</div> -->
      <q-btn
        v-if="!props.data || props.data.status == 'DRAFT'"
        id="onSubmit"
        type="submit"
        unelevated
        class="q-px-md items-center btnBlue"
        label="บันทึก"
        :disable="!isLeave"
        :loading="isLoadCheck"
      >
        <q-tooltip>บันทึกแบบร่าง</q-tooltip>
      </q-btn>
      <q-btn
        v-if="data && statusCheck != 'NEW'"
        id="onSubmit"
        type="button"
        unelevated
        class="q-px-md items-center q-ml-sm"
        color="primary"
        label="ยื่นใบลา"
        @click="onConfirm()"
        :loading="isLoadCheck"
      >
        <q-tooltip>ยื่นใบลา</q-tooltip>
      </q-btn>
    </div>
  </form>
</template>
