<!--
  @description: 新增招标计划--招标人信息
  @author: sunxinman
  @routerPath: /tenderPlan/tenderPlanAdd/tendereeInfo
  @date: 2019-12-23 22:35:58
-->
<template>
  <div>
    <el-form ref="form" :model="formData">
      <!--=======================================================招标人信息集合=======================================================-->
      <div class="mb10">
        <el-button type="primary" @click="addTenderee">
          添加招标人
        </el-button>
        <el-button @click="delTenderee">
          删除
        </el-button>
      </div>
      <div>
        <el-table
          ref="smallTable"
          v-loading="loading"
          :data="tableData"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            align="center"
          />
          <el-table-column
            label="序号"
            width="60"
            type="index"
            align="center"
          />
          <el-table-column
            label="招标人名称"
            prop="bidderName"
            header-align="center"
            align="left"
          />
          <el-table-column
            label="招标人代码"
            prop="bidderNo"
            header-align="center"
            align="center"
            width="200"
          />
        </el-table>
      </div>
    </el-form>
    <!-- 保存按钮 -->
    <div class="but_footer">
      <div class="but_footer-h">
        <div class="but_footer-but">
          <el-button @click="prevForm">
            上一步
          </el-button>
          <el-button :loading="confirmLoading" @click="submitForm('form', '01')">
            保存
          </el-button>
          <el-button :loading="confirmLoading" type="primary" @click="submitForm('form', '00')">
            下一步
          </el-button>
        </div>
      </div>
      <tendereeInfoDialog ref="supplierDialog" @buyersInfo="subsidiaryOrganInfo" />
    </div>
  </div>
</template>
<script>
import { saveBatchPretenderingPurchasesBidder, getPretenderingPurchasesBidderBySchemeId, removeBatchPretenderingPurchasesBidderByIds } from '@/api/purchasePlan/pretcloudPurchasescheme'
/* 组件*/
import tendereeInfoDialog from './tendereeInfo-dialog'
export default {
  name: 'TenderPlanAdd',
  components: {
    tendereeInfoDialog
  },
  props: {
    // 接收步骤条父页面信息
    tenderPlanData: {
      type: Object,
      default: function() {
        return {
          id: ''
        }
      }
    }
  },
  data() {
    return {
      loading: true,
      confirmLoading: false, // 暂存Loding
      formData: {
        pretPurchaseschemePretenderingPurchasesBidderDtoList: [] // 招标人信息集合
      },
      tableData: [],
      multipleSelection: [] // 供应商列表已勾选删除信息
    }
  },
  mounted() {
    this.getPretenderingPurchasesBidder()
  },
  methods: {
    // 初始化 获取数据
    getPretenderingPurchasesBidder() {
      getPretenderingPurchasesBidderBySchemeId(this.tenderPlanData.id).then(res => {
        this.loading = false
        if (res.data) {
          this.tableData = [...res.data]
        }
      }).catch(() => {
        this.loading = false
      })
    },
    // 表格复选框事件
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 删除
    delTenderee() {
      if (this.multipleSelection.length > 0) {
        this.$confirm('确认删除选中记录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          this.handleRemoveSupplier()
        }).catch(() => {
        })
      } else {
        this.$message({
          type: 'warning',
          message: '暂未选择招标人'
        })
      }
    },
    // 获取已选供应商（有schemeId 表示已经存储过的）
    getSelectSupplier() {
      const data = []
      this.multipleSelection.map((item) => {
        if (item.schemeId) {
          data.push(item.id)
        }
      })
      return data
    },
    // 处理删除供应商方法
    handleRemoveSupplier() {
      const ids = this.getSelectSupplier()
      const _this = this // this指向
      if (ids.length > 0) {
        const params = { ids: ids }
        removeBatchPretenderingPurchasesBidderByIds(params).then((res) => {
          this.$message.success(res.header.msg)
          _this.deleteSelection(_this) // 前端-删除已选中的标段
        })
      } else {
        // 当前选中标段的schemeId都为空时调用
        this.deleteSelection(_this) // 前端-删除已选中的标段
      }
    },
    deleteSelection(_this) {
      _this.multipleSelection.map((item) => {
        _this.tableData.splice(this.tableData.lastIndexOf(item), 1)
      })
      _this.$message({
        message: '删除成功',
        type: 'success'
      })
    },
    // 上一步
    prevForm() {
      this.$emit('stepPage', 'prev')
    },
    // 暂存 下一步
    submitForm(formName, type) {
      this.tableData.forEach(val => {
        val.schemeId = this.tenderPlanData.id
      })
      this.formData.pretPurchaseschemePretenderingPurchasesBidderDtoList = [...this.tableData]
      if (this.tableData.length > 0) {
        this.confirmLoading = true// btnLoding
        saveBatchPretenderingPurchasesBidder(this.formData).then(() => {
          if (type === '00') {
            this.$emit('stepPage', 'next')
          } else {
            this.$message({
              type: 'success',
              message: '保存成功'
            })
            this.confirmLoading = false// btnLoding
          }
        }).catch(() => {
          this.confirmLoading = false// btnLoding
        })
      } else {
        this.$message({
          type: 'warning',
          message: '请选择招标人'
        })
      }
    },
    // 添加供应商行数据
    addTenderee() {
      // 取出已选数据id 传回去重
      const ids = []
      this.tableData.map((item) => {
        ids.push(item.id)
      })
      // 10采购人 11代理机构(项目经理)
      this.$refs.supplierDialog.showBuyersDialog('10', ids)
    },
    // 供应商信息
    subsidiaryOrganInfo(val) {
      this.tableData.push(...val)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
