<template>
  <div>
    <!-- 發文列表 -->
    <div
      class="shadow"
      v-show="$route.name === 'PaperPostList'"
    >
      <b-button
        class="btn1"
        @click="paperIssue()"
        :disabled="this.officialDocument.permission === 1"
      >
        <b-icon-file-earmark-plus></b-icon-file-earmark-plus>
          新增發文
        </b-button>
      <b-button class="btn" @click="ExportListModalOpen = true"
        ><b-icon-list-check></b-icon-list-check> 匯出列表</b-button
      >
    </div>

    <!-- 收文列表 -->
    <div
      class="shadow"
      v-show="$route.name === 'PaperReceiveList'"
    >
      <b-button
        class="btn1"
        @click="paperReceive()"
        :disabled="this.officialDocument.permission === 1"
      >
        <b-icon-file-earmark-plus></b-icon-file-earmark-plus>
          新增收文
        </b-button>
      <b-button class="btn" @click="ExportListModalOpen = true"
        ><b-icon-list-check></b-icon-list-check> 匯出列表</b-button
      >
    </div>

    <!-- 檔管作業 -->
    <div class="shadow" v-show="$route.name === 'ArchivesMainTable'">
      <b-button class="btn" @click="ExportListModalOpen = true" >
        <b-icon-list-check></b-icon-list-check> 匯出列表
      </b-button>
    </div>

    <modal
      :isOpen="ExportListModalOpen"
      title="匯出列表"
      v-on:closeEvent="ExportListModalOpen = false"
    >
      <exportList @closeEvent="ExportListModalOpen = false" />
    </modal>
  </div>
</template>

<script>
import exportList from '@/views/DocumentManagement/ModalContent/exportList.vue'
import { mapState, mapMutations } from 'vuex'

export default {
  data () {
    return {
      ExportListModalOpen: false,
      initialOfficialDoc: {},
      initialList: [],
      officialDocument: {}
    }
  },
  mounted () {
    this.usePermission()
  },
  methods: {
    ...mapMutations('officialDocument', ['setOfficialDoc', 'setRecordList', 'setFileList', 'setScanFile', 'resetList']),
    paperIssue () {
      this.resetList()
      this.setOfficialDoc(this.initialOfficialDoc)
      this.$router.push({ name: 'PaperPostCreate' })
      this.setRecordList(this.initialList)
      this.setFileList(this.initialList)
      this.setScanFile(this.initialList)
    },
    paperReceive () {
      this.resetList()
      this.setOfficialDoc(this.initialOfficialDoc)
      this.$router.push({ name: 'PaperReceiveCreate' })
      this.setRecordList(this.initialList)
      this.setFileList(this.initialList)
      this.setScanFile(this.initialList)
    },
    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.officialDocument = a
            }
          }
        }
      })
    }
  },
  computed: {
    ...mapState({
      currentUser: (state) => state.user.current
    })
  },
  components: {
    modal: () => import('@/views/Modal/index.vue'),
    exportList

  }
}
</script>

<style scoped>
.shadow {
  background-color: #ffff;
  height: 65px;
  width: 100%;
  box-shadow: 2px 2px 20px rgba(0, 0, 0, 40);
  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;
}
.btn: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;
}
.btn1:hover {
  color: rgb(255, 255, 255);
  background-color: rgb(49, 68, 175);
}
</style>
