<?php
include 'includes/header.php';
include 'includes/sidebar.php';
require 'includes/db_connect.php';

date_default_timezone_set('Africa/Lagos');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $fullname = trim($_POST['fullname']);
    $reason = trim($_POST['reason']);
    $status = trim($_POST['status']);
    $reg_date = date('Y-m-d H:i:s');

    if (!empty($fullname) && !empty($reason) && !empty($status)) {
        $stmt = $conn->prepare("INSERT INTO visitors (fullname, reason, status, reg_date) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("ssss", $fullname, $reason, $status, $reg_date);
        if ($stmt->execute()) {
            $message = "<div class='alert alert-success'>Visitor details recorded successfully.</div>";
        } else {
            $message = "<div class='alert alert-danger'>Error recording visitor.</div>";
        }
    } else {
        $message = "<div class='alert alert-warning'>All fields are required.</div>";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Enter Visitor</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5">
    <h3 class="mb-4">Visitor Entry Form</h3>

    <?php if (isset($message)) echo $message; ?>

    <form method="post">
        <div class="mb-3">
            <label for="fullname" class="form-label">Full Name</label>
            <input type="text" name="fullname" class="form-control" required>
        </div>

        <div class="mb-3">
            <label for="reason" class="form-label">Reason for Visit</label>
            <textarea name="reason" class="form-control" rows="3" required></textarea>
        </div>

        <div class="mb-3">
            <label for="status" class="form-label">Status</label>
            <select name="status" class="form-select" required>
                <option value="">Select Status</option>
                <option value="Waiting">Waiting</option>
                <option value="Attended">Attended</option>
                <option value="Left">Left</option>
            </select>
        </div>

        <button type="submit" class="btn btn-primary">Submit Visitor</button>
    </form>
</div>
</body>
</html>
