<?php
require_once __DIR__ . '/includes/admin_auth.php';
requireAdmin();

$db = getDB();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $name     = sanitize($_POST['name']);
    $email    = sanitizeEmail($_POST['email']);
    $mobile   = sanitize($_POST['mobile']);
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);

    $stmt = $db->prepare("INSERT INTO users 
    (name,email,mobile,password,status,created_at) 
    VALUES (?,?,?,?,1,NOW())");

    $stmt->execute([
        $name,
        $email,
        $mobile,
        $password
    ]);

    setAdminFlash('success', 'User added successfully');

    header("Location: " . SITE_URL . "/admin/users.php");
    exit;
}

adminHead('Add User');
adminSidebar('users');
?>

<div class="adm-main">

<?php adminTopbar('Add User'); ?>

<div class="adm-content">

<div class="adm-card" style="max-width:700px;">

<form method="POST">

<div style="margin-bottom:15px;">
<label class="lbl">Full Name</label>
<input type="text" name="name" class="form-ctrl" required>
</div>

<div style="margin-bottom:15px;">
<label class="lbl">Email</label>
<input type="email" name="email" class="form-ctrl" required>
</div>

<div style="margin-bottom:15px;">
<label class="lbl">Mobile</label>
<input type="text" name="mobile" class="form-ctrl" required>
</div>

<div style="margin-bottom:20px;">
<label class="lbl">Password</label>
<input type="password" name="password" class="form-ctrl" required>
</div>

<button type="submit" class="btn-gold">
    Add User
</button>

<a href="<?= SITE_URL ?>/admin/users.php"
   style="
   margin-left:10px;
   text-decoration:none;
   padding:10px 16px;
   background:#eee;
   border-radius:8px;
   color:#333;
   font-weight:600;">
   Cancel
</a>

</form>

</div>
</div>
</div>

<?php adminFoot(); ?>