<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
    // Verifica si la factura está anulada basándose en el campo 'anulada' de la tabla 'sma_fel_invoices'
    // Asegúrate de que la comparación sea en mayúsculas, ya que el ENUM es 'SI' y 'NO'
    $anulada = (strtoupper($fel->anulada) == 'SI'); 
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Factura FEL </title>

<style>

body{
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    color:#000;
    background:#fff;
    margin:0;
    padding:0;
}

/* ===== CONTENEDOR PRINCIPAL ===== */

#wrapper{
    width: 100%;
    max-width: 425px;   /* Vista en pantalla */
    margin: 0 auto;
    background:#fff;
}

/* ===== TEXTOS ===== */

h3{ margin:5px 0; }

.center{ text-align:center; }
.right{ text-align:right; }
.left{ text-align:left; }

/* ===== TABLAS ===== */

.table{
    width:100%;
    border-collapse: collapse;
    font-size:12px;
}

.table th{
    border-bottom:1px solid #000;
    padding:3px;
}

.table td{
    padding:3px;
    vertical-align:top;
}

/* ===== TOTALES ===== */

.totals td{
    padding:3px;
    font-size:12px;
}

/* ===== BOTONES ===== */

#buttons{
    margin-top:10px;
}

/* ===== MARCA DE AGUA ===== */
.watermark {
    position: absolute;
    top: 25%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-45deg);
    font-size: 80px;
    color: rgba(255, 0, 0, 0.3); /* Rojo transparente */
    z-index: 1000; /* Asegura que esté encima de todo */
    pointer-events: none; /* Permite interactuar con el contenido debajo */
    user-select: none; /* Evita que se pueda seleccionar el texto */
    width: 100%;
    text-align: center;
}


/* ========================================================= */
/* =================== MODO IMPRESIÃ“N ====================== */
/* ========================================================= */

@media print {

    html, body{
        width:100%;
        margin:0;
        padding:0;
        background:#fff;
    }

    /* ðŸ”¥ Se adapta automÃ¡ticamente a 58 u 80 mm */
    #wrapper{
        width:100%;
        max-width: 58mm;
        margin:0 auto;
        padding:0;
        font-size:11px;
    }
    
/* Clase para 58mm */
    .printer-58mm #wrapper {
        max-width: 58mm;
    }
    
    .table{
        font-size:11px;
    }

    .totals td{
        font-size:11px;
    }

    #buttons{
        display:none !important;
    }

    hr{
        margin:4px 0;
    }

    /* ðŸ”¥ IMPORTANTE â€” permite ambas impresoras */
    @page{
        size:auto;
        margin:0;
    }
}

</style>

</head>

<body>
    
<?php if ($anulada): ?>
    <div class="watermark">ANULADA</div>
<?php endif; ?>

<div id="wrapper">

<!-- ================= EMPRESA ================= -->

<div class="center">
    <h3></h3>
       <strong>NIT: <?= $biller->vat_no?><br>
    <?= $biller->name ?><br>
<?= $biller->address ?>,
<?= $biller->city ?>,<br>
<?= $biller->state ?>,<br>
Telefono: PBX <?= $biller->phone ?><br>
  No. Autorización 
   <br><?= $fel->uuid ?></strong><br>
</div>

<hr>

<!-- ================= DATOS FEL ================= -->

<div class="center">
<strong>DOCUMENTO TRIBUTARIO ELECTRONICO</strong><br>
<strong>FACTURA ELECTRÃ“NICA</strong><br>
Serie: <?= $fel->serie ?><br>
Número: <?= $fel->numero ?><br>
</div>

<hr>

<!-- ================= CLIENTE ================= -->

<strong>Cliente:</strong> <?= $fel->receptor_nombre ?><br>
<strong>NIT:</strong> <?= $fel->client_nit ?><br>
<strong>Dirección:</strong> <?= $fel->receptor_direccion ?><br>

<hr>

<!-- ================= DATOS VENTA ================= -->

<strong>Fecha Venta:</strong> <?= date('d/m/Y H:i', strtotime($sale->date)) ?><br>
<strong>Vendedor:</strong> <?= $user->username?><br>
<strong>Referencia:</strong> <?= $sale->reference_no ?><br>

<?php if (!empty($sale->note)) : ?>
Notas: <?= $sale->note ?><br>
<?php endif; ?>

<hr>

<!-- ================= PRODUCTOS ================= -->

<table class="table">
<thead>
<tr>
<th>#</th>
<th>Descripción</th>
<th>Cant</th>
<th>Precio</th>
<th>Total</th>
</tr>
</thead>
<tbody>

<?php $r=1; foreach($items as $item): ?>

<tr>
<td class="center"><?= $r ?></td>

<td class="left">
<?= $item->product_name ?>
</td>

<td class="center"><?= number_format($item->quantity,2) ?></td>

<td class="right">
Q <?= number_format($item->unit_price,2) ?>
</td>

<td class="right">
<?php $line_total = $item->quantity * $item->unit_price; ?>
Q <?= number_format($line_total,2) ?>
</td>
</tr>

<?php $r++; endforeach; ?>

</tbody>
</table>

<hr>

<!-- ================= TOTALES (DESDE FEL) ================= -->

<table class="totals" width="100%">


<tr>
<td class="left"><strong>Total</strong></td>
<td class="right"><strong>Q <?= $fel->grand_total ?><br></strong></td>
</tr>

</table>

<hr>


<div class="center">
<strong>NO SE ACEPTAN CAMBIOS NI DEVOLUCIONES</strong><br>
<br>
<strong>GRACIAS POR SU COMPRA</strong><br>
</div>
<hr>

<!-- ================= QR FEL ================= -->
<div class="center">
<strong>SUJETO A PAGOS TRIMESTRALES</strong><br>
<br>
<strong>Agente Retenedor de Iva</strong><br>
<?php if(!empty($fel->uuid)): ?>

<?php
$uuid = trim($fel->uuid);

$qr_url = "https://fel.sat.gob.gt/verificador-web/publico/vistas/verificacionDte.jsf?uuid=".$uuid;

$qr_img = "https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=".urlencode($qr_url);
?>

<div style="text-align:center; margin-top:10px;">
    <img src="<?= $qr_img ?>" alt="QR FEL">
</div>

<?php endif; ?>

</div>
<hr>

<!-- ================= CERTIFICADOR ================= -->

<div class="center" style="font-size:10px">
<strong>DATOS DEL CERTIFICADOR</strong><br>
Autorización: <?= $fel->numero_autorizacion ?><br>
Certificador: NIT 7745482-0 DIGIFACT 
<br>
SERVICIOS, SOCIEDAD ANONIMA 
<br>
https://felgtaws.digifact.com.gt
<br>
Fecha Hora Certificación: <?= $fel->issued_at ?><br>
</div>

<hr>

<!-- ================= BOTONES ================= -->

<div id="buttons" class="center">
    <button onclick="window.print()">Imprimir</button>
    
     <a href="<?= site_url('admin/fel') ?>"><button type="button">Volver</button></a>
    </a>
</div>



