<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px"
      @submit.prevent="handleQuery">

      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable size="small"
          @keydown.enter.native="handleQueryEnter" />
      </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 v-loading="loading" :data="userList" ref="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户id" align="center" prop="userId"/>
      <!-- <el-table-column label="用户账户" align="center" prop="username"/> -->
      <el-table-column label="用户昵称" align="center" prop="nickName"/>

    </el-table>
    <div style="margin-top: 30px;display: flex;justify-content: center">
      <el-button type="warning" icon="el-icon-search" @click="confirmSelect">确定选择</el-button>
    </div>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.startIndex" :limit.sync="queryParams.pageLimit"
      @pagination="handlePaginationChange" />
  </div>
</template>

<script>
import { listUser } from "@/api/app/user/userList";
export default {
  name: "userList",
  data() {
    return {
      type: null,
      sendType: null,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      selectUsers: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企微用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        startIndex: 1,
        pageLimit: 5,
        nickName: null
      },

      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        // 如果 companyUserId 为 null，移除列
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handlePaginationChange(row) {
      this.queryParams.startIndex = row.page;
      this.queryParams.pageLimit = row.limit;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        startIndex: 1,
        pageLimit: 5,
        nickName: null,
      }
      this.resetForm("form");
    },
    //确定选择
    confirmSelect() {
      this.$emit("selectUserList", this.selectUsers);
      this.resetSelect();
    },
    //重置选择
    resetSelect() {
      this.$refs.userList.clearSelection();
      this.selectUsers = [];
      this.reset();
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.startIndex = 1;
      this.getList();
    },
    handleQueryEnter(event) {
      // 确保事件对象存在
      if (event && event.preventDefault) {
        event.preventDefault(); // 阻止默认提交行为
      }
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      // 保存当前页的选中项
      const currentPageSelections = selection.map(item => item.userId);
      // 合并选中项
      this.selectUsers = this.selectUsers.filter(item =>
        this.userList.some(tag => tag.userId === item.id) ? currentPageSelections.includes(item.userId) : true
      ).concat(selection.filter(item => !this.selectUsers.some(selected => selected.userId === item.userId)));
      // 更新 single 和 multiple
      this.single = this.selectUsers.length !== 1;
      this.multiple = !this.selectUsers.length;
    },
  }
};
</script>
