<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/config.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/functions.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/header.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/sidebar.php';

// Cek login & akses admin
cek_login();
if ($_SESSION['role'] != 'admin') {
    header("Location: " . BASE_URL . "/index.php?access=denied");
    exit();
}

$error = '';
$success = '';
$username = '';
$nama = '';
$role = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = sanitize($_POST['username']);
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];
    $nama = sanitize($_POST['nama']);
    $role = sanitize($_POST['role']);
    
    if (empty($username) || empty($password) || empty($nama) || empty($role)) {
        $error = "Semua field bertanda * harus diisi!";
    } elseif ($password != $confirm_password) {
        $error = "Password dan konfirmasi password tidak cocok!";
    } elseif (strlen($password) < 6) {
        $error = "Password minimal 6 karakter!";
    } else {
        $check_query = "SELECT * FROM users WHERE username = '$username'";
        $check_result = mysqli_query($conn, $check_query);
        
        if (mysqli_num_rows($check_result) > 0) {
            $error = "Username sudah digunakan!";
        } else {
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            $insert_query = "INSERT INTO users (username, password, nama, role) VALUES ('$username', '$hashed_password', '$nama', '$role')";
            
            if (mysqli_query($conn, $insert_query)) {
                header("Location: " . BASE_URL . "/modules/users/index.php?success=" . urlencode("User baru berhasil ditambahkan!"));
                exit();
            } else {
                $error = "Gagal menambahkan user: " . mysqli_error($conn);
            }
        }
    }
}
?>

<!-- Main Content -->
<div class="main-content flex-1 p-8">
    <div class="flex justify-between items-center mb-6">
        <div>
            <h2 class="text-3xl font-bold text-white">Tambah User Baru</h2>
            <p class="text-slate-400">Buat akun pengguna baru untuk aplikasi.</p>
        </div>
        <a href="<?php echo BASE_URL; ?>/modules/users/index.php" class="bg-slate-700 hover:bg-slate-600 text-white font-bold py-2.5 px-6 rounded-lg transition-colors">Kembali</a>
    </div>
    
    <?php if (!empty($error)): ?>
    <div class="bg-red-500/20 border border-red-500/30 text-red-300 px-4 py-3 rounded-lg relative mb-6 text-sm flex items-center gap-3" role="alert"><i class="fas fa-exclamation-circle"></i><span><?php echo htmlspecialchars($error); ?></span></div>
    <?php endif; ?>
    
    <div class="glass-effect p-6 rounded-xl shadow-lg">
        <form method="POST" action="">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- Kolom Kiri -->
                <div class="space-y-4">
                    <div>
                        <label for="username" class="block text-slate-400 text-sm font-medium mb-2">Username <span class="text-red-400">*</span></label>
                        <input type="text" name="username" id="username" value="<?php echo htmlspecialchars($username); ?>" class="w-full border border-slate-600 bg-slate-700/50 text-slate-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-sky-500 px-3 py-2.5" required>
                        <p class="text-slate-500 text-xs mt-1">Harus unik, tanpa spasi.</p>
                    </div>
                    <div>
                        <label for="nama" class="block text-slate-400 text-sm font-medium mb-2">Nama Lengkap <span class="text-red-400">*</span></label>
                        <input type="text" name="nama" id="nama" value="<?php echo htmlspecialchars($nama); ?>" class="w-full border border-slate-600 bg-slate-700/50 text-slate-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-sky-500 px-3 py-2.5" required>
                    </div>
                    <div>
                        <label for="role" class="block text-slate-400 text-sm font-medium mb-2">Role <span class="text-red-400">*</span></label>
                        <select name="role" id="role" class="w-full border border-slate-600 bg-slate-700/50 text-slate-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-sky-500 px-3 py-2.5" required>
                            <option value="">Pilih Role</option>
                            <option value="admin" <?php echo ($role == 'admin') ? 'selected' : ''; ?>>Admin</option>
                            <option value="hrd" <?php echo ($role == 'hrd') ? 'selected' : ''; ?>>HRD</option>
                            <option value="karyawan" <?php echo ($role == 'karyawan') ? 'selected' : ''; ?>>Karyawan</option>
                        </select>
                    </div>
                </div>
                
                <!-- Kolom Kanan -->
                <div class="space-y-4">
                    <div>
                        <label for="password" class="block text-slate-400 text-sm font-medium mb-2">Password <span class="text-red-400">*</span></label>
                        <input type="password" name="password" id="password" class="w-full border border-slate-600 bg-slate-700/50 text-slate-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-sky-500 px-3 py-2.5" required>
                        <p class="text-slate-500 text-xs mt-1">Minimal 6 karakter.</p>
                    </div>
                    <div>
                        <label for="confirm_password" class="block text-slate-400 text-sm font-medium mb-2">Konfirmasi Password <span class="text-red-400">*</span></label>
                        <input type="password" name="confirm_password" id="confirm_password" class="w-full border border-slate-600 bg-slate-700/50 text-slate-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-sky-500 px-3 py-2.5" required>
                    </div>
                </div>
            </div>
            
            <div class="mt-8 border-t border-slate-700/50 pt-6 flex justify-end gap-4">
                <button type="reset" class="bg-slate-700 hover:bg-slate-600 text-white font-bold py-2.5 px-6 rounded-lg transition-colors">Reset</button>
                <button type="submit" class="bg-sky-600 hover:bg-sky-700 text-white font-bold py-2.5 px-6 rounded-lg transition-colors flex items-center gap-2">
                    <i class="fas fa-save"></i> Simpan
                </button>
            </div>
        </form>
    </div>
</div>

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/yoga/includes/footer.php';
?>
