<!--
   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="decision-modal"
  height="auto"
  draggable
  @before-close="beforeClose"
  >
    <div class="bg-light p-1 text-uppercase">Enter Decision</div>
    <div class="decision-dialog content-padding">
      <div class="decision-buttons">
        <span v-if="admitReasonList.length > 0" class="radio">
          <input id="admit" type="radio" v-model="decision" value="A" name="decision">
          <label for="admit">Admit</label>
        </span>

        <span v-if="denyReasonList.length > 0" class="radio">
          <input id="deny" type="radio" v-model="decision" value="D" name="decision">
          <label for="deny">Deny</label>
        </span>

        <span v-if="holdReasonList.length > 0" class="radio">
          <input id="hold" type="radio" v-model="decision" value="H" name="decision">
          <label for="hold">Hold</label>
        </span>

        <span v-if="archiveReasonList.length > 0" class="radio">
          <input id="archive" type="radio" v-model="decision" value="V" name="decision">
          <label for="archive">Archive</label>
        </span>

        <slot>
          <select v-model="reason" class="full-span">
            <option value="">Reason</option>
            <option v-for="( r, i ) of reasons" :key="i" :value="r">
              {{ r }}
            </option>
          </select>
        </slot>
        <div class="full-span">
          <slot name="model"></slot>
        </div>
      </div>
      <textarea
        v-model="note"
        class="new-note form-control"
        placeholder="Note regarding this decision (optional)..."></textarea>
      <span class="modal-footer">
        <button class="btn btn-outline-primary" @click="saveDecision" :disabled="reason.length<1">Save</button>
        <button class="btn btn-outline-secondary" @click="close">Cancel</button>
      </span>
    </div>
  </modal>
</template>
<script>
  import { mapActions } from 'vuex'

  export default {
    name: 'DecisionDialog',
    props: {
      admitReasonList: {
        type: Array,
        default() {
          return [ 'Admit' ]
        }
      },
      denyReasonList: {
        type: Array,
        default() {
          return [ 'Deny' ]
        }
      },
      holdReasonList: {
        type: Array,
        default() {
          return [ 'Hold' ]
        }
      },
      archiveReasonList: {
        type: Array,
        default() {
          return [ 'Admit Period Closed', 'Wrong Admit Period', 'Duplicate Application' ]
        }
      },
      appId: {
        type: String,
        required: true
      },
      model: {
        type: String
      }
    },
    data() {
      return {
        decision: 'OPEN',
        reason: '',
        note: ''
      }
    },
    computed: {
      reasons () {
        switch(this.decision) {
          case 'A':
            return this.admitReasonList
          case 'D':
            return this.denyReasonList
          case 'H':
            return this.holdReasonList
          case 'V':
            return this.archiveReasonList
          default:
            return []
        }
      }
    },
    methods: {
      ...mapActions(['enterDecision']),
      beforeClose () {
        this.decision = 'OPEN'
      },
      close () {
        this.$modal.hide('decision-modal')
      },
      async saveDecision () {
        await this.enterDecision({
          appId: this.appId,
          decision: this.decision,
          reason: this.reason,
          model: this.model
        })
        this.$modal.hide('decision-modal')
      }
    }
  }
</script>
<style scoped>
  .radio {
    padding: 0rem 0.5rem;
    white-space: nowrap;
  }
  .full-span {
    grid-column: 1 / -1;
  }
  .decision-dialog {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto;
    height: 100%;
    min-height: 15rem;
  }
  .decision-buttons {
    padding: 0.5rem;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(4.5rem, 1fr));
    grid-gap: 0.5rem;
  }
  .edit-area {
    display: flex;
    flex-direction: column;
  }
  .new-note {
    height: 10rem;
  }
</style>
