<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 === 'ManagePostList'">
    <b-button
    class="btn1"
    @click="PostManagementListModalOpen = true"
    :disabled = "this.sendReceivePostManagement.permission === 1"
    >
        <b-icon-box-arrow-up />上傳檔案
      </b-button>
      <!-- 刪除 -->
      <b-button
        class="btn_delete"
        @click="DeletePostManagementModalOpen = true"
        :disabled = "this.sendReceivePostManagement.permission === 1"
        v-b-popover.hover="'JPG, PNG, PDF, DOCX, DOC, XLSX 檔案'"
        title="支援刪除類型 "
      >
        <b-icon-trash-fill></b-icon-trash-fill>&ensp;刪除檔案
      </b-button>
      <!-- 發文參數設置 -->
      <b-button
        class="btn"
        @click="AddPostFileTypeModalOpen = true"
      >
      <!-- :disabled = "this.sendReceivePostManagement.permission === 1" -->
      <b-icon-tools></b-icon-tools> 單位類表
      </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 === 'ManageReceiveList'">
    <b-button
        class="btn1"
        @click="ReceiveManagementListModalOpen = true"
        :disabled="this.sendReceivePostManagement.permission === 1"
        v-b-popover.hover="'JPG, PNG, PDF, DOCX, DOC, XLSX 檔案'"
        title="支援上傳類型 "
      >
        <b-icon-box-arrow-up />上傳檔案
      </b-button>
      <!-- 刪除 -->
      <b-button
        class="btn_delete"
        @click="DeleteReceiveManagementModalOpen = true"
        :disabled="this.sendReceivePostManagement.permission === 1"
        v-b-popover.hover="'JPG, PNG, PDF, DOCX, DOC, XLSX 檔案'"
        title="支援刪除類型 "
      >
        <b-icon-trash-fill></b-icon-trash-fill>&ensp;刪除檔案
      </b-button>
      <!-- 收文參數設置 -->
      <b-button
        class="btn"
        @click="AddReceiveFileTypeModalOpen = true"
      >
      <!-- :disabled = "this.sendReceivePostManagement.permission === 1" -->
      <b-icon-tools></b-icon-tools> 單位類表
      </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 -->
    <modal
      :isOpen="PostManagementListModalOpen"
      title="新增發文資料"
      v-on:closeEvent="PostManagementListModalOpen = false"
    >
      <postManagementList @closeEvent="PostManagementListModalOpen = false" />
    </modal>
    <!-- 新增收文資料modal -->
    <modal
      :isOpen="ReceiveManagementListModalOpen"
      title="新增收文資料"
      v-on:closeEvent="ReceiveManagementListModalOpen = false"
    >
      <receiveManagementList @closeEvent="ReceiveManagementListModalOpen = false" />
    </modal>
     <!-- 刪除發文資料modal -->
     <modal
      :isOpen="DeletePostManagementModalOpen"
      title="刪除發文資料"
      v-on:closeEvent="DeletePostManagementModalOpen = false"
    >
      <deletePostManagement @closeEvent="DeletePostManagementModalOpen = false" />
    </modal>
    <!-- 刪除收文資料modal -->
    <modal
      :isOpen="DeleteReceiveManagementModalOpen"
      title="刪除收文資料"
      v-on:closeEvent="DeleteReceiveManagementModalOpen = false"
    >
      <deleteReceiveManagement @closeEvent="DeleteReceiveManagementModalOpen = false" />
    </modal>
    <!-- 發文單位類別modal -->
    <modal
      :isOpen="AddPostFileTypeModalOpen"
      title="單位類別"
      v-on:closeEvent="AddPostFileTypeModalOpen = false"
    >
      <addPostFileType @closeEvent="AddPostFileTypeModalOpen = false" />
    </modal>
     <!-- 收文單位類別modal -->
     <modal
      :isOpen="AddReceiveFileTypeModalOpen"
      title="單位類別"
      v-on:closeEvent="AddReceiveFileTypeModalOpen = false"
    >
      <addReceiveFileType @closeEvent="AddReceiveFileTypeModalOpen = false" />
    </modal>
    <!-- 匯出列表modal -->

    <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 postManagementList from '@/views/DocumentManagement/ModalContent/postManagementList.vue'
import receiveManagementList from '@/views/DocumentManagement/ModalContent/receiveManagementList.vue'
import deletePostManagement from '@/views/DocumentManagement/ModalContent/deletePostManagement.vue'
import deleteReceiveManagement from '@/views/DocumentManagement/ModalContent/deleteReceiveManagement.vue'
import { mapState, mapMutations, mapActions } from 'vuex'
import addPostFileType from '@/views/DocumentManagement/ModalContent/addPostFileType.vue'
import addReceiveFileType from '@/views/DocumentManagement/ModalContent/addReceiveFileType.vue'
export default {
  data () {
    return {
      ExportListModalOpen: false,
      PostManagementListModalOpen: false,
      ReceiveManagementListModalOpen: false,
      DeletePostManagementModalOpen: false,
      DeleteReceiveManagementModalOpen: false,
      AddPostFileTypeModalOpen: false,
      AddReceiveFileTypeModalOpen: false,
      initialOfficialDoc: {},
      initialList: [],
      officialDocument: {},
      // 權限
      sendReceivePostManagement: {}
    }
  },
  mounted () {
    this.usePermission()
  },
  methods: {
    ...mapMutations('officialDocument', ['setOfficialDoc', 'setRecordList', 'setFileList', 'setScanFile', 'resetList']),
    ...mapActions('tenders', ['initPostFileManagement']),
    ...mapActions('tenders', ['initReceiveFileManagement']),
    async postManagementList () {
      await this.initPostFileManagement()
      this.PostManagementListModalOpen = true
    },
    async receiveManagementList () {
      await this.initReceiveFileManagement()
      this.ReceiveManagementListModalOpen = true
    },
    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
              if (a.name === '收發文資料管理') this.sendReceivePostManagement = a
            }
          }
        }
      })
    }
  },
  computed: {
    ...mapState({
      currentUser: (state) => state.user.current
    })
  },
  components: {
    modal: () => import('@/views/Modal/index.vue'),
    exportList,
    postManagementList,
    receiveManagementList,
    deletePostManagement,
    deleteReceiveManagement,
    addPostFileType,
    addReceiveFileType
  }
}
</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);
}
.btn1.btn-secondary[disabled] {
  background-color: rgb(224, 224, 224);
  color: rgb(150, 150, 150);
}
.btn_delete {
  background-color: rgb(255, 0, 0);
  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;
}
.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>
