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

// Ensure teacher_id is available
if (!isset($_SESSION['teacher_id'])) {
    $_SESSION['error'] = "Teacher not logged in.";
    header("Location: teacher_login.php"); // Redirect to login or an error page
    exit();
}

$teacher_id = $_SESSION['teacher_id'];

// Get homework ID from query parameters
$homework_id = $_GET['homework_id'] ?? 0;

// Fetch submission details
$sql = "SELECT sh.student_id, s.name AS student_name, sh.homework_id, sh.submission, sh.file_path
        FROM student_homework sh
        JOIN students s ON sh.student_id = s.student_id
        WHERE sh.homework_id = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $homework_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $submission = $result->fetch_assoc();
    // Display homework submission details
    echo "<h2>Feedback and Grading</h2>";
    echo "<p><strong>Student:</strong> " . htmlspecialchars($submission['student_name']) . "</p>";
    echo "<p><strong>File Path:</strong> <a href='" . htmlspecialchars($submission['file_path']) . "'>View Submitted File</a></p>";
    echo "<form action='process_feedback.php' method='POST'>";
    echo "<input type='hidden' name='homework_id' value='" . htmlspecialchars($submission['homework_id']) . "'>";
    echo "<input type='hidden' name='student_id' value='" . htmlspecialchars($submission['student_id']) . "'>";
    echo "<label for='feedback'>Feedback:</label><br>";
    echo "<textarea name='feedback' id='feedback' rows='4' required></textarea><br><br>";
    echo "<label for='grade'>Grade:</label>";
    echo "<input type='text' name='grade' id='grade' required><br><br>";
    echo "<input type='submit' value='Submit Feedback'>";
    echo "</form>";
} else {
    echo "<p>No submissions found for this homework.</p>";
}

// Close database connection
$conn->close();
?>
