<!-- card วุฒิที่ใช้สมัครสอบ -->
<template>
  <HeaderTop
    v-model:edit="edit"
    header="วุฒิที่ใช้สมัครสอบ"
    icon="mdi-briefcase"
    :addData="true"
    :editOnly="false"
    :editData="false"
  />
  <q-form ref="myform">
    <div class="row col-12 items-center q-col-gutter-x-sm q-col-gutter-y-xs">
      <div class="col-12 row">
        <div class="col-3">
          <q-select
            :class="getClass(status == 'checkRegister' || status == 'payment')"
            :readonly="!(status == 'checkRegister' || status == 'payment')"
            :borderless="!(status == 'checkRegister' || status == 'payment')"
            :outlined="status == 'checkRegister' || status == 'payment'"
            dense
            lazy-rules
            emit-value
            map-options
            option-label="name"
            option-value="id"
            :options="educationLevelOptions"
            v-model="defaultEducation.educationLevelExamId"
            :rules="[(val) => !!val || `${'กรุณาเลือก วุฒิที่ใช้สมัครสอบ'}`]"
            :label="`${'วุฒิที่ใช้สมัครสอบ'}`"
          />
        </div>
        <div class="col-3">
          <q-input
            class="q-pl-sm"
            :class="getClass(status == 'checkRegister' || status == 'payment')"
            :outlined="status == 'checkRegister' || status == 'payment'"
            dense
            lazy-rules
            autogrow
            :readonly="!(status == 'checkRegister' || status == 'payment')"
            :borderless="!(status == 'checkRegister' || status == 'payment')"
            v-model="defaultEducation.educationName"
            :rules="[(val) => !!val || `${'กรุณากรอก ชื่อปริญญา'}`]"
            :label="`${'ชื่อปริญญา'}`"
          />
        </div>
        <div class="col-3">
          <q-input
            class="q-pl-sm"
            :class="getClass(status == 'checkRegister' || status == 'payment')"
            :outlined="status == 'checkRegister' || status == 'payment'"
            dense
            lazy-rules
            autogrow
            :readonly="!(status == 'checkRegister' || status == 'payment')"
            :borderless="!(status == 'checkRegister' || status == 'payment')"
            v-model="defaultEducation.educationMajor"
            :rules="[(val) => !!val || `${'กรุณากรอก สาขาวิชา/วิชาเอก'}`]"
            :label="`${'สาขาวิชา/วิชาเอก'}`"
          />
        </div>
      </div>
      <div class="col-12 row">
        <div class="col-3">
          <q-input
            :class="getClass(status == 'checkRegister' || status == 'payment')"
            :outlined="status == 'checkRegister' || status == 'payment'"
            dense
            lazy-rules
            autogrow
            :readonly="!(status == 'checkRegister' || status == 'payment')"
            :borderless="!(status == 'checkRegister' || status == 'payment')"
            v-model="defaultEducation.educationLocation"
            :rules="[(val) => !!val || `${'กรุณากรอก ชื่อสถานศึกษา'}`]"
            :label="`${'ชื่อสถานศึกษา'}`"
          />
        </div>
        <div class="col-3">
          <q-select
            class="q-pl-sm"
            :class="getClass(status == 'checkRegister' || status == 'payment')"
            :readonly="!(status == 'checkRegister' || status == 'payment')"
            :borderless="!(status == 'checkRegister' || status == 'payment')"
            :outlined="status == 'checkRegister' || status == 'payment'"
            dense
            lazy-rules
            emit-value
            map-options
            option-label="name"
            option-value="id"
            :options="educationTypeOptions"
            v-model="defaultEducation.educationType"
            :rules="[(val) => !!val || `${'กรุณาเลือก ประเภทสถานศึกษา'}`]"
            :label="`${'ประเภทสถานศึกษา'}`"
          />
        </div>
        <div class="col-3">
          <datepicker
            :readonly="!(status == 'checkRegister' || status == 'payment')"
            v-model="defaultEducation.educationEndDate"
            :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
                class="q-pl-sm"
                :class="getClass(edit)"
                :outlined="edit"
                dense
                lazy-rules
                :borderless="!edit"
                :model-value="date2Thai(defaultEducation.educationEndDate)"
                :rules="[
                  (val) => !!val || `${'กรุณาเลือกวันที่สำเร็จการศึกษา'}`,
                ]"
                hide-bottom-space
                :label="`${'วันที่สำเร็จการศึกษา'}`"
              >
                <template v-slot:prepend>
                  <q-icon
                    name="event"
                    class="cursor-pointer"
                    :style="
                      edit ? 'color: var(--q-primary)' : 'color: var(--q-grey)'
                    "
                  >
                  </q-icon>
                </template>
              </q-input>
            </template>
          </datepicker>
        </div>
        <div class="col-3">
          <q-input
            class="q-pl-sm"
            :class="getClass(status == 'checkRegister' || status == 'payment')"
            :outlined="status == 'checkRegister' || status == 'payment'"
            dense
            lazy-rules
            type="number"
            :readonly="!(status == 'checkRegister' || status == 'payment')"
            :borderless="!(status == 'checkRegister' || status == 'payment')"
            v-model="defaultEducation.educationScores"
            :rules="[(val) => !!val || `${'กรุณากรอก คะแนนเฉลี่ยสะสม'}`]"
            :label="`${'คะแนนเฉลี่ยสะสม'}`"
          />
        </div>
      </div>
      <div class="col-12 row">
        <div class="col-3">
          <q-select
            :class="getClass(status == 'checkRegister' || status == 'payment')"
            :readonly="!(status == 'checkRegister' || status == 'payment')"
            :borderless="!(status == 'checkRegister' || status == 'payment')"
            :outlined="status == 'checkRegister' || status == 'payment'"
            dense
            lazy-rules
            emit-value
            map-options
            option-label="name"
            option-value="id"
            :options="educationLevelOptions"
            v-model="defaultEducation.educationLevelHighId"
            :rules="[(val) => !!val || `${'กรุณาเลือก วุฒิการศึกษาสูงสุด'}`]"
            :label="`${'วุฒิการศึกษาสูงสุด'}`"
          />
        </div>
      </div>
    </div>
  </q-form>
</template>
<script setup lang="ts">
import { ref, onMounted, watch, PropType } from "vue";
import http from "@/plugins/http";
import config from "@/app.config";
import type {
  DataOption,
  Education,
} from "@/modules/03_recruiting/interface/index/Main";
import {
  defaultEducation,
  changeData,
} from "@/modules/03_recruiting/interface/index/Main";
import HeaderTop from "@/modules/03_recruiting/components/top.vue";
import { useRoute } from "vue-router";

import { useCounterMixin } from "@/stores/mixin";
import { useQuasar } from "quasar";

const props = defineProps({
  status: {
    type: String,
    required: true,
  },
  form: {
    type: Object,
    required: true,
  },
  educationLevelOptions: {
    type: Array as PropType<DataOption[]>,
    required: true,
  },
});

const $q = useQuasar();
const edit = ref<boolean>(true);
const myform = ref<any>({});
const route = useRoute();
const candidateId = ref<string>(route.params.candidateId.toString());
const educationTypeOptions = ref<any>([
  {
    name: "รัฐบาล",
    id: "รัฐบาล",
  },
  {
    name: "เอกชน",
    id: "เอกชน",
  },
]);

const mixin = useCounterMixin();
const { messageError, showLoader, hideLoader, date2Thai } = mixin;

const emit = defineEmits(["update:form"]);

watch(myform, async (count: any, prevCount: any) => {
  emit("update:form", count);
});

watch(defaultEducation, async (count: Education, prevCount: Education) => {
  await changeData("education", count);
});

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

const fetchData = async () => {
  showLoader();
  await http
    .get(config.API.candidateEducation(candidateId.value))
    .then((res) => {
      const data = res.data.result;
      if (data != null) {
        defaultEducation.value.educationLevelExamId = data.educationLevelExamId;
        defaultEducation.value.educationName = data.educationName;
        defaultEducation.value.educationMajor = data.educationMajor;
        defaultEducation.value.educationLocation = data.educationLocation;
        defaultEducation.value.educationType = data.educationType;
        defaultEducation.value.educationEndDate = data.educationEndDate;
        defaultEducation.value.educationScores = data.educationScores;
        defaultEducation.value.educationLevelHighId = data.educationLevelHighId;
      }
    })
    .catch((e) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
};

const getClass = (val: boolean) => {
  return {
    "full-width inputgreen cursor-pointer": val,
    "full-width cursor-pointer": !val,
  };
};
</script>
