<!--
  @description: 代理机构-项目管理-已制作招标文件组件
  @Modifier: 肖文龙
  @routerPath:
  @date: 2020/1/13-11:40
-->
<template>
  <div>
    <el-table
      ref="table"
      :data="is_show?tableData:tableData[0]?[tableData[0]]:[]"
    >
      <el-table-column
        type="index"
        label="序号"
        width="60"
        align="center"
      />
      <!--招标/资审文件名称-->
      <el-table-column
        :label="dict.wjmc"
        header-align="center"
        prop="title"
      >
        <template slot-scope="scope">
          <downloadFile
            :down-load="true"
            :file-list="scope.row.bidFileList"
          />
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        prop=""
        label="附件"
      >
        <template slot-scope="scope">
          <downloadFile :down-load="true" :file-list="scope.row.bidAccessoryList" />
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        prop="packNos"
        label="标段（包）编号"
      />
      <!--招标/资审文件状态-->
      <el-table-column
        :label="dict.wjzt"
        prop="useStatus"
        width="140px"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.useStatus==='00' && scope.row.auditStatus!=='02'" class="status4">草稿</span>
          <span v-if="scope.row.auditStatus==='00'" class="status2">待审核</span>
          <span v-if="scope.row.auditStatus==='01'" class="status1">审核通过</span>
          <span v-if="scope.row.auditStatus==='02'" class="status3">审核不通过</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="createTime"
        width="140px"
        label="创建时间"
      />
      <el-table-column
        label="操作"
        align="center"
        width="250px"
      >
        <!--00:待审核 01:审核通过 02:审核不通过-->
        <template slot-scope="scope">
          <el-link
            v-if="scope.row.auditStatus==='01'"
            type="primary"
            @click="tenderDocSee(scope.row)"
          >
            [标书获取记录]
          </el-link>
          <el-link
            v-if="scope.row.auditStatus==='01'"
            type="primary"
            @click="see(scope.row)"
          >
            [下载记录]
          </el-link>
          <el-link
            type="primary"
            @click="lookFile(scope.row)"
          >
            [招标文件附件]
          </el-link>
          <template v-if="tenderBase.isCanOperate">
            <el-link
              v-if="scope.row.auditStatus==='02'||!scope.row.auditStatus"
              type="primary"
              @click="revamped(scope.row)"
            >
              [修改]
            </el-link>
            <el-link
              v-if="scope.row.auditStatus!=='01'&&scope.row.auditStatus!=='00'"
              type="primary"
              @click="deleteRow(scope.row,scope.$index)"
            >
              [删除]
            </el-link>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <div class="more">
      <span v-if="is_show" class="pointer" @click="is_show=false">
        收起更多（0）
        <i class="el-icon-arrow-up" />
      </span>
      <span v-else class="pointer" @click="is_show=true">
        展开更多（{{ tableData.length>0?tableData.length -1:0 }}）
        <i class="el-icon-arrow-down" />
      </span>
    </div>
    <hasTenderProcfilePackDialog ref="hasTenderProcfilePackDialog" />
  </div>
</template>

<script>
/* api*/
import { listMarkedTenderDocPackage, queryTenderDocInfo, removeTenderdocByProcfileId } from '@api/bidding/tenderDoc'
import { downLoadFileUrl, getFileDetail } from '@/api/common/uploadCenter'
/* 公用方法*/
import { cloneObject } from '@assets/js/utils'// 常用方法
/* 混合开发*/
import { tenderDoc } from './../mixins/tenderDoc'// 招标文件/资审文件
/* 公共组件 */
import hasTenderProcfilePackDialog from './hasTenderProcfilePack-dialog' // 查看附件弹框
export default {
  name: 'HasTenderProcfilePackComp',
  components: {
    hasTenderProcfilePackDialog
  },
  mixins: [tenderDoc],
  data() {
    return {
      is_show: true, // 标段展示状态
      timer: true, // 定时器是否可以刷新状态 布尔值
      downLoadFileUrl: downLoadFileUrl,
      searchData: {
        fileQualificationType: '', // 00:资审文件 01:后审文件
        tenderId: this.$route.query.id// 项目Id
      },
      initTableData: [], // 初始化
      tableData: [
        /* {
          auditStatus: '', // 00:待审核 01:审核通过 02:审核不通过
          createTime: '', // 创建时间
          packNos: '', // 标段编号集合
          procfileId: '', // 招标文件id
          tenderId: '', // 项目id
          useStatus: ''// 00:临时(草稿) 01:有效 02:作废
        }*/
      ]
    }
  },
  created() {
    // 取公共tenderDoc.js里commonFileQualificationType
    this.searchData.fileQualificationType = this.commonFileQualificationType
    this.initData()// 初始化数据
  },
  beforeDestroy() {
    // 清除定时器状态
    this.timer = false
  },
  methods: {
    /*
    * 初始化数据
    * procfileId 寻找相同的文件id进行定时刷新
    * */
    initData(procfileId) {
      const data = {
        ...this.searchData
      }
      listMarkedTenderDocPackage(data).then(res => {
        const tableData = cloneObject(res.data)
        /* 循环数组信息进行合并*/
        tableData.forEach((item, index) => {
          this.tableData.forEach(row => {
            // 查询相同文件的信息进行合并
            if (item.procfileId === row.procfileId) {
              const data = { ...row, ...item }
              this.$set(tableData, index, data)
            }
          })
        })
        this.tableData = tableData// 赋值渲染列表
        this.initDataList(procfileId)// 数据组装
      })
    },
    // 数据组装
    initDataList(procfileId) {
      let isType = false // 是否定时刷新 布尔值
      this.tableData.map((item, index) => {
        /*
        * 判断状态是否开启定时刷 并且 查找为草稿的文件信息 并且和当前正在制作的文件是同一个
        * useStatus === 00 草稿
        * */
        if (procfileId && item.useStatus === '00' && procfileId === item.procfileId) {
          isType = true
          // 查询招标文件信息
          queryTenderDocInfo(item.procfileId).then(res => {
            // 判断状态是否为草稿 00 草稿
            if (res.data.useStatus !== '00') { // 非草稿
              /* 审核状态*/
              item.useStatus = res.data.useStatus
              item.auditStatus = res.data.auditStatus
              this.getFile(item, index)// 获取附件信息
            }
          })
        } else if (!procfileId) { // 非定时器刷新时
          this.getFile(item, index)// 获取附件信息
        }
      })
      // 定时刷新
      if (isType && this.timer) {
        setTimeout(() => {
          this.initDataList(procfileId)// 数据组装
        }, 2000)
      }
    },
    /*
    * 获取附件信息
    * item 每条文件信息
    * index 每条文件信息的索引
    * */
    getFile(item, index) {
      /* 招标文件*/
      const bidFileData = {
        ...this.bidFileData, // 文件类型
        bizId: item.procfileId // 文件Id
      }
      getFileDetail(bidFileData).then(response => {
        item.bidFileList = cloneObject(response.data)
        this.$set(this.tableData, index, item)
      }).catch(() => {
      })
      /* 招标文件附件*/
      const bidAccessory = {
        ...this.bidAccessory, // 文件类型
        bizId: item.procfileId // 文件Id
      }
      getFileDetail(bidAccessory).then(response => {
        item.bidAccessoryList = cloneObject(response.data)
        this.$set(this.tableData, index, item)
      })
    },
    // 查看招标文件附件
    lookFile(row) {
      const fileData = {
        ...this.bidAccessory, // 文件类型
        bizId: row.procfileId // 文件Id
      }
      this.$refs.hasTenderProcfilePackDialog.open(fileData)
    },
    // 查看下载记录
    see(row) {
      // 路由携带参数
      const query = {
        ...this.$route.query,
        procfileId: row.procfileId// 文件Id
      }
      this.$router.push({
        path: '/bidding/projectDrive/tenderDoc/tenderProcfileDownload-detail', query: query })
    },
    // 查看标书获取记录列表
    tenderDocSee(row) {
      // 路由携带参数
      const query = {
        ...this.$route.query,
        stageIds: row.stageIds // 阶段iD
      }
      this.$router.push({
        path: '/bidding/projectDrive/tenderDoc/tenderDocTakeRecordsList-detail', query: query })
    },
    // 修改
    revamped(row) {
      this.$emit('revamped', row)
    },
    // 删除
    deleteRow(row, index) {
      this.$confirm('确定删除已选标段（包）吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        removeTenderdocByProcfileId(row.procfileId).then(res => {
          this.tableData.splice(index, 1)
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.$emit('initData')// 调用未制作标段查询接口
        })
      }).catch(() => {
      })
    }
  }
}
</script>
<style lang="less" scoped>

</style>
