<script setup lang="ts">
import { onMounted, ref, useAttrs } from "vue";
import { useQuasar } from "quasar";
import { useCounterMixin } from "@/stores/mixin";
import type { Pagination } from "@/modules/03_recruiting/interface/index/Main";
import { useSpecialTimeStore } from "@/modules/09_leave/stores/SpecialTimeStore";
import DialogReason from "@/components/Dialogs/PopupReason.vue";
import DialogApprove from "@/modules/09_leave/components/4_specialTime/DialogApprove.vue";

import { useRouter } from "vue-router";
const dataSpecialTime = useSpecialTimeStore();
const { fecthList } = dataSpecialTime;
const $q = useQuasar(); // show dialog
const mixin = useCounterMixin();
const router = useRouter();
const { hideLoader, monthYear2Thai } = mixin;
const modalUnapprove = ref(false);
const modalApprove = ref(false);
const detailData = ref<any>();
const editCheck = ref<string>("");
const pagination = ref({
  // sortBy: "desc",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});
const dialogTitle = ref<string>("");
const dialogDesc = ref<string>("");
const name = ref<string>("");
const dateDialog = ref<string>("");
const dateFixDialog = ref<string>("");
const unapprove = async (fullname: string) => {
  dialogTitle.value = " ไม่อนุมัติการลงเวลาพิเศษของ" + fullname;
  name.value = fullname;
  modalUnapprove.value = true;
};
function openModal(data: any, check: string, date: string, dateFix: string) {
  modalApprove.value = true;
  dateDialog.value = date;
  dateFixDialog.value = dateFix;
  editCheck.value = check;
  if (check === "PENDING") {
    detailData.value = data;
  }
  console.log(data);
}
const closeDialog = () => {
  modalUnapprove.value = false;
  modalApprove.value = false;
  editCheck.value = "PENDING";
};
const clickSave = () => {
  modalUnapprove.value = false;
};
// ค้นหาในตาราง
const filterKeyword = ref<string>("");
const filterRef = ref<HTMLInputElement | null>(null);
const resetFilter = () => {
  filterKeyword.value = "";
  if (filterRef.value) {
    filterRef.value.focus();
  }
};

onMounted(async () => {
  fecthList([
    {
      id: "00000000-0000-0000-0000-000000000000",
      fullname: "นางสาวณัฐกา ชมสิน",
      date: "2023-11-01 08:54",
      dateFix: "2023-10-30",
      startTimeMorning: "08:00",
      endTimeMorning: "12:00",
      startTimeAfternoon: "08:00",
      endTimeAfternoon: "12:00",
      status: "PENDING",
    },
    {
      id: "00000000-0000-0000-0000-000000000000",
      fullname: "นางสาวรัชภรณ์ ภักดี",
      date: "2023-10-30 08:55",
      dateFix: "2023-10-29",
      startTimeMorning: "08:00",
      endTimeMorning: "12:00",
      startTimeAfternoon: null,
      endTimeAfternoon: null,
      status: "APPROVE",
    },
    {
      id: "00000000-0000-0000-0000-000000000000",
      fullname: "นางสาวภาพรรณ ลออ",
      date: "2023-10-31 18:54",
      dateFix: "2023-10-30",
      startTimeMorning: null,
      endTimeMorning: null,
      startTimeAfternoon: "13:00",
      endTimeAfternoon: "16:30",
      status: "REJECT",
    },
  ]);
});

const selectedDate = ref<string>("");
const dateMonth = ref<any>({
  month: new Date().getMonth(),
  year: new Date().getFullYear(),
});

const monthYearThai = (val: any) => {
  if (val == null) return "";
  else return monthYear2Thai(val.month, val.year);
};
</script>

<template>
  <div class="toptitle text-dark col-12 row items-center">
    รายการลงเวลากรณีพิเศษ
  </div>
  <q-card flat bordered class="col-12 q-mt-sm q-pt-sm q-pa-md">
    <div class="row col-12 q-col-gutter-sm q-mb-sm">
      <div class="q-gutter-sm">
        <datepicker
          v-model="selectedDate"
          :locale="'th'"
          autoApply
          month-picker
          :enableTimePicker="false"
        >
          <template #year="{ year }">{{ year + 543 }}</template>
          <template #year-overlay-value="{ value }">{{
            parseInt(value + 543)
          }}</template>
          <template #trigger>
            <q-input
              :model-value="monthYearThai(dateMonth)"
              dense
              outlined
              hide-bottom-space
              style="width: 130px"
            >
              <template v-slot:prepend>
                <q-icon
                  name="event"
                  class="cursor-pointer"
                  style="color: var(--q-primary)"
                >
                </q-icon>
              </template>
            </q-input>
          </template>
        </datepicker>
      </div>

      <q-space />

      <q-input
        class="col-xs-12 col-sm-3 col-md-2"
        standout
        dense
        v-model="filterKeyword"
        ref="filterRef"
        outlined
        debounce="300"
        placeholder="ค้นหา"
      >
        <template v-slot:append>
          <q-icon v-if="filterKeyword == ''" name="search" />
          <q-icon
            v-if="filterKeyword !== ''"
            name="clear"
            class="cursor-pointer"
            @click="resetFilter"
          />
        </template>
      </q-input>

      <q-select
        v-model="dataSpecialTime.visibleColumns"
        multiple
        outlined
        dense
        options-dense
        :display-value="$q.lang.table.columns"
        emit-value
        map-options
        :options="dataSpecialTime.columns"
        option-value="name"
        options-cover
        style="min-width: 150px"
        class="col-xs-12 col-sm-3 col-md-2"
      />
    </div>

    <div class="col-12">
      <d-table
        :columns="dataSpecialTime.columns"
        :rows="dataSpecialTime.rows"
        :filter="filterKeyword"
        row-key="tb-list"
        flat
        bordered
        :paging="true"
        dense
        v-model:pagination="pagination"
        :visible-columns="dataSpecialTime.visibleColumns"
      >
        <template v-slot:header="props">
          <q-tr :props="props">
            <q-th v-for="col in props.cols" :key="col.name" :props="props">
              <span class="text-weight-medium">{{ col.label }}</span>
            </q-th>
            <q-th auto-width />
          </q-tr>
        </template>
        <template v-slot:body="props">
          <q-tr :props="props" class="cursor-pointer">
            <q-td v-for="col in props.cols" :key="col.name" :props="props">
              <div v-if="col.name == 'no'">
                {{ props.rowIndex + 1 }}
              </div>
              <div v-else>
                {{ col.value }}
              </div>
            </q-td>
            <q-td auto-width>
              <q-btn
                v-if="props.row.status == 'PENDING'"
                color="orange"
                class="q-px-md"
                dense
                unelevated
                @click="unapprove(props.row.fullname)"
                >ไม่อนุมัติ</q-btn
              >

              <q-btn
                v-if="props.row.status == 'PENDING'"
                color="primary"
                class="q-px-md q-ml-sm"
                dense
                unelevated
                @click="
                  openModal(
                    props.row,
                    'PENDING',
                    props.row.date,
                    props.row.dateFix
                  )
                "
                >อนุมัติ</q-btn
              >
              <q-badge
                v-if="props.row.status == 'APPROVE'"
                rounded
                outline
                color="green"
                label="อนุมัติ"
              />
              <q-badge
                v-if="props.row.status == 'REJECT'"
                rounded
                outline
                color="red"
                label="ไม่อนุมัติ"
              />
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card>
  <DialogReason
    :modal="modalUnapprove"
    :title="dialogTitle"
    :desc="dialogDesc"
    :click-close="closeDialog"
    label="เหตุผล"
    :savaForm="clickSave"
  />
  <DialogApprove
    :modal="modalApprove"
    :closeDialog="closeDialog"
    :date="dateDialog"
    :dateFix="dateFixDialog"
    :editCheck="editCheck"
    :detailData="detailData"
  />
</template>

<style></style>
