<!--
  @description: 设置评标步骤-弹框
  @Modifier：sxm/gmy
  @routerPath:
  @date：  2020-7-30 17:21:19
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.dialog"
    :close-on-click-modal="false"
    :lock-scroll="false"
    title="添加评标步骤"
    width="600px"
    custom-class="el-dialog__body-p8 el-dialog_table"
  >
    <el-form ref="formData" :model="formData" :rules="rules" :inline-message="true">
      <table class="from_table">
        <tr>
          <td>
            <el-form-item label="评标步骤名称" prop="procedureName" />
          </td>
          <td>
            <el-input v-model="formData.procedureName" />
          </td>
        </tr>
        <tr>
          <td>
            <el-form-item ref="procedureType" label="评审类型" prop="procedureType" />
          </td>
          <td>
            <el-select v-model="formData.procedureType" filterable style="width: 100%">
              <el-option
                v-for="item in reviewTypeList"
                :key="item.typecode"
                :label="item.typename"
                :value="item.typecode"
              />
            </el-select>
          </td>
        </tr>
        <tr v-if="flag=='00'">
          <td>
            <el-form-item label="评审选项" prop="reviewOptions" />
          </td>
          <td>
            <el-select v-model="formData.reviewOptions" filterable style="width: 100%">
              <el-option
                v-for="item in reviewOptionList"
                :key="item.typecode"
                :label="item.typename"
                :value="item.typecode"
              />
            </el-select>
          </td>
        </tr>
        <tr v-if="flag=='00'">
          <td>
            <el-form-item label="符合性通过条件" prop="summaryWay" />
          </td>
          <td>
            <el-radio-group v-model="formData.summaryWay">
              <el-radio
                v-for="(item) in summarizingWayList"
                :key="item.typecode"
                :label="item.typecode"
              >
                {{ item.typename }}
              </el-radio>
            </el-radio-group>
          </td>
        </tr>
        <tr v-if="flag!='00' && scoreTypeFlag">
          <td>
            <el-form-item label="分数统计方式" prop="scoreStatisticsWay" />
          </td>
          <td>
            <el-radio-group v-model="formData.scoreStatisticsWay">
              <el-radio
                v-for="(item) in flosWayList"
                :key="item.typecode"
                :label="item.typecode"
              >
                {{ item.typename }}
              </el-radio>
            </el-radio-group>
          </td>
        </tr>
        <tr v-if="flag!='00'">
          <td>
            <el-form-item label="分数" prop="procedureScore" />
          </td>
          <td>
            <money :thousands="true" :unit-fixed="0" :width="'279px'" :money-value="formData" name="procedureScore" />
          </td>
        </tr>
        <tr v-if="flag!='00'">
          <td>
            <el-form-item label="权重（%）" prop="procedureWeight" />
          </td>
          <td>
            <money :thousands="true" :unit-fixed="0" :width="'279px'" :money-value="formData" name="procedureWeight" />
          </td>
        </tr>
      </table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="loading" type="primary" @click="ensure('formData')">确 定</el-button>
      <el-button @click="closeInfo">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* api接口*/
import {
  saveOrUpdateBidEvaluationProcedure,
  getBidEvaluationProcedureById
} from '@/api/procurement/procurementProtdoc' //
/* 公共参数方法*/
import { restoreInitialValue } from '@/assets/js/utils'
import { procurementDocumentsGroupMixin } from '@/views/procurement/procurementDocuments/mixins/procurementDocumentsGroupMixin' // 公共字典
import { procurementDocumentsMixin } from '@/views/procurement/procurementDocuments/mixins/procurementDocumentsMixin'
/* 公共组件 */
import money from '@comp/common/money/money' // 金币组件

/* 验证规则*/
import { checkRate } from '@/assets/js/validate'
export default {
  name: 'EvaluationStepDialog',
  components: {
    money
  },
  mixins: [procurementDocumentsGroupMixin, procurementDocumentsMixin],
  props: {
    dialogData: {
      type: Object,
      default: function() {
        return
      }
    },
    getTenderBase: {
      type: Object,
      default: function() {
        return
      }
    }
  },
  data() {
    return {
      loading: false,
      flag: '00', // 根据评审类型不同 来控制页面不同的显示方式 初始化为00
      scoreTypeFlag: true,
      isEdit: false, // 数据回显编辑页
      lastScore: '', // 数据化回显分数
      formData: {
        id: '',
        packId: '',
        procedureName: '', // 评标步骤名称
        procedureType: '00', // 评审类型
        reviewOptions: '00', // 评审选项
        summaryWay: '00', // 符合性通过条件
        scoreStatisticsWay: '00', // 分数统计方式 00取平均值 01去除最高最低取平均值
        procedureScore: '', // 分值
        procedureWeight: '', // 权重
        serialNumber: '', // 序号
        totalScore: this.dialogData.totalScore // 总分
      },
      rules: {
        procedureName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ], // 评标步骤名称
        procedureType: [{ required: true, message: '不能为空', trigger: 'blur' }], // 评审类型
        reviewOptions: [{ required: true, message: '不能为空', trigger: 'blur' }], // 评审选项
        summaryWay: [{ required: true, message: '不能为空', trigger: 'blur' }], // 符合性通过条件
        scoreStatisticsWay: [{ required: true, message: '不能为空', trigger: 'blur' }], // 分数统计方式
        procedureScore: [
          { required: true, validator: checkRate, trigger: 'blur' }
        ], // 分值
        procedureWeight: [
          { required: true, validator: this.checkWeight, trigger: 'blur' }
        ] // 权重
      }
    }
  },
  watch: {
    'formData.procedureType'(val) {
      // 评审类型监听 00符合性评审；01商务；02技术；03价格；04 商务和技术
      switch (
        val // flag的值控制页面不同的显示 在标签的v-if上面加上判断
      ) {
        case '00':
          this.flag = '00'
          this.formData.procedureWeight = ''
          this.formData.procedureScore = ''
          break
        case '01':
          this.flag = '01'
          this.scoreTypeFlag = true
          break
        case '02':
          this.flag = '01'
          this.scoreTypeFlag = true
          break
        case '04':
          this.flag = '01'
          this.scoreTypeFlag = false
          break
        case '03':
          this.flag = '02'
          this.scoreTypeFlag = true
          break
      }
    },
    'dialogData.dialog'(newval, oldval) {
      // 有评审id代表是编辑进入
      if (this.dialogData.id) {
        this.isEdit = true // 编辑页
        this.getBidEvaluationProcedureById(this.dialogData)
        return
      }
      // 新增进来清空之前的数据
      restoreInitialValue(this.formData)
      this.formData = {
        id: '',
        packId: '',
        procedureName: '', // 评标步骤名称
        procedureType: '00', // 评审类型
        reviewOptions: '00', // 评审选项
        summaryWay: '00', // 符合性通过条件
        scoreStatisticsWay: '00', // 分数统计方式
        procedureScore: '', // 分值
        procedureWeight: '', // 权重
        serialNumber: '', // 序号
        totalScore: this.dialogData.totalScore, // 总分
        status: '00' // 状态 00为保存 01为生效
      }
    }
  },
  created() {
    // 查询数据字典
    // this.inEvalStepCodes()
  },
  methods: {
    // 添加评标步骤
    ensure(formName) {
      this.loading = true
      // 校验输入内容
      this.$refs[formName].validate((valid, field) => {
        if (valid) {
          this.formData.packId = this.getTenderBase.packId
          this.formData.serialNumber = this.dialogData.serialNumber // 序号字段赋值
          /*
          评审类型 00符合性评审；01商务；02技术；03价格；04 商务和技术
          字段: procedureType
          */
          // 符合性无需判断分值
          if (this.formData.procedureType === '00') {
            this.formData.status = '00'
            // 调用保存方法
            this.saveOrUpdateBidEvaluationData(this.formData)
            return
          }
          /*
            非符合性评审校验分值
            分值计算未以后分值数+当前选择的分值数的和 不可超过100
          */
          const goal = this.formData.procedureWeight * 0.01 * (this.formData.procedureScore * 1) // 计算分值
          const sumNum = goal + this.dialogData.totalScore * 1 - this.lastScore // 求总分值
          if (sumNum > 100) {
            this.$confirm('累计总分值已超100分，请修改', '提示', {
              confirmButtonText: '确定',
              confirmButtonClass: 'confirmBtn',
              showCancelButton: false,
              type: 'warning'
            })
              .then(() => {})
              .catch(() => {})
            this.loading = false
            return
          }
          // 调用保存方法
          this.formData.status = '00'
          this.saveOrUpdateBidEvaluationData(this.formData)
        }
        this.loading = false
      })
    },
    // 取消
    closeInfo() {
      restoreInitialValue(this.formData)
      this.dialogData.id = ''
      this.dialogData.dialog = false
    },
    // 根据id获取评标步骤信息
    getBidEvaluationProcedureById(dialogData) {
      const { id, totalScore } = { ...dialogData }
      getBidEvaluationProcedureById(id, totalScore).then(res => {
        this.formData = { ...res.data }
        // 保存初始状态的分数
        this.lastScore = this.formData.procedureWeight * 0.01 * (this.formData.procedureScore * 1) // 计算分值
      })
    },
    // 确认添加评审方法请求
    saveOrUpdateBidEvaluationData(data) {
      if (!this.scoreTypeFlag) {
        data.scoreStatisticsWay = null
      }
      saveOrUpdateBidEvaluationProcedure(data).then(res => {
        this.loading = false
        this.$message({ type: 'success', message: '添加成功' })
        this.dialogData.dialog = false
        this.$emit('watchChild', true)
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style scoped lang="less">
</style>
