  <!--
  @description: 选择供应商弹框
  @Modifier: lxl
  @date: 2021-1-27 10:20:35
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.consignContractVisible"
    :close-on-click-modal="false"
    append-to-body
    title="选择供应商弹框"
    width="1100px"
    custom-class="el-dialog__body-p8 el-dialog_table"
  >
    <section class="clearfix">
      <el-form ref="searchForm" :model="searchForm" inline>
        <el-form-item label="供应商" prop="supplierName">
          <el-input
            v-model="searchForm.supplierName"
            @keyup.enter.native="search"
          />
        </el-form-item>
        <!-- <el-form-item label="SAP编码" prop="partnerNo">
          <el-input
            v-model="searchForm.partnerNo"
            @keyup.enter.native="search"
          />
        </el-form-item> -->
        <el-form-item label="统一社会信用代码" prop="businesslicence">
          <el-input
            v-model="searchForm.businesslicence"
            @keyup.enter.native="search"
          />
        </el-form-item>
        <el-form-item class="fr">
          <!-- 查询 -->
          <el-button type="primary" @click="search">
            搜 索
          </el-button>
          <el-button plain type="info" @click="resetForm">
            重 置
          </el-button>
        </el-form-item>
      </el-form>
    </section>
    <el-table
      ref="multipleTable"
      :row-key="getRowKey"
      :data="list"
      @selection-change="currentChange"
    >
      <el-table-column
        :reserve-selection="true"
        type="selection"
        width="60px"
        align="center"
      />
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="50"
      />
      <el-table-column
        prop="supplierName"
        header-align="center"
        label="供应商"
      />
      <!-- <el-table-column
        prop="partnerNo"
        label="SAP编码"
        min-width="110"
        align="center"
      /> -->
      <el-table-column
        prop="businesslicence"
        label="统一社会信用代码"
        min-width="110"
        header-align="center"
      />
      <el-table-column
        prop="linkerName"
        header-align="center"
        label="联系人"
      />
      <el-table-column
        prop="linkerTel"
        align="center"
        label="联系方式"
      />
    </el-table>
    <pagination :pagination-data="paginationData" @change="loadData" />
    <!--底部操作按钮-->
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="ensure">确 定</el-button>
      <el-button @click="dialogData.consignContractVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
// import { getDeclarationByCommonQuerySupplier } from '@/api/procurement/commonSupplier'// 获取供应信息
import { queryInvititationSupplierMessage } from '@/api/procurement/commonUser' // 获取供应信息
import { saveProcurementPackInvitatinSupplierList, saveProcurementFrameSupplierList } from '@/api/procurement/purchasplan' // 保存供应商

/* 公用方法*/
import { restoreInitialValue, filterObj } from '@/assets/js/utils'
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin'// 分页
/* 组件*/
import pagination from '@comp/common/pagination/pagination'// 分页
export default {
  name: 'SelSupplierDialog',
  components: {
    pagination// 分页
  },
  mixins: [ListMixin],
  props: {
    // 弹框展示状态
    dialogData: {
      type: Object,
      default: function() {
        return {
          purPlanType: '00' // 集采计划打包 00 框架计划 01
        }
      }
    }
  },
  data() {
    return {
      title: '项目类型',
      // 搜索信息
      searchForm: {
        businesslicence: '', // 统一社会信用代码
        supplierName: '', // 供应商名称
        partnerNo: '' // SAP编码
      },
      list: [], // 列表数据
      currentData: []// 当前选择的信息
    }
  },
  watch: {
    // 监听-弹框展示状态
    'dialogData.consignContractVisible'(val) {
      if (val) {
        // 弹框已打开
        this.$nextTick(() => {
          this.currentData = [] // 清空已选数据
          this.$refs.multipleTable.clearSelection() // 清除选中
          this.loadData() // 数据初始化
        })
      }
    }
  },
  methods: {
    // 指定一个key标识这一行的数据
    getRowKey(row) {
      return row.supplierId
    },
    // 当前选择的信息
    currentChange(val) {
      // 单选
      if (this.dialogData.single) {
        if (val.length > 1) {
          // 清除
          this.$refs.multipleTable.clearSelection()
          // pop：最后一个
          this.$refs.multipleTable.toggleRowSelection(val.pop())
        } else {
          // multipleSelection：当前选择的数组
          this.currentData = val
        }
      } else {
        // 多选
        this.currentData = val
      }
    },
    // 加载数据
    loadData() {
      const params = this.getParams()// 查询条件
      queryInvititationSupplierMessage(params).then(response => {
        this.list = response.data.records
        this.paginationData.total = response.data.total
      }).catch(() => {

      })
    },
    // 查询条件
    getParams() {
      const param = Object.assign({
        departId: this.dialogData.departId,
        excludeSupplierIds: this.dialogData.excludeSupplierIds,
        pageNo: this.paginationData.pageNo,
        pageSize: this.paginationData.pageSize
      }, this.searchForm)
      return param
    },
    // 选择信息-确定
    ensure() {
      if (this.currentData) {
        const params = {
          tenderId: this.dialogData.tenderId, // 采购单id
          tenderMethod: this.dialogData.tenderMethod, // 采购方式
          tenderType: this.dialogData.tenderType, // 采购类型
          list: this.currentData // 选择数据
        }
        // 保存选择供应商
        if (this.dialogData.purPlanType === '00') {
          // 是否联合体 00-否;01-是 联合体 && 选择联合体成员不做保存选择信息传给父组件
          if (this.dialogData.ifCombo === '01') {
            const currentDataInfo = filterObj(this.currentData)
            this.$emit('consignContractInfo', currentDataInfo, this.dialogData.supplierType)
            this.dialogData.consignContractVisible = false
          } else {
            saveProcurementPackInvitatinSupplierList(params).then(() => {
              this.$emit('consignContractInfo')
              this.dialogData.consignContractVisible = false
            }).catch(() => {
            })
          }
        } else {
          saveProcurementFrameSupplierList(params).then(() => {
            this.$emit('consignContractInfo')
            this.dialogData.consignContractVisible = false
          }).catch(() => {
          })
        }
        restoreInitialValue(this.searchForm) // 清空搜索条件
      } else {
        this.$message({
          message: `请选择${this.title}`,
          type: 'warning'
        })
      }
    },
    // 搜索
    search() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData()
    },
    // 重置
    resetForm() {
      restoreInitialValue(this.searchForm) // 清空搜索条件
      this.search()
    }
  }
}
</script>
<style scoped lang="less">
 /deep/.el-radio__label {
    display: none;
  }
</style>
