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

const organization = ref<string>(1);
const organizationOptions = ref<any>([{ id: 1, name: "ทั้งหมด" }]);
const visibleColumns = ref<string[]>([
  "no",
  "citizenId",
  "name",
  "position",
  "level",
  "salary",
  "organization",
  "insigniaType",
  "insigniaSend",
  "insigniaLevel",
]);
// หัวตาราง
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: true,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    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: "level",
    align: "left",
    label: "อันดับ/ระดับ",
    sortable: true,
    field: "level",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "salary",
    align: "left",
    label: "เงินเดือน",
    sortable: true,
    field: "salary",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "organization",
    align: "left",
    label: "หน่วยงาน",
    sortable: true,
    field: "organization",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "insigniaType",
    align: "left",
    label: "ประเภทเครื่องราชฯ ปัจจุบัน",
    sortable: true,
    field: "insigniaType",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "insigniaSend",
    align: "left",
    label: "ประเภทเครื่องราชฯ ที่ยื่นขอ",
    sortable: true,
    field: "insigniaSend",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "insigniaLevel",
    align: "left",
    label: "ชั้นเครื่องราชฯ",
    sortable: true,
    field: "insigniaLevel",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "dateSend",
    align: "left",
    label: "วันที่ยื่นขอ",
    sortable: true,
    field: "dateSend",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
// ข้อมูลตาราง (จำลอง)
const rows = ref<any[]>([
  {
    no: "1",
    citizenId: "1xxxxxxxxxx",
    name: "นายใจดี ยอดใจ ",
    position: "นักวิชาการพัสดุ",
    level: "1",
    salary: "15000",
    organization: "บริหาร",
    insigniaType: "ทวีติยาภรณ์ช้างเผือก",
    insigniaSend: "ตริตาภรณ์ช้างเผือก",
    insigniaLevel: "ต่ำกว่าสายสะพาย",
    dateSend: "31 ม.ค. 2566",
  },
  {
    no: "2",
    citizenId: "1xxxxxxxxxx",
    name: "นายจักกริน บัณฑิต",
    position: "นักวิชาการพัสดุ",
    level: "ปฏิบัติการ",
    salary: "15000",
    organization: "บริหาร",
    insigniaType: "ทวีติยาภรณ์ช้างเผือก",
    insigniaSend: "ตริตาภรณ์ช้างเผือก",
    insigniaLevel: "ต่ำกว่าสายสะพาย",
    dateSend: "31 ม.ค. 2566",
  },
  {
    no: "3",
    citizenId: "1xxxxxxxxxx",
    name: "นางสาวกัณฐิมา กาฬสินธุ์",
    position: "นักจัดการงานทั่วไป",
    level: "ปฏิบัติการ",
    salary: "15000",
    organization: "บริหาร",
    insigniaType: "ทวีติยาภรณ์ช้างเผือก",
    insigniaSend: "ตริตาภรณ์ช้างเผือก",
    insigniaLevel: "ต่ำกว่าสายสะพาย",
    dateSend: "31 ม.ค. 2566",
  },
  {
    no: "4",
    citizenId: "1xxxxxxxxxx",
    name: "นางสาวเมขลา กระจ่างมนตรี",
    position: "นักจัดการงานทั่วไป",
    level: "ปฏิบัติการ",
    salary: "15000",
    organization: "บริหาร",
    insigniaType: "ทวีติยาภรณ์ช้างเผือก",
    insigniaSend: "ตริตาภรณ์ช้างเผือก",
    insigniaLevel: "ต่ำกว่าสายสะพาย",
    dateSend: "31 ม.ค. 2566",
  },
]);

const filterKeyword = ref<string>("");
const filterRef = ref<any>(null);
const resetFilter = () => {
  filterKeyword.value = "";
  filterRef.value.focus();
};
const pagination = ref({
  sortBy: "desc",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});
const paging = ref<boolean>(true);
const paginationLabel = (start: string, end: string, total: string) => {
  if (paging.value == true) return " " + start + "-" + end + " ใน " + total;
  else return start + "-" + end + " ใน " + total;
};
</script>
<template>
  <div class="col-12 row q-pa-md">
    <div class="row col-12">
      <div class="row col-12 q-col-gutter-sm">
        <q-select
          v-model="organization"
          label="หน่วยงาน"
          dense
          emit-value
          map-options
          :options="organizationOptions"
          option-value="id"
          option-label="name"
          lazy-rules
          hide-bottom-space
          :readonly="false"
          :borderless="false"
          :outlined="true"
          :hide-dropdown-icon="false"
          style="min-width: 150px"
        />
        <!-- <div>
          <q-btn size="md" icon="mdi-download" flat round color="primary">
            <q-tooltip>ดาวน์โหลด</q-tooltip>
          </q-btn>
          <q-btn size="12px" flat round color="add" icon="mdi-plus">
            <q-tooltip>เพิ่ม</q-tooltip>
          </q-btn>
        </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="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"
          class="col-xs-12 col-sm-3 col-md-2"
        />
      </div>
      <div class="col-12 q-pt-sm">
        <q-table
          ref="table"
          :columns="columns"
          :rows="rows"
          :filter="filterKeyword"
          row-key="name"
          flat
          bordered
          :paging="true"
          dense
          class="custom-header-table"
          :visible-columns="visibleColumns"
          :pagination-label="paginationLabel"
          v-model:pagination="pagination"
        >
          <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"
              @click="nextPage(props.row)"
            >
              <q-td key="no" :props="props">
                {{ props.rowIndex + 1 }}
              </q-td>
              <q-td key="citizenId" :props="props">
                {{ props.row.citizenId }}
              </q-td>
              <q-td key="name" :props="props">
                {{ props.row.name }}
              </q-td>
              <q-td key="position" :props="props">
                {{ props.row.position }}
              </q-td>
              <q-td key="level" :props="props">
                {{ props.row.level }}
              </q-td>
              <q-td key="salary" :props="props">
                {{ props.row.salary }}
              </q-td>
              <q-td key="organization" :props="props">
                {{ props.row.organization }}
              </q-td>
              <q-td key="insigniaType" :props="props">
                {{ props.row.insigniaType }}
              </q-td>
              <q-td key="insigniaSend" :props="props">
                {{ props.row.insigniaSend }}
              </q-td>
              <q-td key="insigniaLevel" :props="props">
                {{ props.row.insigniaLevel }}
              </q-td>
            </q-tr>
          </template>
          <template v-slot:pagination="scope">
            <q-pagination
              v-model="pagination.page"
              active-color="primary"
              color="primary"
              :max="scope.pagesNumber"
              :max-pages="5"
              size="sm"
              boundary-links
              direction-links
            ></q-pagination>
          </template>
        </q-table>
      </div>
    </div>
  </div>
</template>
