<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="群名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入群名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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="groupChatList" ref="groupChatList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="群名" align="center" prop="name" >
        <template slot-scope="scope">
          <i class="el-icon-s-comment"></i>
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="群人数" align="center" prop="groupSizeByChartUser"></el-table-column>


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

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="handlePaginationChange"
    />

  </div>
</template>

<script>
import {
  listGroupChat,
} from '@/api/qw/groupChat'

export default {
  name: "GroupChat",
  props:{
    index:Number
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户群详情表格数据
      groupChatList: [],

      //选择的客户群列表
      selectGroupChartList:[],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        owner: null,
        notice: null,
        memberVersion: null,
        status: null,
        companyId: null,
        corpId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {

  },
  methods: {

    /** 查询客户群列表 */
    getList() {
      this.loading = true;
      listGroupChat(this.queryParams).then(response => {
        this.groupChatList = response.rows;
        this.total = response.total;
        this.loading = false;

      });
    },
    getDetails(corpId){

      this.groupChatList = [];
      this.queryParams.corpId=corpId;
      this.getList();
    },
    handlePaginationChange(row) {
      this.queryParams.pageNum = row.page;
      this.queryParams.pageSize = row.limit;
      this.getList();
    },
    //确定选择
    confirmSelect(){
      this.$emit("selectGroupChatList",this.selectGroupChartList,this.index);
      this.resetSelect();
    },
    //重置选择
    resetSelect(){
      this.$refs.groupChatList.clearSelection();

      this.selectGroupChartList=[];

      //重置
      this.queryParams={
        pageNum: 1,
        pageSize: 10,
        name: null,
        owner: null,
        notice: null,
        memberVersion: null,
        status: null,
        companyId: null,
        corpId: null,
      },
        this.getList();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chatId: null,
        id: null,
        name: null,
        owner: null,
        notice: null,
        memberVersion: null,
        status: "0",
        companyId: null,
        corpId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {

      // 保存当前页的选中项
      const currentPageSelections = selection.map(item => item.chatId);

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

      // 更新 ids 和 selectTagsList
      this.ids = this.selectGroupChartList.map(item => item.chatId);

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

    },
  }
};
</script>
