<!--
  @description: 添加评审点-弹框
  @Modifier：sxm/yf
  @routerPath:
  @date：  2020-9-23 10:45: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="form" :model="formData" :rules="rules" :inline-message="true">
      <table class="from_table">
        <tr>
          <td>
            <el-form-item ref="evaluationName" label="评审点名称" prop="evaluationName" />
          </td>
          <td>
            <el-input v-model="formData.evaluationName" />
          </td>
        </tr>
        <tr v-if="procedureType !== '00'">
          <td>
            <el-form-item ref="minScore" label="最低分" prop="minScore" />
          </td>
          <td>
            <el-input-number v-model="formData.minScore" :min="0" :precision="2" :controls="false" type="number" style="width: 100%" @blur="checkScore(formData.minScore)" />
          </td>
        </tr>
        <tr v-if="procedureType !== '00'">
          <td>
            <el-form-item ref="maxScore" label="最高分" prop="maxScore" />
          </td>
          <td>
            <el-input-number v-model="formData.maxScore" :min="0" :precision="2" :controls="false" type="number" style="width: 100%" @blur="checkScore(formData.maxScore)" />
          </td>
        </tr>
        <tr v-if="procedureType !== '00'">
          <td>
            <el-form-item ref="scoringMethod" label="打分方式" prop="scoringMethod" />
          </td>
          <td>
            <el-radio-group v-model="formData.scoringMethod">
              <el-radio
                v-for="(item) in scoreWayList"
                :key="item.typecode"
                :label="item.typecode"
              >
                {{ item.typename }}
              </el-radio>
            </el-radio-group>
          </td>
        </tr>
        <tr v-if="flag=='01' && procedureType !== '00'">
          <td>
            <el-form-item ref="objectiveOptions" label="客观分选项" prop="objectiveOptions" />
          </td>
          <td>
            <el-input v-model="formData.objectiveOptions" oninput="return this.value=this.value.replace(/[^\d\,]/g,'')" onkeyup="return this.value=this.value.replace(/[^\d\,]/g,'')" onkeydown="return this.value=this.value.replace(/[^\d\,]/g,'')" />
            <p class="tip">
              不同分值之间用逗号隔开，例如：1,2,3
            </p>
          </td>
        </tr>
        <tr>
          <td>
            <el-form-item ref="evaluationStandard" label="评审标准" prop="evaluationStandard" />
          </td>
          <td>
            <el-input
              v-model="formData.evaluationStandard"
              :autosize="{ minRows: 4, maxRows: 4 }"
              type="textarea"
              maxlength="1000"
              show-word-limit
              style="width: 100%"
            />
          </td>
        </tr>
      </table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="loading" type="primary" @click="ensure()">确 定</el-button>
      <el-button @click="dialogData.dialog = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* api接口*/
import { saveOrUpdateEvaluationFactor, queryEvaluationFactorById } from '@/api/procurement/procurementProtdoc'
import { restoreInitialValue } from '@assets/js/utils'
/* 公共参数方法*/
import { procurementDocumentsGroupMixin } from
  '@/views/procurement/procurementDocuments/mixins/procurementDocumentsGroupMixin'// 立项采购-字典
export default {
  name: 'EvaluationStepDialog',
  mixins: [procurementDocumentsGroupMixin],
  props: {
    dialogData: {
      type: Object,
      default: function() {
        return {
          id: '',
          dialog: false, // 是否显示
          lineTallGrade: 0, // 当前点击行的最高分 默认0
          totalPoints: '' // 当前步骤的总积分
        }
      }
    },
    procedureId: {
      type: String,
      default: function() {
        return ''
      }
    },
    procedureType: {
      type: String,
      default: function() {
        return ''
      }
    }
  },
  data() {
    var newReg = (rule, value, callback) => {
      const reg = /^(\d+,)*\d+$/ // 正则校验
      if (value !== '' && !reg.test(value)) {
        this.$message.warning('客观分选项：不同分值之间用逗号隔开 例如：1,2,3 不要以逗号结束')
      } else {
        callback()
      }
    }
    return {
      loading: false,
      flag: '00', // 根据评审类型不同 来控制页面不同的显示方式 初始化为00
      formData: {
        evaluationName: '', // 评审点名称
        evaluationStandard: '', // 评审标准
        id: '', // 主键id
        procedureId: this.procedureId, // 评审步骤主键id
        status: '00',
        minScore: undefined, // 最低分
        maxScore: undefined, // 最高分
        scoringMethod: '00', // 打分方式
        objectiveOptions: '' // 客观分选项
      },
      rules: {
        evaluationName: [{ required: true, message: '不能为空', trigger: 'blur' }], // 评审点名称
        minScore: [{ required: true, message: '不能为空', trigger: 'blur' }], // 最低分
        maxScore: [{ required: true, message: '不能为空', trigger: 'blur' }], // 最高分
        objectiveOptions: {
          validator: newReg,
          trigger: 'blur'
        },
        scoringMethod: [{ required: true, message: '请选择' }]// 打分方式
      }
    }
  },
  watch: {
    'formData.scoringMethod'(val) { // 打分方式监听 00主观分；01客观分
      switch (val) { // flag的值控制页面不同的显示 在标签的v-if上面加上判断
        case '00':
          this.flag = '00'
          break
        case '01':
          this.flag = '01'
          break
      }
    },
    // 弹框打开状态
    'dialogData.dialog'(val) {
      if (val) {
        if (this.dialogData.id) {
          this.queryEvaluationFactorById(this.dialogData.id) // 编辑回显数据
        }
      } else {
        restoreInitialValue(this.formData) // 清空表单值
        this.formData.procedureId = this.procedureId
        // 设置默认打分方式
        this.formData.scoringMethod = '00'
        this.formData.minScore = undefined
        this.formData.maxScore = undefined
        // 清空校验
        this.$refs.form.clearValidate()
      }
    }
  },
  created() {
    // 查询数据字典
    // this.inEvalStepCodes()
  },
  methods: {
    restoreInitialValue,
    ensure() {
      // this.loading = true
      this.$refs.form.validate((valid, field) => {
        if (valid) {
          if (this.procedureType !== '00') {
            const minScore = this.formData.minScore * 1
            const maxScore = this.formData.maxScore * 1
            // 判断最低分是否大于最高分
            if (minScore > maxScore) {
              this.$message.warning('最低分应小于最高分')
              // 清空数据
              this.formData.minScore = undefined
              this.formData.maxScore = undefined
              // this.loading = false
              return
            }
            // 判断最高分是否大于最低分
            if (maxScore <= minScore) {
              this.$message.warning('最高分必须大于最低分')
              // 清空数据
              this.reviewPointData.minScore = undefined
              this.reviewPointData.maxScore = undefined
              // this.loading = false
              return
            }
            // 计算本步骤总分
            const sumNum = this.dialogData.totalPoints - this.dialogData.lineTallGrade + maxScore // 求总分值
            alert(this.dialogData.totalPoints)
            alert(this.dialogData.lineTallGrade)

            alert(maxScore)
            alert(sumNum)
            if (sumNum > 100) {
              this.$confirm('累计总分值已超100分，请修改', '提示', {
                confirmButtonText: '确定',
                confirmButtonClass: 'confirmBtn',
                showCancelButton: false,
                type: 'warning'
              })
                .then(() => {
                  // 清空数据
                  this.reviewPointData.minScore = undefined
                  this.reviewPointData.maxScore = undefined
                })
                .catch(() => {})
              return
            }
          }
          const data = {
            ...this.formData,
            status: '00'
          }
          saveOrUpdateEvaluationFactor(data).then(res => {
            this.loading = false
            this.$message({ type: 'success', message: res.header.msg })
            this.dialogData.dialog = false
            this.$emit('watchChild', true)
          }).catch(() => {
            this.loading = false
          })
        }
        this.loading = false
      })
    },

    // 根据id查询评审指标
    queryEvaluationFactorById(id) {
      queryEvaluationFactorById(id).then(res => {
        if (res.header.ret === '0') {
          this.formData = Object.assign({ ...res.data })
        }
      })
    },
    // 校验分数
    checkScore(val) {
    }
  }
}
</script>

<style scoped lang="less">
  @import '~@assets/style/common/public.less';
  .tip{
    color: @gray;
    font-size: 12px;
  }
</style>
