<!--  -->
<template>
  <div class="m-container">
    <save-form
      :inline="true"
      :model="searchForm"
      @submit="search"
      @reset="onReSet"
    >
      <el-form-item>
        <el-input
          v-model.trim="searchForm.search"
          clearable
          @keyup.enter.native="search"
          placeholder="群组名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button native-type="submit" type="primary">搜索</el-button>
        <el-button native-type="reset" type="danger">重置</el-button>
      </el-form-item>
    </save-form>
    <auth auths="imSendGroupMessage/send">
      <el-button @click="postMsg" type="primary" style="margin-bottom: 20px">
        消息群发
      </el-button>
    </auth>
    <div class="tableWrap">
      <el-table
        style="width: 100%"
        :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
        :data="tableList"
        @selection-change="handleSelectionChange"
        @select="onTableSelect"
        ref="multipleSelection"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          align="center"
          prop="id"
          label="群组"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="群组名称"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="memberNum"
          label="人数"
        ></el-table-column>
      </el-table>
      <pagination
        sync
        :total.sync="rules.total"
        :page.sync="rules.page"
        :limit.sync="rules.limit"
        :page-sizes="rules.page_size"
        @pagination="getList"
      />
    </div>
    <eidtPostMsg ref='eidtPostMsg' @clear="clearCom"/>
  </div>
</template>
<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import load from "@/utils/loading";
import { getTimes } from "@/utils";
import { getGroupList } from "../api";
import eidtPostMsg from '../model/eidtPostMsg'
export default {
  components: { pagination,eidtPostMsg },
  data() {
    return {
      searchForm: {
        search: null,
      },

      tableList: [],
      organList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      addMuiscVisible: false,
      multipleSelection: [],
      chioseIdList: [],
      isNewPage: false,
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 获取分部

    this.init();
  },
  methods: {
    init() {
      this.getList();
    },
    async getList() {
      // let { timer, ...rest } = this.searchForm;
      // let params = {
      //   ...rest,
      //   page: this.rules.page,
      //   rows: this.rules.limit,
      //   ...getTimes(timer, ["startTime", "endTime"]),
      // };
      try {
        const res = await getGroupList({
          ...this.searchForm,
          page: this.rules.page,
          rows: this.rules.limit,
        });
        this.tableList = res.data.rows;
        this.rules.total = res.data.total;
        let idList = this.chioseIdList.map((group) => {
          return group.id;
        });
        this.isNewPage = true;
        this.$nextTick(() => {
          this.tableList.forEach((course) => {
            if (idList.indexOf(course.id) != -1) {
            this.$refs.multipleSelection.toggleRowSelection(course, true);
            }
          });
          this.isNewPage = false;
        });
      } catch (e) {
        console.log(e);
      }
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.searchForm.search =
      this.clearCom()
    },
    postMsg() {
      if (!this.chioseIdList || this.chioseIdList.length <= 0) {
        this.$message.error("请至少选择一个群组");
        return;
      }
      this.$refs.eidtPostMsg.openDioag(this.chioseIdList)
    },
    handleSelectionChange(val) {
      if (val.length > 0) {
        this.chioseIdList = this.chioseIdList.concat(val);
        this.chioseIdList = this.$helpers.lodash.uniqBy(this.chioseIdList, "id");
      } else {
        if (this.isNewPage) return;
        let idList = this.chioseIdList.map((group) => {
          return group.id;
        });
        this.$nextTick(() => {
          let tableIdList = [];
          this.tableList.forEach((group) => {
            tableIdList.push(group.id);
            if (idList.indexOf(group.id) != -1) {
              this.$refs.multipleSelection.toggleRowSelection(group, false);
            }
          });
          this.chioseIdList = this.$helpers.lodash.remove(
            this.chioseIdList,
            function (item) {
              return tableIdList.indexOf(item.id) == -1;
            }
          );
          if (this.chioseIdList.length <= 0) {
            this.clearCom();
          }
        });
      }
    },
    clearCom() {
      this.chioseIdList = [];
      this.$refs.multipleSelection.clearSelection();
    },
    onTableSelect(rows, row) {
      let idList = this.chioseIdList.map((group) => {
        return group.id;
      });
      if (idList.indexOf(row.id) != -1) {
        this.chioseIdList.splice(idList.indexOf(row.id), 1);
        if (this.chioseIdList.length <= 0) {
          this.clearCom();
        }
      }
    },
  },
};
</script>
<style lang='scss' scoped>
</style>
