<?php
$pageTitle = 'Órdenes Pendientes de Pago';
require_once __DIR__ . '/../../config/config.php';
requireLogin();

$db = getDB();

// Órdenes con saldo pendiente
$stmt = $db->query("
    SELECT o.*, p.nombre, p.apellidos, p.documento_numero,
           (SELECT COALESCE(SUM(monto), 0) FROM pagos WHERE orden_id = o.id) as pagado
    FROM ordenes o 
    JOIN pacientes p ON o.paciente_id = p.id 
    WHERE o.estado != 'cancelada'
    HAVING o.total > pagado
    ORDER BY o.fecha_orden DESC
");
$ordenes = $stmt->fetchAll();

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-exclamation-circle"></i> Órdenes Pendientes de Pago</h2>
    <a href="list.php" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left"></i> Volver a Caja
    </a>
</div>

<div class="card">
    <div class="card-body">
        <table class="table table-hover datatable">
            <thead>
                <tr>
                    <th>Código</th>
                    <th>Paciente</th>
                    <th>Fecha</th>
                    <th>Total</th>
                    <th>Pagado</th>
                    <th>Pendiente</th>
                    <th>Estado</th>
                    <th>Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($ordenes as $orden): 
                    $pendiente = $orden['total'] - $orden['pagado'];
                ?>
                <tr>
                    <td><strong><?= $orden['codigo'] ?></strong></td>
                    <td>
                        <?= $orden['nombre'] . ' ' . $orden['apellidos'] ?>
                        <br><small class="text-muted"><?= $orden['documento_numero'] ?></small>
                    </td>
                    <td><?= formatDateTime($orden['fecha_orden']) ?></td>
                    <td><?= formatMoney($orden['total']) ?></td>
                    <td class="text-success"><?= formatMoney($orden['pagado']) ?></td>
                    <td class="text-danger"><strong><?= formatMoney($pendiente) ?></strong></td>
                    <td>
                        <?php
                        $estadoClass = [
                            'pendiente' => 'bg-warning',
                            'en_proceso' => 'bg-info',
                            'completada' => 'bg-success',
                            'entregada' => 'bg-primary'
                        ];
                        ?>
                        <span class="badge <?= $estadoClass[$orden['estado']] ?? 'bg-secondary' ?>">
                            <?= ucfirst(str_replace('_', ' ', $orden['estado'])) ?>
                        </span>
                    </td>
                    <td>
                        <a href="pago.php?orden_id=<?= $orden['id'] ?>" class="btn btn-sm btn-success">
                            <i class="bi bi-cash"></i> Cobrar
                        </a>
                        <a href="<?= SITE_URL ?>/modules/ordenes/view.php?id=<?= $orden['id'] ?>" 
                           class="btn btn-sm btn-outline-info">
                            <i class="bi bi-eye"></i>
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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