<template>
  <base-load-page
    :loadState="pageLoad.loadState"
    :description="pageLoad.description"
    :pageLoadButton="[{ text: '重新加载', callBack: () => getBaseDataAxios() }]"
    :padding="0"
    :margin="0"
    width="100%"
    height="100%"
    :imageSize="120"
    background="transparent"
  >
    <el-scrollbar style="width: 100%; height: 100%" viewStyle="height: 100%;">
      <div class="list">
        <button
          v-for="(room, index) in courseRoomList"
          :key="index"
          class="item teacher-item"
          :class="{ active: currCourseRoom?.id === room?.id }"
          @click="openJoinRoom(room)"
        >
          <!-- 状态标签 - 右上角 -->
          <div class="status-tag" :class="room.status === 1 ? 'active' : ''">
            <span class="dot"></span>
            <span class="text">{{ room.status === 1 ? '已开课' : '未开课' }}</span>
          </div>
          <!-- 课程名称 -->
          <div class="course-title">{{ room.name }}</div>
          <!-- 教师徽章 -->
          <div class="teacher-badge">
            <icon-mdi-school class="icon"></icon-mdi-school>
            <span class="text">{{ room.teacher || '未分配' }}</span>
          </div>
          <!-- 时间信息 -->
          <div class="time-section">
            <div class="time-item">
              <span class="label">开始时间</span>
              <span class="value">{{ formatTime(room.startTime) }}</span>
            </div>
            <div class="time-divider"></div>
            <div class="time-item">
              <span class="label">结束时间</span>
              <span class="value">{{ formatTime(room.endTime) }}</span>
            </div>
          </div>
          <!-- 半透明背景装饰 -->
          <div class="bg-wave bg-wave-1"></div>
          <div class="bg-wave bg-wave-2"></div>
        </button>
      </div>
    </el-scrollbar>
  </base-load-page>
</template>

<script setup lang="ts">
  import { type RemoteCourseRoom, getTeacherCourseRoomListAxios } from '@/api/meeting';
  import IconMdiSchool from '~icons/mdi/school';
  import { onMounted, ref } from 'vue';
  import { useRoute } from 'vue-router';
  import { getDateObj } from '@/utils/time';

  const router = useRouter();
  const route = useRoute();
  const role = ref<'student' | 'teacher'>(String(route.query.role) === 'student' ? 'student' : 'teacher');
  const courseRoomList = ref<RemoteCourseRoom[]>([]);
  const pageLoad = shallowReactive<$PageLoad>({ loadState: true, description: '' });
  const meetingStore = useMeetingStore();
  const currCourseRoom = computed(() => meetingStore.currCourseRoom);
  onMounted(() => {
    getBaseDataAxios();
    role.value = String(route.query.role) === 'student' ? 'student' : 'teacher';
  });

  /**
   * 得到课程列表数据
   */
  async function getBaseDataAxios(isLoang = true) {
    try {
      console.log('获取教师端课程列表');
      const res = await getTeacherCourseRoomListAxios();

      if (res.data.length > 0) {
        courseRoomList.value = res.data;
        pageLoad.loadState = false;
      } else {
        pageLoad.loadState = 'null';
        pageLoad.description = '暂无课程';
      }
    } catch (error: any) {
      console.log('获取课程列表失败====', error);
      if (isLoang) {
        pageLoad.loadState = 'err';
        pageLoad.description = error.msg || '获取数据失败，请重试';
      }
    }
  }

  /**
   * 格式化时间显示
   */
  function formatTime(timeStr: string): string {
    if (!timeStr) {
      return '--:--';
    }
    try {
      const date = getDateObj(timeStr);
      return `${date.month}-${date.day} ${date.hour}:${date.minute}`;
    } catch (error) {
      console.log('时间格式化失败====', error);
      return timeStr;
    }
  }

  /**
   * 打开加入房间页面
   */
  async function openJoinRoom(item: RemoteCourseRoom) {
    try {
      meetingStore.setCurrCourseRoom(item);
      await nextTick();
      await router.push('/meeting/join-room');
    } catch (error) {
      console.log('进入加入房间页面失败====', error);
      Toast.error('进入加入房间页面失败，请重试');
    }
  }
</script>

<style lang="scss" scoped>
  .list {
    box-sizing: border-box;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 16px;
    padding: 16px;

    .item {
      position: relative;
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      justify-content: space-between;
      width: 100%;
      min-width: 0;
      min-height: 220px;
      padding: 24px;
      overflow: hidden;
      cursor: pointer;
      background: rgba(255, 255, 255, 0.4);
      border: 1px solid rgba(226, 232, 240, 0.6);
      border-radius: 16px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
      transition: all 0.3s ease;
      backdrop-filter: blur(10px);

      &:hover {
        background: rgba(255, 255, 255, 0.55);
        border-color: rgba(54, 137, 246, 0.3);
        box-shadow: 0 8px 20px rgba(54, 137, 246, 0.1);
        transform: translateY(-3px);
      }

      &.active {
        background: rgba(240, 248, 255, 0.5);
        backdrop-filter: blur(12px);
        border: 2px solid rgba(54, 137, 246, 0.4);
        box-shadow: 0 6px 16px rgba(54, 137, 246, 0.12);

        &:hover {
          transform: translateY(-2px);
        }
      }

      // 课程名称
      .course-title {
        margin-bottom: 14px;
        font-size: 20px;
        font-weight: 600;
        color: #3b82f6;
        letter-spacing: -0.01em;
      }

      // 教师徽章
      .teacher-badge {
        display: inline-flex;
        gap: 6px;
        align-items: center;
        padding: 8px 14px;
        margin-bottom: 18px;
        background: rgba(239, 246, 255, 0.6);
        border-radius: 12px;

        .icon {
          flex-shrink: 0;
          width: 18px;
          height: 18px;
          color: #3b82f6;
        }

        .text {
          font-size: 14px;
          font-weight: 500;
          color: #1e40af;
        }
      }

      // 状态标签 - 右上角
      .status-tag {
        position: absolute;
        top: 20px;
        right: 20px;
        display: inline-flex;
        gap: 6px;
        align-items: center;
        padding: 7px 14px;
        font-size: 13px;
        font-weight: 600;
        color: #6b7280;
        background: rgba(229, 231, 235, 0.6);
        border: 1px solid rgba(209, 213, 219, 0.4);
        border-radius: 12px;
        transition: all 0.3s ease;

        .dot {
          width: 7px;
          height: 7px;
          background: #9ca3af;
          border-radius: 50%;
          transition: all 0.3s ease;
        }

        .text {
          white-space: nowrap;
        }

        &.active {
          color: #059669;
          background: rgba(16, 185, 129, 0.15);
          border-color: rgba(16, 185, 129, 0.3);

          .dot {
            width: 8px;
            height: 8px;
            background: #10b981;
            box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.25);
          }
        }
      }

      // 时间区域
      .time-section {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        padding-top: 14px;
        border-top: 1px solid rgba(229, 231, 235, 0.4);

        .time-item {
          display: flex;
          flex-direction: column;
          gap: 5px;

          .label {
            font-size: 12px;
            font-weight: 500;
            color: #9ca3af;
            text-transform: uppercase;
          }

          .value {
            font-size: 18px;
            font-weight: 600;
            color: #374151;
          }
        }

        .time-divider {
          width: 1px;
          height: 28px;
          background: #e5e7eb;
        }
      }

      // 半透明波浪背景装饰
      .bg-wave {
        position: absolute;
        z-index: -1;
        pointer-events: none;
        background: radial-gradient(circle, rgba(245, 157, 245, 0.25), transparent);
        border-radius: 50%;

        &.bg-wave-1 {
          top: -100px;
          right: -80px;
          width: 200px;
          height: 200px;
          opacity: 0.7;
        }

        &.bg-wave-2 {
          bottom: -60px;
          left: -50px;
          width: 150px;
          height: 150px;
          opacity: 0.5;
        }
      }
    }
  }
</style>
