<!--
   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>
  <modal
    name="assignment-modal"
    height="auto"
    draggable
    @before-open="beforeOpen"
    @before-close="beforeClose"
  >
    <div class="bg-light p-1 text-uppercase">Change Assignment</div>
    <div class="decision-dialog content-padding">
      <label for="assignee">Assign To:</label>
      <select v-model="newAssignee" id="assignee">
        <option v-if="pool === 'counselor'" value="COUNSELOR_POOL">Any Counselor</option>
        <option v-else-if="pool === 'processor'" value="PROCESSOR_POOL">Any Processor</option>
        <option
        v-for="user of users"
        :key="user.byuId"
        :value="user.byuId"
        >{{ user.name }}</option>
        <option value="UNASSIGNED">Unassigned</option>
      </select>
      <textarea
        v-model="note"
        class="new-note form-control"
        placeholder="Note (optional)..."></textarea>
      <span class="modal-footer">
        <button class="btn btn-outline-primary" @click="saveAssignment" :disabled="!pool && newAssignee === assignedTo">Save</button>
        <button class="btn btn-outline-secondary" @click="close">Cancel</button>
      </span>
    </div>
  </modal>
</template>
<script>
  import { mapGetters, mapMutations, mapActions } from 'vuex'

export default {
  name: 'AssignmentDialog',
  props: {
    appId: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      note: '',
      newAssignee: '',
      assignedTo: '',
      pool: ''
    }
  },
  computed: {
    users () {
      const counselors = this.$store.getters.usersByRole('Counselor')
      const processors = this.$store.getters.usersByRole('Processor')
      if (this.pool) {
        return this.pool === 'counselor' ? counselors : processors
      }
      return counselors.concat(processors)
    }
  },
  methods: {
    ...mapActions(['flagUpdate', 'reassignToPool']),
    beforeOpen (event) {
      console.log('assignment dialog::beforeOpen', event)
      this.assignedTo = event.params.assignedTo
      this.newAssignee = event.params.assignedTo
      this.pool = event.params.pool
    },
    beforeClose () {
      this.note = ''
      this.newAssignee = ''
      this.assignedTo = ''
      this.pool = ''
    },
    close () {
      this.$modal.hide('assignment-modal')
    },
    async saveAssignment () {
      if (['COUNSELOR_POOL', 'PROCESSOR_POOL'].includes(this.newAssignee)) {
        await this.reassignToPool({
          appId: this.appId,
          pool: this.pool
        })
        this.$modal.hide('assignment-modal')
        this.$emit('reassigned')
      } else {
        await this.flagUpdate({
          applicationUuid: this.appId,
          flagState: 'set',
          flagValue: this.newAssignee,
          note: this.note
        })
        this.$modal.hide('assignment-modal')
        this.$emit('reassigned')
      }
    }
  }
}
</script>
<style scoped>
</style>
