<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Reporte <?= date('d/m/Y') ?></title>
    <style>
        body { font-family: Arial, sans-serif; font-size: 12px; margin: 20px; color: #000; }
        h2 { text-align: center; margin-bottom: 5px; }
        .filtros { text-align: center; font-size: 11px; color: #555; margin-bottom: 15px; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        th, td { border: 1px solid #ccc; padding: 5px 8px; text-align: left; font-size: 11px; }
        th { background: #333; color: #fff; }
        .right { text-align: right; }
        .resumen { display: flex; gap: 15px; margin-bottom: 15px; }
        .resumen div { flex: 1; border: 1px solid #ddd; border-radius: 6px; padding: 10px; text-align: center; }
        .resumen .label { font-size: 11px; color: #666; }
        .resumen .valor { font-size: 18px; font-weight: bold; margin-top: 3px; }
        .total-row td { font-weight: bold; background: #f0f0f0; }
    </style>
</head>
<body>
    <h2>Reporte de Ventas</h2>
    <div class="filtros">
        Fecha Inicio: <?= date('d/m/Y', strtotime($startDate)) ?> |
        Fecha Fin: <?= date('d/m/Y', strtotime($endDate)) ?>
        <?php if (!empty($categoryId)): ?>
            <?php foreach ($categories as $cat): if ($cat['id_categoria'] == $categoryId): ?> | Categoria: <?= esc($cat['nombre']) ?><?php endif; endforeach; ?>
        <?php endif; ?>
        <?php if (!empty($productId)): ?>
            <?php foreach ($products as $prod): if ($prod['id_producto'] == $productId): ?> | Producto: <?= esc($prod['nombre']) ?><?php endif; endforeach; ?>
        <?php endif; ?>
    </div>

    <div class="resumen">
        <div><div class="label">Total Ingresos</div><div class="valor" style="color:#059669;">S/ <?= number_format($totalSales, 2) ?></div></div>
        <div><div class="label">Total IGV (18%)</div><div class="valor" style="color:#d97706;">S/ <?= number_format($totalSales * 0.18 / 1.18, 2) ?></div></div>
        <div><div class="label">Total Ganancias</div><div class="valor" style="color:#0891b2;">S/ <?= number_format($totalProfits, 2) ?></div></div>
    </div>

    <table>
        <thead>
            <tr>
                <th>Fecha</th>
                <th>Venta #</th>
                <th>Producto</th>
                <th>Codigo</th>
                <th>Categoria</th>
                <th>Cant.</th>
                <th class="right">P. Unit.</th>
                <th class="right">IGV</th>
                <th class="right">Subtotal</th>
                <th>Vendedor</th>
            </tr>
        </thead>
        <tbody>
            <?php $totalIgv = 0; ?>
            <?php foreach ($sales as $row): ?>
            <?php $igv = $row['subtotal'] * 0.18 / 1.18; $totalIgv += $igv; ?>
            <tr>
                <td><?= date('d/m/Y', strtotime($row['fecha'])) ?></td>
                <td>#<?= $row['id_venta'] ?></td>
                <td><?= esc($row['producto_nombre']) ?></td>
                <td><?= esc($row['codigo_unico']) ?></td>
                <td><?= esc($row['categoria_nombre']) ?></td>
                <td><?= $row['cantidad'] ?></td>
                <td class="right">S/ <?= number_format($row['precio_unitario'], 2) ?></td>
                <td class="right">S/ <?= number_format($igv, 2) ?></td>
                <td class="right">S/ <?= number_format($row['subtotal'], 2) ?></td>
                <td><?= $row['username'] ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr class="total-row">
                <td colspan="7" class="right">Totales</td>
                <td class="right">S/ <?= number_format($totalIgv, 2) ?></td>
                <td class="right">S/ <?= number_format($totalSales, 2) ?></td>
                <td></td>
            </tr>
        </tfoot>
    </table>

    <table>
        <thead>
            <tr>
                <th>Venta #</th>
                <th>Fecha</th>
                <th class="right">Ganancia</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($profits as $row): ?>
            <tr>
                <td>#<?= $row['id_venta'] ?></td>
                <td><?= date('d/m/Y', strtotime($row['fecha'])) ?></td>
                <td class="right">S/ <?= number_format($row['ganancia'], 2) ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr class="total-row">
                <td colspan="2" class="right">Total Ganancias</td>
                <td class="right">S/ <?= number_format($totalProfits, 2) ?></td>
            </tr>
        </tfoot>
    </table>

    <div style="text-align:center;font-size:10px;color:#999;margin-top:20px;">Generado el <?= date('d/m/Y H:i') ?> - VelMox</div>
</body>
</html>