<!--
   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 class="notes-container">
    <h5 v-if="notes.length">
      <small class="text-muted ml-3">
        <select v-model="selectedFilter" v-if="!showOnly">
          <option value="" class="text-muted">All</option>
          <option v-for="filter of filterTypes">{{filter}}</option>
        </select>
      </small>
    </h5>
    <div v-for="note of filteredNotes">
      <h6>
        <span v-if="note.qualifier" class="badge badge-primary">{{note.qualifier}}</span>
        <small class="text-muted">[{{note.type}}]</small>
        {{note.dateTimeCreated}}
        | {{note.createdBy}}
      </h6>
      <ObjectViewer v-if="Array.isArray(note.note)" :arrData="note.note" />
      <ObjectViewer v-else-if="typeof note.note === 'object'" :objData="note.note" />
      <p class="preserve-whitespace" v-else>{{note.note}}</p>
    </div>
  </div>
</template>
<script>
  import ObjectViewer from './ObjectViewer'
  import { mapState } from 'vuex'

  export default {
    name: 'DetailsNotes',
    props: {
      select: {
        type: String
      },
      showOnly: {
        type: String
      }
    },
    components: {
      ObjectViewer
    },
    computed: {
      ...mapState({
        notes: state => state.notes.list
      }),
      filteredNotes: function() {
        const notes = this.notes
        return this.selectedFilter.length > 0
          ? notes.filter(n => n.type === this.selectedFilter)
          : notes
      },
      filterTypes: function() {
        const types = this.notes.map(n => n.type)
        const uniqueTypes = new Set(types)
        return Array.from(uniqueTypes)
      }
    },
    data() {
      return {
        selectedFilter: this.showOnly || this.select || ''
      }
    }
  }
</script>
<style scoped>
.preserve-whitespace {
  white-space: pre;
}
</style>
