<?php include_once "encabezado1.php"; ?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Gestión de Cuentas por Cobrar y Pagar</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
        /* Estilo general de la página */
        body {
            background-color: #f7f9fc;
            font-family: 'Roboto', sans-serif;
            color: #495057;
        }

        /* Estilo del contenedor principal */
        .container {
            margin-top: 50px;
            background-color: #ffffff;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
            transition: transform 0.3s ease;
        }
        .container:hover {
            transform: scale(1.01);
        }

        /* Encabezado principal */
        .header {
            font-size: 30px;
            font-weight: 700;
            color: #007bff;
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 20px;
        }

        /* Botones de acción */
        .btn-custom, .btn-success {
            font-weight: 500;
            border-radius: 5px;
            transition: background-color 0.3s ease, transform 0.3s ease;
        }
        .btn-custom {
            background-color: #007bff;
            color: #fff;
        }
        .btn-custom:hover {
            background-color: #0056b3;
            transform: translateY(-3px);
        }
        .btn-success {
            background-color: #28a745;
            color: #fff;
        }
        .btn-success:hover {
            background-color: #218838;
            transform: translateY(-3px);
        }

        /* Estilos de la tabla */
        .table {
            margin-top: 15px;
        }
        .table-hover tbody tr:hover {
            background-color: #eaf3fc;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .table thead th {
            background-color: #007bff;
            color: #fff;
            font-weight: 500;
            transition: all 0.2s;
        }

        /* Iconos y estilo de celdas */
        .badge {
            font-size: 0.9rem;
            padding: 0.4em 0.7em;
        }
        .badge-success {
            background-color: #28a745;
        }
        .badge-warning {
            background-color: #ffc107;
            color: #212529;
        }
        .action-btns .btn {
            margin-right: 5px;
        }

        /* Animación en los botones */
        .btn-sm {
            transition: transform 0.2s ease-in-out;
        }
        .btn-sm:hover {
            transform: scale(1.1);
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <i class="fas fa-file-invoice-dollar"></i> Gestión de Cuentas
        </div>
        
        <!-- Botones de acciones -->
        <div class="d-flex justify-content-between mb-4">
            <a href="registrar_transaccion.php" class="btn btn-custom"><i class="fas fa-plus"></i> Registrar Transacción</a>
            <a href="reporte.php" class="btn btn-success"><i class="fas fa-chart-line"></i> Generar Reporte</a>
        </div>
        
        <!-- Tabla de Transacciones -->
        <h4 class="text-secondary mb-3"><i class="fas fa-list-alt"></i> Cuentas por Cobrar y Pagar</h4>
        <table class="table table-hover table-bordered">
            <thead>
                <tr>
                    <th><i class="fas fa-hashtag"></i> ID</th>
                    <th><i class="fas fa-exchange-alt"></i> Tipo</th>
                    <th><i class="fas fa-user"></i> Cliente/Proveedor</th>
                    <th><i class="fas fa-calendar-alt"></i> Fecha</th>
                    <th><i class="fas fa-dollar-sign"></i> Monto</th>
                    <th><i class="fas fa-bell"></i> Estado</th>
                    <th><i class="fas fa-cogs"></i> Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php
                include 'conexion1.php';
                $conexion = new mysqli($servidor, $nombre, $clave, $base);
                $result = $conexion->query("SELECT t.id, t.tipo, t.fecha, t.monto, t.estado, 
                                            CASE WHEN t.tipo = 'Cuenta por Cobrar' THEN c.nombre ELSE p.nombre END AS nombre 
                                            FROM transacciones t 
                                            LEFT JOIN clientespa c ON t.cliente_id = c.id 
                                            LEFT JOIN proveedorespa p ON t.proveedor_id = p.id");

                while ($transaccion = $result->fetch_assoc()) {
                    echo "<tr>
                        <td>{$transaccion['id']}</td>
                        <td>{$transaccion['tipo']}</td>
                        <td>{$transaccion['nombre']}</td>
                        <td>{$transaccion['fecha']}</td>
                        <td>$ {$transaccion['monto']}</td>
                        <td><span class='badge " . ($transaccion['estado'] == 'Pagado' ? 'badge-success' : 'badge-warning') . "'>{$transaccion['estado']}</span></td>
                        <td class='action-btns'>
                            <a href='editar_transaccion.php?id={$transaccion['id']}' class='btn btn-sm btn-info'><i class='fas fa-edit'></i></a>
                            <a href='eliminar_transaccion.php?id={$transaccion['id']}' class='btn btn-sm btn-danger'><i class='fas fa-trash'></i></a>
                        </td>
                    </tr>";
                }
                ?>
            </tbody>
        </table>
    </div>

    <!-- Scripts de Bootstrap y jQuery -->
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</body>
</html>