TransWar File Browser

Path: C:\

<?php
// Central-NF: historico.php
error_reporting(E_ALL);
ini_set('display_errors', 0);

$history_file = 'sent_remessas.json';
$history_data = [];

if (file_exists($history_file)) {
    $content = json_decode(file_get_contents($history_file), true);
    if (is_array($content)) {
        // Normaliza em caso do formato antigo
        foreach ($content as $key => $val) {
            if (is_string($val)) {
                $history_data[] = [
                    'remessa' => $val,
                    'nf_number' => 'N/A',
                    'sent_date' => 'Desconhecida'
                ];
            } else {
                $history_data[] = $val;
            }
        }
    }
}

// Ordena pelos mais recentes (assumindo que foram adicionados no final)
$history_data = array_reverse($history_data);

// Filtro via GET
$search_query = isset($_GET['q']) ? trim($_GET['q']) : '';
if (!empty($search_query)) {
    $filtered = [];
    foreach ($history_data as $item) {
        if (strpos((string)$item['remessa'], $search_query) !== false || 
            strpos((string)$item['nf_number'], $search_query) !== false) {
            $filtered[] = $item;
        }
    }
    $history_data = $filtered;
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Central-NF | Histórico de Envios</title>
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;800&display=swap" rel="stylesheet">
    <style>
        :root {
            --bg-color: #f1f5f9;
            --card-bg: #ffffff;
            --primary-accent: #27325a;
            --text-main: #1e293b;
            --text-muted: #64748b;
            --border-color: #e2e8f0;
            --success-color: #10b981;
        }

        * { box-sizing: border-box; margin: 0; padding: 0; }

        body {
            font-family: 'Inter', sans-serif;
            background-color: var(--bg-color);
            color: var(--text-main);
            min-height: 100vh;
            padding: 40px 20px;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            background: #fff;
            padding: 20px 30px;
            border-radius: 12px;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
        }

        .header h1 {
            font-size: 24px;
            color: var(--primary-accent);
        }

        .btn-back {
            padding: 10px 20px;
            background: #f1f5f9;
            color: var(--primary-accent);
            border: 1px solid var(--border-color);
            border-radius: 8px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.2s;
        }

        .btn-back:hover {
            background: #e2e8f0;
        }

        .search-box {
            background: #fff;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
            margin-bottom: 30px;
            display: flex;
            gap: 15px;
        }

        .search-box input {
            flex: 1;
            padding: 12px 16px;
            border: 2px solid var(--border-color);
            border-radius: 8px;
            font-size: 15px;
            font-family: inherit;
        }

        .search-box button {
            padding: 12px 24px;
            background: var(--primary-accent);
            color: white;
            border: none;
            border-radius: 8px;
            font-weight: 600;
            cursor: pointer;
        }

        table {
            width: 100%;
            background: #fff;
            border-radius: 12px;
            overflow: hidden;
            border-collapse: collapse;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
        }

        th, td {
            padding: 16px 20px;
            text-align: left;
            border-bottom: 1px solid var(--border-color);
        }

        th {
            background: #f8fafc;
            color: var(--text-muted);
            font-weight: 600;
            font-size: 13px;
            text-transform: uppercase;
        }

        td {
            font-size: 15px;
        }

        .status-badge {
            background: rgba(16, 185, 129, 0.1);
            color: #065f46;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            background: #fff;
            border-radius: 12px;
            color: var(--text-muted);
        }
    </style>
</head>
<body>

    <div class="container">
        <div class="header">
            <h1>Histórico de Transmissões</h1>
            <div style="display: flex; gap: 10px;">
                <a href="dashboard.php" class="btn-back">Painel T.V.</a>
                <a href="index.php" class="btn-back">Voltar Início</a>
            </div>
        </div>

        <div class="search-box">
            <form method="GET" action="" style="display: flex; flex: 1; gap: 15px;">
                <input type="text" name="q" placeholder="Buscar por número da Remessa ou NF..." value="<?php echo htmlspecialchars($search_query); ?>">
                <button type="submit">Pesquisar</button>
            </form>
            <?php if (!empty($search_query)): ?>
                <a href="historico.php" style="padding: 12px 24px; background: #e2e8f0; color: #475569; border-radius: 8px; text-decoration: none; font-weight: 600;">Limpar</a>
            <?php endif; ?>
        </div>

        <?php if (count($history_data) > 0): ?>
            <table>
                <thead>
                    <tr>
                        <th>Remessa</th>
                        <th>Nº NF</th>
                        <th>Status</th>
                        <th>Data do Envio</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($history_data as $row): ?>
                    <tr>
                        <td><strong><?php echo htmlspecialchars($row['remessa'] ?? '-'); ?></strong></td>
                        <td><?php echo htmlspecialchars($row['nf_number'] ?? '-'); ?></td>
                        <td><span class="status-badge">Concluído</span></td>
                        <td><?php echo htmlspecialchars($row['sent_date'] ?? '-'); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php else: ?>
            <div class="empty-state">
                <h2>Nenhum registro encontrado.</h2>
                <p>Nenhuma remessa foi transmitida com sucesso ainda ou os filtros não retornaram resultados.</p>
            </div>
        <?php endif; ?>

    </div>

</body>
</html>