<template>
  <view class="schedule-detail" v-if="visible">
<!--    <view class="popup-wrapper">-->
<!--      <view class="popup-header">-->
<!--        <text class="popup-title">课次详情</text>-->
<!--        <view class="close-btn" @click="closePopup">-->
<!--          <text class="close-icon">×</text>-->
<!--        </view>-->
<!--      </view>-->

<!--      <view class="popup-content" v-if="loading">-->
<!--        <view class="loading">-->
<!--          <fui-loading></fui-loading>-->
<!--          <text class="loading-text">加载中...</text>-->
<!--        </view>-->
<!--      </view>-->

<!--      <view class="popup-content" v-else-if="error">-->
<!--        <view class="error-message">-->
<!--          <text>{{ errorMessage }}</text>-->
<!--          <view class="retry-btn" @click="fetchScheduleDetail">-->
<!--            <text>重试</text>-->
<!--          </view>-->
<!--        </view>-->
<!--      </view>-->

<!--      <view class="popup-content" v-else>-->
<!--        <view class="course-title">-->
<!--          <text>{{ scheduleDetail.title || '暂无课程名称' }}</text>-->
<!--        </view>-->

<!--        <view class="course-time">-->
<!--          <text>{{ scheduleDetail.course_date || '' }} {{ scheduleDetail.time_slot || '' }}</text>-->
<!--        </view>-->

<!--        <view class="schedule-info">-->
<!--          <view class="info-item">-->
<!--            <text class="info-label">授课教师:</text>-->
<!--            <text class="info-value">{{ scheduleDetail.coach?.name || '未设置' }}</text>-->
<!--          </view>-->

<!--          <view class="info-item">-->
<!--            <text class="info-label">教室:</text>-->
<!--            <text class="info-value">{{ scheduleDetail.venue?.venue_name || '未设置' }}</text>-->
<!--          </view>-->

<!--          <view class="info-item">-->
<!--            <text class="info-label">当前人数:</text>-->
<!--            <text class="info-value">{{ studentCount }}/{{ scheduleDetail.venue?.capacity || 0 }}</text>-->
<!--          </view>-->

<!--          <view class="info-item">-->
<!--            <text class="info-label">课程内容:</text>-->
<!--            <text class="info-value">{{ scheduleDetail.content || '未设置上课内容' }}</text>-->
<!--          </view>-->

<!--          <view class="info-item" v-if="scheduleDetail.remark">-->
<!--            <text class="info-label">备注:</text>-->
<!--            <text class="info-value">{{ scheduleDetail.remark }}</text>-->
<!--          </view>-->

<!--          &lt;!&ndash; 学员列表 &ndash;&gt;-->
<!--          <view class="student-list-section">-->
<!--            <view class="section-title">-->
<!--              <text>学员列表</text>-->
<!--              <text class="status-tag" :class="statusClass">{{ statusText }}</text>-->
<!--            </view>-->
<!--            <view class="student-list" v-if="scheduleDetail.student_courses && scheduleDetail.student_courses.length > 0">-->
<!--              <view class="student-item" v-for="(student, index) in scheduleDetail.student_courses" :key="index">-->
<!--                <view class="student-avatar">-->
<!--                  <image :src="$util.img(student.avatar)" mode="aspectFill"></image>-->
<!--                </view>-->
<!--                <view class="student-info">-->
<!--                  <text class="student-name">{{ student.name }}</text>-->
<!--                  <text class="student-status" :class="{'signed': student.status === 'signed'}">-->
<!--                    {{ student.status === 'signed' ? '已签到' : '未签到' }}-->
<!--                  </text>-->
<!--                </view>-->
<!--              </view>-->
<!--            </view>-->
<!--            <view class="empty-list" v-else>-->
<!--              <text>暂无学员参与此课程</text>-->
<!--            </view>-->
<!--          </view>-->
<!--        </view>-->
<!--      </view>-->

<!--      <view class="popup-footer">-->
<!--        <view class="action-btn adjust-btn" @click="handleAdjustClass">-->
<!--          <text>调课</text>-->
<!--        </view>-->
<!--        <view class="action-btn sign-btn" @click="handleSignIn">-->
<!--          <text>点名</text>-->
<!--        </view>-->
<!--      </view>-->
<!--    </view>-->
  </view>
</template>

<script>
import apiRoute from '@/api/apiRoute.js';

export default {
  name: 'ScheduleDetail',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    scheduleId: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      error: false,
      errorMessage: '加载失败，请重试',
      scheduleDetail: {},
      studentCount: 0
    }
  },
  computed: {
    // 课程状态文本和样式
    statusText() {
      if (!this.scheduleDetail.student_courses || !this.scheduleDetail.student_courses[0]) {
        return '未开始';
      }
      
      const now = new Date();
      const startDate = this.scheduleDetail.student_courses[0].start_date ? 
        new Date(this.scheduleDetail.student_courses[0].start_date) : null;
      const endDate = this.scheduleDetail.student_courses[0].end_date ? 
        new Date(this.scheduleDetail.student_courses[0].end_date) : null;
      
      if (startDate && endDate) {
        if (now >= startDate && now <= endDate) {
          return '上课中';
        } else if (now > endDate) {
          return '已结束';
        } else if (now < startDate) {
          return '未开始';
        }
      }
      
      return '未开始';
    },
    statusClass() {
      switch (this.statusText) {
        case '上课中':
          return 'status-in-progress';
        case '已结束':
          return 'status-ended';
        case '未开始':
        default:
          return 'status-not-started';
      }
    }
  },
  watch: {
    // 监听课程ID变化，重新获取数据
    scheduleId: {
      immediate: true,
      handler(newVal) {
        if (newVal && this.visible) {
          this.fetchScheduleDetail();
        }
      }
    },
    // 监听弹窗可见性变化
    visible(newVal) {
      if (newVal && this.scheduleId) {
        this.fetchScheduleDetail();
      }
    }
  },
  methods: {
    // 获取课程详情
    async fetchScheduleDetail() {
      if (!this.scheduleId) {
        this.error = true;
        this.errorMessage = '课程ID不能为空';
        return;
      }
      
      this.loading = true;
      this.error = false;
      
      try {
        // 使用新接口获取课程安排详情
        const res = await apiRoute.getCourseScheduleInfo({
          id: this.scheduleId
        });
        
        if (res.code === 1 && res.data) {
          this.scheduleDetail = res.data;
          // 计算学生数量
          this.studentCount = this.scheduleDetail.student_courses ? 
            this.scheduleDetail.student_courses.length : 0;
        } else {
          // 如果新接口不可用，尝试使用旧接口
          const fallbackRes = await apiRoute.courseInfo({
            id: this.scheduleId
          });
          
          if (fallbackRes.code === 1 && fallbackRes.data) {
            this.scheduleDetail = fallbackRes.data;
            this.studentCount = this.scheduleDetail.student_courses ? 
              this.scheduleDetail.student_courses.length : 0;
          } else {
            throw new Error(res.msg || fallbackRes.msg || '获取课程详情失败');
          }
        }
      } catch (error) {
        console.error('获取课程详情失败:', error);
        this.error = true;
        this.errorMessage = error.message || '获取课程详情失败，请重试';
      } finally {
        this.loading = false;
      }
    },
    
    // 关闭弹窗
    closePopup() {
      this.$emit('update:visible', false);
    },
    
    // 点名功能
    handleSignIn() {
      // 如果课程已结束，显示提示
      if (this.statusText === '已结束') {
        uni.showToast({
          title: '课程已结束，无法点名',
          icon: 'none'
        });
        return;
      }
      
      // 如果没有学生，显示提示
      if (!this.scheduleDetail.student_courses || this.scheduleDetail.student_courses.length === 0) {
        uni.showToast({
          title: '暂无学员，无法点名',
          icon: 'none'
        });
        return;
      }
      
      // 触发点名事件，由父组件处理
      this.$emit('sign-in', {
        scheduleId: this.scheduleId,
        scheduleDetail: this.scheduleDetail
      });
    },
    
    // 调课功能
    handleAdjustClass() {
      // 如果课程已结束，显示提示
      if (this.statusText === '已结束') {
        uni.showToast({
          title: '课程已结束，无法调课',
          icon: 'none'
        });
        return;
      }
      
      // 触发调课事件，由父组件处理
      this.$emit('adjust-class', {
        scheduleId: this.scheduleId,
        scheduleDetail: this.scheduleDetail
      });
    }
  }
}
</script>

<style lang="less" scoped>
.schedule-detail {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
  display: flex;
  justify-content: center;
  align-items: center;
}

.popup-wrapper {
  width: 90%;
  max-height: 80vh;
  background-color: #434544;
  border-radius: 16rpx;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.popup-header {
  padding: 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #555;
}

.popup-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #fff;
}

.close-btn {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.close-icon {
  font-size: 40rpx;
  color: #fff;
}

.popup-content {
  flex: 1;
  padding: 30rpx;
  overflow-y: auto;
}

.loading, .error-message {
  height: 300rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.loading-text {
  margin-top: 20rpx;
  font-size: 28rpx;
  color: #ccc;
}

.error-message {
  color: #ff6b6b;
  font-size: 28rpx;
  text-align: center;
}

.retry-btn {
  margin-top: 30rpx;
  padding: 12rpx 30rpx;
  background-color: #29d3b4;
  border-radius: 8rpx;
  color: #fff;
  font-size: 24rpx;
}

.course-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
  margin-bottom: 16rpx;
}

.course-time {
  font-size: 28rpx;
  color: #FAD24E;
  margin-bottom: 30rpx;
}

.schedule-info {
  background-color: #333;
  border-radius: 12rpx;
  padding: 24rpx;
}

.info-item {
  display: flex;
  margin-bottom: 20rpx;
}

.info-label {
  width: 160rpx;
  font-size: 26rpx;
  color: #ccc;
}

.info-value {
  flex: 1;
  font-size: 26rpx;
  color: #fff;
}

.student-list-section {
  margin-top: 30rpx;
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 16rpx;
  border-bottom: 1px solid #555;
  margin-bottom: 20rpx;
}

.section-title text {
  font-size: 28rpx;
  color: #fff;
}

.status-tag {
  font-size: 24rpx;
  padding: 4rpx 16rpx;
  border-radius: 30rpx;
}

.status-in-progress {
  background-color: #FAD24E;
  color: #333;
}

.status-ended {
  background-color: #e2e2e2;
  color: #333;
}

.status-not-started {
  background-color: #1cd188;
  color: #fff;
}

.student-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.student-item {
  display: flex;
  align-items: center;
}

.student-avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  overflow: hidden;
  background-color: #555;
  margin-right: 20rpx;
}

.student-avatar image {
  width: 100%;
  height: 100%;
}

.student-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.student-name {
  font-size: 28rpx;
  color: #fff;
  margin-bottom: 6rpx;
}

.student-status {
  font-size: 24rpx;
  color: #ff6b6b;
}

.student-status.signed {
  color: #1cd188;
}

.empty-list {
  padding: 40rpx 0;
  text-align: center;
  color: #999;
  font-size: 28rpx;
}

.popup-footer {
  display: flex;
  padding: 30rpx;
  gap: 20rpx;
  border-top: 1px solid #555;
}

.action-btn {
  flex: 1;
  height: 80rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 8rpx;
  font-size: 28rpx;
}

.adjust-btn {
  background-color: #555;
  color: #fff;
}

.sign-btn {
  background-color: #29d3b4;
  color: #fff;
}
</style>