<!--
  @description:评标-推荐组长
  @Modifier:yf/wxy
  @routerPath:/procurement/evaluationLobbyDrive/expertElectLeader
  @date:2020/08/04-13:48
-->

<template>
  <div class="p0-16">
    <!-- 顶部提示文案 -->
    <template>
      <!-- isElected 是否推选 isRevote 是否平票 loginPersonIsLeader00为产生组长01已产生组长-->
      <el-button v-if="isElected==='00' && !isRevote && loginPersonIsLeader==='00'" type="text">
        <p style="color:red;margin:10px 0">
          提示:您已完成签到，请尽快推选组长！
        </p>
      </el-button>
      <el-button v-if="isElected==='01' && !isRevote && loginPersonIsLeader==='00'" type="text">
        <p style="color:red;margin:10px 0">
          提示:您已完成专家推选，请等待其他专家完成推选！
        </p>
      </el-button>
      <el-button v-if="isRevote" type="text">
        <p v-if="isRevote" style="color:red;margin:10px 0">
          提示:出现平票，请重新推选组长！
        </p>
      </el-button>
      <!-- 当前评委是否为组长 【00–不是，01–是组长】  -->
      <el-button v-if="loginPersonIsLeader==='01' && !isGiveInstruction" type="text">
        <p style="color:red;margin:10px 0">
          {{ leaderName }}
        </p>
      </el-button>
    </template>
    <!-- 专家列表 -->
    <el-table :data="tableData" class="pointer" border>
      <el-table-column type="index" label="序号" min-width="100" align="center" />
      <el-table-column label="姓名" min-width="180" prop="expertName" align="center" />
      <el-table-column label="专家类型" min-width="180" prop="personType" align="center">
        <template slot-scope="scope">
          {{ filterDictText(personTypeList,scope.row.personType) }}
        </template>
      </el-table-column>
      <el-table-column label="评审类别" prop="expertTypes" align="center" min-width="150">
        <template slot-scope="scope">
          <span v-for="(item, index) in scope.row.expertTypes" :key="index">
            {{ filterDictText(expertTypeList,item) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="得票数" min-width="80" prop="getVotenum" align="center" />
      <el-table-column v-if="loginPersonIsLeader==='00'" label="推荐组长" min-width="180" align="center">
        <template slot-scope="scope">
          <el-link v-if="isElected==='00'" type="primary" @click="addExpertLeader(scope.row)">
            <span class="iconfont icon-tuixuan" />
            推选
          </el-link>
          <el-link v-if="isElected==='01'&&scope.row.expertId == electedId" type="primary">
            <span class="iconfont icon-tuixuan" />
          </el-link>
        </template>
      </el-table-column>
      <el-table-column v-if="loginPersonIsLeader=== '01'" label="组长" min-width="180" align="center">
        <template slot-scope="scope">
          <el-link v-if="scope.row.isLeader === '01'" type="primary">
            是
          </el-link>
          <el-link v-else type="primary">
            否
          </el-link>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
/* 公共字典*/
import { procurementCommitteeGroupMixin } from '../../procurementCommittee/mixins/procurementCommitteeGroupMixins'
import { probidCommitteeMixin } from '../../procurementCommittee/mixins/procurementCommitteeMixins'
/* 公用方法*/
import { filterDictText } from '@/assets/js/utils'// 常用方法
/* api接口*/
import {
  listProtProjectProcurementEvalExpert,
  appointProtProjectProcurementEvalExpertLeader,
  checkoutIsElectedLeader,
  checkoutIsEvalLeaderInfo
} from '@api/procurement/procurementEvaluation'
export default {
  name: 'ExpertLeaderAdd',
  inject: ['getTenderBase', 'getDriveTask', 'driveReload'],
  components: {},
  mixins: [procurementCommitteeGroupMixin, probidCommitteeMixin],
  data() {
    return {
      isElected: '00', // 是否已完成推选 00 未推选 01 已推选
      electedId: '', // 推选人id
      isFlatTicket: false, // 是否出现平票
      tableData: [], // 推荐组长数据
      timer: null, // 定时器
      isRevote: false, // 是否重新开始投票
      loginPersonIsLeader: '00', // 是否推选成功
      leaderName: '', // 组长名称
      voteEvalExpertId: '', // 推荐组长评委ID
      isGiveInstruction: false, // 是否下达评标指令
      getTenderBase: this.getTenderBase(), // 项目基本信息
      evalOrderTimer: null // 非组长调定时器查看是否下达评标指令
    }
  },
  mounted() {
    // 获取推荐专家列表
    this.initData()
    // 监视投票数
    setTimeout(() => {
      this.monitoringVotes()
    })
    // 当前人不是组长获取数据
    this.checkOutIsLeader().then(res => {
      if (res.data.isEvalLeader === '00') {
        this.loadData()
      }
    })
  },
  beforeDestroy() {
    // 清楚定时器
    clearInterval(this.timer)
    clearInterval(this.evalOrderTimer)
  },
  methods: {
    filterDictText,
    initData() {
      // 获取推荐专家列表
      listProtProjectProcurementEvalExpert(this.getTenderBase.packId).then(res => {
        this.voteEvalExpertId = res.data.evalExpertId // 推荐组长评委ID
        this.tableData = res.data.expertDrawOffVos // 推荐组长列表数据
        this.leaderName = res.data.showLeaderInfo // 组长信息
        this.loginPersonIsLeader = res.data.isElectedLeader // 是否推选成功
        // 判断是否已投票
        if (res.data.isVotes === '01') {
          // 已完成推选
          this.isElected = '01'
        }
      })
    },
    // 是否推选出组长,是否平票
    loadData() {
      const data = this.assemblyData()
      // curElectProcessStatus 推荐组长情况，00-未设置组长 01-已经设置过组长 03-出现平票
      checkoutIsElectedLeader(data).then(res => {
        // 出现平票
        if (res.data.curElectProcessStatus === '03') {
          // 清除定时器
          clearInterval(this.timer)
          this.isRevote = true // 是否出现平票
          this.$alert(`${res.data.responseContent}平票，请重新推选！`, '提示', {
            confirmButtonText: '我知道了',
            confirmButtonClass: 'confirmBtn',
            type: 'warning'
          }).then(() => {
            // 是否推选  00 未推选 01 已推选
            this.isElected = '00'
          }).catch(() => {})
          return
        }
      }).catch(() => {
        // 清除定时器
        clearInterval(this.timer)
      })
    },
    // 推选组长
    addExpertLeader(data) {
      this.$confirm(`确认推选${data.expertName}为评标组长吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      })
        .then(() => {
          // 发请求设置组长
          // this.isElected = '01' // 是否推选
          // this.isRevote = false // 是否平票
          // this.electedId = data.expertId	 // 推选人id
          // data.isLeader = true // 是否为组长
          const params = this.assemblyData()
          params.voteEvalExpertId = data.evalExpertId
          appointProtProjectProcurementEvalExpertLeader(params).then(res => {
            this.isElected = '01' // 是否推选
            this.isRevote = false // 是否平票
            this.electedId = data.expertId	 // 推选人id
            data.isLeader = true // 是否为组长
            this.loadData()
          })
        })
        .catch(() => {})
    },
    // 监视票数
    monitoringVotes() {
      // 是否已完成推选 00 未推选 01 已推选
      this.timer = setInterval(() => {
        this.loadData()
      }, 15000)
    },
    // 封装请求数据
    assemblyData() {
      const data = {
        evalExpertId: this.getTenderBase.evalExpertId, // 专家评审表ID
        driverId: this.getTenderBase.driverId, // 驱动id
        packId: this.getTenderBase.packId, // 标段id
        evalRule: this.getTenderBase.evalRule // 评审规则 【00–整体步骤 01-单个评委步骤】
      }
      return data
    },
    // 判断是否为组长
    checkOutIsLeader() {
      return checkoutIsEvalLeaderInfo(this.getTenderBase.packId)
    }
  }
}
</script>

<style lang="less" scoped>
.topText{
  position: absolute;
  right: 0;
  top: 0;
}
</style>
