<!--
   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="buttonIcon" />
    <span slot="label">Applicant Type</span>
    <button class="dropdown-item" @click="clearFilter">Show All</button>
    <div
      v-for="(type, i) of appTypes"
      :key="i"
      class="dropdown-item"
    >
      <input
        type="checkbox"
        :id="`filter-button-app-type-option-${i}`"
        :value="type"
        :checked="selectedTypes.includes(type)"
        @change="toggle(type)"
      ></input>
      <label :for="`filter-button-app-type-option-${i}`">{{ type }}</label>
    </div>
  </FilterButton>
</template>
<script>
import FilterButton from './FilterButton'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { faUserTag } from '@fortawesome/free-solid-svg-icons'
import { mapGetters, mapMutations, mapActions } from 'vuex'

export default {
  name: 'FilterButtonAppType',
  components: { FilterButton, FontAwesomeIcon },
  props: {
    appTypes: {
      type: Array,
      default () {
        return [
          'NF',
          'T',
          'F'
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['packetListFilters']),
    buttonIcon: () => faUserTag,
    activeFilter () {
      const filters = this.packetListFilters
      const appTypeFilter = filters.find(f => f.type === 'applicantType')
      return appTypeFilter ? appTypeFilter.value : null
    },
    selectedTypes () {
      const val = this.activeFilter
      return val ? val.split(',') : []
    }
  },
  data () {
    return {
      show: false
    }
  },
  methods: {
    ...mapMutations(['setPacketListFilter', 'clearPacketListFilter']),
    ...mapActions(['fetchPacketList']),
    setFilter(applicantType) {
      this.setPacketListFilter({
        type: 'applicantType',
        value: applicantType
      })
      this.fetchPacketList({force: true})
    },
    toggle (appType) {
      const isActive = this.selectedTypes.includes(appType)
      if (isActive) {
        const newList = this.selectedTypes.filter(t => t !== appType)
        this.setFilter(newList.join(','))
        return
      }
      const newList = this.selectedTypes.concat(appType).sort()
      this.setFilter(newList.join(','))
    },
    clearFilter() {
      this.clearPacketListFilter({type: 'applicantType'})
      this.fetchPacketList({force: true})
      this.show = false
    }
  }
}
</script>
