<!--
   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>
  <div v-if="mission">
    <div v-if="condensed">
      <template v-if="mission.name">
        <div>{{ mission.name }} - {{ mission.type }}</div>
        <div>{{ mission.startMonth }} - {{ mission.endMonth }}</div>
        <div v-if="mission.unitName && mission.unitNumber">{{ mission.unitName }} ({{ mission.unitNumber }})</div>
      </template>
      <template v-else-if="monthsOfService && mission.type">
        <div>{{ mission.typeDisplay }} Mission</div>
        <div>{{monthsOfService}} months of service.</div>
      </template>
      <div v-else>
        No mission service reported.
      </div>
    </div>
    <table v-else class="table table-borderless table-sm">
      <thead>
        <tr>
          <th>Mission Name</th>
          <th>Mission Type</th>
          <th>Mission Language</th>
          <th>Mission Email</th>
          <th>Mission Dates</th>
          <th>Months of Service</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>{{mission.name}}</td>
          <td>{{mission.type}}</td>
          <td>{{mission.language}}</td>
          <td>{{mission.missionEmail}}</td>
          <td>{{mission.startMonth}} - {{mission.endMonth}}</td>
          <td>{{monthsOfService}}</td>
        </tr>
      </tbody>
    </table>
  </div>
  <div v-else>
    No mission service reported.
  </div>
</template>
<script>
  import { mapState } from 'vuex'

  export default {
    name: 'DetailsMission',
    props: {
      condensed: {
        type: Boolean
      }
    },
    computed: {
      ...mapState({
        mission: state => state.application.mission
      }),
      monthsOfService () {
        if (!this.mission) return ''
        const yyyyMm = /^[0-9]{4}-[0-9]{1,2}$/
        const startMonth = this.mission.startMonth
        if (!startMonth || !yyyyMm.test(startMonth)) return ''
        const endMonth = this.mission.endMonth
        if (!endMonth || !yyyyMm.test(endMonth)) return ''
        const [endY, endM] = endMonth.split('-')
        const [startY, startM] = startMonth.split('-')
        const mAdjust = Number(endM) - Number(startM)
        const yDiff = Number(endY) - Number(startY)
        return 12 * yDiff + mAdjust
      }
    }
  }
</script>
