<?php
$judul = "Tambah User";
$menu  = "users";
include 'layout/header.php';

$role_query = mysqli_query($conn, "SELECT * FROM roles");

if($_SESSION['role'] != 'admin'){
    die("Akses ditolak");
}

if(isset($_POST['simpan'])){
    $nama     = mysqli_real_escape_string($conn, $_POST['nama']);
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    // Menggunakan password_hash untuk keamanan
    $password = mysqli_real_escape_string($conn, $_POST['password']);
    $role     = mysqli_real_escape_string($conn, $_POST['role']);
    $status   = mysqli_real_escape_string($conn, $_POST['status']);

    $cek = mysqli_query($conn, "SELECT * FROM users WHERE username='$username'");

    if(mysqli_num_rows($cek) > 0){
        echo "<script>alert('Username sudah digunakan!'); window.history.back();</script>";
        exit;
    }

    $simpan = mysqli_query($conn, "INSERT INTO users (nama, username, password, role, status) VALUES ('$nama', '$username', '$password', '$role', '$status')");

    if($simpan){
        echo "<script>alert('User berhasil ditambahkan'); window.location='users.php';</script>";
        exit;
    }
}
?>

<?php include 'layout/sidebar.php'; ?>

<style>
:root{
    --green-50:#f0fdf4;
    --green-100:#dcfce7;
    --green-200:#bbf7d0;
    --green-400:#4ade80;
    --green-500:#22c55e;
    --green-600:#16a34a;
    --green-700:#15803d;

    --gray-100:#f3f4f6;
    --gray-200:#e5e7eb;
    --gray-400:#9ca3af;
    --gray-700:#374151;
}

.card-custom{
    background:#fff;
    border:1px solid var(--gray-200);
    border-radius:18px;
    overflow:hidden;
    box-shadow:0 4px 15px rgba(0,0,0,.05);
}

.card-header{
    background:#fff !important;
    color:var(--green-600) !important;
    border-bottom:1px solid var(--gray-200);
    font-weight:700;
    font-size:18px;
}

.form-label{
    font-weight:600;
    color:var(--gray-700);
    margin-bottom:6px;
}

.form-control,
.form-select{
    border:1px solid var(--gray-200);
    border-radius:12px;
    min-height:48px;
    background:#fff;
}

.form-control:focus,
.form-select:focus{
    border-color:var(--green-500);
    box-shadow:0 0 0 .2rem rgba(34,197,94,.15);
}

.btn-save{
    background:linear-gradient(
        135deg,
        var(--green-600),
        var(--green-500)
    );
    border:none;
    color:#fff;
    font-weight:600;
    border-radius:12px;
    padding:10px 25px;
}

.btn-save:hover{
    background:linear-gradient(
        135deg,
        var(--green-700),
        var(--green-600)
    );
    color:#fff;
}

.btn-back{
    border-radius:12px;
    padding:10px 25px;
}

.icon-box{
    width:40px;
    height:40px;
    border-radius:12px;
    background:var(--green-100);
    color:var(--green-600);
    display:flex;
    align-items:center;
    justify-content:center;
    font-size:20px;
}
</style>

<div class="container-fluid px-4 py-4">
    <div class="row">
        <div class="col-12 col-lg-10">
            <div class="card card-custom shadow-lg">
                <div class="card-header py-3 px-4">
    <div class="d-flex align-items-center gap-3">
        <div class="icon-box">
            👤
        </div>
        <div>
            <h5 class="mb-0 fw-bold">Tambah User Baru</h5>
            <small class="text-muted">
                Tambahkan akun pengguna sistem
            </small>
        </div>
    </div>
</div>
                <div class="card-body p-4">
                    <form method="POST">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Nama</label>
                                <input type="text" name="nama" class="form-control" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Username</label>
                                <input type="text" name="username" class="form-control" required>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Password</label>
                                <input type="password" name="password" class="form-control" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Role</label>
                            <?php
$role_query = mysqli_query($conn, "SELECT * FROM roles ORDER BY nama_role");
?>

                                <select name="role" class="form-select" required>
                                    <option value="">-- Pilih Role --</option>

                                    <?php while($r = mysqli_fetch_assoc($role_query)) { ?>
                                        <option value="<?= $r['nama_role'] ?>">
                                            <?= ucfirst($r['nama_role']) ?>
                                        </option>
                                    <?php } ?>

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

                        <div class="mb-3">
                            <label class="form-label">Status</label>
                            <select name="status" class="form-select" required>
                                <option value="aktif">Aktif</option>
                                <option value="nonaktif">Nonaktif</option>
                            </select>
                        </div>

                        <div class="pt-3 border-top mt-3">
    <button type="submit" name="simpan" class="btn btn-save">
        💾 Simpan User
    </button>

    <a href="users.php" class="btn btn-outline-secondary btn-back">
        Kembali
    </a>
</div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

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