<?php
$db = new mysqli('localhost', 'k123263_price_us', '2024Brs!2024', 'k123263_price');

if ($db->connect_error) {
    die("Ошибка подключения: " . $db->connect_error);
}

$query = "
SELECT city AS Ciudad, description AS Description, SUM(quantity) AS `Cant(t)`, price_per_unit AS `Precio(t)`, SUM(total) AS `Precio total`
FROM selected_products
GROUP BY city, description
ORDER BY city, description;
";

$result = $db->query($query);
echo "<style>
    .report-table {
        width: 100%;
        border-collapse: collapse;
    }

    .report-table, .report-table th, .report-table td {
        border: 1px solid #ddd;
    }

    .report-table th, .report-table td {
        padding: 10px;
        text-align: left;
    }

    .report-table th {
        background-color: #4CAF50;
        color: white;
    }

    .city-header {
        margin-top: 20px;
        font-size: 20px;
        color: #333;
    }

    .separator {
        margin-top: 10px;
        border: none;
        border-bottom: 1px solid #eee;
    }
</style>";
echo "<div class='report-container'>";

$currentCity = '';
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if ($currentCity !== $row["Ciudad"]) {
            if ($currentCity !== '') echo "</table>"; // Закрываем предыдущую таблицу
            echo "<div class='city-header'>" . $row["Ciudad"] . "</div>";
            echo "<table class='report-table'>";
            echo "<tr><th>Description</th><th>Cant(t)</th><th>Precio(t)</th><th>Precio total</th></tr>";
            $currentCity = $row["Ciudad"];
        }
        echo "<tr><td>" . $row["Description"] . "</td><td>" . $row["Cant(t)"] . "</td><td>" . $row["Precio(t)"] . "</td><td>" . $row["Precio total"] . "</td></tr>";
    }
    echo "</table>"; // Закрываем последнюю таблицу
} else {
    echo "Результатов не найдено";
}

echo "</div>"; // Закрываем контейнер отчета


$db->close();
