<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="dialogVisible"
    :title="title"
    append-to-body
  >
  <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" label-width="100px">
      <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="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 ref="table" :data="list" style="width: 100%" highlight-current-row @selection-change="handleSelectionChange"
     @select="handleSelect" @select-all="handleSelectAll" @row-click="rowClick" v-loading="loading">
      <el-table-column type="" width="50" align="center">
        <template slot-scope="scope">
          <i v-if="scope.row.userId === selectionUserId" style="color: #1890ff;" class="el-icon-circle-check"></i>
        </template>
      </el-table-column>
      <el-table-column label="登录账号" key="userName" prop="userName"
                       :show-overflow-tooltip="true" />
      <el-table-column label="所属代理商"  prop="deptName" :show-overflow-tooltip="true" width="120"/>
      <el-table-column label="用户昵称"  key="nickName" prop="nickName"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="城市"  prop="city"  />
      <el-table-column label="手机号码"  key="phonenumber" prop="phonenumber"
                       width="110"/>
    </el-table>
    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :disabled="!selectionUserId" @click="changeUser" >确认转入该经纪人</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>

</template>

<script>
import { clueChangeUserId } from "@/api/biz/clue";
import {
  listUserByType
} from "@/api/system/user";

export default {
  name: 'BrokerChange',
  props: {
    oldBroker: [String, Number]
  },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      title: '转入经纪人选择',
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: '',
        userType: "jj",
        phonenumber: '',
      },
      list: [],
      selectionUserId: ''
    }
  },
  methods: {
    show() {
      this.list = [];
      this.dialogVisible = true;
      this.getList();
    },
    cancel() {
      this.dialogVisible = false
    },
    getList() {
      this.loading = true;
      listUserByType(this.queryParams).then(response => {
        this.list = response.rows || [];
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        userName: '',
        userType: "jj",
        phonenumber: ''
      };
      this.getList();
    },
    rowClick(row) {
      this.selectionUserId = row.userId;
    },
    // handleSelectionChange(sel) {
    //   // this.selection = sel || [];

    // },
    // handleSelect(row) {
    //   this.selection.forEach(val => {
    //     this.$refs.table.toggleRowSelection(val, false);
    //   });
    //     this.selection = [row];
    //   this.$nextTick(() => {
    //     // this.$refs.table.toggleRowSelection(row, true);
    //     // this.selection.forEach(val => {
    //     //   if (row.userId !== val.userId) {
    //     //     this.$nextTick(() => {
    //     //       this.$refs.table.toggleRowSelection(val, false);
    //     //     });
    //     //   }
    //     // });
    //     // this.$nextTick(() => {
    //     //   this.selection = [row];
    //     // })
    //   });

    // },
    // handleSelectAll() {
    //   //清除选中
    //   this.$refs.table.clearSelection();
    // },
    changeUser() {

      if (this.selectionUserId && this.oldBroker) {
        clueChangeUserId({
          oldUserId: this.oldBroker,
          newUserId: this.selectionUserId,
        }).then(response => {
          this.$emit('changeUser', this.selectionUserId);
          this.cancel();
        });
      }
    },
    // getAllIds(data) {
    //   let ids = [];
    //   // 获取当前对象的 id
    //   ids.push(data.id);
    //   // 如果当前对象有 children 属性，并且 children 是一个数组
    //   if (data.children && Array.isArray(data.children)) {
    //     // 遍历 children 数组
    //     data.children.forEach(child => {
    //       // 递归调用 getAllIds 函数，获取当前子对象及其子对象的 id，并合并到 ids 数组中
    //       ids = ids.concat(this.getAllIds(child));
    //     });
    //   }
    //   return ids;
    // },
  }
}
</script>

<style>

</style>
