<template>
  <el-dialog title="选择用户进行添加" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false">
    <div>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"  label-width="68px">
          <el-form-item label="登录账号" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入登录账号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="真实姓名" prop="realName">
            <el-input
              v-model="queryParams.realName"
              placeholder="请输入真实姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <!-- <el-table-column type="index" width="50" align="center" :index="customIndex" /> -->
          <el-table-column label="登录账号" align="center" key="userName" prop="userName" min-width="100" :show-overflow-tooltip="true" />
          <el-table-column label="真实姓名" align="center" key="realName" prop="realName" min-width="100" :show-overflow-tooltip="true" />
          <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" min-width="100" :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" min-width="120" />
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button v-loading="saving" type="primary" @click="submitForm" >确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listUser, updateBroker,listByNotBroker } from "@/api/system/user";

export default {
  name: 'NoBrokerUser',
  data() {
    return {
      open: false,
      loading: false,
      saving: false,
      userList: [],
      list: [],
      total: 0,
      queryParams: { pageNum: 1, pageSize: 10, hasBroker: '0' },
      currentBrokerId: null,
    }
  },
  methods: {
    show(brokerId) {
      this.currentBrokerId = brokerId;
      this.list = [];
      this.open = true;
      this.resetQuery();
    },
    cancel() {
      this.open = false;
    },
    getList() {
      this.loading = true;
      listByNotBroker(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.loading = false;
      });
    },
    handleSelectionChange(val) {
      this.list = val;
    },
     /** 搜索按钮操作 */
     handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = { pageNum: 1, pageSize: 10, hasBroker: '0' };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    submitForm() {
      if (!this.currentBrokerId || this.list.length === 0) {
        return;
      }
      this.saving = true;
      updateBroker({
        brokerId: this.currentBrokerId,
        userIds: this.list.map(item => item.userId)
      }).then(response => {
        this.saving = false;
        this.$modal.msgSuccess("添加成功");
        this.open = false;
        this.$emit('success');
      }).catch(e => {
        this.saving = false;
      });
    }
  }
}
</script>

<style>

</style>
