<script setup lang="ts">
import { ref, onMounted } from "vue";
import type { QTableProps, QInput } from "quasar";

const zone = ref<string>("");
const zoneOptions = ref<any>([
  "ทั้งหมด",
  "เขตที่ 1 ",
  "เขตที่ 2 ",
  "เขตที่ 3 ",
]);
const belong = ref<string>("");
const belongOptions = ref<any>([
  "ทั้งหมด",
  "สังกัด 1 ",
  "สังกัด 2 ",
  "สังกัด 3 ",
]);
const spand = ref<string>("");
const spandOptions = ref<any>([
  "ทั้งหมด",
  "สถานะการจ่าย 1 ",
  "สถานะการจ่าย 2 ",
  "สถานะการจ่าย 3 ",
]);
const filter = ref<string>("");

const visibleColumns = ref<String[]>([
  "no",
  "citizenId",
  "name",
  "affiliation",
  "affiliationcurrent",
  "county",
  "countycurren",
]);
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: "affiliation",
    align: "left",
    label: "สังกัด ณ วันที่ขอ",
    sortable: true,
    field: "affiliation",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "affiliationcurrent",
    align: "left",
    label: "สังกัด ณ ปัจจุบัน",
    sortable: true,
    field: "affiliationcurrent",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "county",
    align: "left",
    label: "สังกัดงานเขต ณ วันที่ขอ",
    sortable: true,
    field: "county",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "countycurren",
    align: "left",
    label: "สังกัดงานเขต ณ ปัจจุบัน",
    sortable: true,
    field: "countycurren",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const rows = ref<any>([
  {
    no: "1",
    citizenId: "1xxxxxxxxxx",
    name: "นายใจดี ยอดใจ ",
    affiliation: "นักวิชาการพัสดุ",
    affiliationcurrent: "ปฏิบัติการ",
    county: "ปฏิบัติการ",
    countycurren: "บริหาร",
  },
  {
    no: "2",
    citizenId: "1xxxxxxxxxx",
    name: "นายจักกริน บัณฑิต",
    affiliation: "นักวิชาการพัสดุ",
    affiliationcurrent: "ปฏิบัติการ",
    county: "ปฏิบัติการ",
    countycurren: "บริหาร",
  },
]);

onMounted(() => {
  zone.value = "ทั้งหมด";
  belong.value = "ทั้งหมด";
  spand.value = "ทั้งหมด";
});

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="toptitle text-dark col-12 row items-center">
    บันทึกผลการจ่ายใบกำกับ
  </div>
  <q-card class="col-12 q-pa-md">
    <div class="row col-12 q-pb-sm q-gutter-xs">
      <q-select
        outlined
        dense
        v-model="zone"
        :options="zoneOptions"
        label="สำนักงานเขต"
        class="col-xs-12 col-sm-3 col-md-2"
      />
      <q-select
        outlined
        dense
        v-model="belong"
        :options="belongOptions"
        label="สังกัด"
        class="col-xs-12 col-sm-3 col-md-2"
      />
      <q-select
        outlined
        dense
        v-model="spand"
        :options="spandOptions"
        label="สถานะการจ่าย"
        class="col-xs-12 col-sm-3 col-md-2"
      />
      <q-btn
        color="primary"
        icon="mdi-filter-remove"
        @click="zone = 'ทั้งหมด',belong = 'ทั้งหมด',spand = 'ทั้งหมด'"
      />

      <q-space />
      <q-input
        class="col-xs-12 col-sm-3 col-md-2"
        borderless
        dense
        v-model="filter"
        outlined
        debounce="300"
        placeholder="ค้นหา"
      />
      <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="gt-xs q-ml-sm"
      >
      </q-select>
    </div>
    <div>
      <q-table
        ref="table"
        :columns="columns"
        :rows="rows"
        :filter="filter"
        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">
            <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="affiliation" :props="props">
              {{ props.row.affiliation }}
            </q-td>
            <q-td key="affiliationcurrent" :props="props">
              {{ props.row.affiliationcurrent }}
            </q-td>
            <q-td key="county" :props="props">
              {{ props.row.county }}
            </q-td>
            <q-td key="countycurren" :props="props">
              {{ props.row.countycurren }}
            </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>
  </q-card>
</template>

<style lang="scss" scoped>
.custom-header-table {
  max-height: 64vh;

  .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;
  }

  .q-table thead tr:last-child th {
    top: 48px;
  }

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