<script setup lang="ts">
import { ref } from "vue";

/** importStores */
import { useCounterMixin } from "@/stores/mixin";
import { useWorklistDataStore } from "@/modules/09_leave/stores/WorkStore";

/** useStore */
const workStore = useWorklistDataStore();
const mixin = useCounterMixin();
const { date2Thai } = mixin;

const emit = defineEmits(["update:pagination"]);

/** ตัวแปร filter*/
const keyword = ref<string>("");
const pagination = ref({
  page: 1,
});

/**
 * function updateProps
 * @param newPagination paging
 * @param keyword คำค้นหา
 */
function updateProp(newPagination: any, keyword: string) {
  // ส่ง event ไปยัง parent component เพื่ออัพเดทค่า props
  emit("update:pagination", newPagination, 1, keyword);
}

/** Functicon หาค่ามากสุดและปิดวันที่ไม่ให้เลือกวันล่วงหน้า*/
function calculateMaxDate() {
  const today = new Date();
  today.setDate(today.getDate());
  return today;
}

/** function ค้นหาข้อมูล แล้ว อัปเดท Props*/
function filterFn() {
  updateProp(pagination.value, keyword.value);
}
</script>

<template>
  <div class="row col-12 q-col-gutter-sm q-mb-sm">
    <div class="col-xs-12 col-sm-3 col-md-2">
      <datepicker
        menu-class-name="modalfix"
        v-model="workStore.selectDate"
        :locale="'th'"
        autoApply
        :enableTimePicker="false"
        week-start="0"
        :max-date="calculateMaxDate()"
        @update:model-value="filterFn"
      >
        <template #year="{ year }">{{ year + 543 }}</template>
        <template #year-overlay-value="{ value }">{{
          parseInt(value + 543)
        }}</template>
        <template #trigger>
          <q-input
            for="selectDate"
            dense
            outlined
            :model-value="
              workStore.selectDate !== null
                ? date2Thai(workStore.selectDate)
                : null
            "
            hide-bottom-space
            :label="`${'วันที่'}`"
          >
            <template v-slot:prepend>
              <q-icon name="event" class="cursor-pointer text-primary">
              </q-icon>
            </template>
          </q-input>
        </template>
      </datepicker>
    </div>
    <q-space />
    <div class="col-xs-12 col-sm-3 col-md-2">
      <q-input
        for="filterTable"
        dense
        outlined
        v-model="keyword"
        label="ค้นหา"
        debounce="300"
        @keydown.enter.prevent="filterFn"
      >
        <template v-slot:append>
          <q-icon name="search" />
        </template>
      </q-input>
    </div>
    <div class="col-xs-12 col-sm-3 col-md-2">
      <q-select
        for="visibleColumns"
        v-model="workStore.visibleColumns"
        multiple
        outlined
        dense
        options-dense
        :display-value="$q.lang.table.columns"
        emit-value
        style="min-width: 140px"
        map-options
        :options="workStore.columns"
        option-value="name"
        
      />
    </div>
  </div>
</template>

<style scoped></style>
