<template>
  <div>
    <div class="shadow">
      <b-button
        class="btn1"
        v-b-popover.hover="'JPG, PNG, PDF, DOCX, DOC, XLSX 檔案'"
        title="支援上傳類型 "
        @click="checkSave"
        :disabled="qualityAssurance.permission === 1"
      >
        <b-icon-box-arrow-up />上傳檔案
      </b-button>
      <input
        ref="inputFiles"
        type="file"
        class="d-none"
        v-on:change="getInputFiles()"
      />
      <!-- 刪除 -->
      <b-button
        class="btn_delete"
        @click="DeleteQualityAssuranceModalOpen = true"
        :disabled="this.qualityAssurance.permission === 1"
        v-b-popover.hover="'JPG, PNG, PDF, DOCX, DOC, XLSX 檔案'"
        title="支援刪除類型 "
      >
        <b-icon-trash-fill></b-icon-trash-fill>&ensp;刪除檔案
      </b-button>
    </div>
    <!-- 刪除上傳資料modal -->
    <modal
      :isOpen="DeleteQualityAssuranceModalOpen"
      title="刪除品保文件"
      v-on:closeEvent="DeleteQualityAssuranceModalOpen = false"
    >
      <deleteQualityAssurance @closeEvent="DeleteQualityAssuranceModalOpen = false" />
    </modal>
  </div>
</template>
<script>
import deleteQualityAssurance from '@/views/QualityAssurance/ModalContent/deleteQualityAssurance.vue'
import { mapActions, mapState } from 'vuex'
export default {
  components: {
    modal: () => import('@/views/Modal/index.vue'),
    deleteQualityAssurance
  },
  data () {
    return {
      DeleteQualityAssuranceModalOpen: false,
      filesToBeUploaded: [],
      relateFileList: [],
      qualityAssurance: {}
    }
  },
  async mounted () {
    await this.getOtherList(this.currentUser.defaultTender)
    this.usePermission()
  },
  methods: {
    ...mapActions('tenders', ['getOtherList']),
    // 上傳檔案
    async checkSave () {
      this.$refs.inputFiles.click()
    },
    async getInputFiles () {
      if (window.FileReader && window.Blob) {
        let type = this.$refs.inputFiles.files[0].type
        if (
          type === 'image/png' ||
          type === 'application/pdf' ||
          type === 'image/jpeg' ||
          type === 'image/jpg' ||
          type ===
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ||
          type ===
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
          type === 'application/msword'
        ) {
          this.filesToBeUploaded.push(this.$refs.inputFiles.files[0])
          await this.uploadFile(
            this.filesToBeUploaded[this.filesToBeUploaded.length - 1],
            this.filesToBeUploaded.length - 1
          )
        } else {
          alert(
            '不支援的檔案格式！僅支援上傳 JPG, PNG, PDF, DOCX, DOC, XLSX 檔案'
          )
        }
      }
    },
    async uploadFile (file, index) {
      try {
        console.log(file)
        await this.$store.dispatch('tenders/addQualityDoc', {
          id: this.currentUser.defaultTender, file: file
        })
        await this.$store.dispatch('tenders/getQualityDoc', this.currentUser.defaultTender)
        alert('上傳成功！')
        this.$emit('getList')
      } catch (err) {
        alert('上傳失敗！')
      } finally {
        this.$set(this.filesToBeUploaded, index, file)
      }
    },
    async deleteFile () {
      await this.$store.dispatch('tenders/getQualityDoc', this.currentUser.defaultTender)
    },
    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.qualityAssurance = a
            }
          }
        }
      })
    }
  },
  computed: {
    ...mapState({
      currentUser: (state) => state.user.current
    })
  }
}
</script>
<style 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;
}
.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>
