<script setup lang="ts">
import { ref, onMounted } from "vue";
import type { QTableProps } from "quasar";
import { useQuasar } from "quasar";
import { useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import http from "@/plugins/http";
import config from "@/app.config";

const router = useRouter();
const mixin = useCounterMixin();
const { messageError, date2Thai, showLoader, hideLoader } = mixin;
const $q = useQuasar(); //ใช้ noti quasar

const rows = ref<any>([]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "prefix",
    align: "left",
    label: "คำนำหน้า",
    sortable: true,
    field: "prefix",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "fullname",
    align: "left",
    label: "ชื่อ-นามสกุล",
    sortable: true,
    field: "fullname",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "positionType",
    align: "left",
    label: "ตำแหน่งประเภท",
    sortable: true,
    field: "positionType",
    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: "positionLevel",
    align: "left",
    label: "ระดับ",
    sortable: true,
    field: "positionLevel",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "positionExecutive",
    align: "left",
    label: "ตำแหน่งทางการบริหาร",
    sortable: true,
    field: "positionExecutive",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "oc",
    align: "left",
    label: "สังกัด",
    sortable: true,
    field: "oc",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "status",
    align: "left",
    label: "สถานะ",
    sortable: true,
    field: "status",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const visibleColumns = ref<string[]>([
  "prefix",
  "fullname",
  "positionType",
  "position",
  "positionLevel",
  "positionExecutive",
  "oc",
  "status",
]);

onMounted(async () => {
  await fecthlist();
});

const fecthlist = async () => {
  showLoader();
  await http
    .get(config.API.listResign())
    .then((res: any) => {
      console.log(res);
      let data = res.data.result;
      rows.value = data.map((e: any) => ({
        personalId: e.id,
        profileId: e.profileId,
        prefix: e.prefix,
        fullname: e.firstName + "   " + e.lastName,
        positionType: e.positionType,
        position: e.positionLine,
        positionLevel: e.positionLevel,
        positionExecutive: e.positionExecutive,
        oc: e.organizationOrganization,
        status: e.status,
      }));
    })
    .catch((e: any) => {
      console.log(e);
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
};

const paging = ref<boolean>(true);
const pagination = ref({
  sortBy: "desc",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});
const paginationLabel = (start: string, end: string, total: string) => {
  if (paging.value == true) return " " + start + "-" + end + " ใน " + total;
  else return start + "-" + end + " ใน " + total;
};
//ค้นหา คอลัมน์ คอลัมน์ที่แสดง
const filterKeyword = ref<string>("");
const filterRef = ref<any>(null);
const resetFilter = () => {
  filterKeyword.value = "";
  filterRef.value.focus();
};

// const nextPage = (id:string) => {
//   router.push("/retirement/resign/"+id);
// };
</script>
<template>
  <div class="toptitle text-dark col-12 row items-center">รายการลาออก</div>
  <q-card flat bordered class="col-12 q-mt-sm">
    <q-separator />
    <div class="col-12 row q-pa-md">
      <div class="row col-12">
        <div class="row col-12 q-col-gutter-sm">
          <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="fullname"
            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="
                  router.push(`/retirement/resign/${props.row.personalId}`)
                "
              >
                <!-- <q-td key="no" :props="props">
                  {{ props.rowIndex + 1 }}
                </q-td> -->
                <q-td key="prefix" :props="props">
                  {{ props.row.prefix }}
                </q-td>
                <q-td key="fullname" :props="props">
                  {{ props.row.fullname }}
                </q-td>
                <q-td key="positionType" :props="props">
                  {{ props.row.positionType }}
                </q-td>
                <q-td key="position" :props="props">
                  {{ props.row.position }}
                </q-td>
                <q-td key="positionLevel" :props="props">
                  {{ props.row.positionLevel }}
                </q-td>
                <q-td key="positionExecutive" :props="props">
                  {{ props.row.positionExecutive }}
                </q-td>
                <q-td key="oc" :props="props">
                  {{ props.row.oc }}
                </q-td>

                <q-td key="status" :props="props">
                  <q-icon
                    size="20px"
                    v-if="props.row.status == 'PENDING'"
                    name="mdi-timer-sand"
                    color="deep-orange"
                  >
                    <q-tooltip>รอดำเนินการ</q-tooltip>
                  </q-icon>
                  <q-icon size="20px" v-else name="mdi-check" color="teal">
                    <q-tooltip>เสร็จสิ้น</q-tooltip>
                  </q-icon>

                  <!-- {{ props.row.status }} -->
                </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>
  </q-card>
</template>
<style scoped lang="scss">
.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;
  }

  /* 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>
