<!--
  Contenu de la boite de dialogue "Assistant de création"
-->

<template>
  <v-container v-if="step === 1">
    <v-row>
      <v-col cols="6" v-if="ability.can('manage', 'users')">
          <LayoutHeaderUniversalCreationTypeCard
            title="a_person"
            text-content="add_new_person_student"
            icon="fa fa-user"
            type="access"
            @typeClick="onTypeClick"
          />
      </v-col>

      <v-col cols="6" v-if="ability.can('display', 'agenda_page')
                && (
                   ability.can('display', 'course_page') ||
                   ability.can('display', 'exam_page') ||
                   ability.can('display', 'pedagogics_project_page')
                )">
        <LayoutHeaderUniversalCreationTypeCard
          title="an_event"
          text-content="add_an_event_course"
          icon="fa fa-calendar-alt"
          type="event"
          @typeClick="onTypeClick"
        />
      </v-col>

      <v-col cols="6" v-else-if="ability.can('display', 'agenda_page') && ability.can('manage', 'events')">
        <LayoutHeaderUniversalCreationTypeCard
          title="other_event"
          text-content="other_event_text_creation_card"
          icon="far fa-calendar"
          :link="makeAdminUrl('/calendar/create/events')"
        />
      </v-col>

      <v-col cols="6" v-if="ability.can('display', 'message_send_page')
                   && (
                    ability.can('manage', 'emails') ||
                    ability.can('manage', 'mails') ||
                    ability.can('manage', 'texto')
                  )">
        <LayoutHeaderUniversalCreationTypeCard
          title="a_correspondence"
          text-content="sen_email_letter"
          icon="fa fa-comment"
          type="message"
          @typeClick="onTypeClick"
        />
      </v-col>

      <v-col cols="6" v-if="ability.can('manage', 'equipments')">
        <LayoutHeaderUniversalCreationTypeCard
          title="a_materiel"
          text-content="add_any_type_material"
          icon="fa fa-cube"
          :link="makeAdminUrl('/list/create/equipment')"
        />
      </v-col>
    </v-row>
  </v-container>

  <v-container v-if="step === 2">
    <v-row v-if="type === 'access'">
      <v-col cols="6" v-if="isLaw1901">
        <LayoutHeaderUniversalCreationTypeCard
            title="an_adherent"
            text-content="adherent_text_creation_card"
            icon="fa fa-user"
            :link="makeAdminUrl('/universal_creation_person/adherent')"
        />
      </v-col>

      <v-col cols="6" v-if="isLaw1901">
        <LayoutHeaderUniversalCreationTypeCard
            title="a_ca_member"
            text-content="ca_member_text_creation_card"
            icon="fa fa-users"
            :link="makeAdminUrl('/universal_creation_person/ca_member')"
        />
      </v-col>

      <v-col cols="6">
        <LayoutHeaderUniversalCreationTypeCard
            title="a_student"
            text-content="student_text_creation_card"
            icon="fa fa-user"
            :link="makeAdminUrl('/universal_creation_person/student')"
        />
      </v-col>

      <v-col cols="6">
        <LayoutHeaderUniversalCreationTypeCard
            title="a_guardian"
            text-content="guardian_text_creation_card"
            icon="fa fa-female"
            :link="makeAdminUrl('/universal_creation_person/guardian')"
        />
      </v-col>

      <v-col cols="6">
        <LayoutHeaderUniversalCreationTypeCard
            title="a_teacher"
            text-content="teacher_text_creation_card"
            icon="fa fa-graduation-cap"
            :link="makeAdminUrl('/universal_creation_person/teacher')"
        />
      </v-col>

      <v-col cols="6">
        <LayoutHeaderUniversalCreationTypeCard
            title="a_member_of_staff"
            text-content="personnel_text_creation_card"
            icon="fa fa-suitcase"
            :link="makeAdminUrl('/universal_creation_person/personnel')"
        />
      </v-col>

      <v-col cols="6">
        <LayoutHeaderUniversalCreationTypeCard
            title="a_legal_entity"
            text-content="moral_text_creation_card"
            icon="fa fa-building"
            :link="makeAdminUrl('/universal_creation_person/company')"
        />
      </v-col>

      <v-col cols="6" v-if="hasOnlineRegistrationModule">
        <LayoutHeaderUniversalCreationTypeCard
            title="online_registration"
            text-content="online_registration_text_creation_card"
            icon="fa fa-list-alt"
            :link="makeAdminUrl('/online/registration/new_registration')"
        />
      </v-col>

      <v-col cols="6">
        <LayoutHeaderUniversalCreationTypeCard
            title="another_type_of_contact"
            text-content="other_contact_text_creation_card"
            icon="fa fa-plus"
            :link="makeAdminUrl('/universal_creation_person/other_contact')"
        />
      </v-col>
    </v-row>

    <v-row v-if="type === 'event'">
      <!-- /?start=2023-06-12T08:00:00%2B0200&end=2023-06-12T09:00:00%2B0200 -->
      <v-col cols="6" v-if="ability.can('display', 'course_page')">
        <LayoutHeaderUniversalCreationTypeCard
            title="course"
            text-content="course_text_creation_card"
            icon="fa fa-users"
            :link="makeAdminUrl('/calendar/create/courses', {'start': eventDefaultStart, 'end': eventDefaultEnd})"
        />
      </v-col>

      <v-col cols="6" v-if="ability.can('display', 'exam_page')">
        <LayoutHeaderUniversalCreationTypeCard
            title="exam"
            text-content="exam_text_creation_card"
            icon="fa fa-graduation-cap"
            :link="makeAdminUrl('/calendar/create/examens')"
        />
      </v-col>

      <v-col cols="6" v-if="ability.can('display', 'pedagogics_project_page')">
        <LayoutHeaderUniversalCreationTypeCard
            title="educational_services"
            text-content="educational_services_text_creation_card"
            icon="fa fa-suitcase"
            :link="makeAdminUrl('/calendar/create/educational_projects')"
        />
      </v-col>

      <v-col cols="6" v-if="ability.can('manage', 'events')">
        <LayoutHeaderUniversalCreationTypeCard
            title="other_event"
            text-content="other_event_text_creation_card"
            icon="far fa-calendar"
            :link="adminLegacy + '/calendar/create/events'"
        />
      </v-col>
    </v-row>

    <v-row v-if="type === 'message'">
      <v-col cols="6" v-if="ability.can('manage', 'emails')">
        <LayoutHeaderUniversalCreationTypeCard
            title="an_email"
            text-content="email_text_creation_card"
            icon="far fa-envelope"
            :link="makeAdminUrl('/list/create/emails')"
        />
      </v-col>

      <v-col cols="6" v-if="ability.can('manage', 'mails')">
        <LayoutHeaderUniversalCreationTypeCard
            title="a_letter"
            text-content="letter_text_creation_card"
            icon="far fa-file-alt"
            :link="makeAdminUrl('/list/create/mails')"
        />
      </v-col>

      <v-col cols="6" v-if="ability.can('manage', 'texto')">
        <LayoutHeaderUniversalCreationTypeCard
            title="a_sms"
            text-content="sms_text_creation_card"
            icon="fa fa-mobile-alt"
            :link="makeAdminUrl('/list/create/sms')"
        />
      </v-col>
    </v-row>
  </v-container>
</template>

<script setup lang="ts">
  import {Ref, ref} from "@vue/reactivity";
  import {useOrganizationProfileStore} from "~/stores/organizationProfile";
  import {useAbility} from "@casl/vue";
  import {ComputedRef} from "vue";
  import UrlUtils from "~/services/utils/urlUtils";
  import {add, formatISO, startOfHour} from "date-fns";

  const props = defineProps({
    step: {
      type: Number,
      required: true
    }
  })

  const emit = defineEmits(['updateStep'])

  const ability = useAbility()

  const type: Ref<String> = ref('');
  const organizationProfile = useOrganizationProfileStore()
  const runtimeConfig = useRuntimeConfig()

  // Get the start of the next hour as a default event start
  const now: Date = new Date()
  const eventDefaultStart: string = formatISO(startOfHour(add(now, { 'hours': 1 })))
  const eventDefaultEnd: string = formatISO(startOfHour(add(now, { 'hours': 2 })))

  const onTypeClick = (step: Number, Cardtype: String) => {
    type.value = Cardtype;
    emit('updateStep', { stepChoice: step, typeChoice: Cardtype });
  }

  const adminLegacy: Ref<string> = ref(runtimeConfig.baseUrlAdminLegacy)
  const isLaw1901: ComputedRef<boolean> = organizationProfile.isAssociation
  const hasOnlineRegistrationModule: ComputedRef<boolean> = computed(
      () => organizationProfile.hasModule('IEL')
  )

  const makeAdminUrl = (tail: string, query: Record<string, string> = {}) => {
    let url = UrlUtils.join(runtimeConfig.baseUrlAdminLegacy, '#', tail)
    url = UrlUtils.addQuery(url, query)
    return url
  }
</script>
