<?php
include_once '../../controllers/CajaController.php';
$cajaController = new CajaController();
$egresos = [];
$total_egresos = 0;

if (isset($_GET['fecha_inicio']) && isset($_GET['fecha_fin'])) {
    $fecha_inicio = $_GET['fecha_inicio'];
    $fecha_fin = $_GET['fecha_fin'];
    $egresos = $cajaController->listarEgresosPorFecha($fecha_inicio, $fecha_fin);

    foreach ($egresos as $egreso) {
        $total_egresos += $egreso['monto'];
    }
}
?>

<!DOCTYPE html>
<html lang="es">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gestión de Egresos</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
</head>

<body class="bg-gray-100">
    <div class="container mx-auto p-6">
        
        <div class="flex justify-between items-center mb-8">
            <h2 class="text-3xl font-bold text-gray-800">Gestión de Egresos</h2>
            <img src="../../public/images/logo.png" alt="Logo de la Institución" class="w-24">
        </div>

        
        <div class="flex justify-end space-x-4 mb-6">
            <a href="agregar_compra.php" class="bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700 transition duration-300">
                <i class="fas fa-cart-plus mr-2"></i>Agregar Compras
            </a>
            <a href="dashboard_caja.php" class="bg-gray-600 text-white py-2 px-4 rounded-lg hover:bg-gray-700 transition duration-300">
                <i class="fas fa-arrow-left mr-2"></i>Volver a Gestión de Caja
            </a>
        </div>

        
        <form action="egresos.php" method="get" class="bg-white p-6 rounded-lg shadow-md mb-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div>
                    <label for="fecha_inicio" class="block text-sm font-medium text-gray-700">Fecha Inicio:</label>
                    <input type="date" id="fecha_inicio" name="fecha_inicio" required
                        class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>
                <div>
                    <label for="fecha_fin" class="block text-sm font-medium text-gray-700">Fecha Fin:</label>
                    <input type="date" id="fecha_fin" name="fecha_fin" required
                        class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                </div>
            </div>
            <div class="mt-6 text-center">
                <button type="submit" class="bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700 transition duration-300">
                    <i class="fas fa-filter mr-2"></i>Filtrar
                </button>
            </div>
        </form>

        
        <?php if (!empty($egresos)): ?>
            <div class="bg-white p-6 rounded-lg shadow-md mb-6">
                <h3 class="text-xl font-semibold text-gray-800">Total de Egresos: <?php echo number_format($total_egresos, 2); ?> BOB</h3>
            </div>
        <?php endif; ?>

        
        <?php if (!empty($egresos)): ?>
            <div class="bg-white rounded-lg shadow-md overflow-x-auto">
                <table class="min-w-full">
                    <thead class="bg-blue-600 text-white">
                        <tr>
                            <th class="px-6 py-3 text-left">#</th>
                            <th class="px-6 py-3 text-left">Proveedor</th>
                            <th class="px-6 py-3 text-left">Número de Recibo</th>
                            <th class="px-6 py-3 text-left">Monto</th>
                            <th class="px-6 py-3 text-left">Detalle</th>
                            <th class="px-6 py-3 text-left">Fecha de Registro</th>
                            <th class="px-6 py-3 text-left">Acciones</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        <?php foreach ($egresos as $index => $egreso): ?>
                            <tr class="hover:bg-gray-50 transition duration-200">
                                <td class="px-6 py-4"><?php echo $index + 1; ?></td>
                                <td class="px-6 py-4"><?php echo $egreso['proveedor']; ?></td>
                                <td class="px-6 py-4"><?php echo $egreso['nro_recibo']; ?></td>
                                <td class="px-6 py-4"><?php echo $egreso['monto']; ?></td>
                                <td class="px-6 py-4"><?php echo $egreso['detalle']; ?></td>
                                <td class="px-6 py-4"><?php echo $egreso['fecha']; ?></td>
                                <td class="px-6 py-4">
                                    <a href="editar_egreso.php?id=<?php echo $egreso['idegreso']; ?>" class="text-blue-600 hover:text-blue-800 mr-4">
                                        <i class="fas fa-edit mr-1"></i>Editar
                                    </a>
                                    <a href="imprimir_egreso.php?id=<?php echo $egreso['idegreso']; ?>" class="text-green-600 hover:text-green-800 mr-4" target="_blank">
                                        <i class="fas fa-print mr-1"></i>Imprimir
                                    </a>
                                    <a href="egresos.php?eliminar=<?php echo $egreso['idegreso']; ?>" class="text-red-600 hover:text-red-800" onclick="return confirm('¿Estás seguro de que deseas eliminar este egreso?');">
                                        <i class="fas fa-trash mr-1"></i>Eliminar
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php else: ?>
            <p class="text-center text-gray-600 mt-6">No se encontraron egresos en el rango de fechas seleccionado.</p>
        <?php endif; ?>
    </div>
</body>

</html>