<template>
  <div  >
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :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">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="true" :filter-node-method="filterNode" ref="tree"  @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="员工姓名" prop="nickName">
            <el-input v-model="queryParams.nickName" placeholder="请输入员工姓名" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号码" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="员工状态" clearable size="small" style="width: 240px">
              <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
            </el-select>
          </el-form-item> -->
          <el-form-item label="创建时间">
            <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button  icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button icon="el-icon-add" size="mini" @click="addUser">添加员工</el-button>
          </el-form-item>
        </el-form>
        <div  class="tags">
          <el-tag closable @close="delUser(index)" style="margin-bottom:5px;" v-for="(item,index) in users"  >{{item.nickName}}</el-tag>
        </div>
        <el-table  ref="multipleTable"  v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="ID" align="center" prop="userId" :show-overflow-tooltip="true" />
          <el-table-column label="员工编号" align="center" prop="userName" :show-overflow-tooltip="true" />
          <el-table-column label="员工姓名" align="center" prop="nickName" :show-overflow-tooltip="true" />
          <el-table-column label="部门" align="center" prop="dept.deptName" :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="center" prop="phonenumber" width="120" />
          <el-table-column label="今日分配客户数" align="center" prop="nowDayCustomerCount"   />
          <el-table-column label="创建时间"  sortable align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <div class="btns">
          <el-button type="primary" @click="submit"  >确 定</el-button>
        </div>
        </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  getSelectUserList,
  getUser,
  delUser,
  addUser,
  updateUser,
  exportUser,
  resetUserPwd,
  changeUserStatus,
  importTemplate,
} from "@/api/company/companyUser";
import { treeselect } from "@/api/company/companyDept";
export default {
  name: "User",

  data() {
    return {
      selectUser:[],
      users:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 部门树选项
      deptOptions: undefined,
      // 部门名称
      deptName: undefined,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      // 性别状态字典
      sexOptions: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      hasClickClose:false,
      // 用户导入参数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    submit(){
      this.addUser();
      this.$emit('selectUser',this.users);
    },
    itemChcek(){
        let that=this;
        setTimeout(() => {
              var userIds=[];
              for (let index = 0; index < this.users.length; index++) {
                 const user = this.users[index];
                 userIds.push(user.userId);
              }
              that.userList.forEach(row => {
                  if(userIds.indexOf(row.userId)!=-1){
                      that.$refs.multipleTable.toggleRowSelection(row, true);
                  }
              });

        }, 0);
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      let that=this;
      getSelectUserList(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
            this.userList = response.rows;
            this.total = response.total;
            this.loading = false;
            that.itemChcek();
        }
      );
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().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.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectUser=selection;
      this.ids = selection.map((item) => item.userId);
    },
    addUser(){
      if(this.selectUser.length < 1) {
        this.$message.warning("请选择员工")
        return
      }
      var that=this;
      this.selectUser.forEach(element => {
        var flag= that.users.some(item => item.userId === element.userId)
        if(!flag){
           this.users.push(element);
        }
      });
      //this.users=this.users.concat(this.selectUser);
    },
    delUser(index){
      this.users.splice(index,1)
    }
  },
};
</script>
<style >

.btns{
  padding-top: 15px;
  display: flex;
  align-content: center;
  justify-content: flex-end;
}
</style>
