<template>
  <LayoutContainer>
    <UiLoadingPanel v-if="pending" />
    <UiForm
        v-else
        :model="Parameters"
        :entity="parameters"
    >
      <v-row>
        <v-col cols="6">
          <div class="mb-6">
            <div>{{ $t('your_opentalent_website_address_is')}} : </div>
            <div class="ma-2 text-primary"><strong>{{ organizationProfile.website }}</strong></div>
          </div>

          <div class="mb-6">
            <div>{{ $t('subdomains_history') }} : </div>
            <UiLoadingPanel v-if="subdomainsPending" />
            <v-list
                v-else
                :items="subdomains.items"
                item-value="id"
                item-title="subdomain"
                density="compact"
                bg-color="transparent"
            >
              <template #prepend>
                <v-icon icon="fas fa-circle" style="font-size: 5px;"/>
              </template>
            </v-list>
          </div>

          <v-btn
              :disabled="!canAddNewSubdomain"
              class="my-2"
              @click="onAddSubdomainClick"
          >
            {{ $t('record_a_new_subdomain')}}
          </v-btn>
        </v-col>


        <v-col cols="6">
          <!-- les publicationDirectors sont des entités Access -->
          <UiInputAutocomplete
              field="publicationDirectors"
              itemTitle="person.name"
          />

          <UiInputCheckbox
            v-model="parameters.desactivateOpentalentSiteWeb"
            field="desactivateOpentalentSiteWeb"
          />

          <UiInputText
              v-model="parameters.otherWebsite"
              field="otherWebsite"
          />
        </v-col>
      </v-row>
    </UiForm>
  </LayoutContainer>
</template>

<script setup lang="ts">
import {useOrganizationProfileStore} from "~/stores/organizationProfile";
import Parameters from "~/models/Organization/Parameters";
import {useEntityFetch} from "~/composables/data/useEntityFetch";
import {AsyncData} from "#app";
import Subdomain from "~/models/Organization/Subdomain";
import ApiResource from "~/models/ApiResource";

const { fetch, fetchCollection } = useEntityFetch()

const organizationProfile = useOrganizationProfileStore()

if (organizationProfile.parametersId === null) {
  throw new Error('Missing organization parameters id')
}

const { data: parameters, pending } = fetch(Parameters, organizationProfile.parametersId) as AsyncData<Parameters, Parameters | true>


const { data: subdomains, pending: subdomainsPending } = fetchCollection(Subdomain, null, ref({ 'organization' : organizationProfile.id }) )

const canAddNewSubdomain: ComputedRef<boolean> = computed(() => subdomains.value.items.length < 3)





const onAddSubdomainClick = () => {
  if (!canAddNewSubdomain) {
    throw new Error('Max number of subdomains reached')
  }


}

</script>

<style scoped lang="scss">

</style>
