<template>
    <div class="my-course-container custom-iview-split">
        <Loading v-if="listLoading" bgColor="rgba(103, 103, 103, 0.27)"></Loading>
        <Split v-show="!isShowSchd" v-model="split1">
            <!--课程列表-->
            <div class="course-list" slot="left">
                <!-- 列表头部 -->
                <div class="course-list-header tab-box">
                    <span v-for="(item,index) in cusType" :key="index" @click="tabClick(item.value)" :class="{ active: item.value == listType }" class="cus-type-item pane">
                        {{item.label}}
                    </span>
                    <div style="margin-top:0px;float:right;">
                        <Icon type="md-add" v-show="listType == 'private'" class="add-icon" :title="$t('cusMgt.addCus')" @click="addCusStatus = true" />
                        <Icon type="md-create" v-show="listType == 'private' && courseListShow.length" class="add-icon" :title="$t('cusMgt.editCus')" @click="editCus()" />
                        <Icon custom="iconfont icon-schedule" v-show="listType == 'school' && courseListShow.length" class="add-icon" :title="$t('cusMgt.schdTable')" @click="showSchedule" />
                        <Icon type="md-trash" v-show="listType == 'private'" class="add-icon" :title="$t('cusMgt.delCus')" @click="delCourse" />
                    </div>
                </div>
                <!--列表内容-->
                <vuescroll>
                    <div class="cus-list-tab">
                        <div v-for="(item,index) in courseListShow" :key="index" @click="selectCourse(index)" :class="index === curCusIndex ? 'course-list-item block-bg block-bg-active':'course-list-item block-bg'">
                            <p class="course-name">
                                {{item.name}}
                            </p>
                            <p class="course-code">
                                <Icon type="md-pricetags" />
                                {{item.no}}
                            </p>
                        </div>
                        <div v-if="courseListShow.length == 0">
                            <EmptyData :top="150" :textContent="$t('cusMgt.noScCus')"></EmptyData>
                        </div>
                    </div>
                </vuescroll>
            </div>
            <div class="course-info-wrap" slot="right">
                <Split v-model="split2">
                    <!--课程名单列表-->
                    <div class="course-classroom-list" slot="left">
                        <div class="course-classroom-list-header">
                            <span class="course-classroom-label">{{$t('courseManage.classroom.classroomList')}}</span>
                            <Tooltip :content="$t('tip.cusClass')" transfer theme="light" max-width="200">
                                <Icon type="ios-information-circle-outline" color="#1cc0f3" style="margin-left:5px" />
                            </Tooltip>
                            <Icon type="md-add" v-show="listType == 'private'" class="add-icon" @click="newSlStatus = true" />
                            <Icon type="md-create" v-show="listType == 'private' && teaClassList.length" class="add-icon" @click="editListName" />
                            <Icon type="md-trash" v-show="listType == 'private' && teaClassList.length" class="add-icon" @click="removeStuList()" />
                        </div>
                        <div class="course-classroom-list-content">
                            <vuescroll>
                                <div v-for="(item,index) in teaClassList" :key="index" @click="changeClassroom(index)" :class="['block-bg','tea-class-item',curClassIndex == index ? 'block-bg-active':'']">
                                    <p class="class-attr-item">
                                        <span class="attr-label">{{$t('cusMgt.nameLabel')}}</span>
                                        <span class="class-name">{{item.classId ? item.classInfo.name: item.listName || '--'}}</span>
                                    </p>
                                    <p class="class-attr-item">
                                        <span class="attr-label">{{$t('cusMgt.stuCount')}}：</span>
                                        <span class="class-name">{{item.allStu ? item.allStu.length : 0}}{{$t('unit.text7')}}</span>
                                    </p>
                                    <Icon size="30" custom="iconfont icon-qr-code" class="qr-code-icon" @click="showQrCode(index)" v-if="listType == 'private'" :title="$t('cusMgt.qrCodeLabel')" />
                                </div>
                                <EmptyData v-if="teaClassList.length == 0" :top="160" :textContent="$t('cusMgt.noClassList')"></EmptyData>
                            </vuescroll>
                        </div>
                    </div>
                    <div class="course-classroom-info" id="table-height" slot="right">
                        <div class="course-classroom-info-header tab-box" style="padding-right:30px;">
                            <span @click="selectTab('activity')" :class="tabName == 'activity' ? 'course-classroom-label pane active':'course-classroom-label pane'">{{$t('cusMgt.acRecord')}}</span>
                            <span @click="selectTab('record')" :class="tabName == 'record' ? 'course-classroom-label pane active':'course-classroom-label pane'">{{$t('cusMgt.cusRecord')}}</span>
                            <span @click="selectTab('stus')" :class="tabName == 'stus' ? 'course-classroom-label pane active':'course-classroom-label pane'">{{$t('courseManage.classroom.studentList')}}</span>
                            <span v-show="tabName == 'stus' && listType == 'school' && teaClassList[curClassIndex] && teaClassList[curClassIndex].classId" class="group-tips">{{$t('cusMgt.groupTips')}}</span>
                            <!-- 个人课程时段设置暂不实做 -->
                            <!-- <span @click="selectTab('time')" :class="tabName == 'time' ? 'course-classroom-label line-bottom line-bottom-active':'course-classroom-label line-bottom'">{{$t('cusMgt.cusTime')}}</span> -->
                            <div style="float:right;color:white;" v-if="listType == 'private' && tabName == 'stus'">
                                <span class="action-btn-wrap" @click="delStudents">
                                    <Icon type="md-trash" size="16" />
                                    {{$t('cusMgt.delStu')}}
                                </span>
                                <span v-show="$store.state.userInfo.hasSchool" class="action-btn-wrap" @click="addStuStatus = true">
                                    <Icon type="md-add" size="16" />
                                    {{$t('cusMgt.addStu')}}
                                </span>
                            </div>
                            <div style="float:right;" v-if="tabName == 'activity'">
                                <span class="action-btn-wrap light-iview-select">
                                    {{$t('cusMgt.acType')}}
                                    <Select v-model="curAcType" style="width:160px" @on-change="getActivityList">
                                        <Option v-for="(item,index) in acTypeList" :disabled="index > 2" :value="item.value" :key="item.value">{{ item.label }}</Option>
                                    </Select>
                                </span>
                            </div>
                        </div>
                        <!-- 学生名单 -->
                        <div class="course-classroom-info-content animated fadeIn" v-if="tabName == 'stus'">
                            <vuescroll style="height:100%;">
                                <Table :columns="isDefList ? classColumn : listColumn" :data="students" @on-selection-change="(selections)=>{delSelection = selections}" :height="tableHeight" class="system-classroom-table" :loading="stuLoading" :no-data-text="$t('cusMgt.noStu')">
                                    <Loading slot="loading" bgColor="rgba(103, 103, 103, 0.27)"></Loading>
                                    <template slot-scope="{ row }" slot="picture">
                                        <PersonalPhoto :name="row.name || ''" :picture="row.picture" />
                                    </template>
                                    <template slot-scope="{ row,index }" slot="no">
                                        <span>{{row.no || '- -'}}</span>
                                    </template>
                                    <template slot-scope="{ row }" slot="type">
                                        <span>{{row.type == 'school' ? $t('cusMgt.schoolType') : $t('cusMgt.tmIDType')}}</span>
                                    </template>
                                    <template slot-scope="{ row ,index}" slot="action">
                                        <div class="item-tools" v-if="$access.can('admin.*|student-upd')">
                                            <Icon type="md-create" size="18" color="white" @click="resetNo(index)" :title="$t('schoolBaseInfo.editSeat')" />
                                            <Icon type="md-remove-circle" size="18" color="white" style="margin-left:10px" @click="removeStudent(index)" :title="$t('schoolBaseInfo.delStuBtn')" />
                                        </div>
                                    </template>
                                    <template slot-scope="{ row, index }" slot="groupId">
                                        <span>{{row.groupId ? row.groupId : '- -'}}</span>
                                    </template>
                                    <template slot-scope="{ row, index }" slot="groupName">
                                        <span>{{row.groupName ? row.groupName : '--'}}</span>
                                    </template>
                                </Table>
                            </vuescroll>
                        </div>
                        <!-- 课堂记录 -->
                        <div v-show="tabName == 'record'" class="animated fadeIn class-record-wrap">
                            <vuescroll>
                                <List>
                                    <ListItem v-for="(item,index) in itemNum" :key="index">
                                        <ListItemMeta @click.native="toClassRecoerd">
                                            <p slot="title" class="record-name">力的组成</p>
                                            <span slot="avatar" style="margin-top:4px;display: inline-block;margin-left:10px;">
                                                <Icon custom="iconfont icon-whiteboard" size="30" color="white" />
                                            </span>
                                            <div slot="description" style="color:white;">
                                                <span class="hiteacher-version">
                                                    V3.0.32
                                                </span>
                                                <Time class="record-time" :time="((new Date()).getTime() - 60 * 3 * 1000)" />
                                                <div class="record-action-wrap">
                                                    <Icon class="action-icon" type="md-analytics" :title="$t('cusMgt.cusRecord')" @click="toClassRecoerd" />
                                                    <Icon class="action-icon" type="ios-stats" :title="$t('cusMgt.socrateReport')" @click="toClassRecoerd" />
                                                    <Icon class="action-icon" custom="iconfont icon-video" :title="$t('cusMgt.socrateMv')" @click="toClassRecoerd" />
                                                    <Icon class="action-icon" custom="iconfont icon-hi" :title="$t('cusMgt.elNotes')" @click="toClassRecoerd" />
                                                    <Icon class="action-icon" custom="iconfont icon-textbook" :title="$t('cusMgt.lessonContent')" @click="toClassRecoerd" />
                                                    <Icon class="action-icon" type="md-share" :title="$t('cusMgt.share')" @click="toClassRecoerd" />
                                                </div>
                                            </div>
                                        </ListItemMeta>
                                    </ListItem>
                                </List>
                                <EmptyData v-show="itemNum == 0" :textContent="$t('cusMgt.noRecord')" :top="150"></EmptyData>
                            </vuescroll>
                        </div>
                        <!-- 活动记录 -->
                        <div v-show="tabName == 'activity'" class="animated fadeIn class-record-wrap">
                            <vuescroll>
                                <List>
                                    <ListItem v-for="(item,index) in acList" :key="index" style="cursor: pointer;">
                                        <ListItemMeta @click.native="toAcDetail(index)" class="ac-item-warp">
                                            <p slot="title" class="record-name">
                                                {{item.name}}
                                                <span class="evaluation-status-tag" :style="{ borderColor: item.progress == 'pending' ? '#0BADD4' : item.progress == 'going' ? '#1CC0F3' : '#ed4014', color: (item.progress == 'pending' ? '#0BADD4' : item.progress == 'going' ? '#1CC0F3' : '#ed4014')}">
                                                    {{ item.progress == 'pending' ? $t('learnActivity.mgtScEv.pending') : item.progress == 'going' ? $t('learnActivity.mgtScEv.going') : $t('learnActivity.mgtScEv.finish') }}
                                                </span>
                                                <span class="evaluation-status-tag" :style="{ borderColor: item.owner == 'school' ? '#0BADD4' : '#ff9900', color: (item.owner == 'school' ? '#0BADD4' : '#ff9900')}">
                                                    {{ item.owner == 'school' ? $t('cusMgt.school') : $t('cusMgt.private') }}
                                                </span>
                                            </p>
                                            <span slot="avatar" style="margin-top:12px;display: inline-block;margin-left:10px;">
                                                <Icon v-if="curAcType == 'ev'" custom="iconfont icon-test" size="24" color="#8685EF" />
                                                <Icon v-else-if="curAcType == 'vote'" custom="iconfont icon-vote" size="24" color="#6A9A8B" />
                                                <Icon v-else-if="curAcType == 'qu'" custom="iconfont icon-questionnaire" size="24" color="#D19F9C" />
                                                <Icon v-else-if="curAcType == 'hw'" custom="iconfont icon-hw" size="24" color="#CCC" />
                                                <Icon v-else custom="iconfont icon-learning-self" size="24" color="#CCC" />
                                            </span>
                                            <div slot="description">
                                                <span class="ev-attr-wrap">
                                                    <span class="attr-label">
                                                        <Icon type="md-time" size="16" />
                                                        {{$t('cusMgt.acTime')}}:
                                                    </span>
                                                    <span class="attr-value">
                                                        {{dateFormat(item.startTime)}}
                                                        -
                                                        {{dateFormat(item.endTime)}}
                                                    </span>
                                                </span>
                                            </div>
                                        </ListItemMeta>
                                    </ListItem>
                                </List>
                                <EmptyData v-show="acList.length == 0" :textContent="acTypeLabel + $t('cusMgt.noAc')" :top="150"></EmptyData>
                            </vuescroll>
                        </div>
                        <!-- 个人课程时段 暂不实做-->
                        <!-- <div v-show="tabName == 'time'" class="animated fadeIn class-record-wrap">
                            <vuescroll>
                                <h2 style="margin-top:120px;color:#ddd;text-align:center"> 
                                    1、如果教师没有加入学校，拿不到时段设置，不能渲染课表；
                                </h2>
                                <h2 style="margin-top:20px;color:#ddd;text-align:center">
                                    2、个人课程没有关联学段，不能确定使用哪个学段的时间；
                                </h2>
                                <h2 style="margin-top:20px;color:#ddd;text-align:center">
                                    3、如果支持老师个人创建时段管理，会不会变成学校可以用的老师都能用。
                                </h2>
                            </vuescroll>
                        </div>-->
                    </div>
                </Split>
            </div>
        </Split>
        <!-- 教师个人课程表 -->
        <div v-show="isShowSchd" class="priv-table-wrap">
            <vuescroll>
                <div class="table-header light-iview-select">
                    <span v-show="cusPd.length  >  1">{{$t('cusMgt.pdLabel')}}</span>
                    <Select v-model="curPd" style="width:200px" v-show="cusPd.length  >  1">
                        <Option v-for="item in cusPd" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                    <div class="cus-tips-wrap">
                        <Tooltip v-show="noClassCus.length">
                            <div slot="content">
                                <ul style="margin-left: 18px;">
                                    <li v-for="(item,index) in noClassCus" :key="index"> {{`${item.name}`}}</li>
                                </ul>
                            </div>
                            <span class="no-class-tips">{{$t('cusMgt.noClassCus')}}{{`${noClassCus.length}/${schdData.length}`}}</span>
                        </Tooltip>
                        <Tooltip v-show="noTimeCus.length" style="margin-left:15px;">
                            <div slot="content">
                                <ul style="margin-left: 18px;">
                                    <li v-for="(item,index) in noTimeCus" :key="index"> {{`${item.name}`}}</li>
                                </ul>
                            </div>
                            <span class="no-time-tips">{{$t('cusMgt.noTImeCus')}}{{`${noTimeCus.length}/${schdData.length}`}}</span>
                        </Tooltip>
                    </div>
                    <div class="action-btn-wrap">
                        <span @click="isShowSchd = !isShowSchd">
                            <Icon type="md-list" />
                            {{$t('cusMgt.listMode')}}
                        </span>
                    </div>
                </div>
                <TeaTable :schedData="schdData" :teacher="$store.state.userInfo.TEAMModelId" :periodId="curPd" mode="view"></TeaTable>
            </vuescroll>
        </div>
        <!-- 创建名单 -->
        <Modal v-model="newSlStatus" width="600" @on-ok="confirmCreateList" :loading="modalLoading">
            <div slot="header">
                <span class="create-list-title">
                    {{$t('cusMgt.addStuList')}}
                </span>
            </div>
            <Form :label-width="80" label-colon @submit.native.prevent>
                <FormItem :label="$t('cusMgt.addListType')">
                    <RadioGroup v-model="type">
                        <Radio label="create">
                            <span>
                                {{$t('cusMgt.addListType1')}}
                            </span>
                        </Radio>
                        <Radio label="select">
                            <span>
                                {{$t('cusMgt.addListType2')}}
                            </span>
                        </Radio>
                    </RadioGroup>
                </FormItem>
                <FormItem :label="$t('cusMgt.name')" v-if="type == 'create'">
                    <Input v-model="listName" :placeholder="$t('cusMgt.nameHolder')" style="width: 300px" />
                </FormItem>
                <FormItem :label="$t('cusMgt.listLabel')" v-else>
                    <Select v-model="listId" style="width:300px">
                        <Option v-for="(item,index) in stuList" :value="item.id" :key="index">{{ item.name }}</Option>
                    </Select>
                </FormItem>
            </Form>
            <span class="create-list-tips">{{$store.state.userInfo.hasSchool ? $t('cusMgt.createTips1') : $t('cusMgt.createTips2')}}</span>
        </Modal>
        <Modal v-model="addCusStatus" :title="$t('cusMgt.addCus')" @on-ok="confirmAddCus" :loading="modalLoading">
            <Form ref="addCusInfo" :model="addCusInfo" :rules="ruleAddCus">
                <FormItem prop="name" :label="$t('cusMgt.cusName')">
                    <Input type="text" v-model="addCusInfo.name" :placeholder="$t('cusMgt.nameHolder')"></Input>
                </FormItem>
                <FormItem :label="$t('cusMgt.cusCode')" prop="no">
                    <Input type="text" v-model="addCusInfo.no" :placeholder="$t('cusMgt.codeHolder')"></Input>
                </FormItem>
                <FormItem :label="$t('cusMgt.cusDesc')" prop="desc">
                    <Input v-model="addCusInfo.desc" type="textarea" :maxlength="200" show-word-limit :autosize="{ minRows: 4, maxRows: 6 }" :placeholder="$t('cusMgt.descHolder')" />
                </FormItem>
            </Form>
        </Modal>
        <Modal v-model="addStuStatus" :title="$t('cusMgt.addStu')" width="1200" @on-ok="confirmAddStu">
            <StudentList @getSelectInfo="(selction)=>{selections = selction}"></StudentList>
        </Modal>
        <div class="qr-code-wrap" v-show="showQrStatus" @click="showQrStatus = false">
            <div class="qr-code-info" @click.stop>
                <p class="qr-code-title">
                    {{$t('cusMgt.qrCodeLabel')}}
                </p>
                <div id="qrcode" :class="showQrStatus ? 'animated fadeIn':'animated fadeOut'" ref="qrcode" style="padding:15px 25px 20px 25px;background-color:white;width:330px;margin:auto;">
                </div>
                <p class="qr-code-text">
                    (<span style="font-size:17px">{{$t('cusMgt.qrCodeText')}}</span>
                    {{stuListNo}})
                </p>
                <p class="invite-url-text" v-show="inviteUrl">
                    <span style="font-size:14px">{{$t('cusMgt.inviteUrl')}}</span>
                    {{inviteUrl}}
                </p>
                <Icon type="md-copy" class="copy-link-icon" :title="$t('cusMgt.copyUrl')" @click="copyUrl" />
            </div>
        </div>
        <Modal v-model="editClassStatus" :title="$t('cusMgt.renameListTitle')" @on-ok="confirmRename">
            {{$t('cusMgt.listName')}}: <Input v-model="edName" :placeholder="renameBefore" style="width: 300px" />
        </Modal>
    </div>
</template>
<script>
import { mapGetters } from 'vuex'
import QRCode from 'qrcodejs2'
import PersonalPhoto from '@/components/public/personalPhoto/Index.vue'
import StudentList from '@/components/coursemgt/StudentList.vue'
import TeaTable from './TeaTable.vue'
export default {
    components: {
        StudentList, PersonalPhoto, TeaTable
    },
    inject: ['reload'],
    data() {
        // 验证只能是字母和数字
        const validateCode = (rule, value, callback) => {
            let zg = /^[0-9a-zA-Z]+$/
            if (value && !zg.test(value)) {
                callback(new Error(this.$t('cusMgt.codeErr2')))
            } else {
                callback()
            }
        }
        return {
            type: 'create',
            listId: '',
            renameBefore: '',
            edName: '',
            inviteUrl: '',
            stuListNo: '',
            cusType: [],
            curPd: '',
            isShowSchd: false,
            split1: 0.2,
            split2: 0.2,
            acTypeList: [
                {
                    label: this.$t('cusMgt.ev'),
                    value: 'ev'
                },
                {
                    label: this.$t('cusMgt.vote'),
                    value: 'vote'
                },
                {
                    label: this.$t('cusMgt.qu'),
                    value: 'qu'
                },
                {
                    label: this.$t('cusMgt.hw'),
                    value: 'hw'
                },
                {
                    label: this.$t('cusMgt.sl'),
                    value: 'sl'
                }
            ],
            modalLoading: true,
            curAcType: 'ev',
            evList: [],//评测列表
            voteList: [],//投票列表
            quList: [],//问卷列表
            hwList: [],//作业列表
            slList: [],//自主学习
            delSelection: [],
            selections: [],
            createStuList: [],//创建自定义名单学生列表
            tableHeight: 100,
            classColumn: [
                {
                    title: ' ',
                    slot: 'picture',
                    align: 'left ',
                    width: '150'
                },
                {
                    title: this.$t('courseManage.classroom.studentTableC1'),
                    slot: 'no',
                    align: 'center',
                    sortable: true
                },
                {
                    title: this.$t('courseManage.classroom.studentTableC2'),
                    key: 'name',
                    align: 'center '
                },
                {
                    title: this.$t('courseManage.classroom.studentTableC7'),
                    key: 'id',
                    align: 'center',
                    sortable: true
                },
                {
                    title: this.$t('courseManage.classroom.studentTableC5'),
                    slot: 'groupId',
                    align: 'center',
                    sortable: true
                },
                {
                    title: this.$t('courseManage.classroom.studentTableC6'),
                    slot: 'groupName',
                    align: 'center'
                }
            ],
            listColumn: [
                {
                    type: 'selection',
                    align: 'center ',
                    width: '150'
                },
                {
                    title: ' ',
                    slot: 'picture',
                    align: 'center ',
                    width: '150'
                },
                {
                    title: this.$t('cusMgt.tableCol1'),
                    type: 'index',
                    align: 'center ',
                    width: '150'
                },
                {
                    title: this.$t('courseManage.classroom.studentTableC2'),
                    key: 'name',
                    align: 'center '
                },
                {
                    title: this.$t('courseManage.classroom.studentTableC7'),
                    key: 'id',
                    align: 'center',
                    sortable: true
                },
                {
                    title: this.$t('courseManage.classroom.studentTableC8'),
                    slot: 'type',
                    align: 'center'
                },
                {
                    title: this.$t('cusMgt.className'),
                    key: 'className',
                    align: 'center'
                },
            ],
            itemNum: 0,
            editClassStatus: false,
            baseEditStatus: true,
            showQrStatus: false,
            listLoading: false,
            newSlStatus: false,
            stuLoading: false,
            listType: 'private',
            tabName: 'activity',
            addCusStatus: false,
            addStuStatus: false,
            joinQRcode: undefined,
            curCusIndex: 0,
            curClassIndex: 0,
            courseList: [],
            courseListShow: [],
            addCusInfo: {
                name: '',
                no: '',
                id: '',
                desc: ''
            },
            listName: '',
            ruleAddCus: {
                name: [
                    { required: true, message: this.$t('cusMgt.nameHolder'), trigger: 'change' }
                ],
                no: [
                    { validator: validateCode, trigger: 'change' }
                ]
            },
            stuList: []
        }
    },
    methods: {
        confirmRename() {
            let stulist = this.stuList.find(item => {
                return item.id == this.teaClassList[this.curClassIndex].stulist
            })
            if (stulist) {
                stulist.name = this.edName
                stulist.type = 'student'
                stulist.scope = 'private'
                // let params = {
                //     stuList: stulist,
                //     scope: "private"
                // }
                let params = stulist
                this.$api.courseMgmt.upsertStulist(params).then(
                    res => {
                        this.$Message.success(this.$t('cusMgt.updOk'))
                        this.teaClassList[this.curClassIndex].listName = this.edName
                    },
                    err => {
                        this.$Message.error(this.$t('cusMgt.updOk'))
                    }
                ).finally(() => {
                    this.listLoading = false
                })
            }
        },
        editListName() {
            this.edName = this.teaClassList[this.curClassIndex].listName
            this.renameBefore = this.teaClassList[this.curClassIndex].listName
            this.editClassStatus = true
        },
        copyUrl() {
            this.$copyText(encodeURI(this.inviteUrl)).then(
                ok => {
                    this.$Message.success(this.$t("settings.copyModal1"))
                },
                fail => {
                    this.$Message.error(this.$t("settings.copyModal2"))
                }
            )
        },
        initData() {
            this.cusType = [
                {
                    label: this.$t('cusMgt.privCus'),
                    value: 'private'
                }
            ]
            if (this.$store.state.userInfo.hasSchool) {
                this.cusType.unshift({
                    label: this.$t('cusMgt.scCus'),
                    value: 'school'
                })
                this.listType = 'school'
            }
        },
        selectTab(tab) {
            this.tabName = tab
            if (this.tabName == 'activity') {
                this.getActivityList()
            } else if (this.tabName == 'stus') {
                this.getStuList()
            }
        },
        dropdownStates(flag) {
            if (!flag) this.tabClick()
        },
        //课程选项卡点击事件
        tabClick(tab) {
            this.curCusIndex = 0
            this.listType = tab
            this.courseListShow = this.courseList.filter(item => {
                return item.scope == this.listType
            })
            this.selectCourse(0)

        },
        //显示课表模式
        showSchedule() {
            let promises = []
            this.listLoading = true
            this.isShowSchd = !this.isShowSchd
            this.courseListShow.forEach(item => {
                let requestData = {
                    'code': this.$store.state.userInfo.schoolCode,
                    'scope': 'school',
                    'id': item.id
                }
                promises.push(this.$api.courseMgmt.findCusInfo(requestData))
            })
            Promise.all(promises).then(
                resAll => {
                    resAll.forEach(async (res, index) => {
                        if (res.courses && res.courses.length > 0) {
                            res.courses[0].schedule = res.courses[0].schedule ? res.courses[0].schedule : []
                            //过滤当前教师的schedule
                            res.courses[0].schedule = res.courses[0].schedule.filter(item => {
                                return item.teacherId == this.$store.state.userInfo.TEAMModelId
                            })
                            // 获取自定义名单信息
                            let ids = res.courses[0].schedule.map(item => {
                                return item.stulist
                            })
                            for (let i = 0; i < ids.length; i++) {
                                if (!ids[i]) {
                                    ids.splice(i, 1)
                                    i--
                                }
                            }
                            if (ids.length) {
                                try {
                                    let listRes = await this.getListInfo([...ids])
                                    if (listRes) {
                                        let already = this.stuList.map(item => {
                                            return item.id
                                        })
                                        listRes.stuList.forEach(item => {
                                            if (!already.includes(item.id)) {
                                                this.stuList.push(item)
                                            }
                                        })
                                    }
                                } catch (e) {
                                    this.$Message.error('API ERROR!')
                                }
                            }

                            res.courses[0].schedule.forEach(item => {
                                //补充教室信息
                                if (item.classId) {
                                    let classInfo = this.classList.find(classItem => {
                                        return classItem.id == item.classId
                                    })
                                    item.classInfo = {
                                        id: item.classId,
                                        name: classInfo ? classInfo.name : '--'
                                    }
                                }
                                //补充名单信息
                                if (ids.length && item.stulist) {
                                    let listInfo = this.stuList.find(listItem => {
                                        return listItem.id == item.stulist
                                    })
                                    item.listName = listInfo ? listInfo.name : '--'
                                    item.students = listInfo ? listInfo.students : []
                                    item.fullStu = false
                                }
                            })
                        }
                        this.$set(this.courseListShow, index, res.courses[0])
                    })
                },
                err => {
                    this.$Message.error('API ERROR')
                }
            ).finally(() => {
                this.listLoading = false
            })
        },
        //获取学生名单
        getStuList() {
            let schedule = this.teaClassList[this.curClassIndex]
            //先判断自定义名单
            if (schedule && schedule.stulist && !schedule.fullStu) {
                //TODO 学生id换详细信息
                let slist = this.stuList.find(item => {
                    return item.id == schedule.stulist
                })
                console.log('名单信息', slist)
                this.listLoading = true
                this.$api.courseMgmt.findStuSummary({
                    students: slist ? slist.students.filter(item => {
                        return !item.type
                    }) : [],
                    tmdIds: slist ? slist.tmids : []
                }).then(
                    res => {
                        res.stus = res.stus || []
                        res.stus.forEach(item => {
                            //TODO 设置班级名称
                            item.type = 'school'
                            //老师还在学校，被移除学校后这里就拿不到学校的数据了
                            if (this.classList) {
                                let curClass = this.classList.find(classItem => {
                                    return classItem.id == item.classId
                                })
                                item.className = curClass ? curClass.name : '—'
                            }
                        })
                        res.tmdinfos = res.tmdinfos || []
                        res.tmdinfos.forEach(item => {
                            item.type = 'TMID'
                        })
                        //处理醍摩豆Id顺序
                        res.tmdinfos.sort((a, b) => {
                            return slist.tmids.indexOf(a.id) - slist.tmids.indexOf(b.id)
                        })
                        let originIndex = this.courseListShow[this.curCusIndex].schedule.findIndex(item => {
                            return item.stulist == schedule.stulist
                        })
                        this.$set(this.courseListShow[this.curCusIndex].schedule[originIndex], 'allStu', res.stus.concat(res.tmdinfos))
                        this.$set(this.courseListShow[this.curCusIndex].schedule[originIndex], 'fullStu', true)
                    }
                ).finally(() => {
                    setTimeout(() => {
                        this.stuLoading = false
                        this.listLoading = false
                    }, 500)
                })
                return
            }
        },
        //初始化课程课程信息时，根据班级ids查询名单信息
        getStuByClassIds(classIds) {
            let params = {
                'school_code': this.listType == 'school' ? this.$store.state.userInfo.schoolCode : this.$store.state.userInfo.TEAMModelId,
                'ids': classIds
            }
            return this.$api.schoolSetting.getStusByClassId(params)
        },
        /**获取type对应的label */
        getTypeLabel(code) {
            for (let item of this.$GLOBAL.EV_TYPE()) {
                if (item.value == code) {
                    return item.label
                }
            }
        },
        //时间戳转换
        dateFormat(timestamp) {
            var date = new Date(timestamp)
            var Y = date.getFullYear() + '/'
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/'
            var D = date.getDate() + ' '
            return Y + M + D;
        },
        //获取活动列表
        getActivityList() {
            switch (this.curAcType) {
                case 'ev':
                    if (this.evList.length) break //首次（数组为空）访问数据
                    this.findTchAc('Exam')
                    break
                case 'vote':
                    if (this.voteList.length) break
                    this.findTchAc('Vote')
                    break
                case 'qu':
                    if (this.quList.length) break
                    this.findTchAc('Survey')
                    break
                case 'hw':
                    if (this.hwList.length) break
                    this.findTchAc('Homework')
                    break
                case 'sl':
                    if (this.slList.length) break
                    this.findTchAc('Learn')
                    break
                default:
                    break
            }
        },
        //获取活动列表
        findTchAc(type) {
            let requestData = {
                school: this.$store.state.userInfo.schoolCode,
                classes: this.$store.state.userInfo.teachClasses,
                userid: this.$store.state.userInfo.TEAMModelId,
                pk: type
            }
            this.$api.courseMgmt.findTchAc(requestData).then(
                res => {
                    if (!res.error) {
                        switch (this.curAcType) {
                            case 'ev':
                                this.evList = res.datas
                                break
                            case 'vote':
                                this.voteList = res.datas
                                break
                            case 'qu':
                                this.quList = res.datas
                                break
                            case 'hw':
                                this.hwList = res.datas
                                break
                            case 'sl':
                                this.slList = res.datas
                                break
                            default:
                                break
                        }
                    }
                },
                err => {
                    this.$Message.error('API error')
                }
            )
        },
        //获取评测列表
        getEvList() {
            let requestData = {
                code: this.$store.state.userInfo.TEAMModelId
            }
            this.$api.learnActivity.FindExamInfo(requestData).then(
                res => {
                    if (!res.error) {
                        res.examInfo = res.examInfo.sort((a, b) => {
                            return a.createTime - b.createTime > 0 ? -1 : 1
                        })
                        this.evList = res.examInfo
                    } else {
                        this.$Message.error('API ERROR!')
                    }
                }
            )
        },
        //删除学生
        delStudents() {
            if (this.delSelection.length > 0) {
                let names = this.delSelection.map(item => {
                    return item.name
                })
                this.$Modal.confirm({
                    title: this.$t('cusMgt.delStu'),
                    content: `${this.$t('cusMgt.delStuContent')}${names.join(', ')}?`,
                    onOk: () => {
                        this.listLoading = true
                        let delIds = this.delSelection.map(item => {
                            return item.id
                        })
                        //删除学生更新stulist
                        // 当前名单对应的课程安排用于更新UI
                        let schedule = this.courseListShow[this.curCusIndex].schedule.find(item => {
                            return item.stulist == this.teaClassList[this.curClassIndex].stulist && !item.classId //只有自定名单能添加学生
                        })
                        //当前名单完整信息 用于更新名单API
                        let stulist = this.stuList.find(item => {
                            return item.id == this.teaClassList[this.curClassIndex].stulist
                        })
                        //检查校内账号
                        for (let i = 0; i < stulist.students.length; i++) {
                            if (delIds.includes(stulist.students[i].id)) {
                                stulist.students.splice(i, 1)
                                i--
                            }
                        }
                        //检查醍摩豆id
                        for (let i = 0; i < stulist.tmids.length; i++) {
                            if (delIds.includes(stulist.tmids[i])) {
                                stulist.tmids.splice(i, 1)
                                i--
                            }
                        }
                        stulist.type = 'student'
                        stulist.scope = 'private'
                        // let params = {
                        //     stuList: stulist,
                        //     scope: "private"
                        // }
                        let params = stulist
                        this.$api.courseMgmt.upsertStulist(params).then(
                            res => {
                                this.$Message.success(this.$t('cusMgt.delOk'))
                                console.log(schedule)
                                if (schedule) {
                                    for (let i = 0; i < schedule.allStu.length; i++) {
                                        if (delIds.includes(schedule.allStu[i].id)) {
                                            schedule.allStu.splice(i, 1)
                                            i--
                                        }
                                    }
                                    // let s = this._.cloneDeep(schedule.students)
                                    // schedule.students = []
                                    // schedule.students = s
                                }
                            },
                            err => {
                                this.$Message.error(this.$t('cusMgt.delErr'))
                            }
                        ).finally(() => {
                            this.listLoading = false
                        })
                    }
                })
            } else {
                this.$Message.warning(this.$t('cusMgt.delStuTips'))
            }

        },
        //确认添加学生
        confirmAddStu() {
            if (this.selections.length > 0) {
                // 当前名单对应的课程安排用于更新UI
                let schedule = this.courseListShow[this.curCusIndex].schedule.find(item => {
                    return item.stulist == this.teaClassList[this.curClassIndex].stulist && !item.classId //只有自定名单能添加学生，所以classId == ‘’
                })
                //当前名单完整信息 用于更新名单API
                let stulist = this.stuList.find(item => {
                    return item.id == this.teaClassList[this.curClassIndex].stulist
                })
                let stuIds = []
                if (schedule) {
                    stuIds = schedule.allStu.map(item => {
                        return item.id
                    })
                }
                this.selections.forEach(item => {
                    if (!stuIds.includes(item.id)) { //如果已经有学生，根据账号去重
                        let index = schedule.allStu.findIndex(s => {
                            return s.type == 'TMID'
                        })
                        if (index > -1) {
                            schedule.allStu.splice(index, 0, {
                                id: item.id,
                                code: 'Base-' + this.$store.state.userInfo.schoolCode, //学生所在学校的编码，这里先传当前学校编码，暂无未考虑跨校名单
                                schoolId: this.$store.state.userInfo.schoolCode, //学生所在学校的编码，这里先传当前学校编码，暂无未考虑跨校名单
                                name: item.name,
                                className: item.className,
                                classId: item.classId,
                                type: 'school'
                            })
                        } else {
                            schedule.allStu.push({
                                id: item.id,
                                code: 'Base-' + this.$store.state.userInfo.schoolCode, //学生所在学校的编码，这里先传当前学校编码，暂无未考虑跨校名单
                                schoolId: this.$store.state.userInfo.schoolCode, //学生所在学校的编码，这里先传当前学校编码，暂无未考虑跨校名单
                                name: item.name,
                                className: item.className,
                                classId: item.classId,
                                type: 'school'
                            })
                        }
                        stulist.students.push({
                            id: item.id,
                            code: 'Base-' + this.$store.state.userInfo.schoolCode, //学生所在学校的编码，这里先传当前学校编码，暂无未考虑跨校名单
                            schoolId: this.$store.state.userInfo.schoolCode //学生所在学校的编码，这里先传当前学校编码，暂无未考虑跨校名单
                        })
                    }
                })
                this.listLoading = true
                this.saveStuList(stulist)
            }
        },

        //检查课程是否为“空”
        checkNotice(content) {
            if (!content || content == '' || content == '<p><br></p>') {
                return false
            } else {
                return true
            }
        },
        //移除课程名单
        removeStuList() {
            let _this = this
            this.$Modal.confirm({
                title: this.$t('cusMgt.delClassTitle'),
                content: `${this.$t('cusMgt.delStuContent')}${this.teaClassList[this.curClassIndex].listName}?`,
                onOk: function () {
                    _this.listLoading = true
                    let index = _this.curClassIndex
                    _this.curClassIndex = 0
                    _this.courseListShow[_this.curCusIndex].schedule.splice(index, 1)
                    _this.updCusInfo()
                }
            })
        },
        editCus() {
            this.addCusInfo = this._.cloneDeep(this.courseListShow[this.curCusIndex])
            this.addCusStatus = true
        },
        //创建二维码
        createQRCode(url) {
            this.$nextTick(() => {
                // 此时已经渲染完成
                if (this.joinQRcode == undefined) {
                    let qrcode = new QRCode('qrcode', {
                        width: 280, // 设置宽度，单位像素
                        height: 280, // 设置高度，单位像素
                        text: url // 设置二维码内容或跳转地址
                    })
                    this.joinQRcode = qrcode
                } else {
                    this.joinQRcode.clear()
                    this.joinQRcode.makeCode(url)
                }
                let dom = document.getElementById('qrcode')
                if (dom) dom.title = ''
            })
        },
        showQrCode(index) {
            this.showQrStatus = true
            let tName = this.$store.state.userInfo.name
            let listId = this.teaClassList[index].stulist
            let listName = this.teaClassList[index].listName
            let cusName = this.courseListShow[this.curCusIndex].name
            let stulistInfo = this.stuList.find(item => {
                return item.id == listId
            })
            console.log('名单列表', stulistInfo)
            if (stulistInfo) this.stuListNo = stulistInfo.no

            // //这里需要根据站点动态拼接域名
            console.log(window.location)
            let host = window.location.origin
            let callbackUrl = `${host}/joinclass?listName=${listName}&tName=${tName}&listNo=${this.stuListNo}&cusName=${cusName}`
            this.inviteUrl = callbackUrl
            console.log(callbackUrl)
            this.createQRCode(callbackUrl)
        },
        //确认添加自定义名单
        confirmCreateList() {
            if (!this.listName && this.type == 'create') {
                this.$Message.warning(this.$t('cusMgt.nameWarning'))
                this.modalLoading = false
                setTimeout(() => {
                    this.modalLoading = true
                }, 0)
            }
            else if (!this.listId && this.type == 'select') {
                this.$Message.warning(this.$t('cusMgt.selectListTips'))
                this.modalLoading = false
                setTimeout(() => {
                    this.modalLoading = true
                }, 0)
            }
            else if (this.type == 'create') {
                let stuList = {
                    name: this.listName,
                    id: this.$tools.guid(),
                    code: this.$store.state.userInfo.TEAMModelId,
                    creatorId: this.$store.state.userInfo.TEAMModelId,
                    students: [],
                    tmids: [],
                    course: null
                }
                this.saveStuList(stuList, true)
            }
            else if (this.type == 'select') {
                let s = this.stuList.find(item => {
                    return item.id == this.listId
                })
                if (s) {
                    this.courseListShow[this.curCusIndex].schedule = this.courseListShow[this.curCusIndex].schedule || []
                    //检查重复添加
                    let res = this.courseListShow[this.curCusIndex].schedule.find(item => {
                        return item.stulist == this.listId && item.teacherId == this.$store.state.userInfo.TEAMModelId
                    })
                    if (res) {
                        this.$Message.warning(`${res.listName}${this.$t('cusMgt.alreadyExist')}`)
                        this.modalLoading = false
                        setTimeout(() => {
                            this.modalLoading = true
                        }, 0)
                    } else {
                        this.courseListShow[this.curCusIndex].schedule.push({
                            classId: '',
                            notice: '',
                            listName: s.name,
                            stulist: this.listId,
                            teacherId: this.$store.state.userInfo.TEAMModelId,
                            teacherName: this.$store.state.userInfo.name,
                            students: s.students,
                            time: []
                        })
                        this.updCusInfo()
                    }
                }
            }
        },
        // 保存名单
        saveStuList(stuList, isUpdCus) {
            stuList.type = 'student'
            stuList.scope = 'private'
            // let params = {
            //     stuList,
            //     scope: "private"
            // }
            let params = stuList
            this.$api.courseMgmt.upsertStulist(params).then(
                res => {
                    this.newSlStatus = false
                    //TODO 添加到当前课程schedule
                    if (isUpdCus) {
                        this.courseListShow[this.curCusIndex].schedule = this.courseListShow[this.curCusIndex].schedule || []
                        this.courseListShow[this.curCusIndex].schedule.push({
                            classId: '',
                            notice: '',
                            listName: this.listName,
                            stulist: stuList.id,
                            teacherId: this.$store.state.userInfo.TEAMModelId,
                            teacherName: this.$store.state.userInfo.name,
                            students: stuList.students,
                            time: []
                        })
                        this.updCusInfo()
                    }
                },
                err => {
                    this.modalLoading = false
                    setTimeout(() => {
                        this.modalLoading = true
                    }, 0)
                }
            ).finally(() => {
                this.listLoading = false
            })
        },

        changeClassroom(index) {
            this.curClassIndex = index
            if (this.tabName == 'stus') {
                this.getStuList()
            } else if (this.tabName == 'activity') {
                this.getActivityList()
            }
        },
        //查看课堂记录详情
        toClassRecoerd() {
            this.listLoading = true
            setTimeout(() => {
                this.$router.push({ path: '/home/classRecord' })
            }, 500)
        },
        // 查看评测详情
        toAcDetail(index) {
            let acId = this.acList[index].id
            let owner = this.acList[index].owner
            switch (this.curAcType) {
                case 'ev':
                    this.$router.push({
                        path: '/home/evDetail',
                        query: {
                            examId: this.acList[index].id,
                            code: this.acList[index].code
                        }
                    })
                    break
                case 'vote':
                    this.$router.push({
                        path: owner == 'school' ? '/home/manageVote' : '/home/personalVote',
                        query: {
                            acId: acId
                        }
                    })
                    break
                case 'qu':
                    this.$router.push({
                        path: owner == 'school' ? '/home/manageQuestionnaire' : '/home/personalSurvey',
                        query: {
                            acId: acId
                        }
                    })
                    break
                case 'hw':
                    this.$router.push({
                        path: '/home/manageHomeWork',
                        query: {
                            acId: acId
                        }
                    })
                    break
                case 'sl':
                    this.$router.push({
                        path: '/home/SelfLearn',
                        query: {
                            acId: this.acList[index].id
                        }
                    })
                    break
                default:
                    break
            }

        },
        //删除个人课程
        delCourse() {
            this.$Modal.confirm({
                title: this.$t('cusMgt.delCus'),
                content: `${this.$t('cusMgt.delContent')}${this.courseListShow[this.curCusIndex].name}?`,
                onOk: () => {
                    this.$api.courseMgmt.deleteCourse({
                        id: this.courseListShow[this.curCusIndex].id,
                        code: this.courseListShow[this.curCusIndex].code.replace('Course-', ''),
                        scope: 'private'
                    }).then(
                        res => {
                            if (!res.error) {
                                //删除原始数据
                                let cusId = this.courseListShow[this.curCusIndex].id
                                let originIndex = this.courseList.findIndex(item => {
                                    return item.id == cusId
                                })
                                if (originIndex > -1) {
                                    this.courseList.splice(originIndex, 1)
                                }

                                // 删除列表数据
                                let index = this.curCusIndex
                                this.curCusIndex = 0
                                this.courseListShow.splice(index, 1)
                                this.$Message.success(this.$t('cusMgt.delOk'))
                            } else {
                                this.$Message.error(this.$t('cusMgt.delErr'))
                            }
                        },
                        err => {
                            this.$Message.error(this.$t('cusMgt.delErr'))
                        }
                    )
                }
            })
        },
        selectCourse(index) {
            this.curClassIndex = 0
            this.curCusIndex = index
            if (this.courseListShow.length && !this.courseListShow[this.curCusIndex].schedule) {
                this.getCusInfo()
            } else {
                this.changeClassroom(0)
            }
        },
        // 新增个人课程
        confirmAddCus() {
            this.$refs['addCusInfo'].validate((valid) => {
                if (valid) {
                    let requestData = {
                        option: this.addCusInfo.id ? 'update' : 'insert',
                        scope: 'private',
                        course: {
                            name: this.addCusInfo.name,
                            no: this.addCusInfo.no,
                            desc: this.addCusInfo.desc,
                            id: this.addCusInfo.id || this.$tools.guid(),
                            code: this.$store.state.userInfo.TEAMModelId,
                            schedule: this.addCusInfo.schedule || [],
                            scope: 'private',
                            creatorId: this.$store.state.userInfo.schoolCode
                        }
                    }
                    this.$api.courseMgmt.saveOrUpdateCourse(requestData).then(
                        (res) => {
                            if (!res.error) {
                                this.$Message.success(this.addCusInfo.id ? this.$t('cusMgt.editOk') : this.$t('cusMgt.addOk'))
                                //新增课程
                                if (!this.addCusInfo.id) {
                                    this.courseList.push(requestData.course)
                                    this.courseListShow.push(requestData.course)
                                }
                                // 修改课程
                                else {
                                    let index = this.courseListShow.findIndex(item => {
                                        return item.id == this.addCusInfo.id
                                    })
                                    if (index > -1) {
                                        this.courseListShow.splice(index, 1, requestData.course)
                                    }
                                    let oraginIndex = this.courseList.findIndex(item => {
                                        return item.id == this.addCusInfo.id
                                    })
                                    if (index > -1) {
                                        this.courseList.splice(oraginIndex, 1, requestData.course)
                                    }
                                }
                                this.addCusStatus = false
                            } else {
                                this.$Message.error(this.addCusInfo.id ? this.$t('cusMgt.editErr') : this.$t('cusMgt.addErr'))
                                this.modalLoading = false
                                setTimeout(() => {
                                    this.modalLoading = true
                                }, 0)
                            }
                        },
                        (err) => {
                            this.modalLoading = false
                            setTimeout(() => {
                                this.modalLoading = true
                            }, 0)
                        }
                    )
                } else {
                    this.$Message.error(this.$t('cusMgt.formTips'));
                    this.modalLoading = false
                    setTimeout(() => {
                        this.modalLoading = true
                    }, 0)
                }
            })
        },
        // 更新个人课程
        updCusInfo() {
            this.courseListShow[this.curCusIndex].code = this.courseListShow[this.curCusIndex].code.replace('Course-', '')
            this.$api.courseMgmt.saveOrUpdateCourse({
                course: this.courseListShow[this.curCusIndex],
                option: 'update',
                scope: this.courseListShow[this.curCusIndex].scope || 'private'
            }).then(
                res => {
                    this.$Message.success(this.$t('cusMgt.updateOk'))
                    this.getCusInfo()
                }
            ).catch(() => {
                this.$Message.error(this.$t('cusMgt.updateErr'))
            }).finally(() => {
                this.listLoading = false
                this.newSlStatus = false
                this.listName = ''
            })
        },
        //获取个人和校本课程列表，API调整后没有进行分开处理
        getCourseList() {
            this.listLoading = true
            let requestData = {
                'code': this.$store.state.userInfo.TEAMModelId,
                'schoolId': this.$store.state.userInfo.schoolCode,
                'scope': 'private'
            }
            this.$api.courseMgmt.findCourse(requestData).then(
                (res) => {
                    if (res.error == null) {
                        this.courseList = res.courses
                        //设置当前课程列表 
                        this.courseListShow = this.courseList.filter(item => {
                            return item.scope == this.listType
                        })
                        if (this.courseListShow.length == 0) { //如果没有标准课程，则设置默认为个人课程
                            this.listType = 'private'
                            this.courseListShow = this.courseList.filter(item => {
                                return item.scope == this.listType
                            })
                        }
                        if (this.courseListShow.length) {
                            this.getCusInfo()
                        } else {
                            this.listLoading = false
                        }

                    } else {
                        this.$Message.error('API ERROR!')
                        this.listLoading = false
                    }
                },
                (err) => {
                    this.$Message.error('API ERROR!')
                    this.listLoading = false
                }
            )
        },
        //获取课程详细信息
        getCusInfo() {
            this.listLoading = true
            let requestData = {
                'code': this.listType == 'school' ? this.$store.state.userInfo.schoolCode : this.$store.state.userInfo.TEAMModelId,
                'scope': this.listType,
                'id': this.courseListShow[this.curCusIndex].id
            }
            this.$api.courseMgmt.findCusInfo(requestData).then(
                async (res) => {
                    if (!res.error) {
                        if (res.courses && res.courses.length > 0) {
                            res.courses[0].schedule = res.courses[0].schedule ? res.courses[0].schedule : []
                            //过滤当前教师的schedule 
                            res.courses[0].schedule = res.courses[0].schedule.filter(item => {
                                return item.teacherId == this.$store.state.userInfo.TEAMModelId
                            })
                            // 获取自定义名单信息
                            let ids = res.courses[0].schedule.map(item => {
                                return item.stulist
                            })
                            for (let i = 0; i < ids.length; i++) {
                                if (!ids[i]) {
                                    ids.splice(i, 1)
                                    i--
                                }
                            }
                            if (ids.length) {
                                try {
                                    let listRes = await this.getListInfo([...ids])
                                    if (listRes) {
                                        let already = this.stuList.map(item => {
                                            return item.id
                                        })
                                        listRes.stuList.forEach(item => {
                                            if (!already.includes(item.id)) {
                                                this.stuList.push(item)
                                            }
                                        })
                                    }
                                } catch (e) {
                                    this.$Message.error('API ERROR')
                                }
                            }

                            //获取班级名单信息（为了列表显示学生人数）
                            let classIds = res.courses[0].schedule.map(item => {
                                return item.classId
                            })
                            for (let i = 0; i < classIds.length; i++) {
                                if (!classIds[i]) {
                                    classIds.splice(i, 1)
                                    i--
                                }
                            }
                            let classStus = null
                            if (classIds.length) {
                                try {
                                    classStus = await this.getStuByClassIds(classIds)
                                } catch (e) {
                                    this.$Message.error('API ERROR')
                                }
                            }
                            res.courses[0].schedule.forEach(item => {
                                //补充教室信息
                                if (item.classId) {
                                    let classInfo = this.classList.find(classItem => {
                                        return classItem.id == item.classId
                                    })
                                    item.classInfo = {
                                        id: item.classId,
                                        name: classInfo ? classInfo.name : this.$t('cusMgt.hasDelClass')
                                    }
                                    //根据上面查看班级学生的返回结果补充学生名单
                                    if (classStus && classStus.list) {
                                        let studentsInfo = classStus.list.find(i => {
                                            return i.class.id == item.classId
                                        })
                                        if (studentsInfo) {
                                            item.allStu = studentsInfo.students.concat(studentsInfo.tmdids)
                                            item.fullStu = true
                                        }
                                    }
                                }
                                //补充名单信息
                                if (ids.length && item.stulist) {
                                    let listInfo = this.stuList.find(listItem => {
                                        return listItem.id == item.stulist
                                    })
                                    item.listName = listInfo ? listInfo.name : this.$t('cusMgt.hasDelList')
                                    if (listInfo) {
                                        let s = this._.cloneDeep(listInfo.students)
                                        listInfo.tmids.forEach(tidItem => {
                                            s.push({
                                                id: tidItem,
                                                type: 'TMID'
                                            })
                                        })
                                        item.allStu = s
                                    } else {
                                        item.allStu = []
                                    }
                                    item.fullStu = false
                                }
                                //统一数据格式
                                item.classId = item.classId || undefined
                                item.stulist = item.stulist || undefined
                            })
                        }
                        this.$set(this.courseListShow, this.curCusIndex, res.courses[0])
                        this.changeClassroom(0)
                    }
                }
            ).finally(() => {
                this.listLoading = false
            })
        },
        //根据id获取stulist信息
        getListInfo(ids) {
            let requestData = {
                'code': this.listType == 'school' ? this.$store.state.userInfo.schoolCode : this.$store.state.userInfo.TEAMModelId,
                'scope': this.listType,
                'ids': ids
            }
            return this.$api.courseMgmt.findListSummary(requestData)
        },
        getAllStuList() {
            let params = {
                code: this.$store.state.userInfo.TEAMModelId,
                scope: 'private'
            }
            this.$api.courseMgmt.findStulist(params).then(
                res => {
                    this.stuList = res.stuList
                },
                err => {
                    this.$Message.error(this.$t('cusMgt.listAPIErr'))
                }
            )
        }
    },
    created() {
        this.getAllStuList()
        //直接读取登录成功拿到得学校基础信息
        this.$store.dispatch('user/getSchoolProfile').then(res => {
            this.isFirst = true
            this.getCourseList()
        })
        this.$nextTick(() => {
            let dom = document.getElementById('table-height')
            this.tableHeight = dom.offsetHeight - 50
        })
        this.initData()
    },
    watch: {
        '$store.state.userInfo.schoolCode': {
            handler(n, o) {
                this.getCourseList()
            }
        }
    },
    computed: {
        ...mapGetters({
            classList: 'user/getClasses', // 教室ID,
            schoolBase: 'user/getSchoolBase', // 学校基础设置
        }),
        //课程学段
        cusPd() {
            let pds = this.courseList.map(item => {
                return item.period
            })
            let res = []
            pds.forEach(item => {
                let r = res.find(resItem => {
                    return item && resItem && item.id == resItem.id
                })
                if (!r && item && item.id) {
                    res.push(item)
                }
            })
            if (res.length) {
                this.curPd = res[0].id
            }
            return res
        },
        //课程表当前显示的课程
        schdData() {
            let pdCus = []
            if (this.listType == 'school') {
                pdCus = this.courseListShow.filter(item => {
                    return item.period.id == this.curPd && item.schedule
                })
            }
            return pdCus
        },
        //无教室课程
        noClassCus() {
            return this.schdData.filter(item => {
                let res = item.schedule.find(schdItem => {
                    return schdItem.classId && schdItem.teacherId == this.$store.state.userInfo.TEAMModelId
                })
                return !res
            })
        },
        //无时段课程
        noTimeCus() {
            return this.schdData.filter(item => {
                let res = item.schedule.find(schdItem => {
                    return (schdItem.time && schdItem.time.length) && schdItem.teacherId == this.$store.state.userInfo.TEAMModelId
                })
                return !res
            })
        },
        //是否为默认名单
        isDefList() {
            if (this.teaClassList[this.curClassIndex] && this.teaClassList[this.curClassIndex].stulist) {
                return false //有stulist则不是默认名单
            } else {
                return true
            }
        },
        //课程名单 —— 去重处理后的数据
        teaClassList() {
            if (this.courseListShow.length > 0 && this.courseListShow[this.curCusIndex] && this.courseListShow[this.curCusIndex].schedule) {
                //一个名单被安排到不同教室上课会出现重复数据，这里需要去重
                let list = []
                this.courseListShow[this.curCusIndex].schedule.forEach(item => {
                    // 一个名单被安排到不同教室上课会出现重复数据，这里做去重操作
                    let has = list.find(listItem => {
                        return listItem.classId == item.classId && item.stulist == listItem.stulist
                    })
                    if (!has) {
                        list.push(item)
                    }
                })
                return list
            } else {
                return []
            }
        },
        students() {
            if (this.teaClassList && this.teaClassList[this.curClassIndex]) {
                return this.teaClassList[this.curClassIndex].allStu
            }
            else {
                return []
            }
        },
        //当前班级活动列表
        acList() {
            let id = this.teaClassList[this.curClassIndex] ? this.teaClassList[this.curClassIndex].stulist || this.teaClassList[this.curClassIndex].classId : ''
            if (!id) {
                return []
            }
            let data = []
            switch (this.curAcType) {
                case 'ev':
                    data = this.evList
                    break
                case 'vote':
                    data = this.voteList
                    break
                case 'qu':
                    data = this.quList
                    break
                case 'hw':
                    data = this.hwList
                    break
                case 'sl':
                    data = this.slList
                    break
                default:
                    break
            }
            data.sort((a, b) => {
                return b.startTime - a.startTime
            })
            return data.filter(item => {
                return (item.classes && item.classes.includes(id)) || ((item.stuLists && item.stuLists.includes(id)))
            })
        },
        cusTypeLabel() {
            let curObj = this.cusType.find(item => {
                return item.value == this.listType
            })
            return curObj.label
        },
        acTypeLabel() {
            let curObj = this.acTypeList.find(item => {
                return item.value == this.curAcType
            })
            return curObj.label
        },
    }
}
</script>
<style scoped lang="less">
@import "./MyCourse.less";
</style>
<style>
.cus-list-tab .ivu-tabs-bar {
    height: 45px;
}
.cus-list-tab .ivu-tabs-nav {
    height: 45px;
    line-height: 30px;
}
.course-classroom-list-header .ivu-poptip-inner {
    background-color: #404040;
    color: white;
}

.course-classroom-list-header .ivu-poptip-arrow::after {
    display: none;
}

.course-classroom-list-header .ivu-poptip-title:after,
.course-classroom-list-header .ivu-poptip-title {
    display: none;
}
.course-classroom-info-content #loadingBox {
    margin-top: 0px !important;
}
.class-record-wrap .ivu-list-item:hover {
    background: var(--active-item-start);
}
</style>