<!doctype html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Orden de compra</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        @media print {
            .d-print-none { display: none !important; }
            body { background: #fff; }
        }
        body {
            background: #f5f7fb;
            color: #1f2b45;
            font-family: Poppins, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
        }
        .print-card {
            max-width: 980px;
            margin: 20px auto;
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(18, 27, 48, 0.08);
            padding: 24px;
        }
    </style>
</head>
<body>
<?php
$order = is_array($order ?? null) ? $order : [];
$details = is_array($details ?? null) ? $details : [];
$pdfFallback = (bool) ($pdf_fallback ?? false);
$orderNumber = (string) ($order['numero'] ?? ('OC-' . (string) ($order['id'] ?? '')));
?>

<div class="print-card">
    <div class="d-flex justify-content-between align-items-center mb-4 d-print-none">
        <div>
            <h1 class="h4 mb-0">Orden de compra <?= e($orderNumber) ?></h1>
            <small class="text-muted">Veltrix POS</small>
        </div>
        <div class="d-flex gap-2">
            <button class="btn btn-primary" type="button" onclick="window.print()">Imprimir</button>
            <?php if ($pdfFallback): ?>
                <a class="btn btn-success" href="#" onclick="window.print(); return false;">Guardar como PDF</a>
            <?php endif; ?>
            <a class="btn btn-light" href="<?= e(url('compras')) ?>">Volver</a>
        </div>
    </div>

    <?php if ($pdfFallback): ?>
        <div class="alert alert-warning d-print-none">
            No hay motor PDF instalado en el servidor. Usa "Guardar como PDF" desde la impresión del navegador.
        </div>
    <?php endif; ?>

    <div class="mb-4">
        <div class="row g-2">
            <div class="col-md-6">
                <h2 class="h6 mb-2">Proveedor</h2>
                <div><strong><?= e((string) ($order['proveedor'] ?? '-')) ?></strong></div>
                <div>Sucursal: <?= e((string) ($order['sucursal'] ?? 'Sin sucursal')) ?></div>
            </div>
            <div class="col-md-6 text-md-end">
                <h2 class="h6 mb-2">Documento</h2>
                <div><strong>OC: <?= e($orderNumber) ?></strong></div>
                <div>Fecha: <?= e((string) ($order['fecha'] ?? '')) ?></div>
                <div>Estado: <?= e((string) ($order['estado'] ?? 'abierta')) ?></div>
            </div>
        </div>
    </div>

    <div class="mb-4">
        <h2 class="h6 mb-2">Detalle</h2>
        <table class="table table-bordered table-sm">
            <thead>
            <tr>
                <th>Producto</th>
                <th class="text-end">Cantidad</th>
                <th class="text-end">Costo unitario</th>
                <th class="text-end">Impuesto</th>
                <th class="text-end">Total</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($details as $line): ?>
                <tr>
                    <td>
                        <?= e((string) ($line['producto'] ?? '')) ?>
                        <?php if (!empty($line['variante_nombre'])): ?>
                            <small class="d-block text-muted">Variante: <?= e((string) $line['variante_nombre']) ?></small>
                        <?php endif; ?>
                    </td>
                    <td class="text-end"><?= e((string) ((int) round((float) ($line['cantidad'] ?? 0)))) ?></td>
                    <td class="text-end"><?= e(money((float) ($line['costo_unitario'] ?? 0))) ?></td>
                    <td class="text-end"><?= e(money((float) ($line['impuesto'] ?? 0))) ?></td>
                    <td class="text-end"><?= e(money((float) ($line['total_linea'] ?? 0))) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if ($details === []): ?>
                <tr>
                    <td colspan="5" class="text-center text-muted">Sin líneas registradas.</td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>

    <div class="row g-2 justify-content-end">
        <div class="col-md-4">
            <table class="table table-bordered table-sm">
                <tbody>
                <tr>
                    <th>Subtotal</th>
                    <td class="text-end"><?= e(money((float) ($order['subtotal'] ?? 0))) ?></td>
                </tr>
                <tr>
                    <th>Impuestos</th>
                    <td class="text-end"><?= e(money((float) ($order['impuestos'] ?? 0))) ?></td>
                </tr>
                <tr>
                    <th>Total</th>
                    <td class="text-end"><strong><?= e(money((float) ($order['total'] ?? 0))) ?></strong></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
<?php /**PATH /Applications/XAMPP/xamppfiles/htdocs/veltrix-v2/resources/views/modules/compras/order_print.blade.php ENDPATH**/ ?>