<!--
  @description:单一来源- 谈判报表 、谈判纪要
  @Modifier:ysg
  @routerPath: /procurement/procurementDrive/procurementDocumentsPurchase/evaluationResults/negotiateReport
  @date:2020/09/22
-->
<template>
  <div>
    <div class="content-btn">
      <upload
        v-if="btnStatus()"
        ref="upload"
        class="upload-manage-many fl"
        :title="''"
        :code="'001_005_011'"
        :on-change="principalFile"
        :data="procEvaReviewFile"
        :file-list="principalList"
        :before-upload="beforeUpload"
        :auto-delete="true"
        :auto-upload="true"
        :success="successList"
        :accept="acceptWrod"
        :file-size="200"
        file-ref="fileRef"
        :but-text="upBtnText"
        :show-file-list="false"
        :multiple="false"
      />
      <!-- 导出报告 editorStatus()判断是否完成评审或终止评审 -->
      <gx-button v-if="editorStatus()&&getTenderBase.tenderMethod==='11'&&isExportShow" :code="'001_005_021'" :loading="exportLoading" size="small" round @click="createReport">
        {{ exportText }}
      </gx-button>
      <!-- <gx-button v-if="editorStatus()" :code="'001_005_022'" size="small" round @click="changePDF">
        转为pdf
      </gx-button> -->
      <!-- 流转签章 - 暂时隐藏 -->
      <!-- <gx-button v-if="btnStatus()" ize="small" round @click="signature">
        流转文件
      </gx-button> -->
    </div>
    <!-- 附件列表 -->
    <el-table v-loading="loading" :data="tablelist" style="width: 100%;" border class="mb16" @selection-change="handleSelectionChange">
      <!-- 复选按钮 -->
      <el-table-column type="selection" width="55px" align="center" :selectable="checkSelectable" />
      <!-- 序号 -->
      <el-table-column header-align="center" type="index" label="序号" width="60px" align="center" />
      <!-- 文件名称 -->
      <el-table-column prop="pdfName" label="文件名称" align="center">
        <template slot-scope="scope">
          <gx-link
            :underline="false"
            size="mini"
            type="primary"
            @click="preview(scope.row)"
          >
            {{ scope.row.pdfName }}
          </gx-link>
        </template>
      </el-table-column>
      <!-- 上传时间 -->
      <el-table-column prop="createTime" label="上传时间" align="center" width="150px">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime && $Moment(scope.row.createTime).format(momentDateTime) }}</span>
        </template>
      </el-table-column>
      <!-- 签章状态-暂时隐藏 -->
      <!-- <el-table-column prop="signatureStatus" label="签章状态" align="center">
        <template slot-scope="scope">
          {{ filterDictText(signatureStatus, scope.row.signatureStatus) }}
        </template>
      </el-table-column> -->
      <!-- 签章人数-暂时隐藏 -->
      <!-- <el-table-column prop="signatureExpertStatus" label="签章人数" align="center" /> -->
      <!-- 操作 -->
      <el-table-column
        header-align="center"
        label="操作"
        align="center"
        width="250px"
      >
        <template slot-scope="scope">
          <!-- 下载组件 -->
          <downloadFile :one-download="true" :but-type="'link'" :but-text="'[下载]'" :file-list="scope.row.fileList" style="margin-right:8px" />
          <!-- 文件查看情况-暂时隐藏 -->
          <!-- <el-link
            v-if="scope.row.signatureStatus==='01' || scope.row.signatureStatus==='02'"
            :underline="false"
            size="mini"
            type="primary"
            @click="detailList(scope.row)"
          >
            [文件查看情况]
          </el-link> -->
          <gx-link
            v-if="btnStatus()"
            :code="'001_005_017'"
            :underline="false"
            size="mini"
            type="primary"
            @click="delect(scope.row)"
          >
            [删除]
          </gx-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="explanatory-text">
      说明：请上传pdf格式的文件。
    </div>
    <!-- 流转签章弹框 v-if="selectSupplierFlag.dialog" -->
    <select-supplier :dialog-data="selectSupplierFlag" :multiple-file-select="multipleFileSelect" @uplaodData="uplaodData" />
    <!-- 预览pdf弹框 -->
    <el-dialog
      v-dialogDrag
      :visible.sync="isPreviewDialog.dialog"
      :close-on-click-modal="false"
      :lock-scroll="false"
      :title="titleDialog"
      width="80%"
      custom-class="el-dialog__body-p0"
    >
      <pdfDetail :key="isPreviewDialog.key" :pdf-url="pdfUrl" />
    </el-dialog>
    <!-- 签章情况弹框 -->
    <signature-seal :dialog-data="signatureSealFlag" />
  </div>
</template>

<script>
/* 接口 */
import {
  listProtProjectProcurementManagerEvalReport,
  uploadEvalInformManager,
  delExchangeEvalReportSignature,
  exchangeEvalReportToExpertOrSupplier,
  queryEvalProjectStatus,
  reviewReport // 导出评审报告
} from '@api/procurement/procurementEvaluation'
import { queryProcurementRuleRecord } from '@/api/procurement/procurementProject' // 获取项目信息 获取项目规则

/* 公共方法*/
import { filterDictText, randomData, download } from '@/assets/js/utils' // 字典值替换文本通用方法 生成随机Uid
import { moneyFormat } from '@assets/js/utils'// 千分位转换
import { readImageRoot } from '@api/common/uploadCenter' // 图片查看路径
/*  公共组件 */
import downloadFile from '@comp/common/updownload/downloadFile' // 获取附件信息 下载组件
import pdfDetail from '@comp/common/pdf/pdf' // pdf查看
import upload from '@comp/common/updownload/upload' // 附件上传组件
import signatureSeal from './procurementEvaluationSignatureSeal-dialog' // 签章情况
import selectSupplier from '../../procurementDocuments/procurementEvaluationResults/procurementEvaluationSelectSupplier-dialog' // 报价记录
/* 混合开发-参数字段*/
import { procurementSelectMixin } from '../../procurementDocuments/procurementEvaluationResults/mixins/procurementSelectMixin'
import { procurementGroupMixins } from '@views/procurement/mixins/procurementGrounpMixins'
import { procEvaReviewGrounpMixin } from './mixins/procEvaReviewGrounpMixin'

export default {
  components: {
    selectSupplier, upload, pdfDetail, signatureSeal, downloadFile
  },
  mixins: [procurementSelectMixin, procurementGroupMixins, procEvaReviewGrounpMixin],
  inject: ['getTenderBase'],
  props: {
    // 项目经理查看单个评委评审信息
    driveInfo: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false, // loading效果
      exportLoading: false, // 导出评审报告loading
      metaType: this.$route.meta.type, // 路由meta信息
      tablelist: [], // 价格评审表格数据
      selectSupplierFlag: { dialog: false, packId: '', type: '', taskNo: '' }, // 选择供应商弹出框
      // 查看签章情况弹框
      signatureSealFlag: {
        dialog: false, reportId: ''
      },
      // =======预览pdf start===========//
      pdfUrl: '', // 预览pdfurl
      readImageUrl: readImageRoot, // 图片查看路径
      // 预览操作弹框
      isPreviewDialog: {
        dialog: false, // 打开弹框
        isFileType: '', // 判断文件类型 00 word 01 pdf
        key: '' // 动态修改key确保展示最新
      },
      // =======预览pdf End===========//
      titleDialog: '', // 弹框标题
      principalList: [],
      multipleFileSelect: [], // 选择的table文件
      successFlag: false, // 附件上传状态 控制上传成功后调用的保存方法
      getTenderBase: this.getTenderBase(), // 获取项目信息
      evaluationAlreadyEnd: '', // 是否已经结束评标和已完成评标,00-显示结束评标按钮,01-显示结束评标、确认完成评标按钮,02显示已结束评标按钮,03-显示已完成评标按钮
      acceptWrod: '.pdf', // 上传附件格式
      // 生成报告弹框
      createReportFlag: {
        dialog: false
      },
      isExportShow: false, // 是否显示导出报告按钮 现阶段是 评审方式为综合评估法时显示 有效最低价和性价比不显示 通过reviewMethod评审方式字段判断
      exportText: '导出报告', // 导出报告文案
      upBtnText: '上传pdf', // 上传pdf文案 默认显示上传pdf
      reviewMethod: '' // 评审方式 01 综合评估法 02经有效最低价发 03性价比发
    }
  },

  created() {
    this.loadData() // 查询评审报告/谈判纪要列表
    this.initProjectStatus() // 查询是否已经结束评标和已完成评标
    this.initRuleData() // 查询项目规则
    // 获取首次上传附件bizid-随机数
    this.procEvaReviewFile.bizId = randomData()
  },
  methods: {
    filterDictText, // 数据字典替换方法
    moneyFormat, // 千分位转换方法
    /*
    * 查询是否已经结束评标和已完成评标
    * @params packId项目标段id
    * 00-显示结束评标按钮,01-显示结束评标、确认完成评标按钮,02显示已结束评标按钮,03-显示已完成评标按钮
    */
    initProjectStatus() {
      queryEvalProjectStatus(this.getTenderBase.packId).then(res => {
        this.evaluationAlreadyEnd = res.data.qryEvalStatusType
      })
    },
    /*
    *  初始化页面数据  查询评审报告/谈判纪要列表
    * @params { packId: 项目标段id, taskNo: 轮次信息 ·}
    * return response.data  评审报告/谈判纪要列表
    */
    loadData() {
      this.loading = true // loading
      const params = { packId: this.driveInfo.packId, taskNo: this.driveInfo.taskNo }
      listProtProjectProcurementManagerEvalReport(params)
        .then(response => {
          this.loading = false // loading
          this.tablelist = response.data
          this.hanldEvalReportList() // 处理评审报告数据
        })
        .catch(() => {
          this.loading = false // loading
        })
    },
    /*
    *  评审报告/谈判纪要列表数据处理
    *  return 列表数据中增加fileList属
    */
    hanldEvalReportList() {
      this.tablelist && this.tablelist.map(item => {
        this.$set(item, 'fileList', [
          { fileType: this.procEvaReviewFile.fileType, // 评审报告/谈判纪要文件类型
            fileId: item.fileId
          }// 评审报告/谈判纪要附件Id
        ])
      })
    },
    // 查看签章情况
    detailList(row) {
      this.signatureSealFlag.dialog = true
      this.signatureSealFlag.id = row.id
    },
    // 预览上传的pdf
    preview(row) {
      this.titleDialog = 'pdf查看'
      // 拼接pdf的url路径
      const url = this.readImageUrl + row.pdfUrl
      // 加密处理
      this.pdfUrl = encodeURIComponent(url)
      this.isPreviewDialog.dialog = true
      this.isPreviewDialog.isFileType = '01'
      this.isPreviewDialog.key = new Date() * 1
    },
    // 表格数据删除
    delect(row) {
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        const params = {
          reportId: row.id // 列表id
        }
        // 删除方法
        delExchangeEvalReportSignature(params).then(res => {
          if (res.header.ret === '0') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.loadData()
          }
        })
      })
    },
    // ===================================== 流转签章 start ==========================================
    // 流转选择禁用
    checkSelectable(row) {
      if (row.signatureStatus === '00') {
        return true
      } else {
        return false
      }
    },
    // 流转签章
    signature() {
      if (this.multipleFileSelect.length > 0) {
        // 谈判纪要 流转
        if (this.driveInfo.taskNo === '21') {
          this.selectSupplierFlag.packId = this.driveInfo.packId
          this.selectSupplierFlag.taskNo = this.driveInfo.taskNo
          this.selectSupplierFlag.dialog = true
          this.selectSupplierFlag.type = 'signature' // 类型为签章 为了区分供应商展示table的信息
        } else if (this.driveInfo.taskNo === '09') {
          // 谈判报表流转 按钮
          this.signatureBtn()
        }
      } else {
        this.$message.warning({ message: '请选择数据再操作' })
      }
    },
    /*
    * 谈判报表流转按钮
    * @params {packId: 项目标段id, reportIds: 选择文件id集合, taskNo: 选择点击驱动节点序号}
    * 选择流转签章
    * return 返回成功或者失败状态
    */
    signatureBtn() {
      const tempList = []
      // 文件id 以数组传递
      this.multipleFileSelect.forEach(item => {
        tempList.push(item.id)
      })
      var temStr = tempList.join(',')
      const params = { packId: this.driveInfo.packId, reportIds: temStr, taskNo: this.driveInfo.taskNo }
      exchangeEvalReportToExpertOrSupplier(params).then(res => {
        if (res.header.ret === '0') {
          this.$message({
            message: res.header.msg,
            type: 'success'
          })
          this.loadData()
        }
      })
    },
    // 选择流传签章数据
    handleSelectionChange(val) {
      this.multipleFileSelect = val // 获取选择数据
    },
    // 流转签章完成后的回调
    uplaodData() {
      this.loadData()
    },
    // ===================================== 流转签章End ==========================================
    editorStatus() {
      //  不是终止评审 或者完成评审的状态都展示按钮
      // 评审报告 谈判报告 09  谈判纪要 21
      if (this.driveInfo.taskNo === '09' && (this.evaluationAlreadyEnd === '01' || this.evaluationAlreadyEnd === '00')) {
        return true
      }
      return false
    },
    // 按钮展示
    btnStatus() {
      // 不是终止评审 或者完成评审的状态都展示按钮
      if (this.evaluationAlreadyEnd === '01' || this.evaluationAlreadyEnd === '00') {
        return true
      }
      return false
    },
    // ============================== 导出评审报告 start =======================================
    createReport() {
      this.exportLoading = true
      this.exportText = '努力导出报告中'
      this.exportReviewReport(this.getTenderBase.packId)
    },
    /*
    * 导出评审报告方法
    * @params {packId 项目标段id, reviewMethod:评审方式}
    * 根据评审方式的不同导出不同的评审报告
    * return word的文件流
    */
    exportReviewReport(packId) {
      const data = { packId: packId, reviewMethod: this.reviewMethod }
      reviewReport(data).then(res => {
        download(res, '【' + this.getTenderBase.tenderName + '】评审报告.doc')
        this.$message.success({ message: '下载成功' })
        this.exportLoading = false
        this.exportText = '导出报告'
      }).catch(() => {
        this.exportLoading = false
        this.exportText = '导出报告'
      })
    },
    // ============================== 导出评审报告 end =======================================

    // ==========================附件上传 start ==============================================
    // 附件上传
    principalFile(file, fileList) {
      this.principalList = fileList
    },
    // 附件上传成功的回调
    successList(file) {
      this.loading = false
      if (this.successFlag) {
        this.successFlag = false
        this.upBtnText = '上传pdf'
        const data = {
          id: this.procEvaReviewFile.bizId, // UUid
          packId: this.driveInfo.packId, // 标段id
          stageId: '',
          taskNo: this.driveInfo.taskNo
        }
        // 专家上传评审报告状态
        uploadEvalInformManager(data).then(res => {
          this.loadData()
        }).catch(() => {
        })
        this.procEvaReviewFile.bizId = randomData() // 每次上传附件成功 获取下一附件bizid-随机数
      }
    },
    // 上传之前钩子
    beforeUpload(file) {
      const isLt2M = this.$refs.upload.fileSizeUp(file) // 判断附件是否超过大小限制 布尔值 超过false
      if (isLt2M) {
        this.successFlag = true
        this.upBtnText = '附件上传中...'
      } else {
        this.successFlag = false
        this.upBtnText = '上传pdf'
      }
    },
    // ==========================================附件上传 End ====================================
    /*
    * 获取项目规则方法
    * @params { tenderId: tenderId } 项目id
    * 获取评审方式 reviewMethod 01 综合评估法 02有效最低价 03性价比发
    * 目前是仅有综合评估法/有效最低价显示导出报告按钮 其他方式不显示后期会变为都显示导出不同的报告
    */
    initRuleData() {
      const data = {
        tenderId: this.getTenderBase.tenderId
      }
      queryProcurementRuleRecord(data).then(res => {
        this.reviewMethod = res.data.reviewMethod
        if (this.reviewMethod && (this.reviewMethod === '01' || this.reviewMethod === '02')) {
          this.isExportShow = true
        } else {
          this.isExportShow = false
        }
      })
    }
  }
}
</script>
<style lang='less' scoped>
.explanatory-text{
  color: red;
}
.content-btn{
  margin: 15px 0;
}
/deep/.upload-manage-many button{
  border-radius: 20px;
}
</style>
