<template>
  <q-table
    ref="table"
    flat
    :class="
      $q.screen.gt.xs
        ? 'q-px-md custom-header-table2'
        : 'q-px-sm custom-header-table2 custom-table-height'
    "
    v-bind="attrs"
    dense
    virtual-scroll
    :virtual-scroll-sticky-size-start="48"
    :pagination-label="paginationLabel"
    :pagination="initialPagination"
    :rows-per-page-options="[0]"
  >
    <template v-slot:top="props">
      <div class="q-py-sm col-12 row">
        <q-space />
        <div class="items-center row col-12 q-gutter-x-sm">
          <q-btn
            size="13px"
            color="grey-7"
            dense
            flat
            round
            @click="onTab"
            class="shadow-1"
            icon="chevron_right"
            v-if="!isTab"
          >
            <q-tooltip>เปิด</q-tooltip>
          </q-btn>
          <q-btn
            size="12px"
            flat
            round
            color="blue"
            @click="clickSearchPanel"
            icon="mdi-filter-variant"
            v-show="searchPanel == false"
          >
            <q-tooltip>ค้นหาขั้นสูง</q-tooltip>
          </q-btn>
          <q-space />
          <q-input
            v-show="searchPanel == false"
            standout
            dense
            :model-value="inputfilter"
            ref="filterRef"
            @update:model-value="updateInput"
            outlined
            debounce="300"
            placeholder="ค้นหา"
            style="max-width: 200px"
          >
            <template v-slot:append>
              <q-icon v-if="inputfilter == ''" name="search" />
              <q-icon
                v-if="inputfilter !== ''"
                name="clear"
                class="cursor-pointer"
                @click="resetFilter"
              />
            </template>
          </q-input>
          <!-- แสดงคอลัมน์ใน table -->
          <q-select
            :model-value="inputvisible"
            @update:model-value="updateVisible"
            :display-value="$q.lang.table.columns"
            multiple
            outlined
            dense
            :options="attrs.columns"
            options-dense
            option-value="name"
            map-options
            emit-value
            style="min-width: 150px"
            class="gt-xs q-ml-sm"
          />
        </div>
      </div>
      <div class="items-center">
        <q-slide-transition
          v-show="searchPanel"
          class="q-mb-sm"
          :duration="150"
        >
          <div class="my-content">
            <div
              class="row q-pa-xs items-center bg-blue-1"
              style="border-radius: 4px 4px 0px 0px"
            >
              <q-icon
                size="20px"
                color="blue-9"
                name="mdi-filter-variant"
                class="q-mx-sm"
              />
              <div class="text-blue-9 text-subtitle2 text-weight-medium">
                <span>ค้นหาข้อมูลทะเบียนประวัติ</span>
              </div>
              <q-space />
              <q-btn
                color="blue-9"
                icon="mdi-close"
                size="12px"
                flat
                round
                dense
                @click="clickSearchPanel"
              />
            </div>
            <q-separator color="blue-1" />
            <div class="dialog-card-contain">
              <q-card-section class="q-pa-md">
                <div class="row col-12 q-col-gutter-sm">
                  <div class="col-xs-12 col-sm-4">
                    <q-input
                      clearable
                      dense
                      outlined
                      lazy-rules
                      :model-value="profileId"
                      hide-bottom-space
                      label="เลขประจำตัวประชาชน"
                      @update:model-value="updateProfileId"
                      type="number"
                    />
                  </div>
                  <div class="col-xs-12 col-sm-4">
                    <q-input
                      clearable
                      dense
                      outlined
                      lazy-rules
                      :model-value="fullName"
                      hide-bottom-space
                      label="ชื่อ-นามสกุล"
                      @update:model-value="updateFullname"
                    />
                  </div>
                  <div class="col-xs-12 col-sm-2">
                    <datepicker
                      menu-class-name="modalfix"
                      :model-value="retireYear"
                      :locale="'th'"
                      autoApply
                      year-picker
                      :enableTimePicker="false"
                      @update:model-value="updateRetireYear"
                      clearable
                    >
                      <template #year="{ year }">{{ year + 543 }}</template>
                      <template #year-overlay-value="{ value }">{{
                        parseInt(value + 543)
                      }}</template>
                      <template #trigger>
                        <q-input
                          class="full-width inputgreen cursor-pointer q-mb-sm"
                          hide-bottom-space
                          outlined
                          dense
                          lazy-rules
                          borderless
                          :model-value="
                            retireYear == null ? null : retireYear + 543
                          "
                          :label="`${'ปีเกษียณ'}`"
                          clearable
                          @clear="clearDate"
                        >
                          <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-xs-12 col-sm-2">
                    <q-input
                      clearable
                      dense
                      outlined
                      lazy-rules
                      :model-value="govAge"
                      hide-bottom-space
                      label="อายุราชการ(ปี)"
                      type="number"
                      @update:model-value="updateGovAge"
                    />
                  </div>
                  <div class="col-12 row items-center q-col-gutter-md">
                    <q-toggle
                      dense
                      :model-value="isShowRetire"
                      color="primary"
                      @update:model-value="updateIsShowRetire"
                    >
                      แสดงข้อมูลผู้พ้นจากราชการ/เกษียณอายุราชการ/เสียชีวิต/โอน/ลาออก
                    </q-toggle>
                    <q-toggle
                      dense
                      :model-value="isProbation"
                      color="primary"
                      @update:model-value="updateIsProbation"
                    >
                      ทดลองปฏิบัติหน้าที่ราชการ
                    </q-toggle>
                  </div>
                  <div class="col-12 row q-col-gutter-y-md">
                    <div class="row q-gutter-md">
                      <q-radio
                        dense
                        :model-value="profileType"
                        val="officer"
                        label="ข้าราชการสามัญ"
                        class="q-pt-sm"
                        @update:model-value="updateProfileType"
                      />
                      <q-radio
                        dense
                        :model-value="profileType"
                        val="employee"
                        class="q-pt-sm"
                        @update:model-value="updateProfileType"
                      >
                        ลูกจ้าง
                      </q-radio>
                    </div>
                    <q-space />
                    <div>
                      <q-btn
                        dense
                        color="primary"
                        icon="mdi-magnify"
                        label="ค้นหา"
                        class="q-px-md"
                        @click="doSearch"
                      />
                    </div>
                  </div>
                </div>
              </q-card-section>
            </div>
          </div>
        </q-slide-transition>
      </div>
    </template>
    <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 #body="props">
      <slot v-bind="props" name="columns"></slot>
    </template>
  </q-table>
</template>
<script setup lang="ts">
import { ref, useAttrs } from "vue";
import type { Pagination } from "@/modules/04_registry/interface/index/Main";

const attrs = ref<any>(useAttrs());
const table = ref<any>(null);
const filterRef = ref<any>(null);
const searchPanel = ref<boolean>(true);
const initialPagination = ref<Pagination>({
  rowsPerPage: 0,
});

const props = defineProps({
  inputfilter: String,
  inputvisible: Array,
  profileId: String,
  fullName: String,
  oldFullName: String,
  isShowRetire: Boolean,
  isProbation: Boolean,
  profileType: String,
  retireYear: Number || null,
  govAge: Number || null,
  isTab: {
    type: Boolean,
    default: true,
  },
  doSearch: {
    type: Function,
    default: () => console.log("not function"),
  },
  onExport: {
    type: Function,
    default: () => console.log("not function"),
  },
  onTab: {
    type: Function,
    default: () => console.log("not function"),
  },
});

const emit = defineEmits([
  "update:inputfilter",
  "update:inputvisible",
  "update:profileId",
  "update:fullName",
  "update:oldFullName",
  "update:oldName",
  "update:retireYear",
  "update:govAge",
  "update:isShowRetire",
  "update:profileType",
  "update:isProbation",
]);

const updateInput = (value: string | number | null) => {
  emit("update:inputfilter", value);
};
const updateVisible = (value: []) => {
  emit("update:inputvisible", value);
};

const updateProfileId = (value: string | number | null) => {
  emit("update:profileId", value);
};
const updateFullname = (value: string | number | null) => {
  emit("update:fullName", value);
};
const updateRetireYear = (value: string | number | null) => {
  emit("update:retireYear", value);
};
const updateGovAge = (value: string | number | null) => {
  emit("update:govAge", value);
};
const updateIsShowRetire = (value: string | number | null) => {
  emit("update:isShowRetire", value);
};
const updateIsProbation = (value: string | number | null) => {
  emit("update:isProbation", value);
};
const updateProfileType = (value: string | number | null) => {
  emit("update:profileType", value);
};

const clearDate = () => {
  emit("update:retireYear", null);
};

const clickSearchPanel = () => {
  emit("update:retireYear", null);
  emit("update:govAge", null);
  emit("update:oldName", null);
  emit("update:oldFullName", null);
  emit("update:fullName", null);
  emit("update:profileId", null);
  searchPanel.value = !searchPanel.value;
};

const paginationLabel = (start: string, end: string, total: string) => {
  return start + "-" + end + " ใน " + total;
};

const doSearch = () => {
  props.doSearch();
};

const onExport = () => {
  props.onExport();
};

const onTab = () => {
  props.onTab();
};

const resetFilter = () => {
  // reset ค่าที่ค้นหาเมื่อกดปุ่ม X ในกล่องค้นหา
  emit("update:inputfilter", "");
  filterRef.value.focus();
};
</script>
<style lang="scss">
.icon-color {
  color: #4154b3;
}

.my-content {
  background: #fff;
  border: 1px solid rgba(133, 133, 133, 0.2);
  border-radius: 5px;
}
.custom-table-height {
  height: auto !important;
}
.custom-header-table2 {
  .q-table__top {
    padding: 0px !important;
  }
  .q-table__bottom {
    border-top: none !important;
  }
  .q-table__middle {
    border-radius: 5px;
    border: 1px solid rgba(133, 133, 133, 0.2) !important;
  }
  .q-table tr:nth-child(odd) td {
    background: white;
  }

  .q-table tr:nth-child(even) td {
    background: #f8f8f8;
  }

  .q-table thead tr {
    background: #ecebeb;
  }

  .q-table thead tr th {
    position: sticky;
    z-index: 1;
  }

  /* this will be the loading indicator */
  .q-table thead tr:last-child th {
    /* height of all previous header rows */
    top: 48px;
  }

  .q-table thead tr:first-child th {
    top: 0;
  }
}
</style>
