<!--
  @description:项目设置-选择操作用户弹框
  @Modifier: haofengxian
  @routerPath:
  @date: 2020-6-8-15:30
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    title="选择操作者名称"
    width="840px"
    custom-class="el-dialog__body-p8 el-dialog_table"
  >
    <!--搜索-->
    <div class="clearfix">
      <el-form ref="searchForm" :model="searchForm" inline>
        <el-form-item label="联系人姓名" prop="realName">
          <el-input v-model.trim="searchForm.realName" placeholder="联系人姓名" />
        </el-form-item>
        <el-form-item label="部门名称" prop="departName">
          <el-input v-model.trim="searchForm.departName" placeholder="部门名称" />
        </el-form-item>
        <el-form-item class="fr">
          <el-button
            type="primary"
            @click="search('searchForm')"
          >
            搜 索
          </el-button>
          <el-button
            plain
            type="info"
            @click="resetForm('searchForm')"
          >
            重 置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--查询列表-->
    <div>
      <el-table
        ref="singleTable"
        v-loading="loading"
        :data="list"
        class="pointer"
        size="mini"
        @selection-change="currentChange"
      >
        <el-table-column
          type="selection"
          width="45"
        />
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="80"
        />
        <el-table-column
          label="用户名"
          prop="realName"
          width="200"
        />
        <el-table-column
          label="部门名称"
          prop="departName"
        />
      </el-table>
    </div>
    <!--底部操作按钮-->
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="ensure">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
import { listDistributionUserList, saveTenderProjectUser } from '@/api/bidding/biddingProject'
/* 公用方法*/
import { filterObj } from '@/assets/js/utils'// 过滤空的key
/* 混合开发*/
import { ListMixin } from '@/mixins/ListMixin'
export default {
  name: 'BidProjectUserSettingSelect',
  mixins: [ListMixin],
  data() {
    return {
      loading: true,
      dialogVisible: false, // 弹框是否打开状态
      tenderId: this.$route.query.id, // 项目Id
      // 搜索信息
      searchForm: {
        realName: '', // 真实名字
        departName: ''// 名称
      },
      list: [], // 列表数据
      currentData: [], // 当前选择的信息
      userIdList: [] // 已经添加的用户id
    }
  },
  methods: {
    // 打开弹框加载数据（父组件会触发此方法）
    openDialogVisible(userIdList) {
      this.searchForm.realName = ''
      this.searchForm.departName = ''
      this.userIdList = userIdList
      // 加载数据
      this.loadData()
      // 打开弹框
      this.dialogVisible = true
    },
    // 当前选择的信息
    currentChange(val) {
      this.currentData = val
    },
    // 加载数据
    loadData() {
      const params = this.getParams()// 查询条件
      params.userIdList = this.userIdList
      listDistributionUserList(params).then(response => {
        this.list = response.data
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 查询条件
    getParams() {
      const param = Object.assign({}, this.searchForm)
      return filterObj(param)// 过滤空的key
    },
    // 选择信息-确定
    ensure() {
      if (this.currentData.length > 0) {
        const params = {
          tenderId: this.tenderId,
          saveTenderProjectBaseUserDtoList: this.currentData
        }
        saveTenderProjectUser(params).then(res => {
          this.dialogVisible = false
          // 保存成功触发子组件获取列表数据
          this.$emit('loadData')
        }).catch(() => {
        })
      } else {
        this.$message({
          message: '请选择用户',
          type: 'warning'
        })
      }
    },
    // 搜索重置
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.loadData()
    },
    // 搜索
    search() {
      this.loadData()
    }
  }
}
</script>

<style scoped lang="less">

</style>
