<?php
$pageTitle = 'Reporte de Ventas';
require_once __DIR__ . '/../../config/config.php';
requireLogin();

if ($_SESSION['rol'] != 'admin') {
    header('Location: ' . SITE_URL);
    exit;
}

$db = getDB();

// Filtros
$fecha_desde = $_GET['fecha_desde'] ?? date('Y-m-01');
$fecha_hasta = $_GET['fecha_hasta'] ?? date('Y-m-d');

// Obtener datos
$stmt = $db->prepare("
    SELECT 
        DATE(p.fecha_pago) as fecha,
        COUNT(DISTINCT p.orden_id) as num_ordenes,
        SUM(p.monto) as total,
        SUM(CASE WHEN p.metodo_pago = 'efectivo' THEN p.monto ELSE 0 END) as efectivo,
        SUM(CASE WHEN p.metodo_pago = 'tarjeta' THEN p.monto ELSE 0 END) as tarjeta,
        SUM(CASE WHEN p.metodo_pago = 'transferencia' THEN p.monto ELSE 0 END) as transferencia
    FROM pagos p
    WHERE DATE(p.fecha_pago) BETWEEN ? AND ?
    GROUP BY DATE(p.fecha_pago)
    ORDER BY fecha DESC
");
$stmt->execute([$fecha_desde, $fecha_hasta]);
$ventas = $stmt->fetchAll();

// Totales
$totales = [
    'ordenes' => 0,
    'total' => 0,
    'efectivo' => 0,
    'tarjeta' => 0,
    'transferencia' => 0
];

foreach ($ventas as $v) {
    $totales['ordenes'] += $v['num_ordenes'];
    $totales['total'] += $v['total'];
    $totales['efectivo'] += $v['efectivo'];
    $totales['tarjeta'] += $v['tarjeta'];
    $totales['transferencia'] += $v['transferencia'];
}

require_once __DIR__ . '/../../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mt-4 mb-4">
    <h2><i class="bi bi-cash"></i> Reporte de Ventas</h2>
    <div>
        <button onclick="window.print()" class="btn btn-outline-secondary">
            <i class="bi bi-printer"></i> Imprimir
        </button>
        <a href="index.php" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left"></i> Volver
        </a>
    </div>
</div>

<!-- Filtros -->
<div class="card mb-4 no-print">
    <div class="card-body">
        <form method="GET" class="row g-3">
            <div class="col-md-3">
                <label class="form-label">Fecha Desde</label>
                <input type="date" name="fecha_desde" class="form-control" value="<?= $fecha_desde ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Fecha Hasta</label>
                <input type="date" name="fecha_hasta" class="form-control" value="<?= $fecha_hasta ?>">
            </div>
            <div class="col-md-3 d-flex align-items-end">
                <button type="submit" class="btn btn-primary">
                    <i class="bi bi-search"></i> Generar Reporte
                </button>
            </div>
        </form>
    </div>
</div>

<!-- Resumen -->
<div class="row mb-4">
    <div class="col-md-3">
        <div class="card bg-primary text-white">
            <div class="card-body text-center">
                <h6>Total Ventas</h6>
                <h3><?= formatMoney($totales['total']) ?></h3>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card">
            <div class="card-body text-center">
                <h6 class="text-muted">Efectivo</h6>
                <h4><?= formatMoney($totales['efectivo']) ?></h4>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card">
            <div class="card-body text-center">
                <h6 class="text-muted">Tarjeta</h6>
                <h4><?= formatMoney($totales['tarjeta']) ?></h4>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card">
            <div class="card-body text-center">
                <h6 class="text-muted">Transferencia</h6>
                <h4><?= formatMoney($totales['transferencia']) ?></h4>
            </div>
        </div>
    </div>
</div>

<!-- Tabla de ventas -->
<div class="card">
    <div class="card-header">
        <h5 class="mb-0">Detalle por Día</h5>
    </div>
    <div class="card-body">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Fecha</th>
                    <th class="text-center">Órdenes</th>
                    <th class="text-end">Efectivo</th>
                    <th class="text-end">Tarjeta</th>
                    <th class="text-end">Transferencia</th>
                    <th class="text-end">Total</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($ventas as $v): ?>
                <tr>
                    <td><?= formatDate($v['fecha']) ?></td>
                    <td class="text-center"><?= $v['num_ordenes'] ?></td>
                    <td class="text-end"><?= formatMoney($v['efectivo']) ?></td>
                    <td class="text-end"><?= formatMoney($v['tarjeta']) ?></td>
                    <td class="text-end"><?= formatMoney($v['transferencia']) ?></td>
                    <td class="text-end"><strong><?= formatMoney($v['total']) ?></strong></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot class="table-primary">
                <tr>
                    <th>TOTALES</th>
                    <th class="text-center"><?= $totales['ordenes'] ?></th>
                    <th class="text-end"><?= formatMoney($totales['efectivo']) ?></th>
                    <th class="text-end"><?= formatMoney($totales['tarjeta']) ?></th>
                    <th class="text-end"><?= formatMoney($totales['transferencia']) ?></th>
                    <th class="text-end"><?= formatMoney($totales['total']) ?></th>
                </tr>
            </tfoot>
        </table>
    </div>
</div>

<style>
@media print {
    .no-print { display: none !important; }
    .sidebar { display: none !important; }
    .main-content { margin: 0 !important; padding: 0 !important; }
}
</style>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>
