<!-- card บุคคลที่สามารถติดต่อได้ -->
<template>
  <HeaderTop
    v-model:edit="edit"
    header="บุคคลที่สามารถติดต่อได้"
    icon="mdi-account-circle"
    :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-xs-12 col-sm-3 col-md-2">
        <q-select
          :class="getClass(status == 'register' || status == 'rejectRegister')"
          :readonly="!(status == 'register' || status == 'rejectRegister')"
          :borderless="!(status == 'register' || status == 'rejectRegister')"
          :rules="[(val) => !!val || `${'กรุณาเลือก คำนำหน้า'}`]"
          :outlined="status == 'register' || status == 'rejectRegister'"
          dense
          lazy-rules
          v-model="defaultContact.contactprefixId"
          emit-value
          map-options
          option-label="name"
          :options="prefixOptions"
          option-value="id"
          :label="`${'คำนำหน้า'}`"
        />
      </div>
      <div class="col-xs-12 col-sm-3 col-md-3">
        <q-input
          :class="getClass(status == 'register' || status == 'rejectRegister')"
          :outlined="status == 'register' || status == 'rejectRegister'"
          dense
          lazy-rules
          :readonly="!(status == 'register' || status == 'rejectRegister')"
          :borderless="!(status == 'register' || status == 'rejectRegister')"
          v-model="defaultContact.contactfirstname"
          :rules="[(val) => !!val || `${'กรุณากรอก ชื่อ'}`]"
          :label="`${'ชื่อ'}`"
        />
      </div>
      <div class="col-xs-12 col-sm-3 col-md-3">
        <q-input
          :class="getClass(status == 'register' || status == 'rejectRegister')"
          :outlined="status == 'register' || status == 'rejectRegister'"
          dense
          lazy-rules
          :readonly="!(status == 'register' || status == 'rejectRegister')"
          :borderless="!(status == 'register' || status == 'rejectRegister')"
          v-model="defaultContact.contactlastname"
          :rules="[(val) => !!val || `${'กรุณากรอก นามสกุล'}`]"
          :label="`${'นามสกุล'}`"
        />
      </div>
      <div class="col-xs-12 col-sm-3 col-md-4">
        <q-input
          :class="getClass(status == 'register' || status == 'rejectRegister')"
          :outlined="status == 'register' || status == 'rejectRegister'"
          dense
          lazy-rules
          :readonly="!(status == 'register' || status == 'rejectRegister')"
          :borderless="!(status == 'register' || status == 'rejectRegister')"
          v-model="defaultContact.contactrelations"
          :rules="[(val) => !!val || `${'กรุณากรอกความสัมพันธ์'}`]"
          :label="`${'เกี่ยวข้องเป็น'}`"
        />
      </div>
      <div class="col-xs-12 col-sm-3 col-md-4">
        <q-input
          :outlined="status == 'register' || status == 'rejectRegister'"
          dense
          :counter="status == 'register' || status == 'rejectRegister' ? true : false"
          lazy-rules
          type="tel"
          mask="##########"
          maxlength="10"
          :class="getClass(status == 'register' || status == 'rejectRegister')"
          :readonly="!(status == 'register' || status == 'rejectRegister')"
          :borderless="!(status == 'register' || status == 'rejectRegister')"
          v-model="defaultContact.contacttel"
          :label="`${'โทรศัพท์'}`"
          :rules="[
            (val) => val.length == 10 || `${'กรุณากรอก โทรศัพท์'}`,
            (val) => /^[0-9]*$/.test(val) || `${'กรุณากรอกข้อมูลโทรศัพท์ให้ถูกต้อง'}`
          ]"
        />
      </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 { useCounterMixin } from '@/stores/mixin'
import { useDataStore } from '@/stores/data'
import { useRoute } from 'vue-router'
import { useQuasar } from 'quasar'
import type { Contact } from '@/modules/01_exam/interface/index/Main'
import { defaultContact, changeData } from '@/modules/01_exam/interface/index/Main'
import HeaderTop from '@/components/top.vue'
import { DataOption } from '../../interface/index/Main'

const props = defineProps({
  status: {
    type: String,
    required: true
  },
  form: {
    type: Object,
    required: true
  },
  prefixOptions: {
    type: Array as PropType<DataOption[]>,
    required: true
  }
})
const emit = defineEmits(['update:form'])

const $q = useQuasar()
const dataStore = useDataStore()
const { loaderPage } = dataStore
const mixin = useCounterMixin()
const { messageError } = mixin
const edit = ref<boolean>(true)
const myform = ref<any>({})
const route = useRoute()
const examId = ref<string>(route.params.id.toString())
const positionId = ref<string>(route.params.positionId.toString())

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

watch(defaultContact, async (count: Contact, prevCount: Contact) => {
  await changeData('contact', count)
})

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

const fetchData = async () => {
  loaderPage(true)
  await http
    .get(config.API.candidateContact(examId.value, positionId.value))
    .then((res) => {
      const data = res.data.result
      if (data != null) {
        defaultContact.value.contactprefixId = data.contactPrefixId
        defaultContact.value.contactfirstname = data.contactFirstname
        defaultContact.value.contactlastname = data.contactLastname
        defaultContact.value.contactrelations = data.contactRelations
        defaultContact.value.contacttel = data.contactTel
      }
    })
    .catch((e) => {
      //   messageError($q, e)
    })
    .finally(() => {
      loaderPage(false)
    })
}

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