<template>
  <LayoutContainer>
    <header class="mb-4">
      <v-layout>
        <v-btn
          to="/structures"
          nuxt
          plain
        >
          <font-awesome-icon class="icon mr-1" :icon="['fas', 'chevron-left']" />
          {{ $t('go_back') }}
        </v-btn>
      </v-layout>
    </header>

    <v-container class="content">
      <v-layout class="flex-row align-center mb-4">
        <nuxt-img
          v-if="structure.logoId"
          :src="structure.logoId ? 'https://api.opentalent.fr/app.php/_internal/secure/files/' + structure.logoId + '/0x60' : '/images/default.jpg'"
          alt="logo"
        />

        <h2 class="flex mx-4 d-flex align-center">
          {{ structure.name }}
        </h2>

        <div class="d-flex flex-row align-center">
          <a v-if="structure.facebook" :href="structure.facebook" class="facebook" target="_blank" :title="$t('find_us_on') + ' Facebook'">
            <font-awesome-icon class="icon social-icon" :icon="['fab', 'facebook']" />
          </a>
          <a v-if="structure.instagram" :href="structure.instagram" class="instagram" target="_blank" :title="$t('find_us_on') + ' Instagram'">
            <font-awesome-icon class="icon social-icon" :icon="['fab', 'instagram-square']" />
          </a>
          <a v-if="structure.twitter" :href="structure.twitter" class="twitter" target="_blank" title="$t('find_us_on') + ' Twitter'">
            <font-awesome-icon class="icon social-icon" :icon="['fab', 'twitter']" />
          </a>
        </div>
      </v-layout>

      <v-row>
        <v-col cols="12">
          <nuxt-img src="/images/piano.jpg" alt="" class="max-w100" />
        </v-col>
      </v-row>

      <v-row>
        <v-col cols="12">
          <v-chip-group v-if="structure.practices" :max="0" active-class="primary--text" class="justify-center">
            <v-chip v-for="practice in structure.practices" :key="practice">
              {{ $t(practice) }}
            </v-chip>
          </v-chip-group>
        </v-col>
      </v-row>

      <v-divider class="my-2" />

      <v-row class="my-2 py-2">
        <v-col cols="6" class="description">
          <div class="d-flex flex-row mb-3">
            <h4>{{ $t('descriptive') }}</h4>
            <v-spacer />
          </div>
          <p class="pa-2">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
            dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
            ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
            eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
            deserunt mollit anim id est laborum.
          </p>
        </v-col>

        <v-col cols="6" class="contact">
          <div class="d-flex flex-row mb-3">
            <h4>{{ $t('contact') }}</h4>
            <v-spacer />
          </div>

          <table>
            <tr>
              <td>
                <font-awesome-icon class="icon" :icon="['fas', 'map-marker-alt']" />
              </td>
              <td class="mx-2">
                {{ structure.address }}
              </td>
            </tr>
            <tr>
              <td>
                <font-awesome-icon class="icon" :icon="['fas', 'phone-alt']" />
              </td>
              <td class="phone">
                <a :href="'tel:' + structure.telphone">{{ structure.telphone }}</a>
              </td>
            </tr>
            <tr>
              <td>
                <font-awesome-icon class="icon" :icon="['fas', 'envelope']" />
              </td>
              <td class="mail">
                <a :href="'mailto:' + structure.mail">{{ structure.mail }}</a>
              </td>
            </tr>
            <tr>
              <td>
                <font-awesome-icon class="icon" :icon="['fas', 'globe-europe']" />
              </td>
              <td class="website">
                <a :href="structure.website">{{ structure.website }}</a>
              </td>
            </tr>
            <tr>
              <td>
                <font-awesome-icon class="icon" :icon="['fas', 'project-diagram']" />
              </td>
              <td class="network">
                {{ structure.n1Name }}
              </td>
            </tr>
          </table>
        </v-col>
      </v-row>

      <v-row class="mb-4">
        <v-col cols="12">
          <v-btn disabled>
            {{ $t('spot_on_from') }} 01/01/2021
          </v-btn>
        </v-col>
      </v-row>

      <v-row v-if="structure.latitude && structure.longitude">
        <v-col cols="12">
          <v-responsive width="100%" height="450px">
            <no-ssr>
              <l-map
                :zoom="13"
                :center="[structure.latitude, structure.longitude]"
                :options="{ scrollWheelZoom: false, zoomSnap: 0.25 }"
              >
                <l-tile-layer
                  url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
                  attribution="&copy; <a href='http://osm.org/copyright'>OpenStreetMap</a> contributors"
                />
                <l-marker
                  :key="structure.id"
                  :lat-lng="[structure.latitude, structure.longitude]"
                >
                  <l-popup>
                    <b>{{ structure.name }}</b><br>
                    {{ structure.postalCode }} {{ structure.addressCity }}<br>
                    <a :href="structure.website" target="_blank">{{ structure.website }}</a>
                  </l-popup>
                </l-marker>
              </l-map>
            </no-ssr>
          </v-responsive>
        </v-col>
      </v-row>
    </v-container>
  </LayoutContainer>
</template>

<script>
export default {
  async asyncData ({ $config, params }) {
    return await fetch(
      `${$config.baseURL}/api/public/federation_structures/get?organization-id=${params.id}`
    ).then(
      res => res.json()
    ).then(
      (s) => {
        s.n1Id = s.n1Id ? parseInt(s.n1Id) : null
        s.n2Id = s.n2Id ? parseInt(s.n2Id) : null
        s.n3Id = s.n3Id ? parseInt(s.n3Id) : null
        s.n4Id = s.n4Id ? parseInt(s.n4Id) : null
        s.n5Id = s.n5Id ? parseInt(s.n5Id) : null
        s.practices = s.practices !== null ? s.practices.split(',') : []
        s.latitude = s.latitude ? parseFloat(s.latitude) : null
        s.longitude = s.longitude ? parseFloat(s.longitude) : null
        s.address = [s.streetAddress, s.postalCode, s.addressCity].join(' ')

        s.facebook = 'https://facebook.com'
        s.twitter = 'https://twitter.com'
        s.instagram = 'https://instagram.com'

        return { structure: s }
      }
    )
  }
}
</script>

<style scoped lang="scss">
  @import 'assets/style/variables.scss';

  .content {
    margin: 18px 10%;
    max-width: 80%;
  }

  h2 {
    color: $theme;
    font-size: 22px;
  }

  h4 {
    color: #666;
    border-bottom: solid 1px $theme;
    font-size: 22px;
  }

  .social-icon {
    font-size: 24px;
    color: gray;
    margin: auto 4px;
  }

  .description {
    border-right: solid 2px #e4611b;
    width: 45%;
    padding-right: 5%;
  }

  .contact {
    width: 45%;
    padding-left: 5%;
  }

  .contact td {
    padding: 6px 12px;
  }

  .contact .icon {
    color: $theme;
  }
</style>
