<!--
    Copyright or © or Copr. happyDNS (2020)

    contact@happydns.org

    This software is a computer program whose purpose is to provide a modern
    interface to interact with DNS systems.

    This software is governed by the CeCILL license under French law and abiding
    by the rules of distribution of free software.  You can use, modify and/or
    redistribute the software under the terms of the CeCILL license as
    circulated by CEA, CNRS and INRIA at the following URL
    "http://www.cecill.info".

    As a counterpart to the access to the source code and rights to copy, modify
    and redistribute granted by the license, users are provided only with a
    limited warranty and the software's author, the holder of the economic
    rights, and the successive licensors have only limited liability.

    In this respect, the user's attention is drawn to the risks associated with
    loading, using, modifying and/or developing or reproducing the software by
    the user in light of its specific status of free software, that may mean
    that it is complicated to manipulate, and that also therefore means that it
    is reserved for developers and experienced professionals having in-depth
    computer knowledge. Users are therefore encouraged to load and test the
    software's suitability as regards their requirements in conditions enabling
    the security of their systems and/or data to be ensured and, more generally,
    to use and operate it in the same conditions as regards security.

    The fact that you are presently reading this means that you have had
    knowledge of the CeCILL license and that you accept its terms.
  -->

<template>
  <div>
    <div v-if="isLoading" class="mt-5 d-flex justify-content-center align-items-center">
      <b-spinner variant="primary" label="Spinning" class="mr-3" /> {{ $t('wait.retrieving-source') }}
    </div>
    <div v-else>
      <h2 class="mt-3 mb-3">
        <span class="text-monospace">{{ domain.domain }}</span>
        <small class="text-muted">
          {{ $t('domains.views.source-parameters') }}
        </small>
      </h2>
      <p>
        <span class="text-primary">{{ $t('common.name') }}</span><br>
        <strong>{{ source._comment }}</strong>
      </p>
      <p>
        <span class="text-primary">{{ $t('source.source-type') }}</span><br>
        <strong :title="source._srctype">{{ specs[source._srctype].name }}</strong><br>
        <span class="text-muted">{{ specs[source._srctype].description }}</span>
      </p>
      <p v-for="(spec,index) in source_specs.fields" v-show="!spec.secret" :key="index">
        <span class="text-primary">{{ spec.label }}</span><br>
        <strong>{{ source.Source[spec.id] }}</strong>
      </p>
      <p v-if="availableTypes.length">
        <span class="text-primary">{{ $t('source.available-types') }}</span>
        <ul style="column-count: 4;">
          <li v-for="(t, i) in availableTypes" :key="i">
            {{ t | nsrrtype }}
          </li>
        </ul>
      </p>
    </div>
  </div>
</template>

<script>
import SourcesApi from '@/services/SourcesApi'
import SourceSpecsApi from '@/services/SourceSpecsApi'

export default {

  props: {
    domain: {
      type: Object,
      required: true
    }
  },

  data: function () {
    return {
      availableTypes: [],
      source: null,
      source_specs: null,
      specs: null
    }
  },

  computed: {
    isLoading () {
      return this.source == null || this.source_specs == null || this.specs == null
    }
  },

  watch: {
    domain: function (domain) {
      this.updDomain()
    }
  },

  mounted () {
    SourceSpecsApi.getSourceSpecs()
      .then(response => {
        this.specs = response.data
      })
    if (this.domain != null) {
      this.updDomain()
    }
  },

  methods: {
    updDomain () {
      if (this.domain === undefined || this.domain.id_source === undefined) {
        return
      }
      SourcesApi.getSource(this.domain.id_source)
        .then(
          (response) => {
            this.source = response.data

            SourceSpecsApi.getSourceSpecs(this.source._srctype)
              .then(response => (
                this.source_specs = response.data
              ))

            SourcesApi.getAvailableResourceTypes(this.domain.id_source)
              .then(response => (
                this.availableTypes = response.data
              ))
          })
    }
  }
}
</script>
