<?php
session_start();
require_once "../includes/db.php";

if (!isset($_SESSION['admin'])) {
    header("Location: index.php");
    exit;
}

$message = "";
$message_type = "";

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $full_name = trim($_POST['full_name'] ?? '');
    $email     = trim($_POST['email'] ?? '');
    $phone     = trim($_POST['phone'] ?? '');
    $course    = trim($_POST['course'] ?? '');

    if ($full_name === '' || $email === '' || $phone === '' || $course === '') {
        $message = "Please fill in all required fields.";
        $message_type = "danger";
    } else {
        // Example insert logic
        // Table/column names oyage DB ekata anuwa change karanna
        $stmt = $conn->prepare("INSERT INTO students (full_name, email, phone, course) VALUES (?, ?, ?, ?)");
        if ($stmt) {
            $stmt->bind_param("ssss", $full_name, $email, $phone, $course);
            if ($stmt->execute()) {
                $message = "Student added successfully.";
                $message_type = "success";
            } else {
                $message = "Database insert failed: " . $stmt->error;
                $message_type = "danger";
            }
            $stmt->close();
        } else {
            $message = "Prepare failed. Check table/column names.";
            $message_type = "danger";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Student</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body{
            background: #f4f7fb;
            font-family: Arial, sans-serif;
        }
        .page-wrap{
            max-width: 850px;
            margin: 40px auto;
        }
        .card{
            border: 0;
            border-radius: 18px;
            box-shadow: 0 10px 30px rgba(0,0,0,.08);
        }
        .card-header{
            background: linear-gradient(135deg, #0d6efd, #0b5ed7);
            color: #fff;
            border-radius: 18px 18px 0 0 !important;
            padding: 20px 24px;
        }
        .form-control, .form-select{
            min-height: 48px;
            border-radius: 12px;
        }
        .btn{
            border-radius: 12px;
            min-height: 46px;
            padding: 10px 20px;
        }
    </style>
</head>
<body>

<div class="container page-wrap">
    <div class="card">
        <div class="card-header">
            <h3 class="mb-1">Add New Student</h3>
            <p class="mb-0 small">Create a new student record</p>
        </div>

        <div class="card-body p-4">
            <?php if (!empty($message)): ?>
                <div class="alert alert-<?= htmlspecialchars($message_type) ?> alert-dismissible fade show" role="alert">
                    <?= htmlspecialchars($message) ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>

            <form method="POST">
                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label">Full Name <span class="text-danger">*</span></label>
                        <input type="text" name="full_name" class="form-control" placeholder="Enter full name" required>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Email <span class="text-danger">*</span></label>
                        <input type="email" name="email" class="form-control" placeholder="Enter email" required>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Phone <span class="text-danger">*</span></label>
                        <input type="text" name="phone" class="form-control" placeholder="Enter phone number" required>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Course <span class="text-danger">*</span></label>
                        <input type="text" name="course" class="form-control" placeholder="Enter course name" required>
                    </div>
                </div>

                <div class="d-flex gap-2 mt-4">
                    <button type="submit" class="btn btn-primary">Save Student</button>
                    <a href="dashboard.php" class="btn btn-outline-secondary">Back</a>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>