<!--
  @description:采购单归档
  @Modifier: zhangyawen
  @routerPath: /procurementArchived/procurementOrderArchived-add
  @date: 2021-4-12-9:15
-->
<template>
  <div>
    <section class="title p0-16 clearfix">
      <template>
        <div class="title-back pointer" @click="back">
          <i class="iconfont icon-fanhui mr10" />归档信息
        </div>
      </template>
    </section>
    <section class="p0-16">
      <el-form ref="allData" label-width="160px" label-position="left">
        <div class="page-title">
          1.采购单信息
        </div>
        <div>
          <table class="from_table">
            <tr>
              <td>
                <el-form-item label="采购单编号" prop="tenderNo" />
              </td>
              <td>
                {{ archivedInfo.tenderNo }}
              </td>
              <td>
                <el-form-item label="采购单名称" prop="tenderName" />
              </td>
              <td>
                {{ archivedInfo.tenderName }}
              </td>
            </tr>
            <tr>
              <td>
                <el-form-item label="是否框架" prop="planType" />
              </td>
              <td>
                {{ archivedInfo.planType && filterDictText(isFrameList,archivedInfo.planType) }}
              </td>
              <td>
                <el-form-item label="采购员" prop="sourcingUserRealName" />
              </td>
              <td>
                {{ archivedInfo.sourcingUserRealName }}
              </td>
            </tr>
            <tr>
              <td>
                <el-form-item label="采购单状态" prop="purchaseProjectStatus" />
              </td>
              <td>
                {{ archivedInfo.purchaseProjectStatus && filterDictText(protprojectPurchaseProjectStatusList,archivedInfo.purchaseProjectStatus) }}
              </td>
              <td />
              <td />
            </tr>
          </table>
        </div>
      </el-form>
      <el-form ref="form" label-width="160px">
        <div class="page-title">
          <span>2.归档文件</span>
        </div>
        <el-button type="primary" :loading="exportLoading" @click="dowloadArchivedFile">
          一键下载文件
        </el-button>
        <div style="margin-top: 10px">
          <el-table v-loading="loading" :data="tableData" border>
            <el-table-column
              type="index"
              label="序号"
              width="60"
              align="center"
            />
            <el-table-column
              label="文件目录名称"
              align="center"
              prop="fileIndexName"
            />
            <el-table-column
              label="是否必须归档"
              align="center"
              min-width="200"
            >
              <template slot-scope="scope">
                {{ scope.row.isMustArchive && filterDictText(is_must_archiveList,scope.row.isMustArchive) }}
              </template>
            </el-table-column>
            <el-table-column
              label="文件"
              header-align="center"
              align="left"
              min-width="160"
            >
              <template slot-scope="scope">
                <downloadFile v-if="scope.row.archiveFileData" dis-block="block" :data="scope.row.archiveFileData" />
                <downloadFile v-else dis-block="block" :file-list="scope.row.archiveFileList" />
                <el-link
                  v-if="scope.row.restType==='yes'"
                  type="primary"
                  @click.stop="onUpRestFile(scope.row)"
                >
                  上 传
                </el-link>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form>
      <div class="archiveBtn">
        <!-- <el-button
          :loading="confirmLoading"
          type="primary"
          @click="allArchive"
        >
          一键归档
        </el-button> -->
        <el-button
          v-loading.fullscreen.lock="confirmLoading"
          type="primary"
          element-loading-text="归档中，请稍后"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(255, 255, 255, 0.2)"
          @click="allArchive"
        >
          一键归档
        </el-button>
      </div>
    </section>
    <!-- 上传其他附件 -->
    <uploadRestDialog :up-file-dialog-data="upFileDialogData" :rest-file-type="restFileType" @upSuccess="onUpSuccess" @close="onClose" />
  </div>
</template>

<script>
/* api */
// eslint-disable-next-line no-unused-vars
import { getProcurementArchiveList, archiveApi, downloadAllProjectZipFile } from '@/api/bidding/bidArchive'
/* 组件 */
import downloadFile from '@comp/common/updownload/downloadFile' // 附件下载
import upload from '@comp/common/updownload/upload' // 上传
import { download, filterDictText } from '@/assets/js/utils'// 常用方法
import uploadRestDialog from './uploadRest-dialog.vue' // 上传其他文件
/* mixins*/
import { archivedDic } from '.././mixins/dicMixins.js'

export default {
  name: 'ProcurementOrderArchivedAdd',
  components: {
    downloadFile, // 附件下载
    upload,
    uploadRestDialog
  },
  mixins: [archivedDic],
  data() {
    return {
      archivedInfo: {},
      tenderNo: this.$route.params.tenderNo,
      tableData: [],
      isUpdateOtherFile: '00', // 是否上传附件参数
      // 上传其他附件
      upFileDialogData: {
        upFileVisible: false
      },
      // 上传其他附件数据
      restFileType: {
        bizId: '',
        fileType: 'prot_archive_other_file'
      },
      confirmLoading: false, // 按钮loading
      exportLoading: false, // 一键下载loading
      loading: false, // 列表loading
      protprojectPurchaseProjectStatusList: [], // 采购单状态
      is_must_archiveList: [] // 是否必须归档
    }
  },
  created() {
    // 查询数据
    this.initData()

    this.initGroup() // 获取字典方法
  },
  methods: {
    filterDictText,
    // 存储isUpdateOtherFile
    setIsUpdateOtherFile(num) {
      const name = this.archivedInfo.tenderNo
      if (num) {
        this.isUpdateOtherFile = num
        sessionStorage.setItem(name, num)
      } else {
        this.isUpdateOtherFile = sessionStorage.getItem(name) || '00'
      }
    },
    // 获取字典方法
    initGroup() {
      const data = [
        'protprojectPurchaseProjectStatus',
        'is_must_archive'
      ]
      this.$store.dispatch('getGroupType', data).then(res => {
        Object.keys(res.bidTypeData).forEach((key) => {
          this[key] = res.bidTypeData[key]
        })
      })
    },
    // 返回
    back() {
      this.$router.push('/bidArchived/procurementOrderArchived-List')
    },
    // 查询数据
    initData() {
      this.loading = true
      const params = {
        tenderNo: this.tenderNo
      }
      getProcurementArchiveList(params).then(res => {
        this.archivedInfo = res.data
        this.tableData = res.data.archiveFileDetailVoList
        this.setIsUpdateOtherFile()
        this.tableData.push({ fileIndexName: '其他文件', isMustArchive: '00', restType: 'yes', archiveFileData: { bizId: this.archivedInfo.archiveId, fileType: 'prot_archive_other_file' }})
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 一键归档
    allArchive() {
      this.$confirm('确认一键归档吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        this.confirmLoading = true
        const data = {
          archiveId: this.archivedInfo.archiveId, // 归档id
          isUpdateOtherFile: this.isUpdateOtherFile// 是否修改了其他附件
        }
        archiveApi(data).then(res => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.confirmLoading = false
          this.back()// 返回
        }).catch(() => {
          this.confirmLoading = false
        }).finally(() => {
          this.setIsUpdateOtherFile('00')
        })
      })
    },
    // 上传其他附件
    onUpRestFile() {
      this.upFileDialogData.upFileVisible = true
      this.restFileType.bizId = this.archivedInfo.archiveId
    },
    // 其他附件上传成功后改变
    onUpSuccess(res) {
      this.setIsUpdateOtherFile(res)
    },
    // 弹框关闭时
    onClose() {
      this.initData()
    },
    // 一键下载
    dowloadArchivedFile() {
      const params = {
        archiveId: this.archivedInfo.archiveId, // 归档id
        isUpdateOtherFile: this.isUpdateOtherFile, // 是否修改了其他附件
        tenderNo: this.archivedInfo.tenderNo // 采购单编号
      }
      this.exportLoading = true
      downloadAllProjectZipFile(params).then((res) => {
        download(res, `[${this.archivedInfo.tenderNo}]${this.archivedInfo.tenderName}采购单归档文件.zip`)
      }).catch((error) => {
        this.$message.warning(error)
      }).finally(() => {
        this.setIsUpdateOtherFile('00')
        this.exportLoading = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
.page-title {
  margin: 16px 0;
}
.archiveBtn {
  margin: 15px auto;
  text-align: center;
}
</style>
