<?php
session_start();
include('db_connection.php');

/* =========================================================
   AUTH CHECK (TEACHER ONLY)
========================================================= */
if (!isset($_SESSION['teacher_id']) || $_SESSION['role'] !== 'teacher') {
    header("Location: teacher_login.php");
    exit();
}

$teacher_id = $_SESSION['teacher_id'];

/* =========================================================
   FETCH SUBMISSIONS
========================================================= */
$sql = "
    SELECT 
        h.homework_id,
        h.title,
        sh.student_id,
        s.name AS student_name,
        sh.submitted_at,
        sh.file_path,
        sh.feedback
    FROM homework h
    INNER JOIN student_homework sh ON h.homework_id = sh.homework_id
    INNER JOIN students s ON sh.student_id = s.student_id
    WHERE h.teacher_id = ?
    ORDER BY sh.submitted_at DESC
";

$stmt = $conn->prepare($sql);

if (!$stmt) {
    die("Database Error: " . $conn->error);
}

$stmt->bind_param("i", $teacher_id);
$stmt->execute();
$result = $stmt->get_result();
$submissions = $result->fetch_all(MYSQLI_ASSOC);

$stmt->close();
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Feedback & Grading</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>

/* =========================
   GENERAL LAYOUT FIX
========================= */
body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: #f4f6f9;
}

/* =========================
   SIDEBAR
========================= */
.sidebar {
    width: 250px;
    height: 100vh;
    background: #1e2a38;
    position: fixed;
    top: 0;
    left: 0;
    padding-top: 20px;
    color: white;
    overflow-y: auto;
}

.sidebar-header {
    text-align: center;
    padding: 15px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.sidebar a {
    display: block;
    padding: 12px 20px;
    color: #cfd8dc;
    text-decoration: none;
    transition: 0.3s;
}

.sidebar a i {
    margin-right: 10px;
}

.sidebar a:hover {
    background: #2c3e50;
    color: #fff;
    padding-left: 25px;
}

.sidebar a.active {
    background: #3498db;
    color: #fff;
}

.sidebar a.logout {
    color: #ff6b6b;
}

.sidebar a.logout:hover {
    background: #c0392b;
    color: #fff;
}

/* =========================
   MAIN CONTENT (IMPORTANT FIX)
========================= */
.main-content {
    margin-left: 250px;
    padding: 20px;
}

/* =========================
   CARD DESIGN
========================= */
.card {
    border-radius: 12px;
    margin-bottom: 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

/* =========================
   RESPONSIVE FIX
========================= */
@media screen and (max-width: 768px) {

    .sidebar {
        position: relative;
        width: 100%;
        height: auto;
    }

    .main-content {
        margin-left: 0;
    }
}

</style>
</head>

<body>

<!-- =========================
     SIDEBAR
========================= -->
<div class="sidebar">

    <div class="sidebar-header">
        <h4>Teacher Panel</h4>
    </div>

    <a href="teacher_dashboard.php">
        <i class="fas fa-home"></i> Dashboard
    </a>

    <a class="active" href="feedback_grading.php">
        <i class="fas fa-edit"></i> Feedback & Grading
    </a>

    <a href="attendance.php">
        <i class="fas fa-check-circle"></i> Attendance
    </a>

    <a href="add_homework.php">
        <i class="fas fa-book"></i> Assignments
    </a>

    <a href="class_timetable.php">
        <i class="fas fa-calendar-alt"></i> Timetable
    </a>

    <a href="profile.php">
        <i class="fas fa-user"></i> Profile
    </a>

    <a class="logout" href="logout.php">
        <i class="fas fa-sign-out-alt"></i> Logout
    </a>

</div>

<!-- =========================
     MAIN CONTENT (FIXED OVERLAP)
========================= -->
<div class="main-content">

    <h3 class="mb-4">Homework Feedback & Grading</h3>

    <?php if (empty($submissions)): ?>
        <div class="alert alert-info">
            No homework submissions found.
        </div>
    <?php endif; ?>

    <?php foreach ($submissions as $row): ?>
        <div class="card p-3">

            <h5><?php echo htmlspecialchars($row['title']); ?></h5>

            <p><strong>Student:</strong> <?php echo htmlspecialchars($row['student_name']); ?></p>

            <p><strong>Submitted:</strong> <?php echo htmlspecialchars($row['submitted_at']); ?></p>

            <p>
                <strong>File:</strong>
                <a target="_blank"
                   href="/csms/student/uploads/<?php echo htmlspecialchars(basename($row['file_path'])); ?>">
                    View Submission
                </a>
            </p>

            <hr>

            <!-- FEEDBACK FORM -->
            <form action="process_feedback.php" method="POST">

                <input type="hidden" name="student_id" value="<?php echo $row['student_id']; ?>">
                <input type="hidden" name="homework_id" value="<?php echo $row['homework_id']; ?>">

                <div class="mb-2">
                    <label>Feedback</label>
                    <textarea name="feedback" class="form-control" required>
                        <?php echo htmlspecialchars($row['feedback'] ?? ''); ?>
                    </textarea>
                </div>

                <button class="btn btn-primary btn-sm">
                    Submit Feedback
                </button>

            </form>

        </div>
    <?php endforeach; ?>

</div>

</body>
</html>