<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" clearable placeholder="请输入部门名称" prefix-icon="el-icon-search" size="small"
                    style="margin-bottom: 20px"/>
        </div>
        <div class="head-container">
          <el-tree ref="tree" :data="deptOptions" :expand-on-click-node="false"
                   :filter-node-method="filterNode"
                   :props="defaultProps" default-expand-all highlight-current @node-click="handleNodeClick"/>
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px"
                 size="small">
          <el-form-item label="用户名称" prop="username">
            <el-input v-model="queryParams.username" clearable placeholder="请输入用户名称" style="width: 240px"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="userList" highlight-current-row @current-change="handleCurrentChange">
          <el-table-column v-if="columns[0].visible" key="id" align="center" label="用户编号" prop="id"/>
          <el-table-column v-if="columns[1].visible" key="username" :show-overflow-tooltip="true" align="center"
                           label="用户名称"
                           prop="username"/>
          <el-table-column v-if="columns[2].visible" key="nickname" :show-overflow-tooltip="true" align="center"
                           label="用户昵称"
                           prop="nickname"/>
          <el-table-column v-if="columns[3].visible" key="deptName" :show-overflow-tooltip="true" align="center"
                           label="部门"
                           prop="dept.name"/>
        </el-table>

        <pagination v-show="total>0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNo" :total="total"
                    @pagination="loadData"/>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import {listUser, listSimpleDepts, listSimplePosts} from "../../api/system";

export default {
  name: "UserChoose",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 部门树选项
      deptOptions: undefined,
      // 部门名称
      deptName: undefined,
      // 岗位选项
      postOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "name"
      },
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        username: undefined,
        deptId: undefined,
      },
      // 列信息
      columns: [
        {key: 0, label: `用户编号`, visible: true},
        {key: 1, label: `用户名称`, visible: true},
        {key: 2, label: `用户昵称`, visible: true},
        {key: 3, label: `部门`, visible: true},
      ],
      currentRow: {}
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.loadData();
    this.getTreeselect();
  },
  methods: {
    /** 查询用户列表 */
    loadData() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
          this.userList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    getData() {
      return {value: this.currentRow.id, label: this.currentRow.username}
    },
    handleCurrentChange(row) {
      this.currentRow = row;
    },
    /** 查询部门下拉树结构 + 岗位下拉 */
    getTreeselect() {
      listSimpleDepts().then(response => {
        // 处理 deptOptions 参数
        this.deptOptions = [];
        this.deptOptions.push(...this.handleTree(response.data, "id"));
      });
      listSimplePosts().then(response => {
        // 处理 postOptions 参数
        this.postOptions = [];
        this.postOptions.push(...response.data);
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.loadData();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.loadData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },


  }
};
</script>
