<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useQuasar, type QTableProps } from "quasar";
import type { DataOption } from "@/modules/15_development/interface/index/Main";

/** importStore*/
import { useDevelopmentDataStore } from "@/modules/15_development/store/developmentStore";
import { useCounterMixin } from "@/stores/mixin";
import { useRoute, useRouter } from "vue-router";

/** use*/
const router = useRouter()
const store = useDevelopmentDataStore();
const $q = useQuasar();
const { showLoader, hideLoader } = useCounterMixin();

const rows = ref<any>([]);
const agency = ref<string>("");
const agencyOp = ref<DataOption[]>([
  {
    id: "id1",
    name: "name1",
  },
  {
    id: "id2",
    name: "name2",
  },
  {
    id: "id3",
    name: "name3",
  },
]);

const visibleColumns = ref<string[]>([
  "citizenId",
  "name",
  "position",
  "type",
  "level",
  "positionSide",
  "projectName",
]);

const columns = ref<QTableProps["columns"]>([
  {
    name: "citizenId",
    align: "left",
    label: "เลขประจำตัวประชาชน",
    sortable: true,
    field: "citizenId",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "name",
    align: "left",
    label: "ชื่อ - นามสกุล",
    sortable: true,
    field: "name",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },

  {
    name: "position",
    align: "left",
    label: "ตําแหน่ง",
    sortable: true,
    field: "position",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "type",
    align: "left",
    label: "กลุ่มงาน",
    sortable: true,
    field: "type",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "level",
    align: "left",
    label: "ระดับชั้นงาน",
    sortable: true,
    field: "level",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "positionSide",
    align: "left",
    label: "ตําแหน่งทางการบริหาร",
    sortable: true,
    field: "positionSide",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "projectName",
    align: "left",
    label: "ชื่อโครงการ/กิจกรรม/หลักสูตร",
    sortable: true,
    field: "projectName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);

function onAdd(){
    router.push(`/development/employee-history/add`)

}
function onDownload(){

}

function onEdit(){

}
</script>
<template>
  <div class="toptitle text-dark col-12 row items-center">
    ประวัติฝึกอบรม/ดูงานลูกจ้าง
  </div>
  <q-card flat bordered class="q-pa-md">
    <q-toolbar style="padding: 0px">
      <div class="row q-gutter-sm">
        <datepicker
          menu-class-name="modalfix"
          v-model="store.formFilter.year"
          :locale="'th'"
          autoApply
          year-picker
          :enableTimePicker="false"
        >
          <template #year="{ year }">{{ year + 543 }}</template>
          <template #year-overlay-value="{ value }">{{
            parseInt(value + 543)
          }}</template>
          <template #trigger>
            <q-input
              dense
              lazy-rules
              outlined
              :model-value="Number(store.formFilter.year) + 543"
              :label="`${'ปีงบประมาณ'}`"
            >
              <template v-slot:prepend>
                <q-icon
                  name="event"
                  class="cursor-pointer"
                  style="color: var(--q-primary)"
                >
                </q-icon>
              </template>
            </q-input>
          </template>
        </datepicker>
        <q-select
          dense
          outlined
          label="หน่วยงาน"
          v-model="agency"
          :options="agencyOp"
          option-value="id"
          option-label="name"
          style="width: 200px"
        />
      </div>

      <q-btn
        flat
        round
        dense
        icon="add"
        color="primary"
        @click="onAdd()"
      >
        <q-tooltip>เพิ่ม</q-tooltip>
      </q-btn>
      <q-space />
      <q-btn
        flat
        round
        dense
        icon="mdi-arrow-down-bold-circle-outline"
        color="blue"
        @click="onAdd()"
      >
        <q-tooltip>ดาวน์โหลด</q-tooltip>
      </q-btn>
      <div class="row q-gutter-sm">
        <q-input
          standout
          dense
          v-model="store.formFilter.keyword"
          ref="filterRef"
          outlined
          debounce="300"
          placeholder="ค้นหา"
        >
          <template v-slot:append>
            <q-icon v-if="store.formFilter.keyword == ''" name="search" />
            <q-icon
              v-if="store.formFilter.keyword !== ''"
              name="clear"
              class="cursor-pointer"
              @click="store.formFilter.keyword = ''"
            />
          </template>
        </q-input>

        <q-select
          v-model="visibleColumns"
          multiple
          outlined
          dense
          options-dense
          :display-value="$q.lang.table.columns"
          emit-value
          map-options
          :options="columns"
          option-value="name"
          options-cover
          style="min-width: 150px"
        />
      </div>
    </q-toolbar>

    <div class="col-12">
      <d-table
        for="table"
        ref="table"
        :columns="columns"
        :rows="rows"
        row-key="subject"
        flat
        bordered
        dense
        class="custom-header-table"
        :visible-columns="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-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"
              @click="onEdit()"
            >
              <div class="table_ellipsis">
                {{ col.value ? col.value : "-" }}
              </div>
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card>
</template>
