<?php
/**
 * File Edit User - Premium Admin Dashboard
 * Dikembangkan oleh: Sikawarsito, S.Kom
 */

session_start();
include '../config/database.php';

// 1. Proteksi: Hanya admin yang boleh masuk (sesuai kolom 'role' di DB)
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') { 
    header("Location: ../auth/login_page.php"); 
    exit; 
}

// 2. Ambil data user berdasarkan ID
if (!isset($_GET['id'])) {
    header("Location: kelola_siswa.php");
    exit;
}

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

if (!$user) {
    header("Location: kelola_siswa.php");
    exit;
}

// 3. Proses Update Data
$error = "";
$success = "";

if (isset($_POST['update'])) {
    // UBAH: Mengambil input email, username, dan password
    $email = mysqli_real_escape_string($conn, trim(strtolower($_POST['email'])));
    $username = mysqli_real_escape_string($conn, trim($_POST['username']));
    $role = $_POST['role'];
    $status_akun = $_POST['status_akun'];
    $kuota = (int)$_POST['kuota_harian'];
    $new_password = $_POST['password'];

    // Update query disesuaikan dengan kolom-kolom yang diubah
    if (!empty($new_password)) {
        $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
        $update_query = "UPDATE users SET 
            username = '$username',
            email_guru = '$email', 
            password = '$hashed_password',
            role = '$role', 
            status_akun = '$status_akun', 
            kuota_harian = '$kuota' 
            WHERE id = '$id'";
    } else {
        $update_query = "UPDATE users SET 
            username = '$username',
            email_guru = '$email', 
            role = '$role', 
            status_akun = '$status_akun', 
            kuota_harian = '$kuota' 
            WHERE id = '$id'";
    }

    if (mysqli_query($conn, $update_query)) {
        $success = "Data user berhasil diperbarui.";
        // Refresh data user yang sedang ditampilkan
        $query = mysqli_query($conn, "SELECT * FROM users WHERE id = '$id'");
        $user = mysqli_fetch_assoc($query);
    } else {
        $error = "Gagal memperbarui data: " . mysqli_error($conn);
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit User | Admin Intelligence</title>
    
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700&display=swap" rel="stylesheet">

    <style>
        :root { --bg: #f8fafc; --primary: #0d6efd; --border: #e2e8f0; }
        body { font-family: 'Plus Jakarta Sans', sans-serif; background-color: var(--bg); color: #0f172a; margin: 0; }
        
        .edit-container { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 40px 20px; }
        .edit-card { 
            width: 100%; max-width: 550px; background: white; border: 1px solid var(--border); 
            border-radius: 30px; padding: 40px; box-shadow: 0 15px 30px -10px rgba(0,0,0,0.05);
        }
        
        .brand-pill { background: #eff6ff; color: var(--primary); font-size: 0.75rem; font-weight: 700; padding: 6px 16px; border-radius: 50px; display: inline-block; margin-bottom: 20px; }
        
        .form-label { font-weight: 600; font-size: 0.85rem; color: #64748b; margin-bottom: 8px; }
        .form-control, .form-select {
            background: #f8fafc; border: 1px solid var(--border); border-radius: 14px; padding: 12px 18px; color: #0f172a; transition: 0.3s;
        }
        .form-control:focus, .form-select:focus { background: white; border-color: var(--primary); box-shadow: 0 0 0 4px rgba(13, 110, 253, 0.1); }
        .form-control[readonly] { background: #f1f5f9; cursor: not-allowed; }

        .btn-save { background: var(--primary); color: white; border: none; border-radius: 14px; padding: 14px; font-weight: 600; width: 100%; transition: 0.3s; margin-top: 10px; }
        .btn-save:hover { background: #0056b3; transform: translateY(-2px); box-shadow: 0 10px 15px -3px rgba(13, 110, 253, 0.2); }
        
        .back-link { display: flex; align-items: center; justify-content: center; color: #94a3b8; text-decoration: none; font-size: 0.9rem; font-weight: 600; margin-top: 25px; transition: 0.2s; }
        .back-link:hover { color: var(--primary); }
    </style>
</head>
<body>

<div class="edit-container">
    <div class="edit-card">
        <div class="text-center">
            <div class="brand-pill">ADMINISTRATOR TOOL</div>
            <h3 class="fw-bold mb-1">Edit Profil Akun</h3>
            <p class="text-muted small mb-4">Perbarui informasi, kuota, atau status akses user.</p>
        </div>

        <?php if($success): ?>
            <div class="alert alert-success border-0 rounded-4 py-2 text-center small mb-4">
                <i class="bi bi-check-circle-fill me-2"></i> <?= $success ?>
            </div>
        <?php endif; ?>

        <?php if($error): ?>
            <div class="alert alert-danger border-0 rounded-4 py-2 text-center small mb-4">
                <i class="bi bi-exclamation-triangle-fill me-2"></i> <?= $error ?>
            </div>
        <?php endif; ?>

        <form method="POST">
            <div class="row g-3">
                <div class="col-12">
                    <label class="form-label">Username (ID)</label>
                    <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($user['username']) ?>" required>
                </div>

                <div class="col-12">
                    <label class="form-label">Password Baru <span class="text-muted fw-normal" style="font-size: 0.75rem;">(Biarkan kosong jika tidak ingin diubah)</span></label>
                    <input type="text" name="password" class="form-control" placeholder="Masukkan password baru">
                </div>

                <div class="col-12">
                    <label class="form-label">Email Terdaftar</label>
                    <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($user['email_guru'] ?? '') ?>" placeholder="Masukkan email aktif">
                </div>

                <div class="col-md-6">
                    <label class="form-label">Kuota Chat Harian</label>
                    <input type="number" name="kuota_harian" class="form-control" value="<?= $user['kuota_harian'] ?>" required>
                </div>

                <div class="col-md-6">
                    <label class="form-label">Role User</label>
                    <select name="role" class="form-select">
                        <option value="siswa" <?= $user['role'] == 'siswa' ? 'selected' : '' ?>>Siswa</option>
                        <option value="admin" <?= $user['role'] == 'admin' ? 'selected' : '' ?>>Administrator</option>
                    </select>
                </div>

                <div class="col-md-6">
                    <label class="form-label">Status Akses</label>
                    <select name="status_akun" class="form-select">
                        <option value="free" <?= $user['status_akun'] == 'free' ? 'selected' : '' ?>>Layanan FREE</option>
                        <option value="pro" <?= $user['status_akun'] == 'pro' ? 'selected' : '' ?>>Layanan PRO</option>
                        <option value="blokir" <?= $user['status_akun'] == 'blokir' ? 'selected' : '' ?>>BLOKIR AKSES</option>
                    </select>
                </div>
            </div>

            <button type="submit" name="update" class="btn-save mt-4">Simpan Perubahan Data</button>
        </form>

        <a href="kelola_siswa.php" class="back-link">
            <i class="bi bi-arrow-left me-2"></i> Kembali ke Kelola Siswa
        </a>

        <div class="text-center mt-5">
            <small class="text-muted" style="font-size: 10px; letter-spacing: 1px;">
                DEVELOPED BY <b>SIKAWARSITO, S.KOM</b>
            </small>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>