<!--
  @description:变更采购方式新增
  @Modifier: haofengxian
  @routerPath:/bidding/projectDrive/changePurchase/changePurchase-add
  @date: 2020-12-19-14:34
-->
<template>
  <div class="main">
    <el-form
      ref="fromData"
      :rules="rules"
      :model="formData"
      :inline-message="true"
    >
      <table class="from_table">
        <tr class="singleRow">
          <td>
            <el-form-item label="标段（包）" prop="packInfoList" />
          </td>
          <td colspan="3">
            <changePurchasePackComponent
              ref="packComp"
              :select-pack-list="formData.packInfoList"
            />
          </td>
        </tr>
        <tr class="singleRow">
          <td>
            <el-form-item label="原采购方式" prop="oldProcurementMethod" />
          </td>
          <td colspan="3">
            {{ filterDictText(allTenderMethodTypeList,formData.oldProcurementMethod) }}
          </td>
        </tr>
        <tr class="singleRow">
          <td>
            <el-form-item label="拟变更采购方式" prop="newProcurementMethod" />
          </td>
          <td colspan="3">
            <el-select
              v-model="formData.newProcurementMethod"
              placeholder="请选择"
            >
              <el-option
                v-for="(item,index) in newProcurementMethodTypeList"
                :key="index"
                :label="item.typename"
                :value="item.typecode"
              />
            </el-select>
          </td>
        </tr>
        <tr class="singleRow">
          <td>
            <el-form-item ref="reason" label="采购方式变更原因" prop="reason" />
          </td>
          <td colspan="3">
            <gx-input
              v-model="formData.reason"
              :autosize="{ minRows: 6, maxRows: 6}"
              type="textarea"
              placeholder="请输入内容"
            />
          </td>
        </tr>
        <tr class="singleRow">
          <td>
            <el-form-item label="说明附件" />
          </td>
          <td colspan="3">
            <upload
              ref="upload"
              :data="changePurchaseData"
              :auto-delete="true"
              :auto-upload="true"
              title=""
              file-ref="fileRef"
            />
          </td>
        </tr>
      </table>
    </el-form>
    <!--保存按钮-->
    <div class="but_footer">
      <div class="but_footer-h" />
      <div class="but_footer-but">
        <el-button :loading="confirmLoading" @click="saveData('00')">
          保 存
        </el-button>
        <el-button :loading="confirmLoading" type="primary" @click="submitData">
          提 交
        </el-button>
        <el-button :loading="confirmLoading" @click="back">
          返 回
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
/* 接口*/
import { saveException, getExceptionByExceptionId } from '@/api/bidding/changePurchase'// 公告列表
import { characters1_auto } from '@/assets/js/validate'// 校验输入内容
/* 公用方法*/
import { randomData, filterDictText } from '@assets/js/utils' // 常用方法
/* 混入开发 */
import { changePurchase } from '../mixins/changePurchaseMixin.js' // 常用方法
/* 选择标包组件*/
import changePurchasePackComponent from './changePurchasePackComponent'
import upload from '@comp/common/updownload/upload' // 附件上传组件
export default {
  name: 'ChangeBulletinAdd',
  components: {
    changePurchasePackComponent,
    upload// 文件上传
  },
  data() {
    return {
      /* 字典信息-start */
      allTenderMethodTypeList: [], // 招标方式
      newProcurementMethodTypeList: [], // 变更采购方式
      /* 字典信息-end */
      ...changePurchase, // 变更采购方式-公共方法
      tenderBase: this.$store.getters.tenderBase, // 项目信息
      confirmLoading: false, // 暂存、下一步Loding
      formData: {
        tenderId: '', // 项目id
        title: '', // 公告标题
        exceptionId: '', // 变更采购方式 id
        packInfoList: [], // 标段包信息
        exceptionType: '01', // 异常类型 01 变更采购方式
        oldProcurementMethod: '', // 原采购方式 00公开招标 01 邀请招标
        newProcurementMethod: '', // 新采购方式 12:单一来源 13:竞争性谈判
        reason: '', // 原因
        useStatus: ''// 状态 00 保存 01提交
      },
      // 校验
      rules: {
        packInfoList: [{ required: true, validator: characters1_auto, trigger: 'change' }], // 标段包信息
        oldProcurementMethod: [{ required: true, validator: characters1_auto, trigger: 'blur' }], // 原招标方式
        newProcurementMethod: [{ required: true, message: '请选择', trigger: 'blur' }],
        reason: [{ required: true, validator: characters1_auto, trigger: 'blur' }] // 原因
      }
    }
  },
  created() {
    this.initData()// 查询采购方式信息
    this.initGroup()// 获取字典方法
  },
  methods: {
    filterDictText,
    // 获取字典方法
    initGroup() {
      const data = [
        'allTenderMethodType', // 招标方式
        'newProcurementMethodType' // 变更采购方式
      ]
      this.$store.dispatch('getGroupType', data).then(res => {
        Object.keys(res.bidTypeData).forEach((key) => {
          this[key] = res.bidTypeData[key]
        })
      })
    },
    // 查询采购方式信息
    initData() {
    // 判断是否有变更采购id
      if (this.$route.query.exceptionId) {
        const exceptionId = this.$route.query.exceptionId
        getExceptionByExceptionId({ exceptionId: exceptionId }).then(res => {
          this.formData = res.data
          this.changePurchaseData.bizId = this.formData.exceptionId // 附件主键id
        })
      } else {
        this.formData.exceptionId = randomData()// 前端生产 uid
        this.changePurchaseData.bizId = this.formData.exceptionId // 附件主键id
        this.formData.oldProcurementMethod = this.tenderBase.tenderMethod// 招标方式
      }
    },
    // 提交
    submitData() {
      if (!this.$refs.packComp.isCheck()) {
        return // 校验是否选择标段（包）
      }
      // 校验输入内容
      this.$refs.fromData.validate((valid, field) => {
        if (valid) { // 校验通过
          this.$confirm('确认提交?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            confirmButtonClass: 'confirmBtn',
            cancelButtonClass: 'cancelBtn',
            type: 'warning'
          }).then(() => {
            this.saveData('01')// 提交
          }).catch(() => {
          })
        }
      })
    },
    // 保存
    saveData(useStatus) {
      const data = {
        ...this.formData,
        tenderId: this.tenderBase.tenderId, // 项目id
        useStatus: useStatus // 提交状态 00 保存 01 提交
      }
      this.confirmLoading = true
      saveException(data).then(response => {
        this.$message.success(response.header.msg)
        this.confirmLoading = false// btnLoding
        if (useStatus === '01') {
          // 提交时
          this.back()// 返回列表
        }
      }).catch(() => {
        this.confirmLoading = false// btnLoding
      })
    },
    // 返回
    back() {
      this.$router.push({
        path: '/bidding/projectDrive/changePurchase/changePurchase-list',
        query: {
          ...this.$route.query
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
  /deep/.file-list {
    .fileBtn,
    li {
      margin-bottom: 0 !important;
    }
    .name {
      margin-right: 0 !important;
    }
  }
</style>
