<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) { header('Location: login.php'); exit; }
require_once '../src/config/db.php';
require_once '../src/includes/functions.php';
require_once 'includes/lang.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username  = trim($_POST['username']);
    $password  = trim($_POST['password']);
    $full_name = trim($_POST['full_name']);
    $email     = trim($_POST['email']);

    if (!empty($username) && !empty($password) && !empty($full_name)) {
        $existing = get_user_by_username($pdo, $username);
        if ($existing) {
            $error = $isRTL ? 'اسم المستخدم مستخدم بالفعل.' : 'Username already exists.';
        } else {
            try {
                $hashed = password_hash($password, PASSWORD_DEFAULT);
                $stmt = $pdo->prepare("INSERT INTO users (username, password, full_name, email) VALUES (?,?,?,?)");
                $stmt->execute([$username, $hashed, $full_name, $email]);
                header('Location: users.php?success=add'); exit;
            } catch (\PDOException $e) {
                $error = $e->getMessage();
            }
        }
    } else {
        $error = $isRTL ? 'يرجى تعبئة جميع الحقول المطلوبة.' : 'Please fill in all required fields.';
    }
}
$unreadMessagesCount = $pdo->query("SELECT COUNT(*) FROM contact_messages WHERE is_read = 0")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="<?php echo $t['lang']; ?>" dir="<?php echo $t['dir']; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $t['add_user_title']; ?> - VAULTIXAI</title>
    <link rel="stylesheet" href="<?php echo $bootstrapCSS; ?>" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <?php
    $currentPage = 'users';
    include 'includes/sidebar.php';
    ?>

    <div class="main-content">
        <div class="topbar">
            <h1>➕ <?php echo $t['add_user_title']; ?></h1>
            <a href="users.php" class="btn btn-outline-secondary px-4 py-2"><?php echo $t['user_back_btn']; ?></a>
        </div>

        <?php if (isset($error)): ?>
            <div class="alert alert-danger fw-bold mb-4">⚠️ <?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>

        <div class="table-card p-4 p-md-5 mx-auto" style="max-width:680px;">
            <form method="POST" novalidate>
                <div class="row g-3">
                    <div class="col-12">
                        <label for="full_name" class="form-label"><?php echo $t['user_fullname']; ?> *</label>
                        <input type="text" id="full_name" name="full_name" class="form-control form-control-lg"
                               required placeholder="<?php echo $isRTL ? 'مثال: أحمد محمد' : 'e.g. John Smith'; ?>">
                    </div>
                    <div class="col-12 col-md-6">
                        <label for="username" class="form-label"><?php echo $t['user_username']; ?> *</label>
                        <input type="text" id="username" name="username" class="form-control form-control-lg"
                               required placeholder="admin_user" style="font-family:monospace;">
                    </div>
                    <div class="col-12 col-md-6">
                        <label for="email" class="form-label"><?php echo $t['user_email']; ?></label>
                        <input type="email" id="email" name="email" class="form-control form-control-lg"
                               placeholder="user@example.com" style="font-family:monospace;">
                    </div>
                    <div class="col-12">
                        <label for="password" class="form-label"><?php echo $t['user_password']; ?> *</label>
                        <input type="password" id="password" name="password" class="form-control form-control-lg"
                               required placeholder="••••••••">
                    </div>
                </div>
                <div class="d-flex gap-3 mt-4">
                    <button type="submit" class="btn btn-gold flex-grow-1 py-2 fw-bold fs-6">
                        💾 <?php echo $t['user_save_btn']; ?>
                    </button>
                    <a href="users.php" class="btn btn-outline-secondary py-2 px-4"><?php echo $t['user_cancel_btn']; ?></a>
                </div>
            </form>
        </div>
    </div>

    <script src="assets/vendor/bootstrap/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
</body>
</html>
