<!--
   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>
    <template v-for="(hs, i) in highSchoolSummaries">
      <table class="table table-borderless table-sm">
        <thead>
          <tr>
            <th>High School</th>
            <th>Location</th>
            <th>Type</th>
            <th>GPA</th>
            <th>Calculated GPA</th>
            <th>Graduation Date</th>
            <th v-if="showTranscript">Transcript</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              {{hs.highSchool}}
              <small v-if="hs.schoolCode">
                ({{hs.schoolCode}})
              </small>
            </td>
            <td>{{hs.highSchoolCity}} - {{hs.highSchoolState}} - {{hs.highSchoolCountry}}</td>
            <td>{{hs.highSchoolType}}</td>
            <td>{{hsGpa(hs, 'regular') | gpaFilter}}</td>
            <td>{{hsGpa(hs, 'calc') | gpaFilter}}</td>
            <td>{{hs.graduationDate | dateFormat}}</td>
            <td v-if="showTranscript">{{hs.linkToDocumentImage}}</td>
          </tr>
        </tbody>
      </table>
      <DetailsHsTranscript
        v-if="showTranscript && hs.listOfClasses && hs.listOfClasses.length > 0"
        :classes="hs.listOfClasses"
      >
      </DetailsHsTranscript>
    </template>
    <div v-if="highSchoolSummaries.length===0">
      No high school information available
    </div>
  </div>
</template>
<script>
  import DetailsHsTranscript from './DetailsHsTranscript'
  import { mapGetters } from 'vuex'
  import { dateFormat } from './dateTimeFormat'
  import gpaFilter from './gpaFilter'

  export default {
    name: 'DetailsHighSchool',
    components: { DetailsHsTranscript },
    props: {
      showTranscript: {
        type: Boolean,
        default: false
      }
    },
    filters: {
      dateFormat,
      gpaFilter
    },
    computed: {
      ...mapGetters(['highSchoolSummaries']),
    },
    methods: {
      hsGpa (highSchool, specifier) {
        if (highSchool) {
          let value = highSchool.highSchoolGpa
          if (specifier === 'calc') {
            value = highSchool.gpaCalculated
          }
          if (value) {
            const asFloat = Number.parseFloat(value)
            if (!Number.isNaN(asFloat) && asFloat > 0.01) {
              return value
            }
          }
          return ''
        }
      }
    }
  }
</script>
