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

$admin_id = $_SESSION['admin_auth']['id'];

// Handle Form Submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['update_profile'])) {
        $nama = $_POST['nama'];
        $username = $_POST['username'];
        $password = $_POST['password'];

        $pdo->beginTransaction();
        try {
            // Update Name and Username
            $stmt = $pdo->prepare("UPDATE administrator SET nama = ?, username = ? WHERE id = ?");
            $stmt->execute(array($nama, $username, $admin_id));

            // Update Password if not empty
            if (!empty($password)) {
                $hashed = password_hash($password, PASSWORD_DEFAULT);
                $stmt = $pdo->prepare("UPDATE administrator SET password = ? WHERE id = ?");
                $stmt->execute(array($hashed, $admin_id));
            }

            $pdo->commit();
            
            // Update Session
            $_SESSION['admin_auth']['nama'] = $nama;
            $_SESSION['admin_auth']['username'] = $username;
            
            setFlash('success', 'Profil berhasil diperbarui.');
            redirect('profil.php');
        } catch (Exception $e) {
            $pdo->rollBack();
            setFlash('error', 'Gagal memperbarui profil: ' . $e->getMessage());
        }
    }
}

// Get current admin data for display
$stmt = $pdo->prepare("SELECT * FROM administrator WHERE id = ?");
$stmt->execute(array($admin_id));
$admin = $stmt->fetch();

$title = "Profil Administrator";
require_once 'includes/header.php';
?>

<div class="max-w-4xl mx-auto">
    <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
        <!-- Sidebar Info -->
        <div class="md:col-span-1">
            <div class="card p-8 text-center bg-white shadow-sm border border-slate-200">
                <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($admin['nama'], 0, 1) ?>
                </div>
                <h3 class="text-xl font-bold text-slate-800 mb-1"><?= $admin['nama'] ?></h3>
                <p class="text-xs font-bold text-slate-400 uppercase tracking-widest"><?= $admin['username'] ?></p>
                
                <div class="mt-8 pt-8 border-t border-slate-50 space-y-4">
                    <div class="bg-slate-50 p-4 rounded-2xl">
                        <p class="text-[10px] font-bold text-slate-400 uppercase tracking-widest mb-1">Role Akun</p>
                        <p class="text-sm font-bold text-primary">Administrator Sekolah</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Form Card -->
        <div class="md:col-span-2">
            <div class="card p-8 shadow-sm border border-slate-200">
                <h4 class="text-lg font-bold text-slate-800 mb-8 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="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path></svg>
                    Informasi Akun
                </h4>
                
                <form action="" method="POST" class="space-y-6">
                    <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">Nama Lengkap</label>
                            <input type="text" name="nama" value="<?= $admin['nama'] ?>" required class="w-full px-5 py-3 rounded-xl border border-slate-200 focus:border-primary outline-none transition-all">
                        </div>
                        <div>
                            <label class="block text-sm font-bold text-slate-700 mb-2">Username</label>
                            <input type="text" name="username" value="<?= $admin['username'] ?>" required class="w-full px-5 py-3 rounded-xl border border-slate-200 focus:border-primary outline-none transition-all">
                        </div>
                    </div>

                    <div class="p-6 bg-slate-50 rounded-2xl border border-slate-100">
                        <label class="block text-sm font-bold text-slate-700 mb-2">Password Baru</label>
                        <input type="password" name="password" class="w-full px-5 py-3 rounded-xl border border-slate-200 focus:border-primary outline-none transition-all" placeholder="Kosongkan jika tidak ingin ganti password">
                        <p class="mt-2 text-[10px] text-slate-400 font-medium italic">Minimal 6 karakter jika ingin diperbarui.</p>
                    </div>

                    <div class="pt-4 flex gap-4">
                        <a href="index.php" class="flex-1 py-4 text-center text-sm font-bold text-slate-400 hover:text-slate-600 transition-colors">Kembali</a>
                        <button type="submit" name="update_profile" class="flex-[2] btn-primary py-4">Simpan Perubahan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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