<?php
session_start();
header('Content-Type: text/html; charset=utf-8');

// 1. VERIFICAÇÃO DE LOGIN
if (!isset($_SESSION['usuario_id'])) {
    header("Location: login.php");
    exit;
}

$usuario_id = trim((string)$_SESSION['usuario_id']);
$file_usuarios = 'usuarios.json';
$file_pedidos = 'db_pedidos.json';

$usuarios = file_exists($file_usuarios) ? json_decode(file_get_contents($file_usuarios), true) : [];
$pedidos = file_exists($file_pedidos) ? json_decode(file_get_contents($file_pedidos), true) : [];

$user_logado = null;
if (is_array($usuarios)) {
    foreach ($usuarios as $u) {
        $id_json = isset($u['id']) ? trim((string)$u['id']) : '';
        if ($id_json === $usuario_id) {
            $user_logado = $u;
            break;
        }
    }
}

if (!$user_logado) {
    $user_logado = [
        'nome' => 'Usuário',
        'saldo' => 0,
        'email' => 'Não identificado',
        'id' => $usuario_id
    ];
}

$total_corridas = 0;
if (is_array($pedidos)) {
    foreach ($pedidos as $p) {
        $cliente_id_pedido = isset($p['cliente_id']) ? trim((string)$p['cliente_id']) : '';
        if ($cliente_id_pedido === $usuario_id) {
            $total_corridas++;
        }
    }
}
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <title>Meu Perfil | BRT Express</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700;900&display=swap');
        
        /* NOVO PADRÃO: Cinza, Amarelo e Preto */
        body { font-family: 'Inter', sans-serif; background-color: #F3F4F6; color: #111827; }
        
        .bg-brt-yellow { background-color: #FFD100; }
        .header-black { background: #000000; }
        
        .card-white { 
            background: #FFFFFF; 
            border: 1px solid #E5E7EB; 
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.05); 
        }

        .avatar-border { border: 4px solid #F3F4F6; }
    </style>
</head>
<body class="pb-32">

    <div class="header-black h-48 w-full relative flex items-end justify-center pb-12">
        <div class="absolute top-6 right-6">
            <a href="logout.php" class="bg-red-500 text-white px-4 py-2 rounded-xl text-[10px] font-black uppercase tracking-widest shadow-lg">
                Sair <i class="fas fa-sign-out-alt ml-2"></i>
            </a>
        </div>
        
        <div class="relative translate-y-6">
            <div class="w-28 h-28 bg-white rounded-[2.5rem] avatar-border shadow-2xl flex items-center justify-center overflow-hidden">
                <img src="https://ui-avatars.com/api/?name=<?php echo urlencode($user_logado['nome']); ?>&background=FFD100&color=000&size=128" alt="Avatar">
            </div>
            <div class="absolute bottom-1 right-1 w-8 h-8 bg-black rounded-full border-4 border-white flex items-center justify-center text-[10px] text-brt-yellow">
                <i class="fas fa-certificate"></i>
            </div>
        </div>
    </div>

    <div class="px-6 mt-12 text-center">
        <h1 class="text-2xl font-black italic uppercase tracking-tighter text-black"><?php echo htmlspecialchars($user_logado['nome']); ?></h1>
        <p class="text-gray-400 text-[10px] font-bold uppercase tracking-[0.3em] mt-1">Cliente VIP BRT Express</p>
    </div>

    <div class="p-6 max-w-md mx-auto space-y-4">

        <div class="grid grid-cols-2 gap-4">
            <div class="card-white p-5 rounded-[2rem] text-center border-b-4 border-black">
                <p class="text-[9px] font-black text-gray-400 uppercase tracking-widest mb-1 italic">Saldo Disponível</p>
                <p class="text-lg font-black text-black italic">R$ <?php echo number_format($user_logado['saldo'] ?? 0, 2, ',', '.'); ?></p>
            </div>
            <div class="card-white p-5 rounded-[2rem] text-center border-b-4 border-brt-yellow">
                <p class="text-[9px] font-black text-gray-400 uppercase tracking-widest mb-1 italic">Total Corridas</p>
                <p class="text-lg font-black text-black italic"><?php echo $total_corridas; ?></p>
            </div>
        </div>

        <div class="card-white rounded-[2.5rem] overflow-hidden">
            <div class="p-6 space-y-6">
                
                <div class="flex items-center gap-4">
                    <div class="w-10 h-10 bg-gray-100 rounded-xl flex items-center justify-center text-black border border-gray-200">
                        <i class="fas fa-envelope text-xs"></i>
                    </div>
                    <div class="text-left">
                        <p class="text-[8px] font-black text-gray-400 uppercase">E-mail de Acesso</p>
                        <p class="text-xs font-bold text-gray-800"><?php echo htmlspecialchars($user_logado['email']); ?></p>
                    </div>
                </div>

                <div class="flex items-center gap-4">
                    <div class="w-10 h-10 bg-gray-100 rounded-xl flex items-center justify-center text-black border border-gray-200">
                        <i class="fas fa-id-card text-xs"></i>
                    </div>
                    <div class="text-left">
                        <p class="text-[8px] font-black text-gray-400 uppercase">ID da Conta</p>
                        <p class="text-xs font-bold text-gray-800">#<?php echo $user_logado['id']; ?></p>
                    </div>
                </div>

                <div class="flex items-center gap-4">
                    <div class="w-10 h-10 bg-brt-yellow/20 rounded-xl flex items-center justify-center text-black border border-brt-yellow/20">
                        <i class="fas fa-shield-alt text-xs"></i>
                    </div>
                    <div class="text-left">
                        <p class="text-[8px] font-black text-gray-400 uppercase">Status Global</p>
                        <p class="text-xs font-bold text-black uppercase italic">Verificada & Ativa</p>
                    </div>
                </div>

            </div>
        </div>

        <div class="space-y-3 pt-4">
            <a href="contacredito.php" class="w-full bg-black text-brt-yellow font-black py-5 rounded-2xl flex items-center justify-center gap-3 uppercase italic text-[11px] tracking-widest shadow-xl active:scale-95 transition-all">
                <i class="fas fa-wallet"></i> Gerenciar Minha Carteira
            </a>
            
            <a href="https://wa.me/5521986297787" class="w-full card-white text-gray-600 font-black py-5 rounded-2xl flex items-center justify-center gap-3 uppercase italic text-[11px] tracking-widest active:scale-95 transition-all">
                <i class="fab fa-whatsapp text-emerald-500 text-lg"></i> Suporte WhatsApp
            </a>
        </div>

        <p class="text-center text-[9px] text-gray-400 font-black uppercase tracking-[0.4em] pt-8 italic">
            BRT Express • Rio de Janeiro
        </p>

    </div>

    <nav class="fixed bottom-0 left-0 right-0 bg-black border-t border-white/10 px-8 py-4 flex justify-between items-center z-50 rounded-t-[2.5rem] shadow-2xl">
        <a href="index.php" class="flex flex-col items-center group">
            <i class="fas fa-home text-gray-500 group-hover:text-brt-yellow transition-colors"></i>
            <span class="text-[8px] font-black uppercase mt-1 text-gray-500">Início</span>
        </a>
        <a href="acompanhamento.php" class="flex flex-col items-center group">
            <i class="fas fa-map-marker-alt text-gray-500 group-hover:text-brt-yellow"></i>
            <span class="text-[8px] font-black uppercase mt-1 text-gray-500">Monitorar</span>
        </a>
        <a href="contacredito.php" class="flex flex-col items-center group">
            <i class="fas fa-wallet text-gray-500 group-hover:text-brt-yellow"></i>
            <span class="text-[8px] font-black uppercase mt-1 text-gray-500">Carteira</span>
        </a>
        <a href="perfil.php" class="flex flex-col items-center scale-125">
            <i class="fas fa-user text-brt-yellow"></i>
            <span class="text-[8px] font-black uppercase mt-1 text-brt-yellow">Perfil</span>
        </a>
    </nav>

</body>
</html>