<?php
include '../config/koneksi.php';
include '../session.php';

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

if (!isset($_GET['id'])) {
    header("Location: users.php");
    exit;
}

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

$id   = (int)$_GET['id'];
$data = mysqli_query($conn, "SELECT * FROM users WHERE id_user='$id'");
$user = mysqli_fetch_assoc($data);

if (!$user) {
    die("Data user tidak ditemukan");
}

if (isset($_POST['update'])) {
    $nama     = mysqli_real_escape_string($conn, $_POST['nama']);
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $role     = mysqli_real_escape_string($conn, $_POST['role']);
    $status   = mysqli_real_escape_string($conn, $_POST['status']);
    $password = $_POST['password'];

    if (!empty($password)) {
        $password = password_hash($password, PASSWORD_DEFAULT);
        $query = "UPDATE users SET nama='$nama', username='$username', password='$password', role='$role', status='$status' WHERE id_user='$id'";
    } else {
        $query = "UPDATE users SET nama='$nama', username='$username', role='$role', status='$status' WHERE id_user='$id'";
    }

    if (mysqli_query($conn, $query)) {
        echo "<script>alert('Data berhasil diperbarui'); window.location='users.php';</script>";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <title>Edit User</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        :root {
            --green-500: #22c55e;
            --green-600: #16a34a;
            --green-700: #15803d;
            --gray-200: #e5e7eb;
            --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; }
        .icon-box { width:40px; height:40px; border-radius:12px; background:#dcfce7; color:var(--green-600); display:flex; align-items:center; justify-content:center; font-size:20px; }
    </style>
</head>
<body>

<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card card-custom">
                <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">Edit Pengguna</h5>
                            <small class="text-muted">Perbarui data akun pengguna</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"
                                       value="<?= htmlspecialchars($user['nama']) ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Username</label>
                                <input type="text" name="username" class="form-control"
                                       value="<?= htmlspecialchars($user['username']) ?>" required>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Password Baru</label>
                            <input type="text" name="password" class="form-control"
                                   placeholder="Kosongkan jika tidak ingin mengubah password">
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Role</label>
                                <select name="role" class="form-select">
                                    <option value="">-- Pilih Role --</option>
                                    <?php while ($r = mysqli_fetch_assoc($role_query)): ?>
                                        <option value="<?= $r['nama_role'] ?>"
                                            <?= $user['role'] == $r['nama_role'] ? 'selected' : '' ?>>
                                            <?= ucfirst($r['nama_role']) ?>
                                        </option>
                                    <?php endwhile; ?>
                                </select>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Status</label>
                                <select name="status" class="form-select">
                                    <option value="aktif"    <?= $user['status']=='aktif'    ? 'selected' : '' ?>>Aktif</option>
                                    <option value="nonaktif" <?= $user['status']=='nonaktif' ? 'selected' : '' ?>>Nonaktif</option>
                                </select>
                            </div>
                        </div>

                        <div class="pt-3 border-top mt-3">
                            <button type="submit" name="update" class="btn btn-save">💾 Update</button>
                            <a href="users.php" class="btn btn-outline-secondary" style="border-radius:12px;padding:10px 25px;">Kembali</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
</html>