<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="姓名"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="手机号"
        />
      </el-form-item>
      <el-form-item label="部门列表" prop="deptId" v-if="ishy">
        <el-select v-model="queryParams.deptId" @change="getList" clearable>
          <el-option v-for="item in depteventList" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-success" type="primary" size="mini" @click="addSelection">确认</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userList"  @selection-change="handleSelectionChange" :height="ishy?'645':'690'">
      <el-table-column align="center" type="selection" width="60"></el-table-column>
      <el-table-column label="姓名" align="center" prop="nickName"></el-table-column>
      <el-table-column label="岗位" align="center" prop="postName" ></el-table-column>
      <el-table-column label="手机号" align="center" prop="phonenumber"></el-table-column>
    </el-table>
  </div>
</template>

<script>
  import {userFeginlist} from "@/api/forest";

  export default {
    name: "findUserByDept",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 可视化前端配置表表格数据
        userList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          deptId: null,
          nickName:null,
          phonenumber:null,
          deptIds:[]
        },
        // 表单参数
        form: {},
        resUser:[],
      };
    },
    props: {
      deptId: {
        type: String,
        required: true
      },
      depteventList: {
        type: Array,
        required: true
      },
      ishy: {
        type: Boolean,
        required: true
      }
    },
    created() {
      this.queryParams.deptId = this.deptId;
      this.getList();
    },
    methods: {
      /** 查询可视化前端配置表列表 */
      getList() {
        this.loading = true;
        const deptIdAll = [];
        if(this.queryParams.deptId == null ||this.queryParams.deptId == undefined || this.queryParams.deptId == ''){
          for (let i in this.depteventList){
            deptIdAll.push(this.depteventList[i].deptId);
          }
          this.queryParams.deptIds = deptIdAll
        }
        userFeginlist(this.queryParams).then(response => {
          this.userList = response.data;
          this.loading = false;
        });
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.nickName=null;
        this.queryParams.phonenumber=null;
        this.getList();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.resUser = [];
        for (let i in selection){
          const res = {};
          res.id = '';
          res.userId = selection[i].userId;
          res.dept = '';
          res.depteventId = '';
          res.name = selection[i].nickName;
          res.post = selection[i].postName;
          res.telphone = selection[i].phonenumber;
          this.resUser.push(res);
        }
      },
      //数据提交到父页
      addSelection(){
        this.$emit("addSelection",this.resUser);
      }
    }
  };
</script>
