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

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

$db = getDB();
$fecha = $_GET['fecha'] ?? date('Y-m-d');

// Órdenes del día
$stmt = $db->prepare("
    SELECT o.*, p.nombre, p.apellidos,
           (SELECT COUNT(*) FROM orden_detalle WHERE orden_id = o.id) as num_examenes
    FROM ordenes o
    JOIN pacientes p ON o.paciente_id = p.id
    WHERE DATE(o.fecha_orden) = ?
    ORDER BY o.fecha_orden
");
$stmt->execute([$fecha]);
$ordenes = $stmt->fetchAll();

// Pagos del día
$stmt = $db->prepare("
    SELECT p.*, o.codigo as orden_codigo
    FROM pagos p
    JOIN ordenes o ON p.orden_id = o.id
    WHERE DATE(p.fecha_pago) = ?
    ORDER BY p.fecha_pago
");
$stmt->execute([$fecha]);
$pagos = $stmt->fetchAll();

// Totales
$totalOrdenes = count($ordenes);
$totalFacturado = array_sum(array_column($ordenes, 'total'));
$totalCobrado = array_sum(array_column($pagos, 'monto'));

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-calendar-day"></i> Reporte Diario</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>

<!-- Selector de fecha -->
<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</label>
                <input type="date" name="fecha" class="form-control" value="<?= $fecha ?>">
            </div>
            <div class="col-md-3 d-flex align-items-end">
                <button type="submit" class="btn btn-primary">
                    <i class="bi bi-search"></i> Ver Reporte
                </button>
            </div>
        </form>
    </div>
</div>

<h4 class="text-center mb-4">Fecha: <?= formatDate($fecha) ?></h4>

<!-- Resumen -->
<div class="row mb-4">
    <div class="col-md-4">
        <div class="card bg-primary text-white">
            <div class="card-body text-center">
                <h6>Órdenes Generadas</h6>
                <h2><?= $totalOrdenes ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card bg-info text-white">
            <div class="card-body text-center">
                <h6>Total Facturado</h6>
                <h2><?= formatMoney($totalFacturado) ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card bg-success text-white">
            <div class="card-body text-center">
                <h6>Total Cobrado</h6>
                <h2><?= formatMoney($totalCobrado) ?></h2>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <!-- Órdenes -->
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="bi bi-clipboard2-pulse"></i> Órdenes del Día</h5>
            </div>
            <div class="card-body">
                <table class="table table-sm">
                    <thead>
                        <tr>
                            <th>Código</th>
                            <th>Paciente</th>
                            <th>Exámenes</th>
                            <th class="text-end">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($ordenes as $o): ?>
                        <tr>
                            <td><?= $o['codigo'] ?></td>
                            <td><?= $o['nombre'] . ' ' . $o['apellidos'] ?></td>
                            <td><?= $o['num_examenes'] ?></td>
                            <td class="text-end"><?= formatMoney($o['total']) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagos -->
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="bi bi-cash-stack"></i> Pagos del Día</h5>
            </div>
            <div class="card-body">
                <table class="table table-sm">
                    <thead>
                        <tr>
                            <th>Hora</th>
                            <th>Orden</th>
                            <th>Método</th>
                            <th class="text-end">Monto</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($pagos as $p): ?>
                        <tr>
                            <td><?= date('H:i', strtotime($p['fecha_pago'])) ?></td>
                            <td><?= $p['orden_codigo'] ?></td>
                            <td><?= ucfirst($p['metodo_pago']) ?></td>
                            <td class="text-end"><?= formatMoney($p['monto']) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                    <tfoot class="table-success">
                        <tr>
                            <th colspan="3">TOTAL</th>
                            <th class="text-end"><?= formatMoney($totalCobrado) ?></th>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </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'; ?>
