<!--
  @description:新增重新招标
  @Modifier: haofengxian
  @routerPath:/bidding/projectDrive/rebiddingRecord/rebiddingRecordAdd
  @date: 2020-5-13-10:42
-->
<template>
  <div class="main">
    <el-form
      ref="fromData"
      :rules="rules"
      :model="formData"
      :inline-message="true"
    >
      <div class="page-title">
        1.基本信息
      </div>
      <table class="from_table">
        <tr class="singleRow">
          <td>
            <el-form-item label="标段（包）" required />
          </td>
          <td colspan="3">
            <rebiddingRecord-pack-component
              ref="packComp"
              :select-pack-list="selectPackList"
              :rebidding-record-id="rebiddingRecordId"
              @getRebiddingRecordList="getRebiddingRecordList"
            />
          </td>
        </tr>
        <!--        <tr class="singleRow">
          <td>
            <el-form-item label="重新制作招标文件" prop="isChangeDoc"/>
          </td>
          <td colspan="3">
            <el-radio-group v-model="formData.isChangeDoc ">
              <el-radio label="01">是</el-radio>
              <el-radio label="00">否</el-radio>
            </el-radio-group>
          </td>
        </tr>-->
        <tr class="singleRow">
          <td>
            <el-form-item label="重新招标原因" />
          </td>
          <td colspan="3">
            <gx-input
              v-model="formData.rebiddingRecord"
              :autosize="{ minRows: 6, maxRows: 6}"
              gx-maxlength="varchar1000"
              type="textarea"
              placeholder="请输入内容"
            />
          </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>
      </table>
    </el-form>
    <div class="page-title">
      2.沿用投标人
    </div>
    <div>
      <el-table
        v-loading="loading"
        :data="packBidderList"
        width="100%"
      >
        <el-table-column
          type="index"
          label="序号"
          width="60px"
          align="center"
        />
        <el-table-column
          header-align="center"
          label="标段（包）编号"
          prop="packNo"
          align="left"
          min-width="200"
        />
        <el-table-column
          label="购标投标人家数"
          header-align="center"
          prop="buydocnumber"
          min-width="230"
        />
        <el-table-column
          label="沿用投标人家数"
          header-align="center"
          prop="usersuppliernumber"
          min-width="230"
        />
        <el-table-column
          header-align="center"
          label="操作"
          width="200"
        >
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="selectBidder(scope.row, scope.$index)"
            >
              [选择投标人]
            </el-link>
            <el-link
              type="primary"
              @click="selectedBidder(scope.row, scope.$index)"
            >
              [已选投标人]
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--保存按钮-->
    <div class="but_footer">
      <div class="but_footer-h" />
      <div class="but_footer-but">
        <el-button :loading="confirmLoading" type="primary" @click="saveSubmitRebiddingRecord('00')">
          保存
        </el-button>
        <el-button :loading="confirmLoading" type="primary" @click="saveSubmitRebiddingRecord('01')">
          提交
        </el-button>
        <el-button :loading="confirmLoading" @click="back">
          返 回
        </el-button>
      </div>
    </div>
    <!--沿用投标人弹框-->
    <follow-bidder-list-dialog ref="followBidder" @supplierSelect="supplierSelect" />
    <selected-follow-bidder-list-dialog ref="selectedFollowBidder" @supplierSelect="supplierSelect" />
  </div>
</template>

<script>
/* 接口*/
import { saveRebiddingRecord, getRebiddingRecordBuyerNumberList, getRebiddingRecordInfoByRebiddingRecordId } from '@api/bidding/rebiddingRecord'
/* 选择标包组件*/
import rebiddingRecordPackComponent from './rebiddingRecordPackComponent'
import followBidderListDialog from './followBidderList-dialog'
import selectedFollowBidderListDialog from './selectedFollowBidderList-dialog'
import upload from '@comp/common/updownload/upload' // 附件上传组件
/* 公用方法*/
import { cloneObject } from '@assets/js/utils'// 常用方法
/* 常用方法*/
import { randomData } from '@/assets/js/utils/index'
export default {
  name: 'RebiddingRecordAdd',
  components: {
    'rebiddingRecord-pack-component': rebiddingRecordPackComponent,
    'follow-bidder-list-dialog': followBidderListDialog, // 选择沿用投标人弹框
    'selected-follow-bidder-list-dialog': selectedFollowBidderListDialog, // 已选沿用投标人弹框
    upload// 文件上传
  },
  data() {
    return {
      confirmLoading: false, // 暂存、下一步Loding
      tenderBase: this.$store.getters.tenderBase, // 项目信息
      loading: false, // 沿用投标人列表loading
      selectPackList: [], // 获取的已选择的标段（包）
      packBidderList: [], // 已选沿用投标人列表
      rebiddingRecordId: '', // 重新招标记录id
      formData: {
        isChangeDoc: '00', // 是否变更(重新制作)文件 00:否 01:是
        isOperation: '', // 业务操作 保存:00 提交:01
        isSubmit: '', // 是否提交 00否 01是
        orderBy: '', // 排序
        // 包段阶段信息
        packInfoList: [
          // {
          //   buydocnumber: '', // 购表投标家数
          //   packId: '', // 标段id
          //   packName: '', // 标段名称
          //   packNo: '', // 标段编号
          //   // 前沿供应商信息
          //   rebiddingSupplierInfo: [
          //     {
          //       supplierId: '', // 供应商id
          //       supplierName: '' // 供应商名称
          //     }
          //   ],
          //   stageId: '', // 阶段id
          //   usersuppliernumber: '' // 前沿投标家数
          // }
        ],
        rebiddingRecord: '', // 重新招标原因
        rebiddingRecordId: '', // 重新招标记录id
        tenderId: this.$route.query.id // 项目id
      },
      // 上传附件参数
      upLoadData: {
        bizId: '', // 业务主键Id
        fileType: 'rebidding_exception_file_base'// 文件类型
      },
      // 沿用投标人弹框
      dialogData: {
        stageId: '', // 阶段id
        supplierIds: '', // 已选择供应商(逗号隔开)
        packIndex: '' // 标段索引,
      },
      // 校验
      rules: {
        title: [{ required: true, message: '不能为空', trigger: 'blur' }] // 标题
      }
    }
  },
  created() {
    this.initData() // 获取重新招标内容
  },
  methods: {
    // 获取重新招标
    initData() {
      this.rebiddingRecordId = sessionStorage.getItem('bizId')
      // 判断是否有重新招标Id
      if (this.rebiddingRecordId) {
        getRebiddingRecordInfoByRebiddingRecordId(this.rebiddingRecordId).then(res => {
          this.formData = res.data
          // 已选沿用投标人列表
          this.packBidderList = this.formData.packInfoList
          // 获取的已选择的标段（包）
          this.selectPackList = this.formData.packInfoList
          this.upLoadData.bizId = this.rebiddingRecordId
        }).catch(() => {})
      } else {
        this.formData.rebiddingRecordId = randomData()// 随机生成ID
        this.upLoadData.bizId = this.formData.rebiddingRecordId
      }
    },
    /* ==============沿用投标人start======================*/
    // 获取已选标段阶段id集合
    getPackIds() {
      const stageIds = []
      this.selectPackList.map(item => {
        stageIds.push(item.stageId) // 阶段id集合
      })
      return stageIds
    },
    // 获取重新招标购标投标人家数列表
    getRebiddingRecordList() {
      // 已选标包
      this.packBidderList = this.selectPackList
      const stageIds = this.getPackIds().join(',') // 阶段id集合
      getRebiddingRecordBuyerNumberList(stageIds).then(res => {
        const { data } = res
        this.packBidderList.map((item, index) => {
          /*
          * 组装数据
          * !item.usersuppliernumber 表示新添加的标段信息 未选择沿用投标家数
          * */
          if (!item.usersuppliernumber) {
            /* 重新招标购标投标人家数有数据 则取出购标投标人家数 否则添加空字段*/
            if (data && data.length > 0) {
              item.buydocnumber = data[index].buydocnumber // 购标投标人家数
            } else {
              item.buydocnumber = '' // 购标投标人家数
            }
            item.rebiddingSupplierInfo = [] // 前沿供应商信息
            item.usersuppliernumber = '' // 前沿投标家数
            this.packBidderList.push(...item)
          }
        })
      }).catch(() => {
      })
    },
    // 选择投标人
    selectBidder(row, index) {
      this.dialogData.stageId = row.stageId // 阶段id
      this.dialogData.packIndex = index // 标段索引
      const supplierIds = []
      if (row.rebiddingSupplierInfo) {
        row.rebiddingSupplierInfo.map((item) => {
          supplierIds.push(item.supplierId)
        })
      }
      this.dialogData.supplierIds = supplierIds.join(',') // 已选投标人id
      this.$refs.followBidder.openDialog(this.dialogData)
    },
    // 已选沿用投标人
    supplierSelect(data, index, status) {
      if (status === '01') { // 添加供应商
        this.packBidderList[index].rebiddingSupplierInfo = [
          ...this.packBidderList[index].rebiddingSupplierInfo,
          ...data
        ] // 前沿供应商信息
      } else {
        // 查看已选投标人
        this.packBidderList[index].rebiddingSupplierInfo = data // 前沿供应商信息
      }
      // 沿用投标人家数
      this.$set(this.packBidderList[index], 'usersuppliernumber', this.packBidderList[index].rebiddingSupplierInfo.length)
      /* 更新视图*/
      const selectData = cloneObject(this.packBidderList[index])
      this.packBidderList.splice(index, 1, selectData)
    },
    // 查看已选投标人
    selectedBidder(row, index) {
      if (row.rebiddingSupplierInfo) {
        const rowData = {
          isShowRemoveBtn: true, // 是否显示移除按钮
          packIndex: index, // 标段索引
          rebiddingSupplierInfo: row.rebiddingSupplierInfo // 已选投标人信息
        }
        this.$refs.selectedFollowBidder.openDialog(rowData)
      }
    },
    /* ==============沿用投标人end======================*/
    // 调用接口保存/提交
    saveSubmitRebiddingRecord(type) {
      if (!this.$refs.packComp.isCheck()) {
        return // 校验是否选择标段（包）
      }
      this.formData.packInfoList = this.packBidderList
      this.formData.isOperation = type // 业务操作 保存:00 提交:01
      this.formData.isSubmit = type // 是否提交 00否 01是
      let flag = true
      if (type === '01') { // 提交,校验是否沿用供应商,没有沿用给提示
        this.packBidderList.forEach(item => {
          const rebiddingSuppliers = item.rebiddingSupplierInfo
          if (rebiddingSuppliers.length === 0) {
            flag = false
          }
        })
        if (!flag) {
          this.$confirm('标段未沿用投标人,是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            confirmButtonClass: 'confirmBtn',
            cancelButtonClass: 'cancelBtn',
            type: 'warning'
          }).then(() => {
            this.saveRebidRecord(type)
          }).catch(() => { })
        } else {
          this.saveRebidRecord(type)
        }
      } else {
        this.saveRebidRecord(type)
      }
    },
    saveRebidRecord(type) {
      // 校验输入内容
      this.$refs.fromData.validate((valid, field) => {
        if (valid) { // 校验通过
          saveRebiddingRecord(this.formData).then(response => {
            // 保存
            if (type === '00') {
              this.$message({
                type: 'success',
                message: '保存成功'
              })
            } else {
              this.$message({
                type: 'success',
                message: '提交成功'
              })
              this.$router.push({
                path: '/bidding/projectDrive/rebiddingRecord/rebiddingRecordList',
                query: {
                  ...this.$route.query
                }
              })
            }
            this.clarificationParam.clarificationId = randomData()// 随机生成ID
            this.confirmLoading = false// btnLoding
          }).catch(() => {
            this.confirmLoading = false// btnLoding
          })
        }
      })
    },
    back() {
      this.$router.push({
        path: '/bidding/projectDrive/rebiddingRecord/rebiddingRecordList',
        query: {
          ...this.$route.query
        }
      })
    }
  }
}
</script>

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