<!--
   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="grid">
    <table v-for="(list, year) of byYear" :key="year" class="table tbl-sm">
      <thead>
        <tr>
          <th colspan="7">Grade {{ year }}</th>
        </tr>
        <tr>
          <th>Subject</th>
          <th>Course</th>
          <th></th>
          <th colspan="4">Grade</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="c of list" :key="c.courseName">
          <td>{{ c.subject }}</td>
          <td>{{ c.courseName }}</td>
          <td>
            <span v-if="c.ap">AP</span>
            <span v-if="c.repeated">Repeated</span>
          </td>
          <td v-for="(g, i) of c.grades" :key="i">
            {{ g }}
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>
<script>
import { mapState } from 'vuex'

export default {
  name: 'DetailsHsTranscript',
  props: {
    classes: Array
  },
  computed: {
    years () {
      const gradeLevels = this.classes.map(c => c.gradeLevel)
      const unique = Array.from(gradeLevels.reduce((a, c) => {
        return a.add(c)
      }, new Set()))
      return unique.sort((a, b) => Number(a) - Number(b))
    },
    byYear () {
      const classSort = (a, b) => {
        if (a.subject === b.subject) return a.courseName.localeCompare(b.courseName)
        return a.subject.localeCompare(b.subject)
      }
      const years = this.years
      return years.reduce((o, y) => Object.assign(o, {
        [y]: this.classes.filter(c => c.gradeLevel === y).sort(classSort)
      }), {})
    }
  }
}
</script>
<style scoped>
.grid {
  display: grid;
  grid-template-columns: auto auto;
  grid-auto-rows: auto;
  grid-gap: 0.5rem;
}
.card-body {
  padding: 0.5rem;
  display: grid;
  grid-template-columns: 1fr;
  grid-auto-rows: auto;
}
.bg1 {
  background-color: rgba(128, 27, 52, 0.1);
}
.bg2 {
  background-color: rgba(30, 111, 65, 0.1);
}
.bg3 {
  background-color: rgba(170, 128, 14, 0.1);
}
.bg4 {
  background-color: rgba(126, 43, 139, 0.1);
}
</style>
