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

import { useEvaluateStore } from "@/modules/06_evaluate/store";

import Table from "@/components/Table.vue";
import DialogHeader from "@/components/DialogHeader.vue";

/**use*/
const store = useEvaluateStore();

const props = defineProps({
  modal: {
    type: Boolean,
    default: false,
  },
  show: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: "ตรวจสอบคุณสมบัติ",
  },
  educations: Array,
  closeModal: {
    type: Function,
    default: () => {},
  },
});

const filter = ref<string>("");
const rows = ref<any[]>([
  {
    degree: "ปริญญาตรี หรือเทียบเท่า",
    level: "หลักสูตร ๔ ปี",
    expert: "๖ ปี",
    special: "๘ ปี",
    checked: "ปริญญาตรี",
    year: 4,
  },
  {
    degree: "ปริญญาตรี หรือเทียบเท่า",
    level: "หลักสูตร ๕ ปี",
    expert: "๕ ปี",
    special: "๗ ปี",
    checked: "ปริญญาตรี",
    year: 5,
  },
  {
    degree: "ปริญญาตรี หรือเทียบเท่า",
    level: "หลักสูตร ๖ ปี",
    expert: "๔ ปี",
    special: "๖ ปี",
    checked: "ปริญญาตรี",
    year: 6,
  },
  {
    degree: "ปริญญาโท หรือเทียบเท่า",
    level: "-",
    expert: "๔ ปี",
    special: "๖ ปี",
    checked: "ปริญญาโท",
  },
  {
    degree: "ปริญญาเอก หรือเทียบเท่า",
    level: "-",
    expert: "๒ ปี",
    special: "๔ ปี",
    checked: "ปริญญาเอก",
  },
]);

const columns = ref<QTableProps["columns"]>([
  {
    name: "degree",
    align: "left",
    label: "คุณวุฒิ",
    sortable: false,
    field: "degree",
    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",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "expert",
    align: "center",
    label: "ชำนาญการ",
    sortable: false,
    field: "expert",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "special",
    align: "center",
    label: "ชำนาญการพิเศษ",
    sortable: false,
    field: "special",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "checked",
    align: "center",
    label: "",
    sortable: false,
    field: "checked",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);

const visibleColumns = ref<string[]>(
  store.tabMenu == "1"
    ? ["degree", "level", "expert"]
    : ["degree", "level", "special"]
);

watch(props, () => {
  if (!props.show) {
    visibleColumns.value = ["degree", "level", "checked"];
  } else {
    if (store.tabMenu == "1") {
      visibleColumns.value = ["degree", "level", "expert"];
    } else {
      visibleColumns.value = ["degree", "level", "special"];
    }
  }
});
</script>

<template>
  <q-dialog v-model="props.modal" persistent>
    <q-card style="width: 40vw">
      <DialogHeader :tittle="props.title" :close="props.closeModal" />
      <q-separator />
      <q-card-section class="q-px-sm q-pt-none q-pb-sm bg-grey-1">
        <Table
          style="max-height: 80vh"
          :view-mode="false"
          :rows="rows"
          :columns="columns"
          :visible-columns="visibleColumns"
          v-model:inputfilter="filter"
          v-model:inputvisible="visibleColumns"
          :pagination="false"
          :paging="false"
          :inputShow="false"
          :titleText="''"
        >
          <template #columns="props">
            <q-tr :props="props">
              <q-td key="degree" :props="props">
                {{ props.row.degree }}
              </q-td>
              <q-td key="level" :props="props">
                {{ props.row.level }}
              </q-td>
              <q-td key="expert" :props="props">
                {{ props.row.expert }}
              </q-td>
              <q-td key="special" :props="props">
                {{ props.row.special }}
              </q-td>
              <q-td key="checked" :props="props">
                <q-icon
                  v-if="educations?.find((x:any)=> (props.row.year && x.educationLevel == props.row.checked && x.durationYear == props.row.year) || (!props.row.year && x.educationLevel == props.row.checked))"
                  name="checked"
                  color="primary"
                  size="xs"
                />
              </q-td>
            </q-tr>
          </template>
        </Table>
      </q-card-section>
    </q-card>
  </q-dialog>
</template>
