<script setup lang="ts">
import { GradeItem } from "@/api/school/types";
import { watch } from "vue";
import { useUserStore } from "@/store/modules/user";

import { getGradeList, getGradeStudents } from "@/api/school";

const userStore = useUserStore();
defineOptions({
  name: "ClassIndex",
  inheritAttrs: false,
});
/**
 * 班级数据
 */
const gradeData = ref<GradeItem[]>();
// 班级status: 0全部1未结课2已结课
let gradeStatus = ref(0);
async function getGradeItems(schoolId: number, status: number) {
  getGradeList(schoolId, status)
    .then(({ data }) => {
      gradeData.value = data;
    })
    .catch((error) => {
      console.log(error);
    });
}
// 请求班级学生数据
async function getStudentList(grade: GradeItem) {
  getGradeStudents(grade.id)
    .then(({ data }) => {
      grade.students = data;
    })
    .catch((error) => {
      console.log(error);
    });
}
// 改变班级状态
function changeStatus() {
  getGradeItems(userStore.schoolId, gradeStatus.value);
}
// 打开班级请求学生
function collapseGrade(grade: GradeItem) {
  if (!grade.students || grade.students.length === 0) {
    getStudentList(grade);
  }
}
onMounted(() => {
  // 获取班级
  getGradeItems(userStore.schoolId, gradeStatus.value);
});
watch(
  () => userStore.schoolId,
  (newValue, oldValue) => {
    console.log("userStore.schoolId", newValue, oldValue);
    // 学校切换后重新加载数据
    getGradeItems(newValue, gradeStatus.value);
  }
);
</script>

<template>
  <div class="class-container">
    <!-- 班级选择 -->
    <div class="class-select">
      <el-select
        v-model="gradeStatus"
        placeholder="全部班级"
        size="large"
        @change="changeStatus()"
      >
        <el-option key="0" label="全部班级" :value="Number(0)" />
        <el-option key="1" label="未结课班级" :value="Number(1)" />
        <el-option key="1" label="已结课班级" :value="Number(2)" />
      </el-select>
    </div>
    <!-- 班级折叠面板-->
    <div v-for="grade in gradeData" :key="grade.id" class="list-collapse">
      <el-collapse v-model="grade.active" @change="collapseGrade(grade)">
        <el-collapse-item :name="grade.id">
          <template #title>
            <div class="title">
              <span class="blue mr-10">{{ grade.name }}</span>
              <span class="mr-10">教师：{{ grade.teacher_name }}</span>
              <span>班级号：{{ grade.num }}</span>
              <span v-if="grade.grade_status == 1" class="red ml-10"
                >结课时间：{{ grade.grade_time }}</span
              >
              <span class="blue fr"
                >学生：{{ grade.count }}人
                <el-icon
                  :class="
                    grade.active && grade.active.includes(grade.id)
                      ? 'is-active'
                      : ''
                  "
                  ><CaretBottom
                /></el-icon>
              </span>
            </div>
          </template>
          <div>
            <el-table
              :data="grade.students"
              max-height="400"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                label="序号"
                width="80"
                align="center"
              />
              <el-table-column
                prop="name"
                label="学生名称"
                width="180"
                align="center"
              />
              <el-table-column
                prop="phone"
                label="手机号"
                width="180"
                align="center"
              />
              <el-table-column
                prop="count"
                label="训练次数"
                width="180"
                align="center"
              />
              <el-table-column />
            </el-table>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.class-container {
  position: relative;
  padding: 20px 30px;
}
.class-select {
  margin-bottom: 20px;
  .el-select {
    width: 160px;
    margin: 0;
  }
}
:deep(.el-input__wrapper) {
  background: #ffffff;
  border-radius: 12px;
}
/* el-select 各种边框线隐藏**/
:deep(.el-select) {
  --el-select-input-focus-border-color: none !important;
}
:deep(.el-input__wrapper) {
  box-shadow: none !important;
}
:deep(.el-select .el-input__wrapper.is-focus) {
  box-shadow: none !important;
}
:deep(.el-select:hover:not(.el-select--disabled) .el-input__wrapper) {
  box-shadow: none !important;
}
.list-collapse {
  background: #ffffff;
  border-radius: 25px;
  padding: 0 32px;
  margin-bottom: 10px;
  .title {
    width: 100%;
    font-size: 18px;
    .blue {
      color: #4284f2;
    }
    .red {
      color: #e04962;
    }
    .fr {
      float: right;
    }
    .el-icon {
      vertical-align: middle;
      &.is-active {
        transform: rotate(180deg);
      }
    }
  }
  :deep(.el-collapse),
  :deep(.el-collapse-item__header),
  :deep(.el-collapse-item__wrap) {
    border: none;
  }
  :deep(.el-collapse-item__arrow) {
    display: none;
  }
}
:deep(.el-table th.el-table__cell) {
  background: #e9ebee;
}
</style>
