<?php
require_once '../includes/init.php';

$user = new User($pdo);
if (!$user->isLoggedIn() || !$user->isTeacher()) {
    header("Location: ../index.php");
    exit;
}

$message = '';
$error = '';

// Handle Form Submissions
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['add_subject'])) {
        $name = trim($_POST['name']);
        $code = trim($_POST['code']);
        
        if (!empty($name) && !empty($code)) {
            $stmt = $pdo->prepare("INSERT INTO subjects (name, subject_code) VALUES (?, ?)");
            try {
                $stmt->execute([$name, $code]);
                $message = "Subject added successfully.";
            } catch (PDOException $e) {
                $error = "Error adding subject: " . $e->getMessage();
            }
        } else {
            $error = "All fields are required.";
        }
    } elseif (isset($_POST['delete_subject'])) {
        $subject_id = $_POST['subject_id'];
        $stmt = $pdo->prepare("DELETE FROM subjects WHERE subject_id = ?");
        if ($stmt->execute([$subject_id])) {
            $message = "Subject deleted successfully.";
        } else {
            $error = "Failed to delete subject.";
        }
    }
}

$stmt = $pdo->query("SELECT * FROM subjects");
$subjects = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Subjects - Admin</title>
    <link rel="stylesheet" href="../assets/css/admin.css">
</head>
<body>
    <?php include '../includes/admin_sidebar.php'; ?>
    <div class="content">
        <?php include '../includes/page_header.php'; ?>
        <h1>Manage Subjects</h1>
        
        <?php if ($message): ?>
            <div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
        <?php endif; ?>
        <?php if ($error): ?>
            <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>

        <div class="card">
            <h3>Add New Subject</h3>
            <form method="POST">
                <div class="form-group">
                    <label>Subject Name:</label>
                    <input type="text" name="name" required style="width: 100%; padding: 8px;">
                </div>
                <div class="form-group">
                    <label>Subject Code:</label>
                    <input type="text" name="code" required style="width: 100%; padding: 8px;">
                </div>
                <button type="submit" name="add_subject" class="btn btn-primary">Add Subject</button>
            </form>
        </div>

        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Code</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($subjects as $s): ?>
                <tr>
                    <td><?php echo $s['subject_id']; ?></td>
                    <td><?php echo htmlspecialchars($s['name']); ?></td>
                    <td><?php echo htmlspecialchars($s['subject_code']); ?></td>
                    <td>
                        <form method="POST" style="display:inline;" onsubmit="return confirm('Delete subject?');">
                            <input type="hidden" name="subject_id" value="<?php echo $s['subject_id']; ?>">
                            <button type="submit" name="delete_subject" class="btn btn-danger">Delete</button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
        <div class="footer">Designed by ferakeys</div>
    </div>
</body>
</html>
