<!--
   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>
  <table class="table table-bordered">
    <thead>
      <tr>
        <BackgroundBadge label="AR Resident" :value="application.arkansasResident" :cleared="backgroundCleared" />
        <BackgroundBadge label="Univ Discipline" :value="application.universityDiscipline" :cleared="backgroundCleared" />
        <BackgroundBadge label="Sex Offender" :value="application.sexRegistry" :cleared="backgroundCleared" />
        <BackgroundBadge label="Plural Marriage" :value="application.pluralMarriage" :cleared="backgroundCleared" />
        <BackgroundBadge label="Criminal Record" :value="application.convictionRecord" :cleared="backgroundCleared" />
      </tr>
    </thead>
    <tbody>
      <tr v-if="application.universityDiscipline === 'Y'">
        <td colspan="2">
          University Discipline
        </td>
        <td colspan="3">
          <button
            @click="showBGDetail('University Discipline', application.disciplineExplanation)"
            class="btn btn-outline-secondary btn-sm"
            >
            Show Details
          </button>
        </td>
      </tr>
      <tr v-if="application.sexRegistry === 'Y'">
        <td colspan="2">
          Sex Offender Registry
        </td>
        <td colspan="3">
          <button
            @click="showBGDetail('Sex Offender Registry', application.sexRegistryExplanation)"
            class="btn btn-outline-secondary btn-sm"
            >
            Show Details
          </button>
        </td>
      </tr>
      <tr v-if="application.pluralMarriage === 'Y'">
        <td colspan="2">
          Plural Marriage
        </td>
        <td colspan="3">
          <button
            @click="showBGDetail('Plural Marriage', application.pluralMarriageExplanation)"
            class="btn btn-outline-secondary btn-sm"
            >
            Show Details
          </button>
        </td>
      </tr>
      <tr v-if="application.convictionRecord === 'Y'">
        <td colspan="2">
          Criminal Record
        </td>
        <td colspan="3">
          <button @click="showCriminalConvictions" class="btn btn-outline-secondary btn-sm">Show Details</button>
        </td>
      </tr>
    </tbody>
  </table>
</template>
<script>
  import BackgroundBadge from './BackgroundBadge'
  import { dateFormat } from './dateTimeFormat'
  import { mapState, mapGetters } from 'vuex'

  export default {
    name: 'DetailsBackground',
    components: { BackgroundBadge },
    computed: {
      ...mapState({
        application: 'application'
      }),
      ...mapGetters(['flagByValue']),
      backgroundCleared () {
        const bgFlag = this.flagByValue('BI')
        return bgFlag.isSet && bgFlag.isCleared
      }
    },
    methods: {
      showBGDetail(label, explanation) {
        this.$modal.show('dialog', {
          title: label,
          buttons: [ { title: 'Close', default: true } ],
          text: explanation
        })
      },
      showCriminalConvictions() {
        const {criminalConvictions} = this.application
        const formatConviction = c => `
        <li>
          ${dateFormat(c.convictionDate)}
          <p>${c.convictionExplanation}</p>
          <table class="table table-sm">
            <thead>
              <tr>
                <th>Penalties</th>
                <th>Probation</th>
                <th>Resolved</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>${c.penalties}</td>
                <td>${c.probation}</td>
                <td>${c.resolved}</td>
              </tr>
            </tbody>
          </table>
        </li>
        `
        const convictions = criminalConvictions.map(formatConviction)
        const list = `<ol>${convictions}</ol>`
        this.$modal.show('dialog', {
          title: 'Conviction Details',
          buttons: [ { title: 'Close', default: true } ],
          text: list
        })
      }
    }
  }
</script>
<style scoped>
  .strike {
    text-decoration: line-through;
  }
</style>
