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

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $fullname = mysqli_real_escape_string($conn, $_POST['fullname']);
    $reason = mysqli_real_escape_string($conn, $_POST['reason']);
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);
    $status = 'not done';

    $sql = "INSERT INTO visitors (fullname, reason, phone, status, reg_date) 
            VALUES ('$fullname', '$reason', '$phone', '$status', NOW())";
    if ($conn->query($sql)) {
        $success = "Visitor Added Successfully!";
    } else {
        $error = "Error adding visitor: " . $conn->error;
    }
}
?>

<div class="container mt-4">
    <h4 class="text-orange">Register New Visitor</h4>

    <?php if (isset($success)) echo "<div class='alert alert-success'>$success</div>"; ?>
    <?php if (isset($error)) echo "<div class='alert alert-danger'>$error</div>"; ?>

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

        <div class="mb-3">
            <label class="form-label text-orange">Reason for Visit</label>
            <input type="text" name="reason" class="form-control" required>
        </div>

        <div class="mb-3">
            <label class="form-label text-orange">Phone Number</label>
            <input type="text" name="phone" class="form-control" required>
        </div>

        <button type="submit" class="btn btn-orange text-white">Register Visitor</button>
    </form>
</div>

<?php include 'includes/footer.php'; ?>
