<script setup lang="ts">
import useOptionStore from 'src/stores/options';
import {
  dateFormat,
  calculateAge,
  parseAndFormatDate,
  disabledAfterToday,
} from 'src/utils/datetime';
import { useI18n } from 'vue-i18n';

const { locale } = useI18n();
const optionStore = useOptionStore();

const firstName = defineModel<string>('firstName');
const lastName = defineModel<string>('lastName');
const firstNameEN = defineModel<string>('firstNameEN');
const lastNameEN = defineModel<string>('lastNameEN');
const telephoneNo = defineModel<string>('telephoneNo');
const email = defineModel<string>('email');
const gender = defineModel<string>('gender');
const birthDate = defineModel<Date | string | null>('birthDate');
const nationality = defineModel<string>('nationality');

defineProps<{
  dense?: boolean;
  outlined?: boolean;
  readonly?: boolean;
  separator?: boolean;
  employee?: boolean;
  title?: string;
  prefixId: string;
}>();
</script>
<template>
  <div class="col-md-3 col-12 app-text-muted">
    • {{ title || $t('formDialogTitlePersonnel') }}
  </div>
  <div class="col-md-9 col-12 row q-col-gutter-md">
    <q-input
      lazy-rules="ondemand"
      :for="`${prefixId}-input-first-name`"
      :dense="dense"
      outlined
      :readonly="readonly"
      hide-bottom-space
      class="col-md-3 col-6"
      :label="$t('formDialogInputFirstName')"
      v-model="firstName"
      :rules="[
        (val: string) => !!val || $t('formDialogInputFirstNameValidate'),
      ]"
    />
    <q-input
      lazy-rules="ondemand"
      :for="`${prefixId}-input-last-name`"
      :dense="dense"
      outlined
      :readonly="readonly"
      hide-bottom-space
      class="col-md-3 col-6"
      :label="$t('formDialogInputLastName')"
      v-model="lastName"
      :rules="[(val: string) => !!val || $t('formDialogInputLastNameValidate')]"
    />
    <q-input
      lazy-rules="ondemand"
      :for="`${prefixId}-input-first-name-en`"
      :dense="dense"
      outlined
      :readonly="readonly"
      hide-bottom-space
      class="col-md-3 col-6"
      :label="$t('formDialogInputFirstNameEN')"
      v-model="firstNameEN"
      :rules="[
        (val: string) => !!val || $t('formDialogInputFirstNameENValidate'),
      ]"
    />
    <q-input
      lazy-rules="ondemand"
      :for="`${prefixId}-input-last-name-en`"
      :dense="dense"
      outlined
      :readonly="readonly"
      hide-bottom-space
      class="col-md-3 col-6"
      :label="$t('formDialogInputLastNameEN')"
      v-model="lastNameEN"
      :rules="[
        (val: string) => !!val || $t('formDialogInputLastNameENValidate'),
      ]"
    />
    <q-input
      lazy-rules="ondemand"
      v-if="!employee"
      :for="`${prefixId}-input-telephone`"
      :dense="dense"
      outlined
      :readonly="readonly"
      class="col-6"
      :label="$t('formDialogInputTelephone')"
      v-model="telephoneNo"
      mask="##########"
    />
    <q-input
      lazy-rules="ondemand"
      v-if="!employee"
      :for="`${prefixId}-input-email`"
      :dense="dense"
      outlined
      :readonly="readonly"
      :label="$t('formDialogInputEmail')"
      class="col-6"
      v-model="email"
    />
    <q-select
      lazy-rules="ondemand"
      v-if="!employee"
      :id="`${prefixId}-select-gender`"
      :dense="dense"
      outlined
      :readonly="readonly"
      :hide-dropdown-icon="readonly"
      emit-value
      map-options
      :label="$t('formDialogInputGender')"
      class="col-md-3 col-6"
      option-label="label"
      option-value="value"
      v-model="gender"
      :options="optionStore.globalOption.gender"
    />

    <VueDatePicker
      :id="`${prefixId}-input-birth-date`"
      :for="`${prefixId}-input-birth-date`"
      utc
      autoApply
      v-model="birthDate"
      :disabled-dates="disabledAfterToday"
      :teleport="true"
      :dark="$q.dark.isActive"
      :locale="$i18n.locale === 'th-th' ? 'th' : 'en'"
      :enableTimePicker="false"
      :disabled="readonly"
      class="col-md-3 col-6"
    >
      <template #year="{ value }">
        {{ $i18n.locale === 'th-th' ? value + 543 : value }}
      </template>
      <template #year-overlay-value="{ value }">
        {{ $i18n.locale === 'th-th' ? value + 543 : value }}
      </template>
      <template #trigger>
        <q-input
          lazy-rules="ondemand"
          :for="`${prefixId}-input-birth-date`"
          hide-bottom-space
          placeholder="DD/MM/YYYY"
          :label="$t('formDialogInputBirthDate')"
          :dense="dense"
          outlined
          :readonly="readonly"
          :rules="[
            (val: string) =>
              !!val || $t('selectValidate') + $t('formDialogInputBirthDate'),
          ]"
          :mask="readonly ? '' : '##/##/####'"
          :model-value="
            birthDate && readonly
              ? dateFormat(birthDate)
              : dateFormat(birthDate, false, false, true)
          "
          @update:model-value="
            (v) => {
              if (v && v.toString().length === 10) {
                const today = new Date();
                const date = parseAndFormatDate(v, locale);
                birthDate = date && date > today ? today : date;
              }
            }
          "
        >
          <!-- v-model="birthDate" -->
          <template v-slot:prepend>
            <q-icon
              size="xs"
              name="mdi-calendar-blank-outline"
              class="cursor-pointer"
              color="positive"
            />
          </template>
        </q-input>
      </template>
    </VueDatePicker>
    <q-input
      lazy-rules="ondemand"
      :for="`${prefixId}-input-age`"
      :dense="dense"
      outlined
      readonly
      :label="$t('formDialogInputAge')"
      class="col-md-3 col-6"
      :model-value="
        birthDate?.toString() === 'Invalid Date' ||
        birthDate?.toString() === undefined
          ? ''
          : calculateAge(birthDate)
      "
    />
    <q-select
      lazy-rules="ondemand"
      v-if="employee"
      :id="`${prefixId}-select-gender`"
      hide-bottom-space
      :dense="dense"
      outlined
      :readonly="readonly"
      :hide-dropdown-icon="readonly"
      emit-value
      map-options
      :label="$t('formDialogInputGender')"
      class="col-md-3 col-6"
      option-label="label"
      option-value="value"
      v-model="gender"
      :options="optionStore.globalOption.gender"
      :rules="[
        (val: string) =>
          !!val || $t('selectValidate') + $t('formDialogInputGender'),
      ]"
    />
    <q-select
      lazy-rules="ondemand"
      v-if="employee"
      :id="`${prefixId}-select-nationality`"
      hide-bottom-space
      :dense="dense"
      outlined
      :readonly="readonly"
      :hide-dropdown-icon="readonly"
      emit-value
      map-options
      options-dense
      :label="$t('formDialogInputNationality')"
      class="col-md-3 col-6"
      option-label="label"
      option-value="label"
      v-model="nationality"
      :options="optionStore.globalOption.nationality"
      :rules="[
        (val: string) =>
          !!val || $t('selectValidate') + $t('formDialogInputNationality'),
      ]"
    />
  </div>
  <q-separator
    v-if="separator"
    class="col-12 q-mb-md"
    style="padding-block: 0.5px; margin-top: 32px"
  />
</template>
