<?php
require_once __DIR__ . '/config/db.php';
require_once __DIR__ . '/includes/functions.php';
checkSiswa();

$nis = $_SESSION['siswa_auth']['nis'];

// Fetch current student data for display
$stmt = $pdo->prepare("
    SELECT s.*, k.nama as kelas_nama 
    FROM siswa s 
    LEFT JOIN kelas k ON s.id_kelas = k.id 
    WHERE s.nis = ?
");
$stmt->execute(array($nis));
$siswa = $stmt->fetch();

$message = '';
$messageType = '';

// Handle Password Update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_password'])) {
    $current = $_POST['current_password'];
    $new = $_POST['new_password'];
    $confirm = $_POST['confirm_password'];

    // Verify current password
    if (password_verify($current, $siswa['password'])) {
        if ($new === $confirm) {
            if (strlen($new) >= 6) {
                $hashed = password_hash($new, PASSWORD_DEFAULT);
                $stmt = $pdo->prepare("UPDATE siswa SET password = ? WHERE nis = ?");
                $stmt->execute(array($hashed, $nis));
                $message = "Password berhasil diperbarui.";
                $messageType = "success";
            } else {
                $message = "Password minimal 6 karakter.";
                $messageType = "error";
            }
        } else {
            $message = "Konfirmasi password tidak cocok.";
            $messageType = "error";
        }
    } else {
        $message = "Password saat ini salah.";
        $messageType = "error";
    }
}

require_once 'includes/header-siswa.php';
?>

<div class="container mx-auto px-6 py-12">
    <div class="max-w-4xl mx-auto">
        <div class="mb-12">
            <h2 class="text-3xl font-extrabold text-slate-800">Profil Saya</h2>
            <p class="text-slate-500">Kelola informasi pribadi dan keamanan akun Anda.</p>
        </div>

        <?php if ($message): ?>
            <div class="mb-8 p-5 rounded-2xl border <?= $messageType === 'success' ? 'bg-green-50 text-green-700 border-green-100' : 'bg-red-50 text-red-700 border-red-100' ?> flex items-center gap-3 font-bold text-sm">
                <?php if ($messageType === 'success'): ?>
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
                <?php else: ?>
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                <?php endif; ?>
                <?= $message ?>
            </div>
        <?php endif; ?>

        <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
            <!-- Info Card -->
            <div class="md:col-span-1">
                <div class="card p-8 text-center">
                    <div class="w-24 h-24 bg-primary/10 text-primary rounded-full flex items-center justify-center text-3xl font-black mx-auto mb-6">
                        <?= substr($siswa['nama'], 0, 1) ?>
                    </div>
                    <h3 class="text-xl font-bold text-slate-800 mb-1"><?= $siswa['nama'] ?></h3>
                    <p class="text-xs font-bold text-slate-400 uppercase tracking-widest"><?= $siswa['nis'] ?></p>
                    
                    <div class="mt-8 pt-8 border-t border-slate-50 space-y-4">
                        <div class="flex justify-between text-sm">
                            <span class="text-slate-400">Kelas</span>
                            <span class="font-bold text-slate-700"><?= $siswa['kelas_nama'] ?></span>
                        </div>
                        <div class="flex justify-between text-sm">
                            <span class="text-slate-400">Gender</span>
                            <span class="font-bold text-slate-700"><?= $siswa['kelamin'] === 'L' ? 'Laki-laki' : 'Perempuan' ?></span>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Update Password Card -->
            <div class="md:col-span-2">
                <div class="card p-8">
                    <h4 class="text-lg font-bold text-slate-800 mb-6 flex items-center gap-2">
                        <svg class="w-5 h-5 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"></path></svg>
                        Update Keamanan Password
                    </h4>
                    
                    <form action="" method="POST" class="space-y-6">
                        <div>
                            <label class="block text-sm font-bold text-slate-700 mb-2">Password Saat Ini</label>
                            <input type="password" name="current_password" required class="w-full px-5 py-3 rounded-xl border border-slate-100 bg-slate-50 focus:border-primary outline-none transition-all" placeholder="••••••••">
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label class="block text-sm font-bold text-slate-700 mb-2">Password Baru</label>
                                <input type="password" name="new_password" required class="w-full px-5 py-3 rounded-xl border border-slate-100 bg-slate-50 focus:border-primary outline-none transition-all" placeholder="Min. 6 karakter">
                            </div>
                            <div>
                                <label class="block text-sm font-bold text-slate-700 mb-2">Konfirmasi Password</label>
                                <input type="password" name="confirm_password" required class="w-full px-5 py-3 rounded-xl border border-slate-100 bg-slate-50 focus:border-primary outline-none transition-all" placeholder="Ulangi password baru">
                            </div>
                        </div>

                        <div class="pt-4">
                            <button type="submit" name="update_password" class="w-full btn-primary py-4">Simpan Perubahan Password</button>
                        </div>
                    </form>
                </div>
                
                <div class="mt-8 p-6 bg-slate-100 rounded-2xl flex items-start gap-4">
                    <div class="pt-1">
                        <svg class="w-5 h-5 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                    </div>
                    <p class="text-xs text-slate-500 leading-relaxed italic">
                        Pastikan Anda menggunakan password yang kuat dan sulit ditebak. Segera hubungi Admin sekolah jika Anda mengalami kendala saat mengakses akun Anda.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require_once 'includes/footer-siswa.php'; ?>
