<!--
   Copyright 2018 Brigham Young University

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<template>
  <div>
    <div v-if="loaded">
      <span v-if="filenotes.length === 0">No Filenotes</span>
      <DetailsFilenote
         v-for="filenote of filenotes"
         :key="filenote.id"
         v-bind="filenote"
         >
      </DetailsFilenote>
    </div>
    <span v-else-if="error" class="text-error">
      <font-awesome-icon :icon="errorIcon" />
      Error loading filenotes
    </span>
    <span v-else class="text-gray">
      <font-awesome-icon :icon="spinner" spin />
      Loading...
    </span>
  </div>
</template>
<script>
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { faExclamationTriangle, faSpinner } from '@fortawesome/free-solid-svg-icons'

import { mapState } from 'vuex'

import parseFilenotes from './parsers/filenotes'
import DetailsFilenote from './DetailsFilenote'

export default {
  name: 'DetailsFilenoteList',
  components: {
    FontAwesomeIcon,
    DetailsFilenote
  },
  data () {
    return {
      error: false,
      loaded: false,
      filenotes: []
    }
  },
  computed: {
    ...mapState({
      personId: state => state.application.personId
    }),
    spinner () { return faSpinner },
    errorIcon () { return faExclamationTriangle }
  },
  async mounted () {
    const personId = this.personId
    if (!personId) {
      this.error = true
      return
    }
    try {
      const q = new URLSearchParams()
      q.append('incidents', 'false')
      q.append('tasks', 'false')
      q.append('messages', 'false')
      q.append('appointments', 'false')
      q.append('page', '0')
      q.append('categoryId', 'CESADM')
      q.append('sortAscending', 'true')
      q.append('results', '100')
      const url = `https://api.byu.edu:443/domains/ymessage-archive/1.0/student-overview/${personId}?${q.toString()}`

      const headers = {
        'accept': 'application/json'
      }
      const { body, status } = await window.byu.auth.request({ url, headers })

      if (status >= 400) {
        console.error(Error(`Unable to load filenotes! Error code ${status}`))
        this.error = true
        return
      }

      const data = JSON.parse(body)
      const parsedFilenotes = parseFilenotes(data.results)
      console.log('DetailsFilenoteList.vue::parsedFilenotes=', parsedFilenotes)
      // const count = data.totalResults
      this.filenotes = parsedFilenotes
      this.loaded = true
    } catch (err) {
      console.error(err)
      this.error = true
    }
  }
}
</script>
<style scoped>
</style>
