<!--
   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="femaleIcon" />
    <FontAwesomeIcon slot="icon" :icon="maleIcon" />
    <span slot="label">Sex</span>
    <button class="dropdown-item" @click="clearFilter">Show All</button>
    <div
      v-for="(admitPeriod, i) of sexes"
      :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 { faFemale, faMale } from '@fortawesome/free-solid-svg-icons'
import { mapGetters, mapMutations, mapActions } from 'vuex'

export default {
  name: 'FilterButtonAdmitPeriod',
  components: { FilterButton, FontAwesomeIcon },
  filters: { yearTerm: yearTermLongFormat },
  props: {
    sexes: {
      type: Array,
      default() {
        return [
          'F',
          'M'
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['packetListFilters']),
    femaleIcon: () => faFemale,
    maleIcon: () => faMale,
    activeFilter () {
      const filters = this.packetListFilters
      const sexFilter = filters.find(f => f.type === 'sex')
      return sexFilter ? sexFilter.value : null
    },
    selectedPeriods () {
      return this.activeFilter ? this.activeFilter.split(',') : []
    }
  },
  data () {
    return {
      show: false
    }
  },
  methods: {
    ...mapMutations(['setPacketListFilter', 'clearPacketListFilter']),
    ...mapActions(['fetchPacketList']),
    setFilter(sex) {
      this.setPacketListFilter({
        type: 'sex',
        value: sex
      })
      this.fetchPacketList({force: true})
    },
    toggle (sex) {
      const isActive = this.selectedPeriods.includes(sex)
      if (isActive) {
        const newList = this.selectedPeriods.filter(p => p !== sex)
        this.setFilter(newList.join(','))
        return
      }
      const newList = this.selectedPeriods.concat(sex).sort()
      this.setFilter(newList.join(','))
    },
    clearFilter() {
      this.clearPacketListFilter({type: 'sex'})
      this.fetchPacketList({force: true})
      this.show = false
    }
  }
}
</script>
