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

import http from "@/plugins/http";
import config from "@/app.config";
import { useResultsPerformDataStore } from "@/modules/04_registryPerson/stores/ResultsPerformance";
import { useCounterMixin } from "@/stores/mixin";

/** importType*/
import type { QTableProps } from "quasar";
import type { DataDiscipline } from "@/modules/13_salary/interface/response/Main";

/** importComponents*/
import SkeletonTable from "@/components/SkeletonTable.vue";

const $q = useQuasar();
const store = useResultsPerformDataStore();
const { textRangePoint, textPoint } = store;
const { date2Thai, messageError, onSearchDataTable } = useCounterMixin();

/**  props*/
const profileId = defineModel<string>("profileId", { required: true });
const employeeClass = defineModel<string>("employeeClass", { required: true });

const isLoading = ref<boolean>(true);

/** Table*/
const keyword = ref<string>("");
const rows = ref<DataDiscipline[]>([]);
const rowsData = ref<DataDiscipline[]>([]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "date",
    align: "left",
    label: "วันที่ได้รับ",
    sortable: true,
    field: "date",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => date2Thai(v),
  },
  {
    name: "point1Total",
    align: "left",
    label: "ส่วนที่ 1 (น้ำหนัก)",
    sortable: true,
    field: "point1Total",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "point1",
    align: "left",
    label: "ผลประเมินส่วนที่ 1 (คะแนน)",
    sortable: true,
    field: "point1",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "point2Total",
    align: "left",
    label: "ส่วนที่ 2 (น้ำหนัก)",
    sortable: true,
    field: "point2Total",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "point2",
    align: "left",
    label: "ผลประเมินส่วนที่ 2 (คะแนน)",
    sortable: true,
    field: "point2",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "pointSumTotal",
    align: "left",
    label: "ผลรวม (น้ำหนัก)",
    sortable: true,
    field: "pointSumTotal",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "pointSum",
    align: "left",
    label: "ผลประเมินรวม (คะแนน)",
    sortable: true,
    field: "pointSum",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "name",
    align: "left",
    label: "ผลประเมิน",
    sortable: true,
    field: "name",
    format(val, row) {
      return `${textPoint(row.pointSum)} ${textRangePoint(row.pointSum)}`;
    },
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);
const visibleColumns = ref<String[]>([
  "point1Total",
  "point1",
  "point2Total",
  "point2",
  "pointSumTotal",
  "pointSum",
  "name",
  "date",
]);

/** function fetch รายการข้อมูลผลการประเมินการปฏิบัติราชการ*/
function fetchListAssessments() {
  isLoading.value = true;
  http
    .get(
      config.API.profileNewAssessmentsByProfileId(
        profileId.value,
        employeeClass.value
      )
    )
    .then((res) => {
      rowsData.value = res.data.result;
      onSearch();
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      isLoading.value = false;
    });
}

function onSearch() {
  rows.value = onSearchDataTable(
    keyword.value,
    rowsData.value,
    columns.value ? columns.value : []
  );
}

onMounted(() => {
  fetchListAssessments();
});
</script>

<template>
  <q-card-section class="q-pt-none">
    <div class="row items-center q-gutter-x-sm q-pb-sm">
      <q-space />
      <q-input
        dense
        outlined
        v-model="keyword"
        label="ค้นหา"
        class="q-mr-sm"
        @keydown.enter.prevent="onSearch"
      >
        <template v-slot:append>
          <q-icon name="search" />
        </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"
        style="min-width: 140px"
      />
    </div>

    <div class="col-12">
      <SkeletonTable v-if="isLoading" :columns="columns" />

      <d-table
        v-else
        ref="table"
        row-key="id"
        flat
        bordered
        dense
        :paging="true"
        :rows-per-page-options="[20, 50, 100]"
        :visible-columns="visibleColumns"
        :rows="rows"
        :columns="columns"
      >
        <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">
            <q-td v-for="col in props.cols" :key="col.id">
              <div class="table_ellipsis">
                {{ col.value ? col.value : "-" }}
              </div>
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card-section>
</template>
