<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Factura N° {{ venta.numeroFactura }}</title>
    <style>
        @page {
            margin: 30px;
        }
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
            color: #333333;
            margin: 0;
            padding: 0;
        }
        .watermark {
            position: fixed;
            top: 35%;
            left: 15%;
            width: 60%;
            transform: rotate(-35deg);
            font-size: 75px;
            font-weight: bold;
            color: rgba(220, 38, 38, 0.45);
            border: 8px solid rgba(220, 38, 38, 0.45);
            padding: 15px 20px;
            border-radius: 12px;
            text-align: center;
            z-index: 9999;
            text-transform: uppercase;
            letter-spacing: 6px;
        }
        .header-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        .header-table td {
            vertical-align: top;
        }
        .company-title {
            font-size: 22px;
            font-weight: bold;
            color: #1e293b;
            margin-bottom: 5px;
        }
        .company-subtitle {
            font-size: 11px;
            color: #64748b;
        }
        .invoice-box {
            border: 1px solid #cbd5e1;
            background-color: #f8fafc;
            border-radius: 6px;
            padding: 12px;
            text-align: right;
        }
        .invoice-box h2 {
            margin: 0 0 5px 0;
            font-size: 18px;
            color: #0f172a;
        }
        .invoice-box p {
            margin: 3px 0;
            font-size: 11px;
            color: #475569;
        }
        .client-card {
            border: 1px solid #e2e8f0;
            border-radius: 6px;
            padding: 12px;
            margin-bottom: 20px;
            background-color: #ffffff;
        }
        .client-card h3 {
            margin: 0 0 8px 0;
            font-size: 13px;
            color: #334155;
            border-bottom: 1px solid #f1f5f9;
            padding-bottom: 4px;
        }
        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        .items-table th {
            background-color: #1e293b;
            color: #ffffff;
            font-weight: bold;
            font-size: 11px;
            text-transform: uppercase;
            padding: 8px 10px;
            text-align: left;
            border: 1px solid #1e293b;
        }
        .items-table td {
            padding: 8px 10px;
            border: 1px solid #e2e8f0;
            font-size: 11px;
        }
        .items-table tr:nth-child(even) {
            background-color: #f8fafc;
        }
        .text-right {
            text-align: right;
        }
        .text-center {
            text-align: center;
        }
        .totals-table {
            width: 40%;
            margin-left: auto;
            border-collapse: collapse;
        }
        .totals-table td {
            padding: 6px 10px;
            font-size: 12px;
        }
        .totals-table .grand-total {
            font-weight: bold;
            font-size: 14px;
            background-color: #f1f5f9;
            border-top: 2px solid #0f172a;
        }
        .badge-status {
            display: inline-block;
            padding: 3px 8px;
            font-size: 10px;
            font-weight: bold;
            border-radius: 4px;
            color: #ffffff;
        }
        .bg-active { background-color: #10b981; }
        .bg-annulled { background-color: #ef4444; }
        .footer-note {
            margin-top: 40px;
            text-align: center;
            font-size: 10px;
            color: #94a3b8;
            border-top: 1px solid #e2e8f0;
            padding-top: 10px;
        }
    </style>
</head>
<body>

    {% if venta.anulado %}
        <div class="watermark">ANULADO</div>
    {% endif %}

    <table class="header-table">
        <tr>
            <td style="width: 55%;">
                <div class="company-title">MEAT MARKET</div>
                <div class="company-subtitle">Sistema de Gestión de Comercio y Despachos</div>
                <div class="company-subtitle">Rif: J-12345678-9</div>
            </td>
            <td style="width: 45%;">
                <div class="invoice-box">
                    <h2>FACTURA N° {{ venta.numeroFactura }}</h2>
                    <p><strong>Fecha de Emisión:</strong> {{ venta.fechaFactura ? venta.fechaFactura|date('d/m/Y') : '' }}</p>
                </div>
            </td>
        </tr>
    </table>

    <div class="client-card">
        <h3>Información del Cliente</h3>
        <table style="width: 100%; border-collapse: collapse;">
            <tr>
                <td style="width: 50%; padding: 2px 0;"><strong>Cliente:</strong> {{ clienteNombre }}</td>
                <td style="width: 50%; padding: 2px 0;"><strong>Documento / DNI:</strong> {{ clienteDocumento }}</td>
            </tr>
            <tr>
                <td style="width: 50%; padding: 2px 0;"><strong>Correo:</strong> {{ clienteCorreo }}</td>
                <td style="width: 50%; padding: 2px 0;"><strong>Teléfono:</strong> {{ clienteTelefono }}</td>
            </tr>
        </table>
    </div>

    <table class="items-table">
        <thead>
            <tr>
                <th style="width: 5%;">#</th>
                <th style="width: 15%;">Código</th>
                <th style="width: 45%;">Descripción del Producto</th>
                <th class="text-right" style="width: 10%;">Cant.</th>
                <th class="text-right" style="width: 12%;">Precio U.</th>
                <th class="text-right" style="width: 13%;">Total</th>
            </tr>
        </thead>
        <tbody>
            {% for item in items %}
                <tr>
                    <td class="text-center">{{ loop.index }}</td>
                    <td>{{ item.codigo }}</td>
                    <td>{{ item.descripcion }}</td>
                    <td class="text-right">{{ item.cantidad|number_format(2, ',', '.') }}</td>
                    <td class="text-right">${{ item.precio|number_format(2, ',', '.') }}</td>
                    <td class="text-right">${{ item.montoTotal|number_format(2, ',', '.') }}</td>
                </tr>
            {% else %}
                <tr>
                    <td colspan="6" class="text-center" style="color: #64748b;">No hay productos registrados en esta factura.</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

    <table class="totals-table">
        <tr>
            <td class="text-right"><strong>Subtotal:</strong></td>
            <td class="text-right">${{ (montoSubtotal is defined ? montoSubtotal : venta.monto)|number_format(2, ',', '.') }}</td>
        </tr>
        {% if venta.iva %}
        <tr>
            <td class="text-right"><strong>IVA:</strong></td>
            <td class="text-right">${{ (venta.montoIva ? venta.montoIva : 0)|number_format(2, ',', '.') }}</td>
        </tr>
        {% endif %}
        <tr class="grand-total">
            <td class="text-right"><strong>TOTAL FACTURA:</strong></td>
            <td class="text-right">${{ (montoTotalVenta is defined ? montoTotalVenta : venta.montoTotal)|number_format(2, ',', '.') }}</td>
        </tr>
    </table>

    {% if venta.anulado %}
    <div style="margin-top: 20px; padding: 10px; border: 1px solid #fca5a5; background-color: #fef2f2; border-radius: 6px;">
        <strong style="color: #dc2626;">Factura Anulada</strong><br>
        <span style="font-size: 11px; color: #7f1d1d;">
            <strong>Fecha de Anulación:</strong> {{ venta.fechaAnulado ? venta.fechaAnulado|date('d/m/Y h:i A') : 'N/A' }}<br>
            <strong>Motivo:</strong> {{ venta.motivoAnulado ? venta.motivoAnulado : 'Sin especificación' }}<br>
            <strong>Usuario Responsable:</strong> {{ usuarioAnuladoNombre }}
        </span>
    </div>
    {% endif %}

    <div class="footer-note">
        Documento generado digitalmente por el sistema Meat Market - {{ "now"|date("d/m/Y H:i:s") }}
    </div>

</body>
</html>
