<!--
   Copyright 2019 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>
  <FilterButton @show="show = true" @hide="show = false" :show="show">
    <FontAwesomeIcon slot="icon" :icon="calendarIcon" />
    <span slot="label">Admit Period</span>
    <button class="dropdown-item" @click="clearFilter">Show All</button>
    <div
      v-for="(admitPeriod, i) of admitPeriods"
      :key="i"
      class="dropdown-item"
    >
      <input
        type="checkbox"
        :id="`filter-button-admit-period-option-${i}`"
        :value="admitPeriod"
        :checked="selectedPeriods.includes(admitPeriod)"
        @change="toggle(admitPeriod)"
      ></input>
      <label :for="`filter-button-admit-period-option-${i}`">{{ admitPeriod | yearTerm }}</label>
    </div>
  </FilterButton>
</template>
<script>
import FilterButton from './FilterButton'
import { yearTermLongFormat } from './dateTimeFormat'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { faCalendarAlt } from '@fortawesome/free-solid-svg-icons'
import { mapGetters, mapMutations, mapActions } from 'vuex'

export default {
  name: 'FilterButtonAdmitPeriod',
  components: { FilterButton, FontAwesomeIcon },
  filters: { yearTerm: yearTermLongFormat },
  props: {
    admitPeriods: {
      type: Array,
      default() {
        return [
          '20241',
          '20243',
          '20245'
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['packetListFilters']),
    calendarIcon: () => faCalendarAlt,
    activeFilter () {
      const filters = this.packetListFilters
      const admitPeriodFilter = filters.find(f => f.type === 'admitPeriod')
      return admitPeriodFilter ? admitPeriodFilter.value : null
    },
    selectedPeriods () {
      return this.activeFilter ? this.activeFilter.split(',') : []
    }
  },
  data () {
    return {
      show: false
    }
  },
  methods: {
    ...mapMutations(['setPacketListFilter', 'clearPacketListFilter']),
    ...mapActions(['fetchPacketList']),
    setFilter(admitPeriod) {
      this.setPacketListFilter({
        type: 'admitPeriod',
        value: admitPeriod
      })
      this.fetchPacketList({force: true})
    },
    toggle (admitPeriod) {
      const isActive = this.selectedPeriods.includes(admitPeriod)
      if (isActive) {
        const newList = this.selectedPeriods.filter(p => p !== admitPeriod)
        this.setFilter(newList.join(','))
        return
      }
      const newList = this.selectedPeriods.concat(admitPeriod).sort()
      this.setFilter(newList.join(','))
    },
    clearFilter() {
      this.clearPacketListFilter({type: 'admitPeriod'})
      this.fetchPacketList({force: true})
      this.show = false
    }
  }
}
</script>
