<template>
  <!-- 选择用户 -->
  <el-dialog title="选择用户" :visible.sync="visible" width="900px" top="5vh" append-to-body>
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="6" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container dept-tree">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="18" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" @submit.native.prevent>
          <el-form-item label="用户名" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户名"
              clearable
              size="small"
              style="width: 150px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="姓名" prop="nickName">
            <el-input
              v-model="queryParams.nickName"
              placeholder="请输入姓名"
              clearable
              size="small"
              style="width: 150px"
              @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  @row-click="clickRow" ref="table" :data="userList"  height="260px" highlight-current-row>
          <el-table-column label="单选" width="50" align="center">
            <template slot-scope="scope">
              <el-radio class="radio" v-model="nowIndex" :label="scope.row.userId">{{ '' }}</el-radio>
            </template>
          </el-table-column>
          <el-table-column label="用户名" align="center"  prop="userName"  width="150"/>
          <el-table-column label="姓名" align="center"  prop="nickName"/>
          <el-table-column label="部门" align="center" prop="dept.deptName"  />
          <el-table-column label="手机号码" align="center" prop="phonenumber"/>
        </el-table>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectUser">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listUserSelect, listUserSelectByPermi } from '@/api/system/user'
import { listDeptSelect, listDeptSelectByPermi} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: {
    Treeselect
  },
  data() {
    return {
      hasPermi: false,
      // 遮罩层
      visible: false,
      // 选中值,
      user: undefined,
      // 用户表格数据
      userList: null,
      // 部门树选项
      deptOptions: undefined,
      // 部门名称
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        userName: undefined,
        nickName: undefined,
        deptId: undefined
      },
      // 当前选择的单项框值
      nowIndex: "",
      selectUser: undefined
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    // 显示弹框
    show(hasPermi, selectUser) {
      this.hasPermi = hasPermi || false
      this.selectUser = selectUser
      this.getTreeselect();
      this.resetQuery();
      this.visible = true;
    },
    //单击行可选中
    clickRow(row) {
      this.nowIndex = row.userId;
      this.user = row;
    },
    /** 确认按钮操作 */
    handleSelectUser() {
      this.visible = false;
      this.$emit("ok");
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      if (this.hasPermi){
        listUserSelectByPermi(this.queryParams).then(response => {
            this.userList = response.data;
            this.loading = false;
            this.$nextTick(() => {
              if (this.selectUser) {
                this.userList.forEach(row => {
                  if (this.selectUser.userId === row.userId){
                    this.$refs.table.setCurrentRow(row);
                    this.nowIndex = row.userId
                    this.user = row
                  }
                });
              }
            })
          }
        );
      }else{
        listUserSelect(this.queryParams).then(response => {
            this.userList = response.data;
            this.loading = false;
            this.$nextTick(() => {
              if (this.selectUser) {
                this.userList.forEach(row => {
                  if (this.selectUser.userId === row.userId){
                    this.$refs.table.setCurrentRow(row);
                    this.nowIndex = row.userId
                    this.user = row
                  }
                });
              }
            })
          }
        );
      }

    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      if (this.hasPermi){
        listDeptSelectByPermi().then(response => {
          this.deptOptions = response.data;
        });
      }else{
        listDeptSelect().then(response => {
          this.deptOptions = response.data;
        });
      }

    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.nowIndex = '';
      this.user = undefined;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.handleQuery();
    }
  }
};
</script>
<style  lang="scss"  scoped>
.el-tree {
  min-width: 100%;
  display: inline-block;
}
.dept-tree {
  overflow:auto;
  max-height: 280px;
}
</style>
