<!--
  @description: 专家-评标-符合性评审
  @author: yf
  @routerPath: /procurement/evaluationLobbyDrive/complianceReviewAdd
  @date: 2020-08-07 20:06:58
-->
<template>
  <div class="main" style="margin-top:0px;">
    <div>
      <!-- 编辑 -->
      <template v-if="tenderBase.isEdit">
        <!-- 评审编辑表格 -->
        <complianceReviewTableCompAdd
          v-if="tableData.tableData && tableData.tableData.length>0"
          ref="complianceReviewTableComp"
          :table-data="tableData"
          :summarizing="summarizing"
          :get-tender-base="tenderBase"
          @checkSupplierList="checkSupplierList"
        />
        <!-- 底部按钮 -->
        <div class="but_footer">
          <div class="but_footer-h">
            <div class="but_footer-but">
              <el-button size="small" @click="onekeyScore">
                一键评审
              </el-button>
              <el-button :loading="confirmLoding" size="small" type="primary" @click="preservation">
                保存
              </el-button>
              <el-button :loading="confirmLoding" size="small" type="primary" @click="submit">
                提交
              </el-button>
            </div>
          </div>
        </div>
      </template>
      <!-- 查看 -->
      <template v-if="!tenderBase.isEdit">
        <!-- 评审查看表格 -->
        <el-tabs v-model="activeName">
          <span class="fr text">
            <span>共</span>
            <span style="color:red">{{ evalExpertNum }}</span>
            <span>位专家，共</span>
            <span style="color:red">{{ evalFinishExpertNum }}</span>
            <span>位专家完成评审。</span>
          </span>
          <!--==================================我的评审部分===================================================-->
          <el-tab-pane label="我的评审" name="00">
            <!-- 符合性评审查看表格 -->
            <complianceReviewTableCompDetail
              v-if="tableData.tableData && tableData.tableData.length>0"
              ref="complianceReviewTableComp"
              :table-data="tableData"
              :get-tender-base="tenderBase"
              @checkSupplierList="checkSupplierList"
            />
          </el-tab-pane>
          <!--==================================评审汇总部分===================================================-->
          <el-tab-pane label="评审汇总" name="01">
            <!-- 评审汇总表格 -->
            <complianceReviewSummary :get-tender-base="tenderBase" />
          </el-tab-pane>
        </el-tabs>
      </template>
    </div>
  </div>
</template>

<script>
// import driveStepRight from '../procurementEvaluationLobbyDrive/components/procurementDriveStepRight'
import complianceReviewTableCompAdd from './procurementEvaluationReviewTableComp-add'// 采购结果 表格组件
import complianceReviewTableCompDetail from './procurementEvaluationReviewTableComp-detail'// 符合性评审-查看表格
import complianceReviewSummary from './procurementEvaluationReviewSummary-detail' // 符合性评审-评审汇总
/* api接口*/
import {
  queryComplianceReview,
  showComplianceReview,
  saveComplianceReview,
  saveComplianceReviewKeep,
  queryProgressCase,
  checkoutIsEvalLeaderInfo
} from '@api/procurement/procurementEvaluation'
export default {
  name: 'ComplianceReview',
  inject: ['getTenderBase', 'getDriveTask', 'driveReload'],
  components: {
    complianceReviewTableCompAdd,
    complianceReviewTableCompDetail,
    complianceReviewSummary
    // 'drive-step-right': driveStepRight
  },
  mixins: [],
  data() {
    return {
      confirmLoding: false,
      tableData: {
        tableData: [], // 供应商渲染
        tableList: [], // 评审数据渲染
        evalOption: '' // 00- 符合/不符合， 01-通过/不通过 ，02-合格/不合格
      },
      // tenderBase.isEdit: true,
      isPassVerification: true, // 是否能够通过检验
      bidSupplierNum: '', // 投标供应商数量
      evalExpertNum: '', // 评委数据
      evalFinishExpertNum: '', // 已完成评委数量
      evalFinishSupplierNum: '', // 已完成供应商数量
      activeName: '00', // tab默认选中
      tenderBase: this.getTenderBase(), // tenderBase参数
      metaPath: this.$route.meta.path, // 路由meta信息
      supplierData: [], // 页面供应商数据
      summarizing: ''// 否决条件
    }
  },
  mounted() {
    // 初始化数据
    this.initData()
    // 查询再下达评标命令之前有没有更换组长
    this.queryExpertInfo()
  },
  methods: {
    // 初始化
    initData() {
      const data = {
        evalExpertId: this.tenderBase.evalExpertId, // 专家评审表ID
        driverId: this.tenderBase.driverId, // 驱动id
        packId: this.tenderBase.packId, // 标段id
        evalRule: this.tenderBase.evalRule // 评审规则 【00–整体步骤 01-单个评委步骤】
      }
      if (this.tenderBase.isEdit) {
        // 获取评审编辑页面数据
        queryComplianceReview(data).then(res => {
          const factorList = res.data.factorList
          const supplierList = res.data.supplierList
          // 10-21 新加的否决条件返回值
          this.summarizing = res.data.summarizingWayList
          this.tableData = {
            tableList: factorList, // 评审数据渲染
            tableData: supplierList// 供应商渲染
          }
        })
        return
      }
      // 获取评审查看页面数据
      showComplianceReview(data).then(res => {
        const factorList = res.data.factorList
        const supplierList = res.data.supplierList
        this.tableData = {
          tableList: factorList, // 评审数据渲染
          tableData: supplierList// 供应商渲染
        }
        // 刷新页面
        this.$forceUpdate()
      })
      // 获取评委、供应商完成数据
      this.getProgressCase()
    },
    // 保存或提交时 组装数据然后返回
    assemblyData() {
      const tableList = JSON.parse(JSON.stringify(this.tableData.tableList)) // 初始化评审列表
      const reviewList = JSON.parse(JSON.stringify(this.$refs.complianceReviewTableComp.reviewList)) // 编辑后的评审列表
      const supplierList = JSON.parse(JSON.stringify(this.$refs.complianceReviewTableComp.supplierList)) // 供应商数据
      const evalFactorResultDtos = []
      // 遍历第一层初始化评审列表
      tableList.forEach(table => {
        // 添加属性
        this.$set(table, 'evalFactorResultDtos', evalFactorResultDtos)
        const len = reviewList.length - 1
        // 遍历第二层修改编辑后的评审数据
        reviewList.forEach(review => {
          // 查找相同id找到对应的数据
          if (table.id === review.id) {
            table.evalFactorResultDtos = []
            for (const key in review.offerData) {
              if (review.offerData.hasOwnProperty(key)) {
                const element = review.offerData[key]
                // 将客观分转为字符串
                element.objectiveOptions = element.objectiveOptions.toString()
                table.evalFactorResultDtos.push(element)
                // 结论赋值 判断如果是最后一条数据
                if (reviewList[len].offerData.isFixedData !== undefined) {
                  supplierList.forEach(item => {
                    // 他们的id相同 去赋值并提交
                    if (item.supplierId === reviewList[len].offerData[key].supplierId) {
                      item.accessment = reviewList[len].offerData[key].accessment
                      item.score = reviewList[len].offerData[key].evaluationScore ? Number(reviewList[len].offerData[key].evaluationScore) : ''
                    }
                  })
                }
              }
            }
          }
        })
        // 去除重复数据
        table.evalFactorResultDtos = Array.from(new Set(table.evalFactorResultDtos))
        // 删除无用数据
        delete table.evalFactorResultVos
      })
      // 更改最终得分
      // this.supplierFinalScore(supplierList, tableList)
      const data = {
        ...this.tenderBase,
        evalFactorDtos: tableList,
        supplierEvalResultDtos: supplierList
      }
      return data
    },
    // 修改供应商最终得分
    supplierFinalScore(supplierList, tableList) {
      // 遍历供应商数据
      supplierList.forEach(supplier => {
        let score = null
        // 遍历指标数据
        tableList.forEach(review => {
          review.evalFactorResultDtos.forEach(item => {
            if (item.supplierId === supplier.supplierId) {
              score += item.evaluationScore * 1
            }
          })
        })
        supplier.score = score
      })
      return supplierList
    },
    // 智能评审
    onekeyScore() {
      this.$refs.complianceReviewTableComp.onekeyScore()
    },
    // 提交评审结果
    submit() {
      this.confirmLoding = true
      this.$confirm('提交之后将不可修改，确认提交评审结果吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        // 初始化是否通过校验
        this.isPassVerification = true
        // 校验填写信息
        this.$refs.complianceReviewTableComp.reviewList.forEach(reviewData => {
          Object.keys(reviewData.offerData).forEach(key => {
            const offer = reviewData.offerData[key]// 获取供应商报价信息
            // 是否未符合性
            if (this.tenderBase.taskNo === '03') {
              // 判断是否选择了评审结果
              if (offer.evaluationResult === '') {
                this.$message.warning('每个供应商的每个评审点必须评审')
                this.isPassVerification = false
              }
              // 判断选择了不符合的评审结果是否填写了评审意见
              if (offer.evaluationResult === '00' && offer.evaluationOpinion === '') {
                this.$message.warning('不符合的评审点必设置评审意见')
                this.isPassVerification = false
              }
              return
            }
            // 商务经济
            if (!offer.evaluationScore && offer.supplierId && this.tenderBase.taskNo !== '03') {
              this.$message.warning('每个供应商的每个评审点必须评分')
              this.isPassVerification = false
            }
          })
        })
        if (this.isPassVerification) {
          // 调取封装的数据，发送请求
          const data = this.assemblyData()
          saveComplianceReview(data).then(res => {
            this.confirmLoding = false
            this.tenderBase.isEdit = false
            // 刷新驱动
            this.driveReload()
            this.getDriveTask()
          })
        } else {
          this.confirmLoding = false
        }
      }).catch(() => {
        this.confirmLoding = false
      })
    },
    // 保存评审
    preservation() {
      this.confirmLoding = true
      // 调取封装的数据，发送请求
      const data = this.assemblyData()
      // 调取保存接口
      saveComplianceReviewKeep(data).then(res => {
        this.confirmLoding = false
        this.$message.success(res.header.msg)
      })
    },
    // 获取评委、供应商完成数据
    getProgressCase() {
      const data = {
        evalExpertId: this.tenderBase.evalExpertId, // 专家评审表ID
        driverId: this.tenderBase.driverId, // 驱动id
        packId: this.tenderBase.packId, // 标段id
        evalRule: this.tenderBase.evalRule // 评审规则 【00–整体步骤 01-单个评委步骤】
      }
      queryProgressCase(data).then(res => {
        if (res.data) {
          // 投标供应商数量
          this.bidSupplierNum = res.data.bidSupplierNum
          // 专家数量
          this.evalExpertNum = res.data.evalExpertNum
          // 已完成供应商数量
          this.evalFinishSupplierNum = res.data.evalFinishSupplierNum
          // 已完成评委数量
          this.evalFinishExpertNum = res.data.evalFinishExpertNum
        }
      })
    },
    // 获取勾选供应商数据
    checkSupplierList(data) {
      // 接受子组件传递过来的勾选数据
      this.supplierData = data
    },
    // 查询再下达评标命令之前有没有更换组长
    queryExpertInfo() {
      checkoutIsEvalLeaderInfo(this.tenderBase.packId).then(res => {
        this.tenderBase.isCommerceLeader = res.data.isCommerceLeader
        this.tenderBase.isEvalLeader = res.data.isEvalLeader
        this.tenderBase.isTechnologyLeader = res.data.isTechnologyLeader
      })
    }
  }
}
</script>

<style lang="less" scoped>
  @import '~@assets/style/common/public.less';
.driveStep{
  background: #ffffff;
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #E4E7ED;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
.imgBg{
  font-size: 12px;
  margin: 0 2px;
  color: @hyacinthine;
}
.text{
  color: @dark-font;
}
</style>
