<template>
  <div>
    <div v-show="$route.name === 'ConferenceDetail'" class="shadow">
      <b-button @click="backOverview" class="btn1">
        <b-icon-caret-up /> 返回會議總覽
      </b-button>
      <b-button
        @click="AddAgenda"
        class="btn1"
        :disabled="conferenceManagement.permission === 1"
      >
        <b-icon-file-earmark-plus /> 建立議題
      </b-button>
      <b-button
        @click="deleteConference(dataId)"
        class="btn_delete"
        :disabled="conferenceManagement.permission === 1"
      >
        <b-icon-trash /> 刪除
      </b-button>
    </div>
    <div v-show="$route.name === 'AgendaView'" claㄎss="shadow">
      <b-button @click="backAgendaView" class="btn1">
        <b-icon-caret-up /> 返回議題總覽
      </b-button>
      <b-button
        class="btn_delete"
        @click="postCaseClosed"
        :disabled="havePostCaseClosed.permission === 1"
      >
        <b-icon icon="file-earmark-excel"></b-icon> 解除列管
      </b-button>
    </div>
    <div v-show="$route.name === 'ConsultAgenda'" class="shadow">
      <b-button @click="backAgendaView" class="btn1">
        <b-icon-caret-up />返回議題總覽
      </b-button>
    </div>
    <!-- 建立議題 Modal -->
    <modal
      :is-open="AddAgendaModalOpen"
      title="建立議題"
      @closeEvent="AddAgendaModalOpen = false"
    >
      <addAgenda @closeEvent="addAgenda" />
    </modal>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
import addAgenda from '@/views/ConferenceManagement/ModalContent/addAgenda.vue'
export default {
  components: {
    modal: () => import('@/views/Modal/index.vue'), // 不用寫在import只需寫在這邊
    addAgenda
  },
  data () {
    return {
      AddAgendaModalOpen: false,
      dataId: '',
      conferenceSchedule: {},
      conferenceManagement: {},
      havePostCaseClosed: {}
    }
  },
  async created () {
    if (this.$route.query.fid !== undefined) {
      await this.$store.dispatch(
        'conference/lookupConference',
        this.$route.query.fid
      )
    }
  },
  async mounted () {
    this.dataId = this.$route.query.fid
    await this.$store.dispatch('schedule/getScheduleList')
    for (let i = 0; i < this.scheduleList.length; i++) {
      const element = this.scheduleList[i]
      if (element.conferenceId === this.$route.query.fid) {
        this.conferenceSchedule = element
      }
    }
    this.usePermission()
  },
  methods: {
    ...mapActions('issue', ['patch', 'initIssue']),
    backOverview () {
      this.$router.push({ name: 'Overview' })
    },
    backAgendaView () {
      this.$router.push({ name: 'Agenda' })
    },
    async postCaseClosed () {
      var wasNull = false
      if (this.Issue.resolution.length === 0) wasNull = true
      if (wasNull) alert('未新增結論!')
      else {
        var yes = confirm('確定解除列管?')
        if (yes) {
          alert('解除列管成功')
          this.Issue.issue_status = '解除列管'
          await this.patch()
          // console.log(this.Issue)
          this.$router.push({ name: 'Agenda' })
        } else {
          alert('取消解除列管')
        }
      }
    },
    async AddAgenda () {
      await this.initIssue()
      this.AddAgendaModalOpen = true
    },
    async deleteConference (id) {
      if (this.conference.officialDocId === null) {
        if (!confirm('確定刪除會議嗎?')) return
        await this.$store.dispatch('conference/deleteConference', id)
        await this.$store.dispatch('schedule/deleteSchedule', this.conferenceSchedule._id)
        alert('此會議已刪除')
        this.backOverview()
      } else {
        alert('已新增收發文，不可刪除')
      }
    },
    async addAgenda () {
      this.AddAgendaModalOpen = false
      this.$emit('addAgenda')
    },
    usePermission () { // 確認權限
      Object.values(this.currentUser.tenderPermissions).forEach(obj => {
        if (obj.tenderId === this.currentUser.defaultTender) {
          let permission = obj.permission
            .filter(name => name.title === '工作協同')
            .map(val => { return val.functionItem })
          for (const element of permission) {
            for (const a of element) {
              if (a.name === '會議管理') this.conferenceManagement = a
              if (a.name === '解除列管') this.havePostCaseClosed = a
            }
          }
        }
      })
    }
  },
  computed: {
    ...mapState({
      conference: (state) => state.conference.Conference,
      scheduleList: (state) => state.schedule.ScheduleList,
      Issue: (state) => state.issue.Issue,
      currentUser: (state) => state.user.current
    })
  }
}
</script>

<style lang="scss" scoped>
.shadow {
  background-color: #ffff;
  height: 65px;
  width: 100%;
  box-shadow: 2px 2px 20px rgba(0, 0, 0, 40);
  /* border-bottom-width:3px;
    border-bottom-color:#1D325F;
    border-bottom-style:solid */
  position: fixed;
  top: 55px;
  z-index: 500;
}
.btn {
  background-color: white;
  font-size: 16px;
  margin-top: 15px;
  margin-left: 15px;
  padding-left: 7px;
  padding-right: 7px;
  padding-top: 5px;
  padding-bottom: 5px;
  color: #383838;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.3);
  border: none;
  hover {
    color: black;
    background-color: rgb(228, 228, 228);
  }
}
.btn1 {
  background-color: rgb(75, 102, 255);
  font-size: 16px;
  margin-top: 15px;
  margin-left: 15px;
  padding-left: 7px;
  padding-right: 7px;
  padding-top: 5px;
  padding-bottom: 5px;
  color: #ffffff;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.3);
  border: none;
  hover {
    color: rgb(255, 255, 255);
    background-color: rgb(49, 68, 175);
  }
}
.btn_delete {
  background-color: rgb(255, 0, 0);
  font-size: 16px;
  padding: 5px 7px;
  color: #ffffff;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
  border: none;
}
.btn_delete:hover {
  color: rgb(255, 255, 255);
  background-color: rgb(199, 0, 0);
}
.btn_delete.btn-secondary[disabled] {
  background-color: rgb(224, 224, 224);
  color: rgb(150, 150, 150);
}
</style>
