<!--
   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">Assigned To</span>
    <button class="dropdown-item" @click="clearFilter">Show All</button>
    <div 
      v-for="(user, i) of users"
      :key="user.byuId"
      class="dropdown-item"
    >
      <input
        type="checkbox"
        :id="`filter-button-assigned-to-option-${i}`"
        :value="user.byuId"
        :checked="selectedUsers.includes(user.byuId)"
        @change="toggle(user.byuId)"
      ></input>
      <label :for="`filter-button-assigned-to-option-${i}`">{{ user.name }}</label>
    </div>
  </FilterButton>
</template>
<script>
import FilterButton from './FilterButton'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { faUser } from '@fortawesome/free-solid-svg-icons'
import { mapMutations, mapActions, mapGetters } from 'vuex'

export default {
  name: 'FilterButtonAssignedTo',
  components: { FilterButton, FontAwesomeIcon },
  computed: {
    ...mapGetters(['usersByRole', 'packetListFilters']),
    buttonIcon: () => faUser,
    users() {
      const counselors = this.usersByRole('Counselor')
      const processors = this.usersByRole('Processor')
      return counselors.concat(processors)
    },
    activeFilter () {
      const filters = this.packetListFilters
      const admitPeriodFilter = filters.find(f => f.type === 'assignedTo')
      return admitPeriodFilter ? admitPeriodFilter.value : null
    },
    selectedUsers () {
      const val = this.activeFilter
      return val ? val.split(',') : []
    }
  },
  data () {
    return {
      show: false
    }
  },
  methods: {
    ...mapMutations(['setPacketListFilter', 'clearPacketListFilter']),
    ...mapActions(['fetchPacketList']),
    setFilter(assignedTo) {
      this.setPacketListFilter({
        type: 'assignedTo',
        value: assignedTo
      })
      this.fetchPacketList({force: true})
    },
    toggle (userId) {
      const isActive = this.selectedUsers.includes(userId)
      if (isActive) {
        const newList = this.selectedUsers.filter(u => u !== userId)
        this.setFilter(newList.join(','))
        return
      }
      const newList = this.selectedUsers.concat(userId).sort()
      this.setFilter(newList.join(','))
    },
    clearFilter() {
      this.clearPacketListFilter({type: 'assignedTo'})
      this.fetchPacketList({force: true})
      this.show = false
    }
  }
}
</script>
