<!--
  @description: 采购报价评审办法弹框
  @Modifier：gmy
  @date:2020-7-2- 11:39:19
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.dialog"
    :close-on-click-modal="false"
    :lock-scroll="false"
    append-to-body
    title="选择评审方式/评审方法"
    width="760px"
  >
    <div class="p8">
      <el-form
        ref="formData"
        :model="formData"
        :rules="rules"
        :inline-message="true"
      >
        <table class="from_table">
          <tr>
            <td><el-form-item label="评审方式" prop="line" /></td>
            <td>
              <el-radio-group v-model="formData.line">
                <el-radio
                  v-for="(item) in protproject_lineList"
                  :key="item.id"
                  :label="item.typecode"
                  @change="lineChange"
                >
                  {{ item.typename }}
                </el-radio>
              </el-radio-group>
            </td>
          </tr>
          <tr v-if="dialogData.tenderMethod != '12'">
            <td><el-form-item label="评审办法" prop="reviewMethod" /></td>
            <td>
              <el-radio-group v-model="formData.reviewMethod">
                <el-radio
                  v-for="(item) in protproject_reviewMethodList"
                  v-if="!((dialogData.tenderMethod === '11' || dialogData.tenderMethod === '13') && item.typecode === '03')"
                  :key="item.id"
                  :label="item.typecode"
                  :disabled="bidEvaluationStatusDisable"
                >
                  {{ item.typename }}
                </el-radio>
              </el-radio-group>
            </td>
          </tr>
        </table>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="confirmLoading" type="primary" @click="okdialog('formData')">确认</el-button>
      <el-button :loading="confirmLoading" @click="close">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* api*/
import { saveReviewMethod } from '@/api/procurement/procurementProject'
import { getProjectDrawOffEntityId } from '@/api/procurement/procurementEvaluation'
import { qryOnLineEvalRuleProjectEvalStatus } from '@/api/procurement/procurementEvaluation'

/* 公共方法*/
import { procurementGroupMixins } from '@/views/procurement/mixins/procurementGrounpMixins' // 公共字典
import { procurementOfferMixin } from '../mixins/procurementOfferMixin' // 公共数据
export default {
  name: 'OfferReviewaddDialogVue',
  inject: ['getDriveTask'],
  mixins: [procurementGroupMixins, procurementOfferMixin],
  props: {
    dialogData: {
      type: Object,
      default: function() {
        return
      }
    }
  },
  data() {
    return {
      confirmLoading: false, // loading
      bidEvaluationStatusDisable: false, // 评审办法禁用控制
      rules: {
        reviewMethod: [{ required: true, message: '请选择', trigger: 'blur' }], // 评审办法
        line: [{ required: true, message: '请选择', trigger: 'blur' }] // 选择线上线下

      }
    }
  },
  watch: {
    // 监听弹框状态的打开
    'dialogData.dialog'(val) {
      if (val) {
        // 初始化加载评审方法
        this.formData.reviewMethod = this.dialogData.reviewMethod
        // 初始化加载评审方式
        if (this.dialogData.line) {
          this.formData.line = this.dialogData.line
        }
        // 查询项目规则字典方法
        this.initqueryProcurementProjectRuleInitInfo()
        if (this.dialogData.line === '01') {
          this.getBidEvaluationStatus()
        }
      }
    }
  },
  methods: {
    lineChange(value) { // 切换评审方式
      if (value === '01') {
        // 初始化加载评审方法
        this.formData.reviewMethod = this.dialogData.reviewMethod
        this.getBidEvaluationStatus()
      } else {
        this.bidEvaluationStatusDisable = false
      }
    },
    getBidEvaluationStatus() { // 线上评审办法查询开始评标状态 控制评审方法
      qryOnLineEvalRuleProjectEvalStatus({ packId: this.dialogData.packId }).then(res => {
        this.bidEvaluationStatusDisable = !res.data
      }).catch(() => {
      })
    },
    // 确认
    okdialog(formName) {
      this.$refs[formName].validate((valid, field) => {
        if (valid) { // 校验通过
          this.confirmLoading = true
          const data = {
            packId: this.dialogData.packId, // 项目标段id
            reviewMethod: this.formData.reviewMethod, // 评审方法
            line: this.formData.line // 评审方式
          }
          this.saveReviewMethodData(data)
        } else {
          this.confirmLoading = false
          return false
        }
      })
    },
    // 封装保存评审办法
    saveReviewMethodData(data) {
      saveReviewMethod(data).then(res => {
        // 判断线上线下 line 00 线下 01 线上
        if (data.line === '00') {
          this.confirmLoading = false
          // 提交评审后刷新驱动节点
          this.getDriveTask()
          return
        }
        // 调用保存评审方式评审办法的方法
        this.saveGetProjectDrawOffEntityId()
      }).catch(() => {
        this.confirmLoading = false
      })
    },
    // 封装保存评审方式评审办法
    saveGetProjectDrawOffEntityId() {
      getProjectDrawOffEntityId(this.dialogData.packId).then(res => {
        this.confirmLoading = false
        // 刷新驱动节点
        this.getDriveTask()
      }).catch(() => {
        this.confirmLoading = false
      })
    },
    // 取消
    close() {
      Object.keys(this.formData).forEach(val => {
        this.formData[val] = []
      })
      this.dialogData.dialog = false
    }
  }
}
</script>

<style lang='less' scoped>

</style>
