
<!--  -->
<template>
  <div class="m-container">
    <h2>
      <el-page-header @back="onCancel" :content="detail.name"></el-page-header>
    </h2>
    <div class="m-core">
      <descriptions :column="3" class="desc">
        <descriptions-item label="群里聊名称：">
          <div class="flexBox">
            {{ detail.name || "--" }}
            <i class="el-icon-edit" @click="reseDetail('name')"></i>
          </div>
        </descriptions-item>
        <descriptions-item label="所属乐团：">
          {{ detail.tags || "--" }}
        </descriptions-item>

        <descriptions-item label="群聊类型：">
          <div class="flexBox">
            {{ detail.groupType | catgGoupType }}
            <i class="el-icon-edit" @click="reseDetail('type')"></i>
          </div>
        </descriptions-item>
      </descriptions>
      <save-form
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
        ref="searchForm"
      >
        <el-form-item prop="search">
          <el-input
            v-model="searchForm.search"
            placeholder="成员姓名/手机号"
          ></el-input>
        </el-form-item>

        <el-form-item prop="roleType">
          <el-select
            v-model.trim="searchForm.roleType"
            filterable
            clearable
            placeholder="角色"
          >
            <el-option
              v-for="(item, index) in catRoleList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></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="bWrap">
        <el-button type="primary" @click="addMembers">添加成员</el-button>
        <el-button type="danger">删除成员</el-button>
      </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="userId"
            label="编号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="user.username"
            label="姓名"
          ></el-table-column>

          <el-table-column
            align="center"
            prop="user.phone"
            label="手机号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="roleType"
            label="角色"
          ></el-table-column>
          <el-table-column align="center" prop="roleType" label="操作">
            <template slot-scope="scope">
              <div>
                <auth
                  auths="imSendGroupMessage/update"
                  v-if="!scope.row.sendFlag"
                >
                  <el-button type="text" @click="resetGroup(scope.row)"
                    >修改角色</el-button
                  >
                </auth>
                <auth
                  auths="imSendGroupMessage/delete"
                  v-if="!scope.row.sendFlag"
                >
                  <el-button type="text" @click="deleteStudent(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>
    </div>
      <eidtCatInfo ref="eidtCatInfo" @getList="getList" />
      <addMember ref="addMember" @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 { getGroupMemberList, getGroupDetail } from "./api";
import { getSubject } from "@/api/buildTeam";
import countTo from "vue-count-to";
import { catRoleList } from "@/utils/searchArray";
import eidtCatInfo from "./model/eidtCatInfo";
import addMember from './model/addMember'
// catRoleFilter
export default {
  components: { pagination, "count-to": countTo ,eidtCatInfo,addMember},
  data() {
    return {
      catRoleList,
      searchForm: {
        search: null,
        groupType: null,
      },
      detail: {
        groupType: "",
        id: "",
        img: "",
        introduce: "",
        memberNum: 0,
        memo: "",
        name: "",
        tags: "",
        tenantId: 0,
        type: "",
      },
      chatName: "",
      tableList: [{}],
      soundList: [],
      cooperationList: [],
      musicList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      multipleSelection: [],
      chioseIdList: [],
      isNewPage: false,
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 获取分部
    this.imGroupId = this.$route.query.imGroupId;
    this.$store.dispatch("setBranchs");
    this.init();
  },
  methods: {
    init() {
      getSubject({}).then((res) => {
        if (res.code == 200) {
          this.soundList = res.data;
        }
      });
      this.getDetail();
      this.getList();
    },
    async getDetail() {
      try {
        const res = await getGroupDetail({ imGroupId: this.imGroupId });
        this.detail = { ...res.data };
      } catch (e) {
        console.log(e);
      }
    },
    async getList() {
      try {
        const res = await getGroupMemberList({
          page: this.rules.page,
          rows: this.rules.limit,
          ...this.searchForm,
          imGroupId: this.$route.query.imGroupId,
        });
        // this.tableList = res.data;
        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();
    },
    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();
        }
      }
    },
    onReSet() {
      this.rules = {
        ...this.rules,
        page: 1,
      };
      this.$refs.searchForm.resetFields();
      this.search();
    },
    onCancel() {
      this.$store.dispatch("delVisitedViews", this.$route);
      this.$router.push({
        path: "/childrensDay",
      });
    },
    deleteStudent(row) {},
    reseDetail(type){
        this.$refs.eidtCatInfo.openResetDioag(this.detail,type);
    },
    addMembers(){
      this.$refs.addMember.openDioag()
    }
  },
};
</script>
<style lang='scss' scoped>
.bWrap {
  display: flex;
  flex-direction: row;
  margin-bottom: 20px;
}
.statistic {
  .statistic-content > span {
    font-size: 20px !important;
    &:first-child {
      font-size: 24px !important;
      color: rgba(0, 0, 0, 0.85);
      font-weight: bold;
    }
  }
}
.desc {
  margin-bottom: 20px;
}
.flexBox {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  i {
    font-size: 18px;
    cursor: pointer;
  }
}
</style>
