<template>
  <div class="app-container">
    <el-row>
      <!--左半部分-->
      <el-col :span="5">
        <div style="width:90%; height:95vh; overflow-y:auto">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>单位列表</span>
            </div>
            <el-tree
              v-loading = "treeLoading"
              ref="tree2"
              :data="deptTree"
              :props="defaultProps"
              :default-expand-all="expandAllNode"
              :expand-on-click-node="expandNodeClick"
              class="filter-tree"
              @node-click = "handleNodeClick"
            />
          </el-card>
        </div>
      </el-col>
      <!--右半部分-->
      <el-col :span="19">
        <!--筛选条件-->
        <div class="search-div">
          <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
            <el-row>
              <el-form-item class="selectForm-container-item" prop="keywords">
                <el-input v-model.trim="listQuery.keywords" placeholder="姓名/手机号" clearable/>
              </el-form-item>
              <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            </el-row>
          </el-form>
        </div>
        <!--查询结果Table显示-->
        <el-table v-loading="listLoading" :data="list" class="table" border stripe>
          <!--<el-table-column-->
          <!--type="index"-->
          <!--width="55"/>-->
          <el-table-column v-for="column in columns" :key="column.value" :label="column.text" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row[column.value] }}
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <div class="pagination-container">
          <el-pagination
            v-show="total>listQuery.limit"
            :current-page="listQuery.page"
            :page-sizes="[20,30,50]"
            :page-size="listQuery.limit"
            :total="total"
            align="center"
            layout="total, sizes, prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </el-col>
    </el-row>
    <!--编辑用户的对话框-->
    <edit-user v-show="editShow" ref="edituser" @watchChild="fetchData"/>
    <role-assign v-show="roleAssignShow" ref="roleassign" @watchChild="fetchData"/>
  </div>
</template>

<script>
import request from '@/utils/request'
import { getDeptTreeList } from '@/api/system/dept'
import { toTreeList } from '@/utils/structure'

export default {
  name: 'MobileList',
  data() {
    return {
      radio: '',
      password: '111111',
      listQuery: {
        keywords: '',
        deptid: '',
        page: 1,
        limit: 20,
        sort: 'id'
      },
      columns: [
        {
          text: '真实姓名',
          value: 'name',
          width: 50
        },
        {
          text: '所在单位',
          value: 'deptName',
          width: 150
        },
        {
          text: '手机号',
          value: 'phone'
        },
        {
          text: '账户',
          value: 'account'
        }
      ],
      list: [],
      total: 0,
      deptTree: null,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      treeLoading: false,
      listLoading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      editShow: false, // 编辑组件是否显示
      roleAssignShow: false, // 角色分配组件是否显示
      expandAllNode: true,
      expandNodeClick: false
    }
  },
  created() {
    this.fetchDeptTree()
    this.fetchData()
  },
  methods: {
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取用户数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }

      request({
        url: 'mgr/mobileAllListPage',
        method: 'post',
        params: this.listQuery
      }).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },

    // 获取组织结构树
    fetchDeptTree() {
      this.treeLoading = true
      getDeptTreeList(this.listQuery).then(response => {
        this.deptTree = toTreeList(response.data.list, '0', true)
        this.treeLoading = false
      })
    },
    // 点击左侧组织结构项触发
    handleNodeClick(data) {
      this.listQuery.deptid = data.id
      this.fetchData()
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    },
    // 在嵌套列表list中删除指定元素
    deleteItem(list, des) {
      list.forEach((value, index) => {
        if (value.id === des.id) {
          list.splice(index, 1)
        } else {
          if (value.children && value.children.length > 0) {
            this.deleteItem(value.children, des)
          }
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .form-container{
    margin-bottom: 20px;
  }
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
  .el-date-editor{
    .el-range-separator{
      width: 7% !important;
    }
  }
  .el-range-separator{
    width: 7% !important;
  }
</style>
