<?php
require_once '../config/koneksi.php';
require_once '../helpers/functions.php';

if (!isLogin() || !isAdmin()) {
    redirect('../auth/login.php');
}

if (isset($_POST['simpan'])) {
    $nama = trim($_POST['nama']);
    $username = trim($_POST['username']);
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $role = $_POST['role'];

    if (empty($nama) || empty($username) || empty($_POST['password']) || empty($role)) {
        setFlash('danger', 'Semua field wajib diisi.');
        redirect('user_tambah.php');
    }

    $cek = mysqli_prepare($conn, "SELECT id FROM users WHERE username=?");
    mysqli_stmt_bind_param($cek, "s", $username);
    mysqli_stmt_execute($cek);
    $result = mysqli_stmt_get_result($cek);

    if (mysqli_num_rows($result) > 0) {
        setFlash('danger', 'Username sudah digunakan.');
        redirect('user_tambah.php');
    }

    $stmt = mysqli_prepare($conn, "INSERT INTO users (nama, username, password, role, saldo) VALUES (?, ?, ?, ?, 0)");
    mysqli_stmt_bind_param($stmt, "ssss", $nama, $username, $password, $role);
    mysqli_stmt_execute($stmt);

    setFlash('success', 'User berhasil ditambahkan.');
    redirect('users.php');
}

$title = "Tambah User";
include '../layouts/header.php';
?>

<div class="container-fluid">
    <div id="overlay" class="overlay"></div>

    <div class="row">
        <div class="col-lg-2 p-0">
            <?php include '../layouts/sidebar.php'; ?>
        </div>

        <div class="col-lg-10 p-0">
            <?php include '../layouts/navbar.php'; ?>

            <div class="content-wrap">
                <?php showFlash(); ?>

                <div class="card p-4">
                    <div class="mb-4">
                        <h4 class="section-title mb-1">Tambah User</h4>
                        <p class="text-muted mb-0">Tambahkan akun admin atau client baru.</p>
                    </div>

                    <form method="POST" class="row g-3">
                        <div class="col-md-6">
                            <label>Nama Lengkap</label>
                            <input type="text" name="nama" class="form-control" required>
                        </div>

                        <div class="col-md-6">
                            <label>Username</label>
                            <input type="text" name="username" class="form-control" required>
                        </div>

                        <div class="col-md-6">
                            <label>Password</label>
                            <input type="password" name="password" class="form-control" required>
                        </div>

                        <div class="col-md-6">
                            <label>Role</label>
                            <select name="role" class="form-select" required>
                                <option value="">-- Pilih Role --</option>
                                <option value="admin">Admin</option>
                                <option value="client">Client</option>
                            </select>
                        </div>

                        <div class="col-12 d-flex gap-2 mt-3">
                            <button type="submit" name="simpan" class="btn btn-primary">Simpan</button>
                            <a href="users.php" class="btn btn-outline-secondary">Kembali</a>
                        </div>
                    </form>
                </div>

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

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