/*ZONA DE PERSONALIZACION PAGINAS PDFS*/

<?php
defined('BASEPATH') or exit('No direct script access allowed');

// 1. Configuración de Marca y Colores - Grupo Código Mexa
$pdf->SetFont('helvetica', '', 10); 
$logo_url = base_url('uploads/company/' . get_option('company_logo'));
$color_azul = '#12436A'; 
$color_rojo = '#D32F2F';
$color_gris_claro = '#E8EAEB'; 

// Función para formato de moneda (En Negritas y alineado)
$render_moneda = function($monto, $font_size = '10pt', $color = '#000000') use ($proposal) {
    return '<div align="right" style="font-size: '.$font_size.'; color: '.$color.';"><b>' . 
           '$ ' . number_format($monto, 2, '.', ',') . ' ' . $proposal->currency_name . 
           '</b></div>';
};

// -------------------------------------------------------------------------
// 2. ENCABEZADO
// -------------------------------------------------------------------------
$pdf->SetMargins(0, 0, 0);
$pdf->SetY(6); 

$html_header = '
<table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td width="28%" valign="middle" align="left">
            <table width="100%" cellpadding="0" cellspacing="0" border="0">
                <tr><td style="font-size: 30pt; line-height: 30pt;">&nbsp;</td></tr> 
                <tr><td style="background-color:' . $color_azul . '; height: 24px;">&nbsp;</td></tr>
            </table>
        </td>
        <td width="44%" align="center" valign="middle">
            <img src="' . $logo_url . '" width="390">
        </td>
        <td width="28%" valign="middle" align="right">
            <table width="100%" cellpadding="0" cellspacing="0" border="0">
                <tr><td style="font-size: 30pt; line-height: 30pt;">&nbsp;</td></tr> 
                <tr><td style="background-color:' . $color_azul . '; height: 24px;">&nbsp;</td></tr>
            </table>
        </td>
    </tr>
</table>';

$pdf->writeHTML($html_header, true, false, true, false, '');

// -------------------------------------------------------------------------
// 3. CUERPO (DATOS Y BLOQUE UNIFICADO DE PRODUCTOS)
// -------------------------------------------------------------------------
$pdf->SetMargins(15, 25, 15);
$pdf->SetY(45); 

// Datos del Cliente, Folio (F alineada) y Fecha
$html_datos = '
<table width="100%" cellpadding="0" border="0">
    <tr>
        <td width="60%" align="left" style="font-size: 11pt;">
            <b>Cotización a:</b> <span style="font-size: 11.5pt;"><b>' . $proposal->proposal_to . '</b></span>
        </td>
        <td width="40%" align="right">
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="45%" align="left"><b>Folio:</b></td>
                    <td width="55%" align="right">' . format_proposal_number($proposal->id) . '</td>
                </tr>
                <tr>
                    <td width="45%" align="left"><b>Fecha:</b></td>
                    <td width="55%" align="right">' . _d($proposal->date) . '</td>
                </tr>
            </table>
        </td>
    </tr>
</table><br>';

$pdf->writeHTML($html_datos, true, false, true, false, '');

// UNIFICACIÓN DE SOLICITUD Y TABLA PARA ELIMINAR ESPACIO
$html_bloque_unificado = '
<table width="100%" cellpadding="5" border="0" style="margin-bottom: 0px;">
    <tr>
        <td style="background-color: ' . $color_azul . '; color: #ffffff; text-align: center; font-size: 11pt;">
            <b>SOLICITUD: ' . mb_strtoupper($proposal->subject, 'UTF-8') . '</b>
        </td>
    </tr>
</table>
<div style="line-height: 4px;">&nbsp;</div>
<table width="100%" cellpadding="8" cellspacing="0" style="border: 1px solid ' . $color_azul . '; border-collapse: collapse; margin-top: 0px;">
    <tr style="background-color: ' . $color_azul . '; color: #ffffff; font-size: 9.5pt;">
        <td width="10%" align="center"><b>CANT.</b></td>
        <td width="55%" align="left" style="border-left: 1.5pt solid #ffffff;"><b>DESCRIPCIÓN</b></td>
        <td width="35%" align="right" style="border-left: 1.5pt solid #ffffff;"><b>IMPORTE</b></td>
    </tr>';

$fondo_gris = false;
foreach ($proposal->items as $item) {
    $color_f = $fondo_gris ? $color_gris_claro : '#ffffff';
    $imp_fila = $item['qty'] * $item['rate'];
    $html_bloque_unificado .= '
    <tr style="background-color: ' . $color_f . ';">
        <td width="10%" align="center" style="border-bottom: 1px solid ' . $color_azul . ';">' . floatVal($item['qty']) . '</td>
        <td width="55%" align="left" style="border-bottom: 1px solid ' . $color_azul . ';"><b>' . $item['description'] . '</b><br><small>' . $item['long_description'] . '</small></td>
        <td width="35%" align="right" style="border-bottom: 1px solid ' . $color_azul . ';">' . $render_moneda($imp_fila) . '</td>
    </tr>';
    $fondo_gris = !$fondo_gris; 
}
$html_bloque_unificado .= '</table>';

// Eliminamos posibles saltos de línea de la descripción para evitar espacios arriba
$descripcion_limpia = trim(str_replace('{proposal_items}', '', $proposal->content));

$pdf->writeHTML($descripcion_limpia . $html_bloque_unificado, true, false, true, false, '');

// -------------------------------------------------------------------------
// 4. PIE DE PÁGINA
// -------------------------------------------------------------------------
$pdf->SetY(-85); 

$html_footer_info = '
<table width="100%" cellpadding="0">
    <tr>
        <td width="55%" align="left" style="font-size: 10pt; line-height: 1.5;">
            <b>Seamos Aliados Estratégicos para tu empresa</b><br><br>
            <b>Más información al:</b><br>
            <b>Whatsapp o Teléfono: (+52) 56 3951 9486</b><br>
            <b>Correo: <a href="mailto:ayuda@codigomexa.com" style="color:#000000; text-decoration:none;">ayuda@codigomexa.com</a></b>
        </td>
        <td width="45%">
            <table width="100%" cellpadding="4">
                <tr>
                    <td width="50%" align="right">Sub Total:</td>
                    <td width="50%" align="right">' . $render_moneda($proposal->subtotal, '10pt') . '</td>
                </tr>
                <tr>
                    <td width="50%" align="right">IVA 16%:</td>
                    <td width="50%" align="right">' . $render_moneda($proposal->total_tax, '10pt') . '</td>
                </tr>
                <tr style="background-color: ' . $color_rojo . '; color: #ffffff;">
                    <td width="35%" align="left" style="font-size: 12pt; font-weight: bold; line-height: 1.6;">Total:</td>
                    <td width="65%" align="right">' . $render_moneda($proposal->total, '12pt', '#ffffff') . '</td>
                </tr>
            </table>
        </td>
    </tr>
</table><br>';

$pdf->writeHTML($html_footer_info, true, false, true, false, '');

$pdf->SetMargins(0, 0, 0);
$pdf->SetX(0);
$pdf->SetY(-45); 
$html_footer_lines = '
<table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td width="48%" style="border-bottom: 2px solid ' . $color_azul . ';">&nbsp;</td>
        <td width="4%">&nbsp;</td>
        <td width="40%" style="border-bottom: 1px solid #000000;">&nbsp;</td>
        <td width="4%">&nbsp;</td>
        <td width="4%" style="border-bottom: 2px solid ' . $color_azul . ';">&nbsp;</td>
    </tr>
</table>';
$pdf->writeHTML($html_footer_lines, true, false, true, false, '');

$pdf->SetMargins(15, 0, 15);
$pdf->SetX(15);
$html_footer_text = '
<table width="100%" cellpadding="4">
    <tr>
        <td width="48%" align="left"><b><a href="https://codigomexa.com" style="color:#000000; text-decoration:none;">codigomexa.com</a></b></td>
        <td width="4%">&nbsp;</td>
        <td width="40%" align="center" style="font-size: 10pt;"><b>Samuel O. Alonso</b></td>
        <td width="8%">&nbsp;</td>
    </tr>
</table>';
$pdf->writeHTML($html_footer_text, true, false, true, false, '');

/*ZONA DE PERSONALIZACION PAGINAS CLIENTES assets/images/backcm.jpeg*/

/* --- Personalización de Propuestas Código Mexa --- */

/* Ocultar el sello nativo gigante de Perfex */
.stamp {
    display: none !important;
}

/* 1. Fondo de la página de propuesta con ruta desde la raíz */
body.proposal-view {
    background-image: url('/assets/images/backcm.jpeg') !important;
    background-size: cover !important;
    background-position: center center !important;
    background-attachment: fixed !important;
    background-color: #f0f0f0 !important; /* Color de respaldo por si la imagen tarda en cargar */
}

/* 2. Color azul corporativo para negritas y títulos */
body.proposal-view b, 
body.proposal-view strong, 
body.proposal-view .bold,
body.proposal-view h4,
body.proposal-view .proposal-html-info b {
    color: #12436A !important;
}

/* 3. Ajuste de legibilidad para los contenedores blancos */
body.proposal-view .panel_s {
    background-color: rgba(255, 255, 255, 0.95) !important;
    border: none !important;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1) !important;
}