<?php
/**
 * admin/setting_akun.php - Keamanan Akun Admin
 * Fitur: Ikon Mata (Show/Hide Password), Update Email & Username
 * Developer: Sikawarsito, S.Kom
 */
session_start();
include '../config/database.php';

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') { 
    header("Location: ../auth/login_page.php"); exit; 
}

$user_id = $_SESSION['user_id'];
// Mengambil username dan email_guru (sebagai pengganti nama)
$user_data = mysqli_fetch_assoc(mysqli_query($conn, "SELECT username, email_guru FROM users WHERE id = '$user_id'"));

$msg = "";
if (isset($_POST['update_akun'])) {
    $username_baru = mysqli_real_escape_string($conn, trim($_POST['username']));
    $email_baru    = mysqli_real_escape_string($conn, trim(strtolower($_POST['email'])));
    $pw_baru       = $_POST['password_baru'];
    $pw_konf       = $_POST['password_konf'];

    // Cek agar username tidak menggunakan spasi
    if (preg_match('/\s/', $username_baru)) {
        $msg = "<div class='alert alert-danger shadow-sm'>Username tidak boleh mengandung spasi!</div>";
    } else {
        // Query disesuaikan menjadi email_guru
        $sql = "UPDATE users SET email_guru = '$email_baru', username = '$username_baru' WHERE id = '$user_id'";
        
        if (!empty($pw_baru)) {
            if ($pw_baru === $pw_konf) {
                $hash_pw = password_hash($pw_baru, PASSWORD_BCRYPT);
                $sql = "UPDATE users SET email_guru = '$email_baru', username = '$username_baru', password = '$hash_pw' WHERE id = '$user_id'";
            } else {
                $msg = "<div class='alert alert-danger shadow-sm'>Konfirmasi password tidak cocok!</div>";
            }
        }

        if ($msg == "") {
            if (mysqli_query($conn, $sql)) {
                // Sesi nama diisi dengan username karena kolom nama sudah dihapus
                $_SESSION['nama'] = $username_baru; 
                $msg = "<div class='alert alert-success shadow-sm'>Akun berhasil diperbarui!</div>";
                
                // Refresh data untuk ditampilkan di input form
                $user_data['username'] = $username_baru;
                $user_data['email_guru'] = $email_baru;
            } else {
                $msg = "<div class='alert alert-danger shadow-sm'>Gagal mengupdate: " . mysqli_error($conn) . "</div>";
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Setting Akun | Admin</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">
    <style>
        body { background: #f4f7f6; font-family: 'Inter', sans-serif; }
        .card-setting { border: none; border-radius: 20px; box-shadow: 0 10px 30px rgba(0,0,0,0.05); }
        .input-group-text { background: transparent; border-left: none; cursor: pointer; color: #6c757d; }
        .form-control { border-right: none; }
        .form-control:focus { border-color: #dee2e6; box-shadow: none; }
        .form-control:focus + .input-group-text { border-color: #dee2e6; }
    </style>
</head>
<body>
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card card-setting p-4 bg-white">
                <h4 class="fw-bold mb-4"><i class="bi bi-shield-lock-fill text-primary me-2"></i> Pengaturan Akun</h4>
                
                <?= $msg ?>

                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label small fw-bold">Email Terdaftar</label>
                        <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($user_data['email_guru'] ?? '') ?>" required style="border-right: 1px solid #dee2e6;">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label small fw-bold">Username</label>
                        <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($user_data['username'] ?? '') ?>" required style="border-right: 1px solid #dee2e6;">
                    </div>
                    
                    <hr class="my-4">
                    <p class="text-muted small">Kosongkan password jika tidak ingin mengubah.</p>
                    
                    <div class="mb-3">
                        <label class="form-label small fw-bold">Password Baru</label>
                        <div class="input-group">
                            <input type="password" name="password_baru" id="pw1" class="form-control" placeholder="Minimal 6 karakter">
                            <span class="input-group-text" onclick="togglePw('pw1', 'eye1')">
                                <i class="bi bi-eye-slash" id="eye1"></i>
                            </span>
                        </div>
                    </div>

                    <div class="mb-4">
                        <label class="form-label small fw-bold">Konfirmasi Password Baru</label>
                        <div class="input-group">
                            <input type="password" name="password_konf" id="pw2" class="form-control" placeholder="Ulangi password">
                            <span class="input-group-text" onclick="togglePw('pw2', 'eye2')">
                                <i class="bi bi-eye-slash" id="eye2"></i>
                            </span>
                        </div>
                    </div>

                    <button type="submit" name="update_akun" class="btn btn-primary w-100 rounded-pill py-2 fw-bold shadow">Update Akun</button>
                    <a href="index.php" class="btn btn-link w-100 text-muted mt-2 text-decoration-none small">Kembali</a>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    function togglePw(inputId, eyeId) {
        const input = document.getElementById(inputId);
        const eye = document.getElementById(eyeId);
        
        if (input.type === "password") {
            input.type = "text";
            eye.classList.remove("bi-eye-slash");
            eye.classList.add("bi-eye");
        } else {
            input.type = "password";
            eye.classList.remove("bi-eye");
            eye.classList.add("bi-eye-slash");
        }
    }
</script>
</body>
</html>