<!--  -->
<template>
  <div class="m-container">
    <save-form :inline="true" :model="searchForm" @submit="search" @reset="onReSet" ref="searchForm">
      <el-form-item>
        <el-input v-model.trim="searchForm.search" clearable @keyup.enter.native="e => {
            e.target.blur();
            $refs.searchForm.save();
            search();
          }
          " placeholder="群聊名称"></el-input>
      </el-form-item>
      <el-form-item prop="groupType">
        <el-select v-model.trim="searchForm.groupType" clearable filterable placeholder="群聊类型">
          <el-option v-for="(item, index) in catgGoupTypeList" :key="index" :value="item.value"
            :label="item.label"></el-option>
        </el-select>
      </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>
    <div class="btnWraps">
      <auth auths="imSendGroupMessage/send">
        <el-button @click="postMsg" type="primary" style="margin-bottom: 20px">
          消息群发
        </el-button>
      </auth>
      <auth auths="imGroup/createGroup">
        <el-button @click="addChat" type="primary" style="margin-bottom: 20px">
          创建群聊
        </el-button>
      </auth>
    </div>

    <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="memo" label="群备注"></el-table-column>
        <el-table-column align="center" prop="groupType" label="群聊类型">
          <template slot-scope="scope">
            <div>
              {{ scope.row.groupType | catgGoupType }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="groupType" label="群类型">
          <template slot-scope="scope">
            <div>
              {{ scope.row.type | catType }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="memberNum" label="人数"></el-table-column>
        <el-table-column align="center" prop="memberNum" label="操作">
          <template slot-scope="scope">
            <div>
              <auth auths="/chatDetail">
                <el-button type="text" @click="gotoCatDetail(scope.row)">详情</el-button>
              </auth>
              <auth auths="imGroup/updateImGroup">
                <el-button type="text" @click="cancelCat(scope.row)">解散</el-button>
              </auth>
            </div>
          </template>
        </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" />
    <eidtCatInfo ref="eidtCatInfo" @getList="getList" />
    <addChatForm ref="addChatForm" @getList="getList" />
  </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, dismissGroup } from "../api";
import { catgGoupTypeList } from "@/utils/searchArray";
import eidtPostMsg from "../model/eidtPostMsg";
import eidtCatInfo from "../model/eidtCatInfo";
import addChatForm from "../model/addChatForm";
export default {
  components: { pagination, eidtPostMsg, eidtCatInfo, addChatForm },
  data() {
    return {
      searchForm: {
        search: null,
        groupType: ""
      },
      catgGoupTypeList,
      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.searchForm.groupType = "";
      this.rules.page = 1;
      this.getList();
    },
    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();
        }
      }
    },
    resetCat(row) {
      this.$refs.eidtCatInfo.openResetDioag(row);
    },
    gotoCatDetail(row) {
      this.$router.push({
        path: "/operateManager/chatDetail",
        query: { imGroupId: row.id, name: row.name }
      });
      console.log(row);
    },
    addChat() {
      this.$refs.addChatForm.openDioag();
    },
    async cancelCat(row) {
      try {
        await this.$confirm("是否解散群组" + row.name + "?", "提示", {
          type: "warning"
        });
        const res = await dismissGroup({ imGroupId: row.id });
        this.$message.success("取消成功");
        this.getList();
      } catch (e) {
        console.log(e);
      }
    }
  }
};
</script>
<style lang="scss" scoped></style>
