<!-- Component d'un formulaire d'une durée pour un curriculum -->
<template>
  <main>
    <LayoutContainer>
      <v-card class="mb-5 mt-4">
        <FormToolbar title="educationTiming" icon="fa-calendar"/>

        <UiForm
          :id="id"
          :model="model"
          :query="query()"
          :submitActions="submitActions">
          <template #form.input="{entry, updateRepository}">
            <v-container fluid class="container">
              <v-row>

                <v-col cols="12" sm="6">
                  <UiInputText
                    field="timing"
                    label="timing"
                    :data="entry['timing']"
                    @update="updateRepository"
                    type="number"
                    :rules="rules().timingRules"
                  />
                </v-col>

              </v-row>
            </v-container>

          </template>

          <template #form.button>
            <NuxtLink :to="{ path: '/parameters/education'}" class="no-decoration">
              <v-btn class="mr-4 ot_light_grey ot_grey--text">
                {{ $t('back') }}
              </v-btn>
            </NuxtLink>
          </template>

        </UiForm>
      </v-card>
    </LayoutContainer>
  </main>
</template>

<script lang="ts">
import {defineComponent, computed, useContext} from '@nuxtjs/composition-api'
import { Repository as VuexRepository } from '@vuex-orm/core/dist/src/repository/Repository'
import { Query, Model } from '@vuex-orm/core'
import { SUBMIT_TYPE} from '~/types/enums'
import { repositoryHelper } from '~/services/store/repository'
import {AnyJson} from "~/types/interfaces";
import {EducationTiming} from "~/models/Education/EducationTiming";

export default defineComponent({
  props: {
    id:{
      type: [Number, String],
      required: true
    }
  },
  setup () {
    const {app:{i18n}} = useContext()
    const repository: VuexRepository<Model> = repositoryHelper.getRepository(EducationTiming)
    const query: Query = repository.query()

    const submitActions = computed(() => {
      let actions:AnyJson = {}
      actions[SUBMIT_TYPE.SAVE_AND_BACK] = { path: `/parameters/education` }
      actions[SUBMIT_TYPE.SAVE] = { path: `/parameters/education/education_timing/` }
      return actions
    })

    /** todo Computed properties needs to be returned as functions until nuxt3 : https://github.com/nuxt-community/composition-api/issues/207 **/
    return {
      model: EducationTiming,
      query: () => query,
      rules: () => getRules(i18n),
      panel: 0,
      submitActions
    }
  },
  beforeDestroy() {
    repositoryHelper.cleanRepository(EducationTiming)
  }
})

function getRules (i18n: any) {
  return {
    timingRules: [
      (timingValue: number) => (timingValue > 0) || i18n.t('value_need_to_be_bigger_than_0')
    ]
  }
}

</script>
