<script setup lang="ts">
import { ref, reactive, watch, onMounted } from "vue";
import dialogHeader from "@/components/DialogHeader.vue";
import type { QTableProps } from "quasar";
import { useCounterMixin } from "@/stores/mixin";
import { useRoute } from "vue-router";
import type { RequestItemsObject } from "@/modules/04_registryNew/interface/request/Education";
import type { ResponseObject } from "@/modules/04_registryNew/interface/response/Education";
import { QForm, useQuasar } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";

const mixin = useCounterMixin();
const $q = useQuasar();
const {
  dialogRemove,
  dialogConfirm,
  showLoader,
  hideLoader,
  messageError,
  success,
  date2Thai,
} = mixin;

const dialog = ref<boolean>(false);
const dialogStatus = ref<string>("create");
const mode = ref<string>("table");
const columns = ref<QTableProps["columns"]>([
  {
    name: "educationLevel",
    align: "left",
    label: "ระดับศึกษา",
    sortable: true,
    field: "educationLevel",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "institute",
    align: "left",
    label: "สถานศึกษา",
    sortable: true,
    field: "institute",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "startDate",
    align: "left",
    label: "ตั้งแต่",
    sortable: true,
    field: (v) =>
      v.isDate
        ? date2Thai(v.startDate)
        : new Date(v.startDate).getFullYear() + 543,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "endDate",
    align: "left",
    label: "ถึง",
    sortable: true,
    field: (v) =>
      v.isDate ? date2Thai(v.endDate) : new Date(v.endDate).getFullYear() + 543,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "finishDate",
    align: "left",
    label: "วันที่สำเร็จการศึกษา",
    sortable: true,
    field: "finishDate",
    format: (v) => date2Thai(v),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "isEducation",
    align: "left",
    label: "เป็นวุฒิการศึกษาในตำแหน่ง",
    sortable: true,
    field: "isEducation",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => (v === true ? "ใช่" : "ไม่ใช่"),
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "degree",
    align: "left",
    label: "วุฒิการศึกษา",
    sortable: true,
    field: "degree",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "field",
    align: "left",
    label: "สาขาวิชา/ทาง",
    sortable: true,
    field: "field",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "fundName",
    align: "left",
    label: "ทุน",
    sortable: true,
    field: "fundName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "gpa",
    align: "left",
    label: "เกรดเฉลี่ย",
    sortable: true,
    field: "gpa",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "country",
    align: "left",
    label: "ประเทศ",
    sortable: true,
    field: "country",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "other",
    align: "left",
    label: "ข้อมูลการติดต่อ",
    sortable: true,
    field: "other",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "duration",
    align: "left",
    label: "ระยะเวลา",
    sortable: true,
    field: "duration",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "durationYear",
    align: "left",
    label: "ระยะเวลาหลักสูตร (ปี)",
    sortable: true,
    field: "durationYear",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "note",
    align: "left",
    label: "หมายเหตุ",
    sortable: true,
    field: "note",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);
const historyColumns = ref<QTableProps["columns"]>([
  {
    name: "educationLevel",
    align: "left",
    label: "ระดับศึกษา",
    sortable: true,
    field: "educationLevel",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "institute",
    align: "left",
    label: "สถานศึกษา",
    sortable: true,
    field: "institute",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "startDate",
    align: "left",
    label: "ตั้งแต่",
    sortable: true,
    field: (v) =>
      v.isDate
        ? date2Thai(v.startDate)
        : new Date(v.startDate).getFullYear() + 543,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "endDate",
    align: "left",
    label: "ถึง",
    sortable: true,
    field: (v) =>
      v.isDate ? date2Thai(v.endDate) : new Date(v.endDate).getFullYear() + 543,
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "finishDate",
    align: "left",
    label: "วันที่สำเร็จการศึกษา",
    sortable: true,
    field: "finishDate",
    format: (v) => date2Thai(v),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "isEducation",
    align: "left",
    label: "เป็นวุฒิการศึกษาในตำแหน่ง",
    sortable: true,
    field: "isEducation",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    format: (v) => (v === true ? "ใช่" : "ไม่ใช่"),
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "degree",
    align: "left",
    label: "วุฒิการศึกษา",
    sortable: true,
    field: "degree",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "field",
    align: "left",
    label: "สาขาวิชา/ทาง",
    sortable: true,
    field: "field",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "fundName",
    align: "left",
    label: "ทุน",
    sortable: true,
    field: "fundName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "gpa",
    align: "left",
    label: "เกรดเฉลี่ย",
    sortable: true,
    field: "gpa",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "country",
    align: "left",
    label: "ประเทศ",
    sortable: true,
    field: "country",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "other",
    align: "left",
    label: "ข้อมูลการติดต่อ",
    sortable: true,
    field: "other",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "duration",
    align: "left",
    label: "ระยะเวลา",
    sortable: true,
    field: "duration",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "durationYear",
    align: "left",
    label: "ระยะเวลาหลักสูตร (ปี)",
    sortable: true,
    field: "durationYear",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "note",
    align: "left",
    label: "หมายเหตุ",
    sortable: true,
    field: "note",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "lastUpdateFullName",
    align: "left",
    label: "ผู้ดำเนินการ",
    sortable: true,
    field: "lastUpdateFullName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
  {
    name: "lastUpdatedAt",
    align: "left",
    label: "วันที่แก้ไข",
    sortable: true,
    field: "lastUpdatedAt",
    format: (v) => date2Thai(v),
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
    sort: (a: string, b: string) =>
      a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
  },
]);

const rows = ref<ResponseObject[]>([]);
const historyRows = ref<ResponseObject[]>([]);
const editId = ref<string>("");
const route = useRoute();
const id = ref<string>(route.params.id.toString());
const isDate = ref<string>("false");
const educationOption = ref([
  { label: "ใช่", value: true },
  { label: "ไม่ใช่", value: false },
]);

const educationOptionFilter = ref([
  { label: "ใช่", value: true },
  { label: "ไม่ใช่", value: false },
]);

const educationLevelOption = ref<any>([]);
const educationLevelOptionFilter = ref([]);

const historyDialog = ref<boolean>(false);
const educationData = reactive<RequestItemsObject>({
  isDate: isDate.value === "false" ? false : true,
  profileId: id.value,
  educationLevel: "",
  institute: "",
  startYear: new Date().getFullYear(),
  endYear: new Date().getFullYear(),
  finishDate: new Date(),
  startDate: new Date(),
  endDate: new Date(),
  isEducation: null,
  degree: "",
  field: "",
  fundName: "",
  gpa: "",
  country: "",
  other: "",
  duration: "",
  durationYear: null,
  note: "",
  educationLevelId: "",
  positionPath: "",
  positionPathId: "",
});

const keyword = ref<string>("");
const historyKeyword = ref<string>("");

const pagination = ref({
  page: 1,
  rowsPerPage: 10,
});

const historyPagination = ref({
  page: 1,
  rowsPerPage: 10,
});

const visibleColumns = ref<string[]>([
  "educationLevel",
  "institute",
  "degree",
  "field",
  "gpa",
  "country",
  "duration",
  "durationYear",
  "other",
  "fundName",
  "isEducation",
  "endDate",
  "startDate",
  "finishDate",
  "note",
]);
const historyVisibleColumns = ref<string[]>([
  "educationLevel",
  "institute",
  "degree",
  "field",
  "gpa",
  "country",
  "duration",
  "durationYear",
  "other",
  "fundName",
  "isEducation",
  "endDate",
  "startDate",
  "finishDate",
  "note",
  "lastUpdateFullName",
  "lastUpdatedAt",
]);

async function onSubmit() {
  dialogConfirm(
    $q,
    async () => {
      dialogStatus.value === "create" ? addData() : editData(editId.value);
      closeDialog();
    },
    "ยืนยันการบันทึกข้อมูล",
    "ต้องการยืนยันการบันทึกข้อมูลนี้หรือไม่ ?"
  );
}

function filterSelector(val: string, update: Function, refData: string) {
  switch (refData) {
    case "educationOption":
      update(() => {
        educationOption.value = educationOptionFilter.value.filter(
          (v: any) => v.label.indexOf(val) > -1
        );
      });
      break;
    case "educationLevelOption":
      update(() => {
        educationLevelOption.value = educationLevelOptionFilter.value.filter(
          (v: any) => v.label.indexOf(val) > -1
        );
      });
      break;
    default:
      break;
  }
}

function clearForm() {
  isDate.value = "false";
  educationData.educationLevel = "";
  educationData.institute = "";
  educationData.startYear = new Date().getFullYear();
  educationData.endYear = new Date().getFullYear();
  educationData.finishDate = new Date();
  educationData.startDate = new Date();
  educationData.endDate = new Date();
  educationData.isEducation = null;
  educationData.degree = "";
  educationData.field = "";
  educationData.fundName = "";
  educationData.gpa = "";
  educationData.country = "";
  educationData.other = "";
  educationData.duration = "";
  educationData.durationYear = null;
  educationData.note = "";
}

function editForm(row: any) {
  dialogStatus.value = "edit";
  editId.value = row.id;
  isDate.value = row.isDate ? "true" : "false";
  educationData.educationLevel = row.educationLevel;
  educationData.institute = row.institute;
  educationData.finishDate = row.finishDate;
  educationData.startDate = row.startDate;
  educationData.endDate = row.endDate;
  educationData.isEducation = row.isEducation;
  educationData.degree = row.degree;
  educationData.field = row.field;
  educationData.fundName = row.fundName;
  educationData.gpa = row.gpa;
  educationData.country = row.country;
  educationData.other = row.other;
  educationData.duration = row.duration;
  educationData.durationYear = row.durationYear;
  educationData.note = row.note;
  educationData.startYear = +row.startDate.slice(0, 4);
  educationData.endYear = +row.endDate.slice(0, 4);
  dialog.value = true;
}

function closeDialog() {
  clearForm();
  dialog.value = false;
}
function closeHistoryDialog() {
  historyDialog.value = false;
}

async function fetchData(id: string) {
  showLoader();
  await http
    .get(config.API.profileNewEducationByProfileId(id))
    .then(async (res) => {
      rows.value = res.data.result;
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

async function fetchEducationLevel() {
  showLoader();
  await http
    .get(config.API.orgEducationLevel)
    .then(async (res) => {
      res.data.result.map((r: any) => {
        educationLevelOption.value.push({
          value: r.id,
          label: r.name,
        });
      });
      educationLevelOptionFilter.value = educationLevelOption.value;
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

async function fetchHistoryData(id: string) {
  showLoader();
  await http
    .get(config.API.profileNewEducationHisByEducationId(id))
    .then(async (res) => {
      historyRows.value = res.data.result;
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

async function addData() {
  await http
    .post(config.API.profileNewEducation, {
      ...educationData,
      startYear: undefined,
      endYear: undefined,
      isDate: isDate.value === "false" ? false : true,
    })
    .then(() => {
      fetchData(id.value);
      success($q, "บันทึกข้อมูลสำเร็จ");
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

function editData(idData: string) {
  http
    .patch(config.API.profileNewEducationByEducationId(idData), {
      ...educationData,
      profileId: undefined,
      startYear: undefined,
      endYear: undefined,
      isDate: isDate.value === "false" ? false : true,
    })
    .then(() => {
      fetchData(id.value);
      success($q, "บันทึกข้อมูลสำเร็จ");
    })
    .catch((err) => {
      messageError($q, err);
    })
    .finally(() => {
      hideLoader();
    });
}

// function deleteData(idData: string) {
//   dialogRemove($q, () =>
//     http
//       .delete(config.API.profileNewEducationByEducationId(idData))
//       .then(() => {
//         fetchData(id.value);
//         success($q, "ลบข้อมูลสำเร็จ");
//       })
//       .catch((err) => {
//         messageError($q, err);
//       })
//       .finally(() => {
//         hideLoader();
//       })
//   );
// }

onMounted(async () => {
  await fetchData(id.value);
  fetchEducationLevel();
});
</script>

<template>
  <div class="row items-center q-gutter-x-sm q-pb-sm">
    <q-btn
      round
      flat
      dense
      color="primary"
      icon="add"
      @click="
        () => {
          dialogStatus = 'create';
          clearForm();
          dialog = true;
        }
      "
    >
      <q-tooltip>เพิ่มข้อมูล</q-tooltip></q-btn
    >

    <q-space />
    <q-input dense outlined v-model="keyword" label="ค้นหา">
      <template v-slot:append>
        <q-icon name="search" />
      </template>
    </q-input>
    <q-select
      v-if="mode === 'table'"
      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"
    />

    <q-btn-toggle
      v-model="mode"
      dense
      class="no-shadow toggle-borderd"
      toggle-color="grey-4"
      :options="[
        { value: 'table', slot: 'table' },
        { value: 'card', slot: 'card' },
      ]"
    >
      <template v-slot:table>
        <q-icon
          name="format_list_bulleted"
          size="24px"
          :style="{
            color: mode === 'table' ? '#787B7C' : '#C9D3DB',
          }"
        />
      </template>

      <template v-slot:card>
        <q-icon
          name="mdi-view-grid-outline"
          size="24px"
          :style="{
            color: mode === 'card' ? '#787B7C' : '#C9D3DB',
          }"
        />
      </template>
    </q-btn-toggle>
  </div>

  <d-table
    :grid="mode === 'card'"
    ref="table"
    :columns="columns"
    :rows="rows"
    row-key="name"
    flat
    bordered
    :paging="true"
    :filter="keyword"
    dense
    v-model:pagination="pagination"
    :rows-per-page-options="[20, 50, 100]"
    class="custom-header-table"
    :visible-columns="visibleColumns"
  >
    <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-th auto-width />
      </q-tr>
    </template>
    <template v-slot:body="props" v-if="mode === 'table'">
      <q-tr :props="props" class="cursor-pointer">
        <q-td v-for="col in props.cols" :key="col.id">
          <div>
            {{ col.value === "" ? "-" : col.value }}
          </div>
        </q-td>
        <q-td auto-width>
          <q-btn
            color="primary"
            flat
            dense
            round
            class="q-mr-xs"
            size="14px"
            icon="mdi-pencil-outline"
            clickable
            @click="
              () => {
                editForm(props.row);
              }
            "
          >
            <q-tooltip>แก้ไขข้อมูล</q-tooltip>
          </q-btn>
          <q-btn
            color="info"
            flat
            dense
            round
            size="14px"
            icon="mdi-history"
            @click="
              () => (fetchHistoryData(props.row.id), (historyDialog = true))
            "
          >
            <q-tooltip>ประวัติแก้ไขประวัติการศึกษา</q-tooltip>
          </q-btn>
          <!-- <q-btn
            color="red"
            flat
            dense
            round
            size="14px"
            icon="mdi-delete"
            clickable
            @click.stop="deleteData(props.row.id)"
            v-close-popup
          >
            <q-tooltip>ลบข้อมูล</q-tooltip>
          </q-btn> -->
        </q-td>
      </q-tr>
    </template>
    <template v-slot:item="props" v-else>
      <div
        class="q-pa-xs col-xs-12 col-sm-6 col-md-6 col-lg-6 grid-style-transition"
      >
        <q-card bordered>
          <q-card-actions align="right" class="bg-grey-3">
            <q-btn
              flat
              round
              color="primary"
              size="14px"
              icon="mdi-pencil-outline"
              @click="editForm(props.row)"
            >
              <q-tooltip>แก้ไขข้อมูล</q-tooltip>
            </q-btn>
            <q-btn
              flat
              round
              color="info"
              size="14px"
              icon="history"
              @click="
                () => (fetchHistoryData(props.row.id), (historyDialog = true))
              "
            >
              <q-tooltip>ประวัติแก้ไขประวัติการศึกษา</q-tooltip>
            </q-btn>
          </q-card-actions>
          <q-separator />
          <div class="row">
            <div class="col-3 q-pa-sm label-color">ระดับการศึกษา</div>
            <div class="col-4 q-pa-sm">{{ props.cols[0].value }}</div>
            <div class="col-2 q-pa-sm label-color">สถานศึกษา</div>
            <div class="col-3 q-pa-sm">{{ props.cols[1].value }}</div>
            <div class="col-12">
              <div :class="`row bg-color`">
                <div class="col-3 q-pa-sm label-color">ตั้งแต่</div>
                <div class="col-4 q-pa-sm">
                  {{ props.cols[2].value }}
                </div>

                <div class="col-2 q-pa-sm label-color">ถึง</div>
                <div class="col-3 q-pa-sm">
                  {{ props.cols[3].value }}
                </div>
              </div>
            </div>
            <div class="col-3 q-pa-sm label-color">วันที่สำเร็จการศึกษา</div>
            <div class="col-9 q-pa-sm">
              {{ props.cols[4].value }}
            </div>
            <div class="col-12">
              <div :class="`row bg-color`">
                <div class="col-4 q-pa-sm label-color">
                  เป็นวุฒิการศึกษาในตำแหน่ง
                </div>
                <div class="col-3 q-pa-sm">
                  {{ props.cols[5].value ? "ใช่" : "ไม่ใช่" }}
                </div>
                <div class="col-2 q-pa-sm label-color">วุฒิการศึกษา</div>
                <div class="col-3 q-pa-sm">
                  {{ props.cols[6].value ? props.cols[6].value : "-" }}
                </div>
              </div>
            </div>
            <div class="col-3 q-pa-sm label-color">สาขาวืชา/ทาง</div>
            <div class="col-4 q-pa-sm">
              {{ props.cols[7].value ? props.cols[7].value : "-" }}
            </div>
            <div class="col-2 q-pa-sm label-color">ทุน</div>
            <div class="col-3 q-pa-sm">
              {{ props.cols[8].value ? props.cols[8].value : "-" }}
            </div>
            <div class="col-12">
              <div :class="`row bg-color`">
                <div class="col-3 q-pa-sm label-color">เกรดเฉลี่ย</div>
                <div class="col-4 q-pa-sm">
                  {{ props.cols[9].value ? props.cols[9].value : "-" }}
                </div>
                <div class="col-2 q-pa-sm label-color">ประเทศ</div>
                <div class="col-3 q-pa-sm">
                  {{ props.cols[10].value ? props.cols[10].value : "-" }}
                </div>
              </div>
            </div>
            <div class="col-3 q-pa-sm label-color">ข้อมูลการติดต่อ</div>
            <div class="col-4 q-pa-sm">
              {{ props.cols[11].value ? props.cols[11].value : "-" }}
            </div>
            <div class="col-2 q-pa-sm label-color">ระยะเวลา</div>
            <div class="col-3 q-pa-sm">
              {{ props.cols[12].value ? props.cols[12].value : "-" }}
            </div>
          </div>
          <div class="col-12">
            <div :class="`row bg-color`">
              <div class="col-3 q-pa-sm label-color">ระยะเวลาหลักสูตร (ปี)</div>
              <div class="col-4 q-pa-sm">
                {{ props.cols[13].value === 0 ? "-" : props.cols[13].value }}
              </div>
              <div class="col-2 q-pa-sm label-color">หมายเหตุ</div>
              <div class="col-3 q-pa-sm">
                {{ props.cols[14].value ? props.cols[14].value : "-" }}
              </div>
            </div>
          </div>
        </q-card>
      </div>
    </template>
  </d-table>

  <q-dialog v-model="dialog" class="dialog" persistent>
    <q-card>
      <q-form @submit.prevent greedy @validation-success="onSubmit()">
        <dialog-header
          :tittle="
            dialogStatus == 'edit'
              ? 'แก้ไขข้อมูลประวัติการศึกษา'
              : 'เพิ่มข้อมูลประวัติการศึกษา'
          "
          :close="closeDialog"
        />
        <q-separator />
        <q-card-section style="max-height: 60vh" class="scroll">
          <div class="row q-col-gutter-sm">
            <div class="col-6">
              <q-select
                outlined
                dense
                emit-value
                option-value="label"
                option-label="label"
                map-options
                bg-color="white"
                v-model="educationData.educationLevel"
                :options="educationLevelOption"
                input-debounce="0"
                use-input
                @filter="(inputValue:string,
                    doneFn:Function) => filterSelector(inputValue, doneFn,'educationLevelOption'
                    ) "
                :rules="[(val) => !!val || `${'กรุณาเลือกระดับการศึกษา'}`]"
                label="ระดับการศึกษา"
                class="inputgreen"
                hide-bottom-space
              />
            </div>
            <div class="col-6">
              <q-input
                outlined
                v-model="educationData.institute"
                label="สถานศึกษา"
                bg-color="white"
                dense
                class="inputgreen"
                :rules="[(val) => !!val || `${'กรุณากรอกสถานศึกษา'}`]"
                hide-bottom-space
              />
            </div>
          </div>
          <q-card
            flat
            bordered
            class="q-px-md q-pb-md q-my-sm borderCard"
          >
            <div class="row col-12 q-gutter-md q-py-sm text-grey-7">
              <q-radio
                v-model="isDate"
                checked-icon="task_alt"
                unchecked-icon="panorama_fish_eye"
                val="false"
                label="ปี"
                class="inputgreen"
                dense
              />
              <q-radio
                v-model="isDate"
                checked-icon="task_alt"
                unchecked-icon="panorama_fish_eye"
                val="true"
                class="inputgreen"
                label="วัน/เดือน/ปี"
                dense
              />
            </div>
            <div v-if="isDate === 'false'" class="row q-col-gutter-sm">
              <div class="col">
                <datepicker
                  menu-class-name="modalfix"
                  v-model="educationData.startYear"
                  :locale="'th'"
                  autoApply
                  year-picker
                  :enableTimePicker="false"
                  @update:modelValue="
                  (v:number) =>
                    (educationData.startDate = new Date(
                      `${v}-01-01T00:00:02.010+07:00`
                    ))
                "
                >
                  <template #year="{ year }">{{ year + 543 }}</template>
                  <template #year-overlay-value="{ value }">{{
                    parseInt(value + 543)
                  }}</template>
                  <template #trigger>
                    <q-input
                      dense
                      outlined
                      class="inputgreen"
                      hide-bottom-space
                      :model-value="educationData.startYear + 543"
                      :rules="[
                        (val) => !!val || `${'กรุณาเลือกปีที่เริ่มต้นศึกษา'}`,
                      ]"
                      :label="`${'ปีที่เริ่มต้นศึกษา'}`"
                    >
                      <template v-slot:prepend>
                        <q-icon
                          name="event"
                          class="cursor-pointer"
                          style="color: var(--q-primary)"
                        >
                        </q-icon>
                      </template>
                    </q-input>
                  </template>
                </datepicker>
              </div>
              <div class="col">
                <datepicker
                  menu-class-name="modalfix"
                  v-model="educationData.endYear"
                  :locale="'th'"
                  autoApply
                  year-picker
                  :enableTimePicker="false"
                  @update:modelValue="
                  (v:number) =>
                    (educationData.endDate = new Date(
                      `${v}-01-01T00:00:02.010+07:00`
                    ))
                "
                >
                  <template #year="{ year }">{{ year + 543 }}</template>
                  <template #year-overlay-value="{ value }">{{
                    parseInt(value + 543)
                  }}</template>
                  <template #trigger>
                    <q-input
                      dense
                      class="inputgreen"
                      lazy-rules
                      outlined
                      hide-bottom-space
                      :model-value="educationData.endYear + 543"
                      :rules="[
                        (val) => !!val || `${'กรุณาเลือกปีที่จบการศึกษา'}`,
                      ]"
                      :label="`${'ปีที่จบการศึกษา'}`"
                    >
                      <template v-slot:prepend>
                        <q-icon
                          name="event"
                          class="cursor-pointer"
                          style="color: var(--q-primary)"
                        >
                        </q-icon>
                      </template>
                    </q-input>
                  </template>
                </datepicker>
              </div>
            </div>
            <div v-if="isDate === 'true'" class="row q-col-gutter-sm">
              <div class="col">
                <datepicker
                  menu-class-name="modalfix"
                  v-model="educationData.startDate"
                  :locale="'th'"
                  autoApply
                  :enableTimePicker="false"
                  week-start="0"
                  @update:modelValue="
                  (v: Date) =>
                    (educationData.startYear = parseInt(
                      v.toString().slice(11, 15)
                    ))
                "
                >
                  <template #year="{ year }">{{ year + 543 }}</template>
                  <template #year-overlay-value="{ value }">{{
                    parseInt(value + 543)
                  }}</template>
                  <template #trigger>
                    <q-input
                      outlined
                      class="inputgreen"
                      dense
                      lazy-rules
                      :model-value="date2Thai(educationData.startDate)"
                      :rules="[
                        (val) =>
                          !!val || `${'กรุณาเลือกวันที่เริ่มต้นศึกษา'}`,
                      ]"
                      hide-bottom-space
                      :label="`${'วันที่เริ่มต้นศึกษา'}`"
                    >
                      <template v-slot:prepend>
                        <q-icon
                          name="event"
                          class="cursor-pointer"
                          color="primary"
                        >
                        </q-icon>
                      </template>
                    </q-input>
                  </template>
                </datepicker>
              </div>
              <div class="col">
                <datepicker
                  menu-class-name="modalfix"
                  v-model="educationData.endDate"
                  :locale="'th'"
                  autoApply
                  :enableTimePicker="false"
                  week-start="0"
                  @update:modelValue="
                  (v: Date)  =>
                    (educationData.endYear = parseInt(
                      v.toString().slice(11, 15)
                    ))
                "
                >
                  <template #year="{ year }">{{ year + 543 }}</template>
                  <template #year-overlay-value="{ value }">{{
                    parseInt(value + 543)
                  }}</template>
                  <template #trigger>
                    <q-input
                      outlined
                      dense
                      class="inputgreen"
                      lazy-rules
                      :model-value="date2Thai(educationData.endDate)"
                      :rules="[
                        (val) => !!val || `${'กรุณาเลือกวันที่จบการศึกษา'}`,
                      ]"
                      hide-bottom-space
                      :label="`${'วันที่จบการศึกษา'}`"
                    >
                      <template v-slot:prepend>
                        <q-icon
                          name="event"
                          class="cursor-pointer"
                          color="primary"
                        >
                        </q-icon>
                      </template>
                    </q-input>
                  </template>
                </datepicker>
              </div>
            </div>
          </q-card>
          <div class="row q-col-gutter-sm">
            <div class="col-6">
              <datepicker
                menu-class-name="modalfix"
                v-model="educationData.finishDate"
                :locale="'th'"
                autoApply
                :enableTimePicker="false"
                week-start="0"
              >
                <template #year="{ year }">{{ year + 543 }}</template>
                <template #year-overlay-value="{ value }">{{
                  parseInt(value + 543)
                }}</template>
                <template #trigger>
                  <q-input
                    outlined
                    bg-color="white"
                    dense
                    class="inputgreen"
                    lazy-rules
                    :model-value="date2Thai(educationData.finishDate)"
                    :rules="[
                      (val) => !!val || `${'กรุณาเลือกวันที่สำเร็จการศึกษา'}`,
                    ]"
                    hide-bottom-space
                    :label="`${'วันที่สำเร็จการศึกษา'}`"
                  >
                    <template v-slot:prepend>
                      <q-icon
                        name="event"
                        class="cursor-pointer"
                        color="primary"
                      >
                      </q-icon>
                    </template>
                  </q-input>
                </template>
              </datepicker>
            </div>
            <div class="col-6">
              <q-select
                outlined
                dense
                emit-value
                map-options
                bg-color="white"
                v-model="educationData.isEducation"
                :options="educationOption"
                option-value="value"
                class="inputgreen"
                option-label="label"
                input-debounce="0"
                use-input
                @filter="(inputValue:string,
                    doneFn:Function) => filterSelector(inputValue, doneFn,'educationOption'
                    ) "
                :rules="[
                  (val) =>
                    val !== null || `${'กรุณาเลือกวุฒิการศึกษาในตำแหน่ง'}`,
                ]"
                label="เป็นวุฒิการศึกษาในตำแหน่ง"
                hide-bottom-space
              />
            </div>

            <div class="col-6">
              <q-input
                outlined
                v-model="educationData.degree"
                label="วุฒิการศึกษา"
                class="inputgreen"
                bg-color="white"
                dense
                :rules="[(val) => !!val || `${'กรุณากรอกวุฒิการศึกษา'}`]"
                hide-bottom-space
              />
            </div>
            <div class="col-6">
              <q-input
                outlined
                class="inputgreen"
                v-model="educationData.field"
                label="สาขาวิชา/ทาง"
                bg-color="white"
                dense
                hide-bottom-space
              />
            </div>

            <div class="col-6">
              <q-input
                outlined
                v-model="educationData.fundName"
                label="ทุน"
                class="inputgreen"
                bg-color="white"
                dense
                hide-bottom-space
              />
            </div>
            <div class="col-6">
              <q-input
                outlined
                class="inputgreen"
                v-model="educationData.gpa"
                label="เกรดเฉลี่ย"
                bg-color="white"
                dense
                mask="#.##"
                hide-bottom-space
              />
            </div>

            <div class="col-6">
              <q-input
                outlined
                class="inputgreen"
                v-model="educationData.country"
                label="ประเทศ"
                bg-color="white"
                dense
                hide-bottom-space
              />
            </div>
            <div class="col-6">
              <q-input
                outlined
                class="inputgreen"
                v-model="educationData.other"
                label="ข้อมูลติดต่อ"
                bg-color="white"
                dense
                hide-bottom-space
              />
            </div>

            <div class="col-6">
              <q-input
                outlined
                class="inputgreen"
                v-model="educationData.duration"
                label="ระยะเวลา"
                :rules="[(val) => !!val || `${'กรุณากรอกระยะเวลา'}`]"
                bg-color="white"
                dense
                hide-bottom-space
              />
            </div>
            <div class="col-6">
              <q-input
                outlined
                class="inputgreen"
                v-model="educationData.durationYear"
                label="ระยะเวลาหลักสูตร (ปี)"
                :rules="[
                  (val) => !!val || `${'กรุณากรอกระยะเวลาหลักสูตร (ปี)'}`,
                ]"
                bg-color="white"
                type="number"
                dense
                hide-bottom-space
              />
            </div>

            <div class="col-12">
              <q-input
                outlined
                class="inputgreen"
                v-model="educationData.note"
                label="หมายเหตุ"
                bg-color="white"
                dense
                type="textarea"
              />
            </div>
          </div>
        </q-card-section>
        <q-separator />

        <q-card-actions align="right">      
            <q-btn
              id="onSubmit"
              type="submit"
              dense
              unelevated
              label="บันทึก"
              color="public"
              class="q-px-md"
            >
              <q-tooltip>บันทึกข้อมูล</q-tooltip>
            </q-btn>
         </q-card-actions>
      </q-form>
    </q-card>
  </q-dialog>

  <q-dialog v-model="historyDialog" class="dialog" persistent>
    <q-card style="min-width: 80%">
      <dialog-header
        tittle="ประวัติแก้ไขประวัติการศึกษา"
        :close="closeHistoryDialog"
      />
      <q-separator />

      <q-card-section style="max-height: 50vh" class="scroll">
          <q-toolbar style="padding: 0px" class="text-primary q-mb-sm">
            <q-space />
            <q-input
              dense
              outlined
              bg-color="white"
              v-model="historyKeyword"
              label="ค้นหา"
              class="q-mr-sm"
            >
              <template v-slot:append>
                <q-icon name="search" />
              </template>
            </q-input>

            <q-select
              v-model="historyVisibleColumns"
              multiple
              outlined
              dense
              bg-color="white"
              options-dense
              :display-value="$q.lang.table.columns"
              emit-value
              map-options
              :options="historyColumns"
              option-value="name"
              options-cover
              style="min-width: 150px"
            />
          </q-toolbar>
          <d-table
            ref="table"
            :columns="historyColumns"
            :rows="historyRows"
            row-key="name"
            flat
            bordered
            :paging="true"
            dense
            :filter="historyKeyword"
            v-model:pagination="historyPagination"
            :rows-per-page-options="[20, 50, 100]"
            class="custom-header-table"
            :visible-columns="historyVisibleColumns"
          >
            <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-th auto-width />
              </q-tr>
            </template>
            <template v-slot:body="props">
              <q-tr :props="props" class="cursor-pointer">
                <q-td v-for="col in props.cols" :key="col.id">
                  <div>
                    {{ col.value === "" ? "-" : col.value }}
                  </div>
                </q-td>
                <q-td auto-width> </q-td>
              </q-tr>
            </template>
          </d-table>
      </q-card-section>
    </q-card>
  </q-dialog>
</template>

<style scoped>
.borderCard {
  border: 1px solid #d0d0d0;
}

.label-color {
  color: #747474cc;
}

.bg-color {
  background-color: #fafafa;
}
</style>
