<!--  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      班级列表
    </h2>
    <div class="m-core">
      <save-form
        ref="searchForm"
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item prop="search">
          <el-input
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="search"
            placeholder="请输入乐团编号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="organIdList">
          <el-select
            class="multiple"
            filterable
            multiple
            style="width: 180px !important"
            v-model.trim="searchForm.organIdList"
            clearable
            placeholder="请选择分部"
          >
            <el-option
              v-for="(item, index) in selects.branchs"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- musicClassTypeList -->
        <el-form-item prop="type">
          <el-select v-model.trim="searchForm.type" clearable filterable>
            <el-option
              v-for="(item, index) in musicClassTypeList"
              :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="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column align="center" prop="musicGroupId" label="乐团编号">
            <template slot-scope="scope">
              <copy-text>{{ scope.row.musicGroupId }}</copy-text>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="musicGroupName"
            label="乐团名称"
          ></el-table-column>
          <el-table-column align="center" prop="organName" label="分部名称">
            <template slot-scope="scope">
              <copy-text>{{ scope.row.organName }}</copy-text>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="name"
            label="班级名称"
          ></el-table-column>
          <el-table-column align="center" prop="type" label="班级类型">
            <template slot-scope="scope">
              <div>{{ scope.row.type | classType }}</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="studentNum"
            label="当前班级人数"
          >
            <template slot-scope="scope">
              <div>{{ scope.row.studentNum }}人</div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop label="主教老师">
            <template slot-scope="scope">
              <div v-if="scope.row.classGroupTeacherMapperList">
                <p
                  v-for="(item, index) in scope.row.classGroupTeacherMapperList"
                  v-if="item.teacherRole == 'BISHOP'"
                  :key="index"
                >
                  {{ item.userName }}
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="助教老师">
            <template slot-scope="scope">
              <div v-if="scope.row.classGroupTeacherMapperList">
                <p
                  v-for="(item, index) in scope.row.classGroupTeacherMapperList"
                  v-if="item.teacherRole == 'TEACHING'"
                  :key="index"
                >
                  {{ item.userName }}
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="已上课时">
            <template slot-scope="scope">
              <div>{{ scope.row.currentClassTimes }}</div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="总课数">
            <template slot-scope="scope">
              <div>{{ scope.row.totalClassTimes }}</div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="addCompound(scope.row)"
                v-if="
                  scope.row.type != 'MUSIC_NETWORK' &&
                  !isAddCom(scope.row) &&
                  permission('classGroup/spanGroupMergeClassSplitClassAffirm')
                "
                >添加合班</el-button
              >
              <el-button
                type="text"
                v-if="
                  isAddCom(scope.row) &&
                  permission('classGroup/spanGroupMergeClassSplitClassAffirm')
                "
                @click="cancleCompound(scope.row)"
                >取消合班</el-button
              >
            </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>
    <classCompound
      :compoundList="compoundList"
      v-if="permission('classGroup/spanGroupMergeClassSplitClassAffirm')"
      @clearCom="clearCom"
      @getList="getList"
      @cancleCompound="cancleCompound"
    />
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import load from "@/utils/loading";
import { musicClassTypeList } from "@/utils/searchArray";
import { getClassGroupPage } from "@/api/buildTeam";
import { permission } from "@/utils/directivePage";
import classCompound from "./componentClass/classCompound";
export default {
  components: { pagination, classCompound },
  data() {
    return {
      searchForm: {
        search: null,
        organIdList: [],
        type: "",
      },
      musicClassTypeList,
      tableList: [],
      compoundList: [],
      hightCount: 0,
      hightOnlineCount: 0,
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 获取分部
    this.$store.dispatch("setBranchs");
    this.init();
  },
  methods: {
    init() {
      this.getList();
    },
    async getList() {
      try {
        let { organIdList, ...result } = this.searchForm;
        console.log(result);
        let obj = {
          ...result,
          organIdList: this.searchForm.organIdList.join(","),
          page: this.rules.page,
          rows: this.rules.limit,
        };
        const res = await getClassGroupPage(obj);
        this.rules.total = res.data.total;
        this.tableList = res.data.rows;
      } catch (err) {
        console.log(err);
      }
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.$refs.searchForm.resetFields();
      this.search();
    },
    addCompound(row) {
      //  scope.row.type != 'HIGH_ONLINE' &&scope.row.type != 'HIGH'&&
      this.hightOnlineCount = 0;
      this.hightCount = 0;
      this.compoundList.push(row);
      this.compoundList = [...new Set(this.compoundList)];

        this.compoundList.forEach((classes) => {
          if (classes.type == "HIGH") {
            this.hightCount++;
          }
          if (classes.type == "HIGH_ONLINE") {
            this.hightOnlineCount++;
          }
        });
      if(this.hightOnlineCount&&this.hightOnlineCount != this.compoundList.length){
        this.$message.error("线上基础技能班仅能和线上基础技能班合并");
          this.hightOnlineCount = 0;
          this.hightCount = 0;
          this.compoundList = [];
      }
      if(this.hightCount&&this.hightCount != this.compoundList.length){
           this.$message.error("基础技能班仅能和基础技能班合并");
          this.hightOnlineCount = 0;
          this.hightCount = 0;
          this.compoundList = [];
      }
    },
    isAddCom(row) {
      let flag = false;
      this.compoundList.forEach((com) => {
        if (com.id == row.id) {
          flag = true;
        }
      });
      return flag;
    },
    cancleCompound(row) {
      let indexNum = null;
      this.compoundList.forEach((com, index) => {
        if (com.id == row.id) {
          indexNum = index;
          if (row.type == "HIGH") this.hightCount--;
          if (row.type == "HIGH_ONLINE") this.hightOnlineCount--;
        }
      });
      if (indexNum + "") {
        this.compoundList.splice(indexNum, 1);
      }
    },
    clearCom() {
      this.compoundList = [];
      this.hightOnlineCount = 0;
      this.hightCount = 0;
    },
    permission(str, parent) {
      return permission(str, parent);
    },
  },
};
</script>
<style lang='scss' scoped>
</style>
