<!--
   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 v-if="category">
    <h2>{{ category }}</h2>
    <ul class="list">
      <slot></slot>
      <li v-for="report of reportsWithCategory(category)" :key="report.name">
        <slot name="link" :report="report">
          <router-link :title="report.long_description" :to="{ path: reportBasePath, query: { reportName: report.name } }">
            {{ report.description }}
          </router-link>
        </slot>
      </li>
    </ul>
  </div>
  <div v-else class="report-list">
    <div v-for="category of categories" :key="category">
      <h2>{{ category }}</h2>
      <ul class="list">
        <li v-if="category === 'admin'"><a class="byu edu" href="http://link.byu.edu/gro" target="_blank" data-disable-navigate="true">Manage Admins</a></li>
        <li v-if="category === 'resources'">User Role: {{ $store.state.userInfo.role }}</li>
        <li v-for="report of reportsWithCategory(category)" :key="report.name">
          <router-link :title="report.long_description" :to="{ path: reportBasePath, query: { reportName: report.name } }">
            {{ report.description }}
            </router-link>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'

export default {
  name: 'ReportList',
  props: {
    reportBasePath: String,
    category: String
  },
  computed: {
    ...mapState({
      reports: state => state.reports.list,
      institution: state => state.ui.institution
    }),
    categories () {
      // Filters reports to have reports specefic to dp institution
      const reports = this.reports.filter(ins => ins.institution.includes(state.institution)).map(ins => ins.institution)
      const allObjectCategories = reports.map(o => o.category)
      let uniqueCategories = new Set()
      allObjectCategories.forEach((category) => uniqueCategories.add(category))
      return uniqueCategories.filter(c => c && c !== 'system')
    }
  },
  methods: {
    reportsWithCategory(category) {
      return this.reports.filter(o => o.category === category)
    }
  }
}
</script>
<style scoped>
.list{
  -webkit-padding-start: 0;
  padding: 0;
  list-style-type: none;
  list-style-position: outside;
}
.report-list{
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 15%;
}
</style>
