<?php
session_start();
$meu_cpf = $_SESSION['usuario_cpf'] ?? '';
$pedidos = json_decode(file_get_contents('db_pedidos.json'), true) ?: [];
$corridas_disponiveis = array_filter($pedidos, function($p) { return $p['status'] == 'pendente'; }); // Para o alerta no radar

$minhas_entregas = array_filter($pedidos, function($p) use ($meu_cpf) {
    return $p['entregador_id'] == $meu_cpf && in_array($p['status'], ['finalizado', 'concluído']);
});
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body class="bg-gray-50">
    <div class="p-6">
        <h1 class="font-black italic uppercase text-xl mb-6">Meu Histórico</h1>
        <?php if(empty($minhas_entregas)): ?>
            <div class="text-center py-20 opacity-20"><i class="fas fa-box-open text-5xl mb-4"></i><p class="font-black uppercase text-xs">Vazio</p></div>
        <?php else: ?>
            <?php foreach(array_reverse($minhas_entregas) as $entrega): ?>
                <div class="bg-white p-5 rounded-[2rem] shadow-sm border border-gray-100 mb-4 flex items-center justify-between">
                    <div class="flex items-center gap-3">
                        <div class="w-10 h-10 bg-black text-[#FFD100] rounded-full flex items-center justify-center"><i class="fas fa-check text-xs"></i></div>
                        <div><p class="text-[11px] font-black uppercase italic leading-none"><?php echo $entrega['destino'] ?? 'Entrega'; ?></p><p class="text-[9px] text-gray-400 font-bold mt-1"><?php echo $entrega['data'] ?? date('d/m/Y'); ?></p></div>
                    </div>
                    <p class="text-xs font-black italic">R$ <?php echo number_format($entrega['valor'] ?? 0, 2, ',', '.'); ?></p>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <nav class="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-100 px-8 py-5 z-[100] rounded-t-[2.5rem] shadow-[0_-10px_40px_rgba(0,0,0,0.08)]">
        <div class="max-w-md mx-auto flex justify-between items-center text-gray-300">
            <a href="admin.php" class="flex flex-col items-center transition-all <?php echo (basename($_SERVER['PHP_SELF']) == 'admin.php') ? 'text-black' : ''; ?>">
                <div class="relative">
                    <i class="fas fa-satellite-dish text-xl"></i>
                    <?php if (count($corridas_disponiveis) > 0): ?><span class="absolute -top-1 -right-1 w-2 h-2 bg-red-500 rounded-full animate-ping"></span><?php endif; ?>
                </div>
                <span class="text-[8px] font-black uppercase mt-1 italic">Radar</span>
            </a>
            <a href="carteira_entregador.php" class="flex flex-col items-center transition-all <?php echo (basename($_SERVER['PHP_SELF']) == 'carteira_entregador.php') ? 'text-black' : ''; ?>">
                <i class="fas fa-wallet text-xl"></i>
                <span class="text-[8px] font-black uppercase mt-1 italic">Ganhos</span>
            </a>
            <a href="historico_entregas.php" class="flex flex-col items-center transition-all <?php echo (basename($_SERVER['PHP_SELF']) == 'historico_entregas.php') ? 'text-black' : ''; ?>">
                <i class="fas fa-list-ul text-xl"></i>
                <span class="text-[8px] font-black uppercase mt-1 italic">Corridas</span>
            </a>
            <a href="perfil_entregador.php" class="flex flex-col items-center transition-all <?php echo (basename($_SERVER['PHP_SELF']) == 'perfil_entregador.php') ? 'text-black' : ''; ?>">
                <div class="w-6 h-6 rounded-full bg-gray-100 border border-gray-200 flex items-center justify-center overflow-hidden">
                    <i class="fas fa-user text-xs"></i>
                </div>
                <span class="text-[8px] font-black uppercase mt-1 italic">Perfil</span>
            </a>
        </div>
    </nav>
    <div class="h-24"></div>
</body>
</html>