<?php error_reporting(0); ?>
<?php    session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?php include '../admin/includes/styles.php'; ?>
    <title> Learning Materials | <?php echo $school_name; ?> </title>

    <style>
        .progress-badge {
            position: absolute;
            top: 10px;
            right: 10px;
        }
        .material-card {
            position: relative;
            transition: all 0.3s;
            height: 100%;
        }
        .material-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 6px 12px rgba(0,0,0,0.15);
        }
        .subject-section {
            margin-bottom: 30px;
        }
        .material-icon {
            font-size: 50px;
            margin: 20px 0;
        }
        .progress-ring {
            width: 60px;
            height: 60px;
        }
    </style>



    <?php
    session_start();
    if(isset($_SESSION['student'])){

    require_once '../admin/Classes/ELearning.php';
    $elearning = new ELearning();

    // Get student ID
    $sql = "SELECT studentID, name, classesID, sectionid FROM student WHERE username = :username";
    $conn->query($sql);
    $conn->bind(':username', $_SESSION['student']);
    $student = $conn->fetchSingle();

    if (!$student) {
        header("Location: $student_login_link");
        exit();
    }

    $student_id = $student->studentID;

    ?>

</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

    <!-- Navbar -->
    <?php include 'includes/notifications.php'; ?>
    <!-- /.navbar -->

    <!-- Main Sidebar Container -->
    <?php include "includes/sidebar.php";?>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0"> Learning Materials </h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="<?php echo $conn->student_url(); ?>">Home</a></li>
                            <li class="breadcrumb-item active">Learning Materials </li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <section class="content">
            <div class="container-fluid">

                <!-- Progress Summary -->
                <div class="row">
                    <?php
                    $materials = $elearning->getStudentMaterials($student_id);

                    $total_materials = count($materials);
                    $completed = 0;
                    $in_progress = 0;
                    $not_started = 0;

                    foreach ($materials as $material) {
                        if ($material->progress_status == 'completed') $completed++;
                        elseif ($material->progress_status == 'in_progress') $in_progress++;
                        else $not_started++;
                    }

                    $completion_percentage = $total_materials > 0 ? round(($completed / $total_materials) * 100) : 0;
                    ?>

                    <div class="col-lg-3 col-6">
                        <div class="small-box bg-info">
                            <div class="inner">
                                <h3><?php echo $total_materials; ?></h3>
                                <p>Total Materials</p>
                            </div>
                            <div class="icon">
                                <i class="fas fa-book"></i>
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-3 col-6">
                        <div class="small-box bg-success">
                            <div class="inner">
                                <h3><?php echo $completed; ?></h3>
                                <p>Completed</p>
                            </div>
                            <div class="icon">
                                <i class="fas fa-check-circle"></i>
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-3 col-6">
                        <div class="small-box bg-warning">
                            <div class="inner">
                                <h3><?php echo $in_progress; ?></h3>
                                <p>In Progress</p>
                            </div>
                            <div class="icon">
                                <i class="fas fa-spinner"></i>
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-3 col-6">
                        <div class="small-box bg-danger">
                            <div class="inner">
                                <h3><?php echo $completion_percentage; ?>%</h3>
                                <p>Overall Progress</p>
                            </div>
                            <div class="icon">
                                <i class="fas fa-chart-pie"></i>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Materials by Subject -->
                <?php
                if (!empty($materials)) {
                    // Group materials by subject
                    $materials_by_subject = [];
                    foreach ($materials as $material) {
                        $materials_by_subject[$material->subject_name][] = $material;
                    }

                    foreach ($materials_by_subject as $subject => $subject_materials) {
                        ?>
                        <div class="subject-section">
                            <div class="card card-outline card-primary">
                                <div class="card-header">
                                    <h3 class="card-title">
                                        <i class="fas fa-book"></i> <?php echo htmlspecialchars($subject); ?>
                                    </h3>
                                    <div class="card-tools">
                                        <span class="badge badge-primary"><?php echo count($subject_materials); ?> Materials</span>
                                    </div>
                                </div>
                                <div class="card-body">
                                    <div class="row">
                                        <?php foreach ($subject_materials as $material) {
                                            // Determine icon and color
                                            $icon_class = '';
                                            $icon_color = '';
                                            switch ($material->material_type) {
                                                case 'pdf':
                                                    $icon_class = 'fa-file-pdf';
                                                    $icon_color = 'text-danger';
                                                    break;
                                                case 'video':
                                                    $icon_class = 'fa-video';
                                                    $icon_color = 'text-primary';
                                                    break;
                                                case 'document':
                                                    $icon_class = 'fa-file-word';
                                                    $icon_color = 'text-info';
                                                    break;
                                                case 'link':
                                                    $icon_class = 'fa-link';
                                                    $icon_color = 'text-success';
                                                    break;
                                                case 'audio':
                                                    $icon_class = 'fa-music';
                                                    $icon_color = 'text-warning';
                                                    break;
                                            }

                                            // Progress badge
                                            $progress_badge = '';
                                            $progress_color = '';
                                            if ($material->progress_status == 'completed') {
                                                $progress_badge = '<i class="fas fa-check-circle"></i> Completed';
                                                $progress_color = 'success';
                                            } elseif ($material->progress_status == 'in_progress') {
                                                $progress_badge = '<i class="fas fa-spinner"></i> In Progress';
                                                $progress_color = 'warning';
                                            } else {
                                                $progress_badge = '<i class="fas fa-circle"></i> Not Started';
                                                $progress_color = 'secondary';
                                            }
                                            ?>
                                            <div class="col-md-4 col-sm-6 mb-4">
                                                <div class="card material-card">
                                                    <?php if ($progress_badge): ?>
                                                        <span class="badge badge-<?php echo $progress_color; ?> progress-badge">
                                            <?php echo $progress_badge; ?>
                                        </span>
                                                    <?php endif; ?>

                                                    <div class="card-body text-center">
                                                        <div class="material-icon">
                                                            <i class="fas <?php echo $icon_class . ' ' . $icon_color; ?>"></i>
                                                        </div>
                                                        <h5 class="card-title"><?php echo htmlspecialchars($material->title); ?></h5>

                                                        <?php if ($material->description): ?>
                                                            <p class="card-text text-muted small">
                                                                <?php echo substr(htmlspecialchars($material->description), 0, 80); ?>...
                                                            </p>
                                                        <?php endif; ?>

                                                        <?php if ($material->duration): ?>
                                                            <p class="text-muted small">
                                                                <i class="fas fa-clock"></i> Duration: <?php echo $material->duration; ?>
                                                            </p>
                                                        <?php endif; ?>

                                                        <!-- Progress Bar -->
                                                        <?php if ($material->progress > 0): ?>
                                                            <div class="progress mb-3" style="height: 5px;">
                                                                <div class="progress-bar bg-success" role="progressbar"
                                                                     style="width: <?php echo $material->progress; ?>%"
                                                                     aria-valuenow="<?php echo $material->progress; ?>"
                                                                     aria-valuemin="0" aria-valuemax="100">
                                                                </div>
                                                            </div>
                                                            <small class="text-muted"><?php echo $material->progress; ?>% Complete</small>
                                                        <?php endif; ?>

                                                        <div class="mt-3">
                                                            <?php if ($material->last_accessed): ?>
                                                                <small class="text-muted d-block mb-2">
                                                                    <i class="fas fa-clock"></i> Last accessed: <?php echo date('M d, Y', strtotime($material->last_accessed)); ?>
                                                                </small>
                                                            <?php endif; ?>

                                                            <a href="view-material?id=<?php echo $material->id; ?>"
                                                               class="btn btn-primary btn-block">
                                                                <i class="fas fa-play"></i>
                                                                <?php echo $material->progress_status == 'completed' ? 'Review' :
                                                                    ($material->progress_status == 'in_progress' ? 'Continue' : 'Start Learning'); ?>
                                                            </a>
                                                        </div>

                                                        <!-- Rating Display -->
                                                        <?php if ($material->avg_rating): ?>
                                                            <div class="mt-2">
                                                                <small class="text-warning">
                                                                    <?php
                                                                    $rating = round($material->avg_rating);
                                                                    for ($i = 1; $i <= 5; $i++) {
                                                                        echo $i <= $rating ? '<i class="fas fa-star"></i>' : '<i class="far fa-star"></i>';
                                                                    }
                                                                    ?>
                                                                    <span class="text-muted">(<?php echo number_format($material->avg_rating, 1); ?>)</span>
                                                                </small>
                                                            </div>
                                                        <?php endif; ?>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php
                    }
                } else {
                    ?>
                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body text-center py-5">
                                    <i class="fas fa-inbox fa-5x text-muted mb-3"></i>
                                    <h4>No Learning Materials Available</h4>
                                    <p class="text-muted">Your teachers haven't uploaded any learning materials yet. Check back later!</p>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <?php include "../admin/includes/footer.php";?>


    <script>
        $(document).ready(function(){
            $('.select2').select2();

        })

    </script>


    <?php
    }else{
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no">


        <?php include '../admin/includes/styles.php'; ?>
        <title>Login to continue </title>

        <!-- END PAGE LEVEL PLUGINS/CUSTOM STYLES -->

    </head>
    <body>
    <div class="container" style="margin-top: 20%;">
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="section-title text-center">
                    <h6> You need to login to continue </h6>
                    <div class="spinner-border text-info" role="status">
                        <span class="sr-only">Loading...</span>
                        <meta http-equiv='refresh' content='3; <?php echo $student_login_link; ?>'>
                    </div>
                    <p>Redirecting to the login  page...</p>
                </div>
            </div>
        </div>
    </div>

    <?php
    }

    ?>
