<!--
  @description: 新增招标计划--供应商信息
  @author: sunxinman
  @routerPath: /tenderPlan/tenderPlanAdd/supplierInfo
  @date: 2019-12-23 22:35:58
-->
<template>
  <div>
    <el-form ref="form" :model="formData" label-width="160px">
      <!--=======================================================供应商信息集合=======================================================-->
      <div class="mb10">
        <el-button type="primary" @click="addSupplierTr">
          添加供应商
        </el-button>
        <el-button @click="delSupplier">
          删除
        </el-button>
        <el-select v-model="purcatalogCateoryCode" filterable placeholder="请选择" style="margin-left: 16px">
          <el-option
            v-for="item in categoryOptions"
            :key="item.id"
            :label="item.categoryName"
            :value="item.categoryCode"
          />
        </el-select>
      </div>
      <div>
        <el-table
          ref="supplier"
          v-loading="loading"
          :data="tableData"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            fixed
            type="selection"
            align="center"
          />
          <el-table-column
            fixed
            type="index"
            label="序号"
            align="center"
            width="60"
          />
          <el-table-column
            fixed
            label="供应商编号"
            width="160"
            prop="supplierNo"
            header-align="center"
          />
          <el-table-column
            fixed
            label="供应商名称"
            min-width="230"
            prop="supplierName"
            header-align="center"
          />
          <el-table-column
            label="注册资本金（元）"
            width="150"
            prop="registeredCapital"
            header-align="center"
          />
          <el-table-column
            label="法定代表人"
            width="120"
            prop="legalRepresentative"
            header-align="center"
          />
          <el-table-column
            label="注册地址"
            min-width="230"
            prop="registeredAddress"
            header-align="center"
          />
          <el-table-column
            label="企业性质"
            width="120"
            prop="enterpriseNature"
            header-align="center"
          />
          <el-table-column
            label="企业性质分类"
            width="120"
            prop="enterpriseNatureClass"
            header-align="center"
          />
          <el-table-column
            label="上市情况"
            width="120"
            prop="listingInfo"
            header-align="center"
          />
          <el-table-column
            label="上市代码"
            width="120"
            prop="listingCode"
            header-align="center"
          />
          <el-table-column
            label="股权结构"
            width="120"
            prop="ownershipStructure"
            header-align="center"
          />
          <el-table-column
            label="供应商资质"
            width="180"
            prop="supplierQualification"
            header-align="center"
          />
          <el-table-column
            label="近5年来类似业绩"
            width="120"
            prop="recentFiveYearsSimilarAchievement"
            header-align="center"
          />
          <el-table-column
            label="联系人"
            width="120"
            prop="contacts"
            header-align="center"
          />
          <el-table-column
            label="联系方式"
            width="120"
            prop="contactInfo"
            header-align="center"
          />
          <el-table-column
            label="其他"
            width="120"
            prop="othersInfo"
            header-align="center"
          />
        </el-table>
      </div>
      <!-- 保存按钮 -->
      <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>
      </div>
    </el-form>
    <supplierDialog ref="supplierDialog" @buyersInfo="subsidiaryOrganInfo" />
  </div>
</template>
<script>
import supplierDialog from './supplier-dialog'
import { saveBatchPretenderingPurchasesSupplier, getPretenderingPurchasesSupplierById, removeBatchPretenderingPurchasesSupplierByIds } from '@/api/purchasePlan/pretcloudPurchasescheme'
import { queryQualificationCateoris } from '@/api/bidding/bidCalibration'
export default {
  name: 'TenderPlanAdd',
  components: {
    supplierDialog
  },
  props: {
    // 接收步骤条父页面信息
    tenderPlanData: {
      type: Object,
      default: function() {
        return {
          id: ''
        }
      }
    }
  },
  data() {
    return {
      loading: true,
      confirmLoading: false, // 暂存Loding
      formData: {
        pretPurchaseschemePretenderingPurchasesSupplierDtoList: []// 供应商信息集合
      },
      tableData: [],
      multipleSelection: [], // 供应商列表已勾选删除信息
      purcatalogCateoryCode: '', // 分类编码/分类名称 模糊收索使用
      categoryOptions: [] // 获取资审类型信息
    }
  },
  mounted() {
    this.getSupplierList()
    this.getQualificationCateoris()
  },
  methods: {
    // 获取资审类型信息
    getQualificationCateoris() {
      queryQualificationCateoris().then(res => {
        this.categoryOptions = res.data
      })
    },
    //  根据id获取供应商信息集合
    getSupplierList() {
      const id = this.tenderPlanData.id
      getPretenderingPurchasesSupplierById(id).then(res => {
        this.loading = false
        if (res.data) {
          this.tableData = [...res.data]
        }
      }).catch(() => {
        this.loading = false
      })
    },
    // 删除
    delSupplier() {
      if (this.multipleSelection.length > 0) {
        this.$confirm('确认删除选中记录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          this.handleRemoveSupplier()
        }).catch(() => {
        })
      } else {
        this.$message.warning('暂未选择供应商')
      }
    },
    // 供应商表格复选框事件
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 获取已选供应商（有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 }
        removeBatchPretenderingPurchasesSupplierByIds(params).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          _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.map(val => {
        val.schemeId = this.tenderPlanData.id
      })
      this.formData.pretPurchaseschemePretenderingPurchasesSupplierDtoList = [...this.tableData]
      if (this.tableData.length) {
        this.confirmLoading = true
        saveBatchPretenderingPurchasesSupplier(this.formData).then(() => {
          if (type === '00') {
            this.$emit('stepPage', 'next')
          } else {
            this.$message({
              type: 'success',
              message: '保存成功'
            })
          }
          this.confirmLoading = false
        }).catch(() => {
          this.confirmLoading = false
        })
      } else {
        this.$message({
          type: 'warning',
          message: '请先添加供应商'
        })
      }
    },
    // 添加供应商行数据
    addSupplierTr() {
      if (this.purcatalogCateoryCode) {
        // 取出已选数据id 传回去重
        const ids = []
        this.tableData.map((item) => {
          ids.push(item.id)
        })
        this.$refs.supplierDialog.showBuyersDialog(ids, this.purcatalogCateoryCode)
      } else {
        this.$message.warning('请选择资审类型')
      }
    },
    // 供应商信息
    subsidiaryOrganInfo(val) {
      this.tableData.push(...val)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
