<!--
  @description: 招标项目-绩效管理列表-添加绩效目录
  @Modifier: 徐宝林
  @routerPath:/bidding/performanceManagement-add
  @date: 2020/12/05
-->
<template>
  <div>
    <section class="title p0-16 clearfix">
      <template>
        <div class="title-back pointer" @click="back">
          <i class="iconfont icon-fanhui mr10" />{{ $route.meta.name }}
        </div>
      </template>
    </section>
    <section class="p0-16">
      <el-form
        ref="fromData"
        :rules="rules"
        :inline-message="true"
        :model="formData"
      >
        <table class="from_table">
          <tr>
            <td>
              <el-form-item label="项目名称" prop="tenderName" />
            </td>
            <td>
              <gx-input
                v-model="formData.tenderName"
                placeholder="请选择"
                gx-maxlength="varchar200"
                style="width:calc(100% - 180px)"
                disabled
              />
              <el-button type="primary" @click="tenderNameOpen">
                选择
              </el-button>
              <el-button @click="tenderNameChange('clear')">
                清空
              </el-button>
            </td>
            <td>
              <el-form-item label="项目编号" prop="tenderNo" />
            </td>
            <td>
              <gx-input
                v-model="formData.tenderNo"
                gx-maxlength="varchar200"
                placeholder="项目编号"
              />
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="包件名称" prop="packName" />
            </td>
            <td>
              <gx-input
                v-model="formData.packName"
                gx-maxlength="varchar200"
                placeholder="包件名称"
              />
            </td>
            <td>
              <el-form-item label="包件编号" prop="packNo" />
            </td>
            <td>
              <gx-input
                v-model="formData.packNo"
                gx-maxlength="varchar200"
                placeholder="包件编号"
              />
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="招标人名称" prop="tenderPersonName" />
            </td>
            <td>
              <gx-input
                v-model="formData.tenderPersonName"
                gx-maxlength="varchar200"
                placeholder="招标人名称"
              />
            </td>
            <td>
              <el-form-item label="招标代理机构名称" prop="agencyName" />
            </td>
            <td>
              <gx-input
                v-model="formData.agencyName"
                gx-maxlength="varchar200"
                placeholder="招标代理机构名称"
              />
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="中标人名称" prop="bidName" />
            </td>
            <td>
              <gx-input
                v-model="formData.bidName"
                gx-maxlength="varchar200"
                placeholder="中标人名称"
              />
            </td>
            <td>
              <el-form-item label="机构联系电话" prop="bidDepartPhone" />
            </td>
            <td>
              <gx-input
                v-model="formData.bidDepartPhone"
                gx-maxlength="varchar200"
                placeholder="机构联系电话"
              />
            </td>
          </tr>
          <tr>
            <td>
              <el-form-item label="中标日期" prop="bidDate" />
            </td>
            <td>
              <gx-input
                v-model="formData.bidDate"
                gx-maxlength="varchar200"
                placeholder="中标日期"
              />
            </td>
            <td>
              <el-form-item label="中标价格" prop="bidPrice" />
            </td>
            <td>
              <gx-input
                v-model="formData.bidPrice"
                gx-maxlength="varchar200"
                placeholder="中标价格"
              />
            </td>
          </tr>
        </table>
      </el-form>
    </section>
    <!--保存按钮-->
    <div class="but_footer">
      <div class="but_footer-h" />
      <div class="but_footer-but">
        <el-button :loading="confirmLoading" @click="submit('00')">
          保存
        </el-button>
        <el-button @click="back">
          返回
        </el-button>
      </div>
    </div>
    <!-- 中标人名称选择弹框 -->
    <archivedSelectDialog ref="tenderName" @call-back="tenderNameChange" />
  </div>
</template>

<script>
/* 接口*/
import {
  addPerformanceManagement, // 新增
  updatePerformanceManagement, // 修改
  getAllPerformanceManagementById // 获取详情
} from '@/api/bidding/bidCalibration'
/* 公用方法*/
import { validateInput, phone, characters1_auto } from '@/assets/js/validate'// 校验输入内容
/* 组件*/
import archivedSelectDialog from './archivedSelect-dialog'// 中标人名称选择弹框
/* 组件*/
import { randomData } from '@/assets/js/utils' // 随机生成id
/* 组件*/
import { getTime } from '@api/common/tools.js' // 获取服务器时间
export default {
  name: 'ClarificationAdd',
  components: {
    archivedSelectDialog// 中标人名称选择弹框
  },
  data() {
    return {
      confirmLoading: false,
      /* 字典信息-end */
      formData: {
        id: '', // 主键Id
        tenderName: '', // 招标项目名称
        tenderNo: '', // 招标项目编号
        packName: '', // 包件名称
        packNo: '', // 包件编号
        tenderPersonName: '', // 招标人名称
        agencyName: '', // 招标代理机构名称
        bidName: '', // 中标人名称
        bidDepartPhone: '', // 机构联系电话
        bidDate: '', // 中标日期
        bidPrice: '' // 中标价格
      },
      // 校验
      rules: {
        tenderName: [{ required: true, validator: characters1_auto, trigger: 'change' }], // 招标项目名称
        tenderNo: [{ required: true, validator: validateInput, trigger: 'blur' }], // 招标项目编号
        packName: [{ required: true, validator: validateInput, trigger: 'blur' }], // 包件名称
        packNo: [{ required: true, validator: validateInput, trigger: 'blur' }], // 包件编号
        tenderPersonName: [{ required: true, validator: validateInput, trigger: 'blur' }], // 招标人名称
        agencyName: [{ required: true, validator: validateInput, trigger: 'blur' }], // 招标代理机构名称
        bidName: [{ required: true, validator: validateInput, trigger: 'blur' }], // 中标人名称
        bidDepartPhone: [{ required: true, validator: phone, trigger: 'blur' }], // 机构联系电话
        bidDate: [{ required: true, validator: validateInput, trigger: 'blur' }], // 中标日期
        bidPrice: [{ required: true, validator: validateInput, trigger: 'blur' }] // 中标价格
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    // 初始化
    init() {
      // 获取系统时间
      this.getNowTimes()
      if (this.$route.params.id) {
        this.queryRepresentInfo(this.$route.params.id)
      } else {
        this.formData.id = randomData()// 随机生成Id
      }
    },
    getNowTimes() {
      // 调用获取当前时间异步方法
      getTime().then((res) => {
        this.nowTimes = res.data.time
        this.formData.bidDate = this.$Moment(this.nowTimes).format(this.momentDate)
      })
    },
    // 获取详情
    queryRepresentInfo(id) {
      getAllPerformanceManagementById(id).then(res => {
        this.formData = res.data
      }).catch(() => {

      })
    },
    // 返回
    back() {
      // 返回路由
      const backPathObj = {
        '1': '/testAndCertify/bidPerformanceList', // 投标人
        '2': '/testAndCertify/theTenderPerformanceList', // 招标人
        '3': '/testAndCertify/agencyPerformanceList', // 代理机构
        '4': '/testAndCertify/expertsPerformanceList' // 专家
      }
      this.$router.push(backPathObj[this.$route.meta.type])
    },
    // 中标人名称-打开弹框
    tenderNameOpen() {
      this.$refs.tenderName.open()
    },
    // 中标人名称数据操作
    tenderNameChange(data) {
      if (data === 'clear') { // 清空
        this.formData.tenderName = ''// 中标人名称
      } else { // 选择
        this.formData.tenderName = data.code// 中标人名称
        // this.formData.rewardPunishmentPersonName = data.Name
        // this.formData.rewardPunishmentAddress = data.Address
        // this.formData.rewardPunishmentDepartPhone = data.Phone
      }
    },
    /*
    * 保存接口
    * type 01 生效 00 保存
    * */
    saveData(type) {
      this.getNowTimes()
      this.confirmLoading = true// btnLoding
      // 判断项目id是否存在 不存在走新增方法 存在走修改
      if (!this.$route.params.id) {
        this.addPerformanceManagement(type)
      } else {
        this.updatePerformanceManagement(type)
      }
    },
    // 新增
    addPerformanceManagement(type) {
      addPerformanceManagement(this.formData).then(response => {
        this.$message.success('操作成功')
        this.confirmLoading = false// btnLoding
        // 生效
        if (type === '01') {
          // 生效成功后返回列表
          this.back()
        }
      }).catch(() => {
        this.confirmLoading = false// btnLoding
      })
    },
    // 修改
    updatePerformanceManagement(type) {
      updatePerformanceManagement(this.formData).then(response => {
        this.$message.success('操作成功')
        this.confirmLoading = false// btnLoding
        // 生效
        if (type === '01') {
          // 生效成功后返回列表
          this.back()
        }
      }).catch(() => {
        this.confirmLoading = false// btnLoding
      })
    },
    // 提交
    submit() {
      this.$refs.fromData.validate((valid, field) => {
        if (valid) { // 校验通过
          this.$confirm('确定生效吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            confirmButtonClass: 'confirmBtn',
            cancelButtonClass: 'cancelBtn',
            type: 'warning'
          }).then(() => {
            this.saveData('01')
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="less">

</style>
