<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../auth/login.php");
    exit();
}

require_once __DIR__ . '/../../controllers/SaleController.php';
$saleController = new SaleController();
$salesBySeller = $saleController->getSalesBySeller();
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Reporte de Ventas por Vendedor</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h1 class="mt-3">Reporte de Ventas por Vendedor</h1>
        <table class="table">
            <thead>
                <tr>
                    <th>Vendedor</th>
                    <th>Nro. de Ventas</th>
                    <th>Total Vendido</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($salesBySeller as $sale): ?>
                    <tr>
                        <td><?php echo $sale['username']; ?></td>
                        <td><?php echo $sale['sale_count']; ?></td>
                        <td>Q<?php echo $sale['total_sales']; ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <a href="../../controllers/ReportController.php?action=generate_sales_report" class="btn btn-success" target="_blank">Exportar a PDF</a>
        <a href="dashboard.php" class="btn btn-primary">Volver al Dashboard</a>
    </div>
</body>
</html>