<?php
include 'conn.php';

$logDir = __DIR__ . '/logs';
$logs = [];
foreach (glob("$logDir/upload_log_*.log") as $file) {
    $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
        if (preg_match('/\[(.*?)\] IP: (.*?) \| Uploaded: (\d+) \| New: (\d+) \| Duplicates: (\d+)/', $line, $m)) {
            $logs[] = [
                'datetime' => $m[1],
                'ip' => $m[2],
                'uploaded' => (int)$m[3],
                'new' => (int)$m[4],
                'duplicates' => (int)$m[5],
            ];
        }
    }
}
usort($logs, fn($a, $b) => strtotime($b['datetime']) <=> strtotime($a['datetime']));
?>
<!DOCTYPE html>
<html>

<head>
    <title>📊 Upload Logs</title>
    <style>
        body {
            font-family: "Segoe UI", Arial, sans-serif;
            margin: 40px;
            background: #f4f6f9;
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        table {
            width: 90%;
            margin: auto;
            border-collapse: collapse;
            background: #fff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            overflow: hidden;
        }

        th,
        td {
            padding: 12px 16px;
            text-align: center;
            border-bottom: 1px solid #eee;
        }

        th {
            background: #007bff;
            color: white;
            text-transform: uppercase;
            font-size: 13px;
        }

        tr:hover {
            background-color: #f1f7ff;
        }

        .summary {
            display: flex;
            justify-content: space-around;
            width: 90%;
            margin: 20px auto;
            font-weight: bold;
            color: #333;
        }

        .box {
            padding: 12px 20px;
            border-radius: 6px;
        }

        .uploaded {
            background: #e8f0ff;
        }

        .new {
            background: #e6ffea;
        }

        .dup {
            background: #fff3e6;
        }

        .back {
            text-align: center;
            margin: 15px;
        }

        .back a {
            color: #007bff;
            text-decoration: none;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <h2>📊 Upload Logs Summary <small style="float: right;"><a href="<?= $path ?>upload.php">Upload New File</a></small>
    </h2>
    <?php
    $totalUploads = count($logs);
    $sumUploaded = array_sum(array_column($logs, 'uploaded'));
    $sumNew = array_sum(array_column($logs, 'new'));
    $sumDuplicates = array_sum(array_column($logs, 'duplicates'));
    ?>
    <div class="summary">
        <div class="box uploaded">📁 Total Uploads: <?= $totalUploads ?></div>
        <div class="box new">🆕 Total New Numbers: <?= $sumNew ?></div>
        <div class="box dup">♻️ Total Duplicates: <?= $sumDuplicates ?></div>
    </div>

    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Date & Time</th>
                <th>IP Address</th>
                <th>Total Uploaded</th>
                <th>New Numbers</th>
                <th>Duplicates</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($logs)): ?>
                <tr>
                    <td colspan="6">No logs found.</td>
                </tr>
            <?php else: ?>
                <?php foreach ($logs as $i => $log): ?>
                    <tr>
                        <td><?= $i + 1 ?></td>
                        <td><?= htmlspecialchars($log['datetime']) ?></td>
                        <td><?= htmlspecialchars($log['ip']) ?></td>
                        <td><?= $log['uploaded'] ?></td>
                        <td style="color:green;"><?= $log['new'] ?></td>
                        <td style="color:#d98b00;"><?= $log['duplicates'] ?></td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>


</body>

</html>