<!--
  @description: 代理机构-项目管理-定标-确认中标人
  @author: 肖文龙
  @author: hfx 2020/2/20 10:00 修改页面丢失排序
  @routerPath:/bidding/projectDrive/calibration/confirmBidWinner
  @date:2020年2月6日11:16:36
  isCanOperate: true可以操作 false不可以查看只有查看权限
-->
<template>
  <div class="main">
    <section v-if="tenderBase.isCanOperate" class="clearfix">
      <div v-if="!winBidBtn||WinIsShow" class="mb10 fr">
        <el-button v-if="!winBidBtn" :loading="loadingBtn" type="primary" @click="winSign">
          确定中标人
        </el-button>
        <el-button v-if="WinIsShow" :loading="loadingBtn" type="primary" @click="resetwinSign">
          重新确定中标人
        </el-button>
      </div>
    </section>
    <div>
      <el-table
        v-loading="loading"
        :data="tableData"
        size="small"
        @selection-change="selectionChange"
      >
        <el-table-column
          :selectable="unChecked"
          type="selection"
          width="50"
        />
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="50"
        />
        <el-table-column
          prop="supplierName"
          label="投标供应商"
          header-align="center"
          min-width="300px"
        />
        <el-table-column
          prop="evaluationPrice"
          label="最终报价总额（万元）"
          header-align="center"
          align="right"
          min-width="200"
        />
        <el-table-column
          prop="total"
          label="最终得分"
          header-align="center"
          align="right"
          min-width="140"
        />
        <el-table-column
          prop="isRecommend"
          label="是否推荐"
          align="center"
          min-width="80"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.isRecommend === '00'" class="status3">否</span>
            <span v-else-if="scope.row.isRecommend === '01'" class="status1">是</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="sort"
          label="排名"
          align="center"
          min-width="100"
        />
        <!--已确定中标时展示-->
        <el-table-column
          v-if="winBidBtn===true"
          prop="isWinBiding"
          label="是否中标"
          align="center"
          min-width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.isWinBiding === '00'" class="status2">未中标</span>
            <span v-else-if="scope.row.isWinBiding === '01'" class="status1">中标</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import {
  getCandidateWinningBidder,
  getResetWinningResultIsCanShowStatus,
  resetWinningResult,
  saveWinningBidder,
  getWinningBidder
} from '@/api/bidding/bidCalibration'

export default {
  name: 'ConfirmBidWinner',
  inject: ['getDriveTask'],
  data() {
    return {
      tenderBase: this.$store.getters.tenderBase, // 项目信息
      loadingBtn: false, // 是否确定中标人
      loading: true,
      groupId: this.$store.getters.groupId, // 包Id 项目Id
      WinIsShow: false, // 重新确定中标人按钮是否可以显示  false:不显示 true:显示
      isSelectMoreBidder: '', // 是否多个中标人 00 否 01 是
      winBidBtn: true, // 中标按钮  是否置灰
      // 确定中标人参数
      winningBidder: {
        supplierId: '', // 确定中标人Id
        packId: '',
        stageId: '',
        tenderId: '',
        id: '' // 主键Id
      },
      tableData: [], // 供应商列表
      tableDataSelection: [] // 选择的供应商
    }
  },
  created() {
    // 获取系统配置信息
    this.$store.dispatch('SetSysConfig', 'isSelectMoreBidder').then((data) => {
      this.isSelectMoreBidder = data.configValue
    })
    // 获取候选中标人列表
    this.initData()
    // 获取重新确定中标人按钮是否可以显示状态
    this.getWinShowStatus()
  },
  methods: {
    // 获取重新确定中标人按钮是否可以显示状态
    getWinShowStatus() {
      const data = {
        stageId: this.groupId.stageId
      }
      getResetWinningResultIsCanShowStatus(data).then(res => {
        this.WinIsShow = res.data === '01'//  00:不显示 01:显示
      })
    },
    // 获取候选中标人列表
    initData() {
      const data = {
        stageId: this.groupId.stageId
      }
      getCandidateWinningBidder(data).then(response => {
        const { data: { btnClickStatus, dataList }} = response
        // 中标按钮是否展示 01 已确定 置灰
        if (btnClickStatus === '01') {
          this.winBidBtn = true
          this.intWinningBidder() // 查询已确定中标人列表
        } else {
          this.winBidBtn = false
          this.tableData = dataList
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 查询已确定中标人
    intWinningBidder() {
      const data = {
        stageId: this.groupId.stageId
      }
      getWinningBidder(data).then(response => {
        this.tableData = response.data// 未确定中标人列表
      }).catch(() => {
      })
    },
    //   中标btn
    winSign() {
      // 判断中标人模式是否已得到数据
      if (!this.isSelectMoreBidder) {
        this.$message.warning('数据加载中，请稍后操作')
        return
      }
      // 判断是否为单个中标人模式 && 只能选择一个中标人
      if (this.isSelectMoreBidder === '00' && this.tableDataSelection.length !== 1) {
        this.$message.warning('只能选择一个中标人')
        return
      }
      // 判断是否为多个个中标人模式 && 最少选择一个中标
      if (this.isSelectMoreBidder === '01' && this.tableDataSelection.length < 1) {
        this.$message.warning('请选择中标人')
        return
      }
      this.$confirm('是否确认中标, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        this.winningBidder = {
          ...this.groupId,
          winningBidderInfoDtoList: this.tableDataSelection// 中标人信息
        }
        this.loadingBtn = true
        saveWinningBidder(this.winningBidder).then(response => {
          this.$message({
            type: 'success',
            message: '确认中标'
          })
          this.loadingBtn = false
          this.initData()// 获取候选中标人列表
          this.getDriveTask()// 刷新驱动开启下一节点
        }).catch(() => {
          this.loadingBtn = false
        })
      }).catch(() => {
      })
    },
    // 重新确定中标人btn
    resetwinSign() {
      this.$confirm('是否重新确定中标人?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        const data = {
          stageId: this.groupId.stageId
        }
        this.loadingBtn = true
        resetWinningResult(data).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.loadingBtn = false
          this.WinIsShow = false // 重新确定中标人按钮是否可以显示
          this.initData()// 获取候选中标人列表
        })
      }).catch(() => {
        this.loadingBtn = false
      })
    },
    // 选择中标人
    selectionChange(val) {
      const supplierList = []
      val.forEach(supplier => {
        supplierList.push({
          supplierId: supplier.supplierId, // 供应商id
          supplierName: supplier.supplierName // 供应商名称
        })
      })
      this.tableDataSelection = supplierList
    },
    unChecked(row, index) {
      if (row.isWinBiding) {
        return 0
      } else {
        return 1
      }
    }
  }
}
</script>
<style lang="less" scoped>

  .successfulBidder {
    padding: 10px 20px 10px;
  }

  .tdInfo {
    img {
      display: block;
      width: 100%;
      margin: 0 auto;
    }
  }
</style>
