<template>
  <div class="app-container">
    <el-alert
              title="注意事项"
              type="warning"
              description="此处 显示了 所有销售绑定 的企业微信账号信息"
              :closable="false"
              center
              show-icon>
    </el-alert>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" @submit.prevent="handleQuery">

      <el-form-item label="企微员工账号" prop="qwUserId">
        <el-input
          v-model="queryParams.qwUserId"
          placeholder="请输入企微员工账号"
          clearable
          size="small"
          @keydown.enter.native="handleQueryEnter"
        />
      </el-form-item>
      <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="企微员工账号" align="center" prop="qwUserId" />
        <el-table-column label="企微员工昵称" align="center" prop="qwUserName"/>
        <el-table-column label="企微员工部门" align="center" prop="departmentName"/>
        <el-table-column label="后台员工昵称" align="center" prop="nickName"/>
        <el-table-column label="后台员工用户名" align="center" prop="userName" />

    </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.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="handlePaginationChange"
    />
  </div>
</template>

<script>
import { listUser} from "@/api/qw/user";


export default {
  name: "qwUserListByCompany",
  data() {
    return {
      type:null,
      sendType:null,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      selectUsers: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企微用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        qwUserId: null,
        companyId: null,
        companyUserId: null,
        corpId: null,
        nickName: null
      },
      // 表单参数
      form: {},

      // 表单校验
      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.pageNum = row.page;
      this.queryParams.pageSize = row.limit;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        qwUserId: null,
        companyId: null,
        companyUserId: null,
        corpId: null
      };
      this.resetForm("form");
    },
    //确定选择
    confirmSelect(){
      this.$emit("selectUserList",this.selectUsers);
      this.resetSelect();
    },
    //重置选择
    resetSelect(){
      this.$refs.userList.clearSelection();

      this.selectUsers=[];
      //重置
      this.queryParams={
        pageNum: 1,
        pageSize: 5,
        qwUserId: null,
        companyId: null,
        companyUserId: null,
        corpId: null,
        nickName: null
      },
        this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 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.id);

      // 合并选中项
      this.selectUsers = this.selectUsers.filter(item =>
        this.userList.some(tag => tag.id === item.id) ? currentPageSelections.includes(item.id) : true
      ).concat(selection.filter(item => !this.selectUsers.some(selected => selected.id === item.id)));

      // 更新 single 和 multiple
      this.single = this.selectUsers.length !== 1;
      this.multiple = !this.selectUsers.length;

    },

  }
};
</script>
