<!-- Page de détails de l'organization courante -->

<template>
  <LayoutContainer>
    <!-- Définit le contenu des trois slots du header de la page -->
    <LayoutBannerTop>
      <template #block1>
        {{ entry.name }}
      </template>
      <template #block2>
        N°Siret : {{ entry.siretNumber }}
      </template>
      <template #block3>
        {{ entry.description }}
      </template>
    </LayoutBannerTop>

    <!-- Rend le contenu de la page -->
    <NuxtChild />
  </LayoutContainer>
</template>

<script lang="ts">
import { computed, defineComponent, useContext, ComputedRef } from '@nuxtjs/composition-api'
import { Item, Query } from '@vuex-orm/core'
import { QUERY_TYPE } from '~/types/enums'
import { Organization } from '~/models/Organization/Organization'
import { queryHelper } from '~/services/store/query'
import { repositoryHelper } from '~/services/store/repository'
import {ContactPoint} from "~/models/Core/ContactPoint";
import {BankAccount} from "~/models/Core/BankAccount";
import {OrganizationAddressPostal} from "~/models/Organization/OrganizationAddressPostal";
import {Country} from "~/models/Core/Country";
import {TypeOfPractice} from "~/models/Organization/TypeOfPractice";
import {Network} from "~/models/Network/Network";
import {NetworkOrganization} from "~/models/Network/NetworkOrganization";
import {AddressPostal} from "~/models/Core/AddressPostal";
import {OrganizationArticle} from "~/models/Organization/OrganizationArticle";
import {File} from "~/models/Core/File"

export default defineComponent({
  name: 'Organization',
  middleware({ $ability, redirect }) {
    if(!$ability.can('display', 'organization_page'))
      return redirect('/error')
  },
  setup () {
    const { store } = useContext()

    const repository = repositoryHelper.getRepository(Organization)
    const query: ComputedRef<Query> = computed(() => repository.query())
    const entry: ComputedRef<Item> = computed(() => {
      return queryHelper.getItem(query.value, store.state.profile.organization.id)
    })

    return {
      entry
    }
  },
  async asyncData ({ store, $dataProvider }) {
    await $dataProvider.invoke({
      type: QUERY_TYPE.MODEL,
      model: Organization,
      id: store.state.profile.organization.id
    })
  },
  beforeDestroy() {
    repositoryHelper.cleanRepository(Organization)
    repositoryHelper.cleanRepository(ContactPoint)
    repositoryHelper.cleanRepository(BankAccount)
    repositoryHelper.cleanRepository(OrganizationAddressPostal)
    repositoryHelper.cleanRepository(AddressPostal)
    repositoryHelper.cleanRepository(Country)
    repositoryHelper.cleanRepository(TypeOfPractice)
    repositoryHelper.cleanRepository(Network)
    repositoryHelper.cleanRepository(NetworkOrganization)
    repositoryHelper.cleanRepository(OrganizationArticle)
    repositoryHelper.cleanRepository(File)
  },
})

</script>
