<!--
  @description: 招标项目-归档-查看归档详情
  @Modifier：肖文龙
  @routerPath:
  @date：  2019/12/03
-->
<template>
  <div>
    <table class="tableData mb10">
      <tr>
        <td>招标编号</td>
        <td>{{ row.tenderNo }}</td>
        <td>标段（包）名称</td>
        <td>{{ row.packName }}</td>
      </tr>
    </table>
    <!-- 招标文件根据类型区分 -->
    <dl class="projectInfoDetail">
      <dt>
        <p class="projectInfoDetail_icon">
          <i class="iconfont icon-biaoduanbaoxinxi" />
        </p>
        公告列表
      </dt>
      <dd>
        <el-table
          :data="tableDataTypeCandidate('02')"
          style="width: 100%;margin-top:10px"
        >
          <el-table-column
            label="名称"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <span>
                {{ filterDictText(bulletinTypeMethod, score.row.bulletinType) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="文件"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <div v-for="(items,index) in score.row.List" :key="index" style="color:#007CCC; cursor: pointer;" @click.stop="getFile(items)">
                <span> <i class="iconfont icon-downloadRecord archived" />   {{ items.attachmenttitle }}</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </dd>
    </dl>
    <dl class="projectInfoDetail">
      <dt>
        <p class="projectInfoDetail_icon">
          <i class="iconfont icon-biaoduanbaoxinxi" />
        </p>
        邀请函列表
      </dt>
      <dd>
        <el-table
          :data="projectArchiveOldInvitationVoList"
          style="width: 100%;margin-top:10px"
        >
          <el-table-column
            :formatter="formatCgformField"
            prop="cgformField"
            label="文件类型"
            header-align="center"
            align="left"
          >
            <template slot-scope="score">
              邀请函
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            prop="content"
          >
            <template slot-scope="score">
              <el-link
                type="primary"
                @click.stop="getContent(score.row)"
              >
                <span>查看详情</span>
              </el-link>
              <!-- <span v-html="score.row.content" /> -->
            </template>
          </el-table-column>
          <el-table-column
            prop="tenderDocumentReceiveEndTime"
            label="文件"
            header-align="center"
            align="left"
            min-width="300px"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click.stop="getFile(scope.row)"
              >
                <span><i class="iconfont icon-downloadRecord archived" />                {{ scope.row.attachmenttitle }}</span>
              </el-link>
            </template>
          </el-table-column>
        </el-table>
      </dd><dd />
    </dl>
    <!-- 招标/采购文件列表 -->
    <dl class="projectInfoDetail">
      <dt>
        <p class="projectInfoDetail_icon">
          <i class="iconfont icon-biaoduanbaoxinxi" />
        </p>
        招标/采购文件列表
      </dt>
      <dd>
        <el-table
          :data="projectArchiveOldProcfileVoList"
          style="width: 100%;margin-top:10px;"
        >
          <el-table-column
            label="名称"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <span>招标/采购</span>
            </template>
          </el-table-column>
          <el-table-column
            label="文件"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <div v-for="(items,index) in score.row.fileList" :key="index" style="color:#007CCC; cursor: pointer;" @click.stop="getFile(items)">
                <span><i class="iconfont icon-downloadRecord archived" /> {{ items.attachmenttitle }}</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </dd>
    </dl>
    <dl class="projectInfoDetail">
      <dt>
        <p class="projectInfoDetail_icon">
          <i class="iconfont icon-biaoduanbaoxinxi" />
        </p>
        投标列表
      </dt>
      <dd>
        <!-- 老平台开标一览表 -->
        <el-table
          :data="projectArchiveOldBidVoList"
          style="width: 100%;"
        >
          <el-table-column
            :formatter="formatCgformField"
            prop="cgformField"
            label="文件类型"
            header-align="center"
            align="left"
          />
          <el-table-column
            prop="resultStatus"
            label="是否中标"
            header-align="center"
            align="left"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.resultStatus==='01'">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column
            label="文件"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <div v-for="(items,index) in score.row.duplicate" :key="index" style="color:#007CCC; cursor: pointer;" @click.stop="getOpen(items.supplierName,projectArchiveOldBidVoList[score.$index].fileList)">
                <!-- <span> {{ items.supplierName }}</span> -->
                <span>
                  <i class="iconfont icon-guidangguanli" style="font-size:12px" />
                  {{ items.supplierName }}
                </span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </dd>
    </dl>
    <!--  老平台投标文件展示 -->
    <dl class="projectInfoDetail">
      <dt>
        <p class="projectInfoDetail_icon">
          <i class="iconfont icon-biaoduanbaoxinxi" />
        </p>
        评标报表
      </dt>
      <dd>
        <el-table
          :data="projectArchiveOldEvaluationVoList"
          style="width: 100%;margin-top:10px;"
        >
          <el-table-column
            label="名称"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <span>评标报表</span>
            </template>
          </el-table-column>
          <el-table-column
            label="文件"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <div v-for="(items,index) in score.row.fileList" :key="index" style="color:#007CCC; cursor: pointer;" @click.stop="getFile(items)">
                <span><i class="iconfont icon-downloadRecord archived" /> {{ items.attachmenttitle }}</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </dd>
    </dl>
    <!-- 招标文件根据类型区分 -->
    <dl class="projectInfoDetail">
      <dt>
        <p class="projectInfoDetail_icon">
          <i class="iconfont icon-biaoduanbaoxinxi" />
        </p>
        候选人公示列表
      </dt>
      <dd>
        <el-table
          :data="tableDataTypeCandidate('00')"
          style="width: 100%;margin-top:10px"
        >
          <el-table-column
            label="名称"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <span>
                {{ filterDictText(bulletinTypeMethod, score.row.bulletinType) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="文件"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <div v-for="(items,index) in score.row.List" :key="index" style="color:#007CCC; cursor: pointer;" @click.stop="getFile(items)">
                <span> <i class="iconfont icon-downloadRecord archived" />   {{ items.attachmenttitle }}</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </dd>
    </dl>
    <!-- 招标文件根据类型区分 -->
    <dl class="projectInfoDetail">
      <dt>
        <p class="projectInfoDetail_icon">
          <i class="iconfont icon-biaoduanbaoxinxi" />
        </p>
        结果公告列表
      </dt>
      <dd>
        <el-table
          :data="tableDataTypeCandidate('01')"
          style="width: 100%;margin-top:10px"
        >
          <el-table-column
            label="名称"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <span>
                {{ filterDictText(bulletinTypeMethod, score.row.bulletinType) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="文件"
            prop="attachmenttitle"
          >
            <template slot-scope="score">
              <div v-for="(items,index) in score.row.List" :key="index" style="color:#007CCC; cursor: pointer;" @click.stop="getFile(items)">
                <span> <i class="iconfont icon-downloadRecord archived" />   {{ items.attachmenttitle }}</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </dd>
    </dl>
    <!-- 邀请函内容 -->
    <contentDialog v-if="contentDialog.visible" :dialog-data="contentDialog" />
    <!-- 文件下载 -->
    <fileDialog v-if="dialogData.visible" :dialog-data="dialogData" />
  </div>
</template>
<script>
/* api */
import { downLoadFile } from '@/api/common/uploadCenter'
import { getArchiveOldList, getAllProjectZipFile } from '@/api/bidding/bidArchive.js'
/* 公用方法 */
import { filterDictText, download } from '@/assets/js/utils'// 常用方法
import fileDialog from './file-dialog' // 文件下载
import contentDialog from './content-dialog' // 邀请函内容
/* 混入-字典信息 */
import { tenderArchivedGroup, bidArchiveddGroup } from './../mixins/bidArchivedGroupMixin'// 字典信息
import { bidArchived } from './../mixins/bidArchivedMixin'// 公共方法
import { commonMixins } from './mixins/commonMixins'// 公共方法
/* 组件 */
import downloadFile from '@comp/common/updownload/downloadFile'// 附件下载
import upload from '@comp/common/updownload/upload' // 附件上传组件
export default {
  name: 'ArchivedCompDetail',
  components: {
    fileDialog,
    downloadFile, // 附件下载
    upload,
    contentDialog
  },

  mixins: [commonMixins],
  props: {
    // 项目信息
    packId: {
      type: String,
      default: ''
    },
    row: {
      type: Object,
      default: function() {
        return
      }
    }
  },
  data() {
    return {
      dialogData: {
        // 文件下载
        visible: false
      },
      contentDialog: {
        // 邀请函内容
        visible: false
      },
      ...tenderArchivedGroup, // 字典信息
      ...bidArchiveddGroup,
      ...bidArchived, // 公共方法
      loading: false, // 列表loading
      tableData: [], // 归档文件列表
      // 项目信息
      fromData: {
        archiveId: '', // 归档id
        tenderNo: '', // 项目编号
        packName: '', // 标段名称
        packStatus: '', // 标段状态 00 项目未生效 01 项目已生效
        archiveStatus: '', // 归档状态 00 未归档 01 已归档
        archiveFile: [] // 归档文件
      },
      // 下载所有项目文件
      zipFileData: {
        archiveId: '', // 归档id
        isUpdateOtherFile: ''// 是否修改了其他附件 00:否 01:是
      },
      projectArchiveOldEvaluationVoList: [], // 老平台 开标一览表
      projectArchiveOldBidVoList: [], // 老平台开标一览表、投标文件
      projectArchiveOldBulletinVoList: [],
      projectArchiveOldInvitationVoList: [], // 邀请函
      projectArchiveOldProcfileVoList: [] // 招标采购文件
    }
  },
  computed: {
    tableDataTypeCandidate() {
      // type 00 候选人公示(结果公示) 01结果公示 02 其他所有类型
      return function(type) {
        if (type === '00') {
          // 候选人公示(结果公示)
          return this.projectArchiveOldBulletinVoList.filter((data) => {
            return data.bulletinType === '05'
          })
        } else if (type === '01') {
          // 候选人公示(结果公示)
          return this.projectArchiveOldBulletinVoList.filter((data) => {
            return data.bulletinType === '06'
          })
        } else {
          return this.projectArchiveOldBulletinVoList.filter((data) => {
            return data.bulletinType !== '05' && data.bulletinType !== '06'
          })
        }
      }
    }
  },
  watch: {
    projectArchiveOldBidVoList: {
      handler(newVal, oldVal) {
        // console.log('count的值发生了变化：' + this.projectArchiveOldBidVoList)
      },
      // 代表在wacth里声明了count这个方法之后立即先去执行handler方法
      immediate: true
    }
  },
  created() {
    // 查询数据
    this.initData()
  },
  methods: {
    getContent(row) {
      this.contentDialog.visible = true
      this.contentDialog.row = row
    },
    filterDictText,
    // 触发-返回父组件回调函数事件
    initCalBack() {
      // 只有（招标项目-驱动）需要
      this.$emit('call-back', {
        archiveId: this.fromData.archiveId, // 归档id
        archiveStatus: this.fromData.archiveStatus, // 归档状态
        isUpdateOtherFile: this.fromData.isUpdateOtherFile// 是否修改了其他附件 00:否 01:是
      })
    },
    getOpen(supplierName, fileList) {
      this.dialogData.visible = true
      this.dialogData.supplierName = supplierName
      this.dialogData.fileList = fileList
    },
    formatCgformField(row, column, cellValue) {
      if (row.cgformField === 'bulletinContent') {
        return filterDictText(this.bulletinTypeMethod, row.bulletinType)
      } else if (row.cgformField === 'purfileid') {
        return '招标文件'
      } else if (row.cgformField === 'decryptBidFile') {
        return '投标文件'
      } else if (row.cgformField === 'bidOpeningDocumentFile') {
        return '开标一览表'
      } else if (row.cgformField === 'attachment' || row.cgformField === 'Attrationid') {
        return '附件'
      } else {
        return row.attachmenttitle
      }
    },

    // 查询数据
    initData() {
      this.loading = true
      getArchiveOldList({ packId: this.packId }).then(res => {
        // this.tableData = res.data
        var arrayData = res.data
        // 老平台开标一览表展示
        if (arrayData.projectArchiveOldEvaluationVoList.length) {
          this.projectArchiveOldEvaluationVoList.push(JSON.parse(JSON.stringify(res.data.projectArchiveOldEvaluationVoList[0])))
          this.projectArchiveOldEvaluationVoList[0].fileList = res.data.projectArchiveOldEvaluationVoList
        }
        // 招标采购文件
        if (arrayData.projectArchiveOldProcfileVoList.length) {
          this.projectArchiveOldProcfileVoList.push(JSON.parse(JSON.stringify(res.data.projectArchiveOldProcfileVoList[0])))
          this.projectArchiveOldProcfileVoList[0].fileList = res.data.projectArchiveOldProcfileVoList
        }
        // 老平台投标文件展示
        if (arrayData.projectArchiveOldBidVoList.length) {
          const bidOpening = arrayData.projectArchiveOldBidVoList.filter(items => items.resultStatus === '01')
          const notOpening = arrayData.projectArchiveOldBidVoList.filter(items => items.resultStatus !== '01')
          if (bidOpening.length) {
            // 中标
            this.projectArchiveOldBidVoList.push(JSON.parse(JSON.stringify(bidOpening[0])))
            this.projectArchiveOldBidVoList[this.projectArchiveOldBidVoList.length - 1].fileList = bidOpening
            // 去重复
            this.projectArchiveOldBidVoList[this.projectArchiveOldBidVoList.length - 1].duplicate = this.duplicatedistinct(bidOpening, 'supplierName')
          }
          if (notOpening.length) {
            // 未中标

            this.projectArchiveOldBidVoList.push(JSON.parse(JSON.stringify(notOpening[0])))
            console.log(this.projectArchiveOldBidVoList)
            this.projectArchiveOldBidVoList[this.projectArchiveOldBidVoList.length - 1].fileList = notOpening
            // 去重复
            this.projectArchiveOldBidVoList[this.projectArchiveOldBidVoList.length - 1].duplicate = this.duplicatedistinct(notOpening, 'supplierName')
          }
        }
        // 根据bulletinType组成新的数字
        if (arrayData.projectArchiveOldBulletinVoList.length) {
          this.projectArchiveOldBulletinVoList = this.typeNewData(arrayData.projectArchiveOldBulletinVoList)
        }
        // 邀请函
        if (arrayData.projectArchiveOldInvitationVoList.length) {
          this.projectArchiveOldInvitationVoList = arrayData.projectArchiveOldInvitationVoList
          console.log(this.projectArchiveOldInvitationVoList.length)
        }

        this.loading = false
      })
    },
    // 根据类型组成新数字
    typeNewData(arr) {
      const dataArr = []
      arr.map(mapItem => {
        if (dataArr.length === 0) {
          dataArr.push({ bulletinType: mapItem.bulletinType, List: [mapItem] })
        } else {
          const res = dataArr.some(item => {
            if (item.bulletinType === mapItem.bulletinType) {
              item.List.push(mapItem)
              return true
            }
          })
          if (!res) {
            dataArr.push({ bulletinType: mapItem.bulletinType, List: [mapItem] })
          }
        }
      })
      return dataArr
    },
    // 去重复
    duplicatedistinct(arr, key) {
      const newObj = {}
      const newArr = []
      for (let i = 0; i < arr.length; i++) {
        const temp = arr[i]
        if (!newObj[temp[key]]) {
          newObj[temp[key]] = newArr.push(temp)
        }
      }
      return newArr
    },

    // 下载项目所有归档文件
    dowloadArchivedFile() {
      const data = {
        ...this.zipFileData,
        archiveId: this.fromData.archiveId // 归档id
      }
      return getAllProjectZipFile(data).then(res => {
        const fileTitle = res.data.title
        // const url = downLoadFileUrl + '?fileId=' + res.data.fileId + '&fileType=' + res.data.fileType
        // console.log(url)
        // downloadUrlFile(url)
        const params = {
          fileId: res.data.fileId,
          fileType: res.data.fileType
        }
        downLoadFile(params).then((res) => {
          download(res, fileTitle + '.zip')
        })
      }).catch(() => {
      })
    }
  }
}
</script>
<style lang="less" scoped>
.archived{
  font-size: 14px;
  // margin-top: -100px;
}
</style>
