<template>
  <BasicModal
    :title="isEditMode ? 'ویرایش سابقه تحصیلی' : 'افزودن سابقه تحصیلی جدید'"
    title-en="Education History"
    width="95%"
    max-width="48rem"
    min-width="auto"
    :show-close-button="true"
  >
    <template #default="{ close }">
      <form class="add-education" @submit.prevent="onSubmit(close)">
        <BadgeSelectField
          v-model="form.degree"
          label="مقطع تحصیلی"
          :options="degreeOptions"
          :error="errors.degree"
        />

        <TextField
          v-model="form.universityName"
          name="universityName"
          label="نام دانشگاه"
          :error="errors.universityName"
          @blur="validateAt('universityName', form.universityName)"
        />

        <TextField
          v-model="form.fieldOfStudy"
          name="fieldOfStudy"
          label="رشته تحصیلی"
          :error="errors.fieldOfStudy"
          @blur="validateAt('fieldOfStudy', form.fieldOfStudy)"
        />

        <div class="add-education__row">
          <TextField
            v-model="form.startYear"
            name="startYear"
            label="سال شروع"
            inputmode="numeric"
            :convert-digits="true"
          />
          <TextField
            v-model="form.endYear"
            name="endYear"
            label="سال پایان"
            inputmode="numeric"
            :convert-digits="true"
          />
        </div>

        <div class="add-education__divider" />

        <footer class="add-education__footer">
          <BaseButton type="submit" text="ذخیره اطلاعات" custom-class="add-education__submit">
            <template #appendIcon>
              <SvgIcon name="arrow-left" :size="18" color="#fff" />
            </template>
          </BaseButton>
        </footer>
      </form>
    </template>
  </BasicModal>
</template>

<script setup>
import { ref } from 'vue'
import * as yup from 'yup'
import useYup from '@/composables/useYup'
import useModal from '@/composables/useModal'
import BasicModal from '@/components/BasicModal.vue'
import BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import TextField from '@/components/form/TextField.vue'
import BadgeSelectField from '@/components/form/BadgeSelectField.vue'
import { useCompleteInfoStore } from '@/features/missionary/store/complete-info'
import { EDUCATION_DEGREES } from '@/features/missionary/constants/complete-info'

defineOptions({ name: 'MissionaryAddEducationModal' })

const store = useCompleteInfoStore()
const { getModal } = useModal()

const modalData = getModal('MissionaryAddEducationModal')?.data ?? {}
const editingIndex = modalData.index ?? null
const isEditMode = editingIndex != null

const degreeOptions = Object.entries(EDUCATION_DEGREES).map(([value, label]) => ({ value, label }))

const emptyForm = () => ({
  degree: null,
  universityName: '',
  fieldOfStudy: '',
  startYear: '',
  endYear: '',
})

const form = ref({ ...emptyForm(), ...modalData.entry })

const schema = yup.object({
  degree: yup.string().nullable().required('مقطع تحصیلی را انتخاب کنید'),
  universityName: yup.string().trim().required('نام دانشگاه را وارد کنید'),
  fieldOfStudy: yup.string().trim().required('رشته تحصیلی را وارد کنید'),
})

const { validate, validateAt, errors } = useYup(schema)

const onSubmit = async (close) => {
  const { isValid } = await validate(form.value)
  if (!isValid) return
  store.upsertEducation({ ...form.value }, editingIndex)
  close?.()
}
</script>

<style lang="scss" scoped>
.add-education {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  text-align: start;
  padding-block: 0.25rem 0.5rem;

  &__row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0 0.875rem;
  }

  &__divider {
    border-block-end: 1px solid #ddd;
    margin-block: 0.5rem 0.75rem;
  }

  &__footer {
    display: flex;
    justify-content: flex-start;
  }

  &__submit {
    min-width: 14rem;
    padding: 0 1.5rem;
  }
}
</style>
