<!--
  @description:新增重新招标
  @Modifier: haofengxian
  @routerPath:/bidding/projectDrive/stopbiddingRecord/stopbiddingRecordAdd
  @date: 2020-5-13-10:42
-->
<template>
  <div class="main">
    <el-form
      ref="fromData"
      :model="formData"
      :rules="rules"
      :inline-message="true"
    >
      <table class="from_table">
        <tr class="singleRow">
          <td>
            <el-form-item label="标段（包）" required />
          </td>
          <td colspan="3">
            <stopbiddingRecord-pack-component
              ref="packComp"
              :select-pack-list="formData.packInfoList"
            />
          </td>
        </tr>
        <tr class="singleRow">
          <td>
            <el-form-item label="异常原因" prop="unusualReasonType" />
          </td>
          <td colspan="3">
            <el-select
              v-model="formData.unusualReasonType"
              placeholder="请选择"
            >
              <el-option
                v-for="(item,index) in unusualReasonTypeList"
                :key="index"
                :label="item.typename"
                :value="item.typecode"
              />
            </el-select>
          </td>
        </tr>
        <tr class="singleRow">
          <td>
            <el-form-item label="异常报告附件" />
          </td>
          <td colspan="3">
            <!--异常报告附件上传-->
            <upload
              ref="upload"
              :data="upLoadData"
              :auto-delete="true"
              :auto-upload="true"
              title=""
              file-ref="fileRef"
            />
          </td>
        </tr>
        <tr class="singleRow">
          <td>
            <el-form-item label="异常原因记录" />
          </td>
          <td colspan="3">
            <gx-input
              v-model="formData.unusualReasonRecord"
              :autosize="{ minRows: 6, maxRows: 6}"
              gx-maxlength="varchar1000"
              type="textarea"
              placeholder="请输入内容"
            />
          </td>
        </tr>
      </table>
    </el-form>
    <!--保存按钮-->
    <div class="but_footer">
      <div class="but_footer-h" />
      <div class="but_footer-but">
        <el-button :loading="confirmLoading" type="primary" @click="saveNextStep('fromData')">
          保存，进入下一步
        </el-button>
        <el-button
          :loading="confirmLoading"
          @click="back"
        >
          返 回
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
/* 接口*/
import { saveStopbiddingRecord, getStopbiddingRecordInfo } from '@api/bidding/stopbiddingRecord'
/* 选择标包组件*/
import stopbiddingRecordPackComponent from './stopbiddingRecordPackComponent'
import upload from '@comp/common/updownload/upload' // 附件上传组件
/* 常用方法*/
import { randomData } from '@/assets/js/utils/index'
export default {
  name: 'RebiddingRecordAdd',
  components: {
    'stopbiddingRecord-pack-component': stopbiddingRecordPackComponent,
    upload// 文件上传
  },
  data() {
    return {
      confirmLoading: false, // 暂存、下一步Loding
      tenderBase: this.$store.getters.tenderBase, // 项目信息
      tenderId: this.$route.query.id, // 项目id
      formData: {
        packInfoList: [], // 包段信息列表
        tenderId: this.$route.query.id, // 项目id
        stopbiddingRecordId: '', // 招标终止记录表id
        unusualReasonRecord: '', // 异常原因记录
        unusualReasonType: '' // 异常原因类型 :00购标人不足,01投标人不足,02解密成功不足,03评审合格不足,04资审合格不足,05不认可,06撤项,07单一来源,08询价,09谈判,10其他
      },
      // 上传附件参数
      upLoadData: {
        bizId: '', // 业务主键Id
        fileType: 'stopbidding_exception_file_base '// 文件类型
      },
      // 校验
      rules: {
        unusualReasonType: [{ required: true, message: '请选择', trigger: 'blur' }] // 异常原因
      },
      unusualReasonTypeList: [] // 异常原因下拉菜单选项
    }
  },
  created() {
    // 字典信息查询的key 异常原因
    this.getGroupUnusualReasonType()
    this.initData()
  },
  methods: {
    getGroupUnusualReasonType() {
      const data = ['unusualReasonType']
      this.$store.dispatch('getGroupType', data).then(res => {
        this.unusualReasonTypeList = res.bidTypeData.unusualReasonTypeList
      })
    },
    // 获取终止招标信息
    initData() {
      const stopbiddingBulletinId = sessionStorage.getItem('bizId') // 终止招标公告id
      // 判断是否有终止招标公告id
      if (stopbiddingBulletinId) {
        this.upLoadData.bizId = sessionStorage.getItem('stopbiddingRecordId')
        getStopbiddingRecordInfo(stopbiddingBulletinId).then(res => {
          this.formData = res.data
          this.formData.tenderId = this.tenderId // 项目id
        }).catch(() => {})
      } else {
        this.formData.stopbiddingRecordId = randomData()// 随机生成终止招标记录id
        this.upLoadData.bizId = this.formData.stopbiddingRecordId
      }
    },
    // 保存下一步
    saveNextStep(formName) {
      if (!this.$refs.packComp.isCheck()) {
        return // 校验是否选择标段（包）
      }
      // 校验输入内容
      this.$refs[formName].validate((valid, field) => {
        if (valid) { // 校验通过
          saveStopbiddingRecord(this.formData).then(response => {
            this.$router.push({
              path: '/bidding/projectDrive/stopbiddingRecord/makeStopbiddingRecordAdd',
              query: {
                ...this.$route.query
              }
            })
            sessionStorage.setItem('stopbiddingRecordId', this.formData.stopbiddingRecordId) // 终止招标记录id
            this.confirmLoading = false// btnLoding
          }).catch(() => {
            this.confirmLoading = false// btnLoding
          })
        }
      })
    },
    back() {
      this.$router.push({
        path: '/bidding/projectDrive/stopbiddingRecord/stopbiddingRecordList',
        query: {
          ...this.$route.query
        }
      })
    }
  }
}
</script>

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