<?php
/**
 * Freefactu Presupuesto Template
 *
 * This template is based on the invoice template but adapted for quotations/estimates.
 * No QR code, no Verifactu elements, but uses the same professional styling.
 *
 * IMPORTANT: This template uses inline <style> tags intentionally.
 * This is NOT a WordPress page - it generates standalone HTML for DOMPDF
 * to convert into a PDF document. DOMPDF requires inline CSS because:
 * 1. External stylesheets cannot be reliably loaded during PDF generation
 * 2. wp_enqueue_style() is not available in this context (no WordPress page rendering)
 * 3. The HTML is never displayed in a browser - only converted to PDF
 *
 * @see https://github.com/dompdf/dompdf/wiki/CSSCompatibility
 *
 * @var array $data Template data prepared by Presupuestos extension
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Extract template data
$freefactu_presupuesto_number = isset( $data['presupuesto_number'] ) ? $data['presupuesto_number'] : '';
$freefactu_presupuesto_date   = isset( $data['presupuesto_date'] ) ? $data['presupuesto_date'] : '';
$freefactu_validity_date      = isset( $data['validity_date'] ) ? $data['validity_date'] : '';
$freefactu_validity_days      = isset( $data['validity_days'] ) ? $data['validity_days'] : 30;
$freefactu_company            = isset( $data['company'] ) ? $data['company'] : array();
$freefactu_customer           = isset( $data['customer'] ) ? $data['customer'] : array();
$freefactu_items              = isset( $data['items'] ) ? $data['items'] : array();
$freefactu_totals             = isset( $data['totals'] ) ? $data['totals'] : array();
$freefactu_tax_breakdown      = isset( $data['tax_breakdown'] ) ? $data['tax_breakdown'] : array();
$freefactu_order_id           = isset( $data['order_id'] ) ? $data['order_id'] : '';
$freefactu_pdf_title          = isset( $data['pdf_title'] ) ? $data['pdf_title'] : __( 'Presupuesto', 'freefactu' );
$freefactu_pdf_footer         = isset( $data['pdf_footer'] ) ? $data['pdf_footer'] : '';

// Helper function to format currency
if ( ! function_exists( 'freefactu_format_amount' ) ) {
    function freefactu_format_amount( $amount, $show_sign = false ) {
        return number_format( $amount, 2, ',', '.' );
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title><?php echo esc_html( sprintf( 
        /* translators: 1: PDF Title, 2: Presupuesto Number */
        __( '%1$s %2$s', 'freefactu' ), 
        $freefactu_pdf_title, 
        $freefactu_presupuesto_number  
    ) ); ?></title>
    <?php // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet -- DOMPDF PDF template: wp_enqueue_style() is unavailable in this context. See file docblock. ?>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: DejaVu Sans, sans-serif; font-size: 10pt; line-height: 1.4; color: #333; padding: 10mm 15mm; }
        
        /* Top header: Company left, Title/Info right (no QR for presupuestos) */
        .top-header { margin-bottom: 8mm; display: table; width: 100%; }
        .top-header > div { display: table-cell; vertical-align: top; }
        .top-header .company-info { width: 50%; text-align: left; }
        .top-header .invoice-info { width: 50%; text-align: right; }
        
        /* Company info styles */
        .company-logo { margin-bottom: 3mm; }
        .company-logo .logo-image { max-width: 50mm; max-height: 30mm; height: auto; }
        .site-name { font-size: 9pt; color: #666; margin-bottom: 1mm; }
        .company-name { font-size: 9pt; font-weight: bold; color: #333; margin-bottom: 1mm; }
        .company-nif { font-size: 9pt; font-weight: bold; color: #333; margin-bottom: 1mm; }
        .company-address { font-size: 9pt; color: #666; }
        
        /* Invoice/Presupuesto info styles */
        .invoice-title { font-size: 16pt; font-weight: bold; color: #0073aa; margin-bottom: 2mm; }
        .invoice-number { font-size: 11pt; font-weight: bold; margin-bottom: 1mm; }
        .invoice-date { font-size: 9pt; color: #666; }
        .validity-info { margin-top: 3mm; padding: 2mm; background-color: #e7f3ff; border: 1px solid #0073aa; border-radius: 2mm; font-size: 8pt; }
        
        /* Customer section - full width below header */
        .customer-section { margin-bottom: 8mm; padding: 5mm; background-color: #f8f9fa; border: 1px solid #e9ecef; }
        .customer-title { font-size: 10pt; font-weight: bold; color: #666; margin-bottom: 3mm; text-transform: uppercase; }
        .customer-name { font-size: 9pt; font-weight: bold; color: #555; margin-bottom: 1mm; }
        .customer-nif { font-size: 9pt; font-weight: bold; color: #555; margin-bottom: 2mm; }
        .customer-details { font-size: 9pt; color: #555; }
        
        .items-table { width: 100%; border-collapse: collapse; margin: 8mm 0; }
        .items-table th { background-color: #0073aa; color: white; padding: 3mm 2mm; text-align: left; font-size: 9pt; font-weight: bold; }
        .items-table th.text-right { text-align: right; }
        .items-table td { padding: 2.5mm 2mm; border-bottom: 1px solid #e9ecef; font-size: 9pt; }
        .items-table td.text-right { text-align: right; }
        .items-table tbody tr:nth-child(even) { background-color: #f8f9fa; }
        
        .tax-section { margin: 8mm 0; width: 60%; margin-left: auto; }
        .tax-title { font-size: 10pt; font-weight: bold; margin-bottom: 3mm; text-align: left; }
        .tax-table { width: 100%; border-collapse: collapse; }
        .tax-table th, .tax-table td { padding: 2mm; font-size: 9pt; border: 1px solid #e9ecef; }
        .tax-table th { background-color: #f8f9fa; text-align: left; font-weight: bold; }
        .tax-table td.text-right { text-align: right; }
        
        .totals-section { margin-top: 5mm; }
        .totals-table { width: 40%; margin-left: auto; border-collapse: collapse; }
        .totals-table td { padding: 2mm 3mm; font-size: 10pt; }
        .totals-table td.label { text-align: right; color: #666; }
        .totals-table td.value { text-align: right; font-weight: bold; }
        .totals-table tr.total-row td { font-size: 12pt; border-top: 2px solid #0073aa; padding-top: 3mm; }
        .totals-table tr.total-row td.value { color: #0073aa; }
        
        .invoice-footer { margin-top: 15mm; padding-top: 5mm; border-top: 1px solid #e9ecef; font-size: 8pt; color: #999; text-align: center; }
        
        .validity-notice { margin-top: 10mm; padding: 4mm; background-color: #fff8e5; border: 1px solid #ffb900; border-radius: 2mm; font-size: 9pt; text-align: center; }
    </style>
</head>
<body>
    <!-- Top Header: Company (left) + Presupuesto Info (right) -->
    <div class="top-header">
        <!-- Company info (left) -->
        <div class="company-info">
            <?php if ( ! empty( $freefactu_company['logo'] ) ) : ?>
                <div class="company-logo">
                    <img src="<?php echo esc_url( $freefactu_company['logo'] ); ?>" alt="<?php echo esc_attr( $freefactu_company['name'] ?? '' ); ?>" class="logo-image" />
                </div>
            <?php endif; ?>
            <?php if ( ! empty( $freefactu_company['site_name'] ) && $freefactu_company['site_name'] !== ( $freefactu_company['name'] ?? '' ) ) : ?>
                <div class="site-name"><?php echo esc_html( $freefactu_company['site_name'] ); ?></div>
            <?php endif; ?>
            <div class="company-name"><?php echo esc_html( $freefactu_company['name'] ?? '' ); ?></div>
            <div class="company-nif">NIF: <?php echo esc_html( $freefactu_company['nif'] ?? '' ); ?></div>
            <?php if ( ! empty( $freefactu_company['address'] ) ) : ?>
                <div class="company-address"><?php echo esc_html( $freefactu_company['address'] ); ?></div>
            <?php endif; ?>
            <?php if ( ! empty( $freefactu_company['email'] ) ) : ?>
                <div class="company-address"><?php echo esc_html( $freefactu_company['email'] ); ?></div>
            <?php endif; ?>
            <?php if ( ! empty( $freefactu_company['phone'] ) ) : ?>
                <div class="company-address"><?php echo esc_html( $freefactu_company['phone'] ); ?></div>
            <?php endif; ?>
        </div>

        <!-- Presupuesto info (right) -->
        <div class="invoice-info">
            <div class="invoice-title"><?php echo esc_html( strtoupper( $freefactu_pdf_title ) ); ?></div>
            <div class="invoice-number">
                <?php echo esc_html( sprintf( 
                    /* translators: %s: Presupuesto number */
                    __( 'Nº: %s', 'freefactu' ), 
                    $freefactu_presupuesto_number 
                ) ); ?>
            </div>
            <div class="invoice-date"><?php echo esc_html( sprintf( 
                /* translators: %s: Presupuesto date */
                __( 'Fecha: %s', 'freefactu' ), 
                $freefactu_presupuesto_date 
            ) ); ?></div>
            <?php if ( $freefactu_order_id ) : ?>
                <div class="invoice-date"><?php echo esc_html( sprintf( 
                    /* translators: %s: Order ID */
                    __( 'Ref. Pedido: #%s', 'freefactu' ), 
                    $freefactu_order_id 
                ) ); ?></div>
            <?php endif; ?>
            <?php if ( ! empty( $freefactu_validity_date ) ) : ?>
                <div class="validity-info">
                    <strong><?php esc_html_e( 'Válido hasta:', 'freefactu' ); ?></strong> <?php echo esc_html( $freefactu_validity_date ); ?>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Customer Information (full width) -->
    <div class="customer-section">
        <div class="customer-title"><?php esc_html_e( 'Datos del Cliente', 'freefactu' ); ?></div>
        <div class="customer-name">
            <?php 
            $freefactu_customer_name = trim( $freefactu_customer['name'] ?? '' );
            if ( ! empty( $freefactu_customer['company'] ) ) {
                $freefactu_customer_name .= ( $freefactu_customer_name ? ' - ' : '' ) . $freefactu_customer['company'];
            }
            echo esc_html( $freefactu_customer_name ?: __( 'Cliente', 'freefactu' ) );
            ?>
        </div>
        <?php if ( ! empty( $freefactu_customer['nif'] ) ) : ?>
            <div class="customer-nif">NIF/DNI: <?php echo esc_html( $freefactu_customer['nif'] ); ?></div>
        <?php endif; ?>
        <div class="customer-details">
            <?php if ( ! empty( $freefactu_customer['address_1'] ) ) : ?>
                <?php echo esc_html( $freefactu_customer['address_1'] ); ?><br>
            <?php endif; ?>
            <?php if ( ! empty( $freefactu_customer['address_2'] ) ) : ?>
                <?php echo esc_html( $freefactu_customer['address_2'] ); ?><br>
            <?php endif; ?>
            <?php 
            $freefactu_location_parts = array_filter( array(
                $freefactu_customer['postcode'] ?? '',
                $freefactu_customer['city'] ?? '',
                $freefactu_customer['country'] ?? ''
            ) );
            if ( ! empty( $freefactu_location_parts ) ) : ?>
                <?php echo esc_html( implode( ', ', $freefactu_location_parts ) ); ?><br>
            <?php endif; ?>
            <?php if ( ! empty( $freefactu_customer['phone'] ) ) : ?>
                <?php esc_html_e( 'Tel:', 'freefactu' ); ?> <?php echo esc_html( $freefactu_customer['phone'] ); ?><br>
            <?php endif; ?>
            <?php if ( ! empty( $freefactu_customer['email'] ) ) : ?>
                <?php echo esc_html( $freefactu_customer['email'] ); ?>
            <?php endif; ?>
        </div>
    </div>

    <!-- Order Line Items -->
    <table class="items-table">
        <thead>
            <tr>
                <th style="width: 45%;"><?php esc_html_e( 'Descripción', 'freefactu' ); ?></th>
                <th class="text-right" style="width: 12%;"><?php esc_html_e( 'Cantidad', 'freefactu' ); ?></th>
                <th class="text-right" style="width: 15%;"><?php esc_html_e( 'Precio Unit.', 'freefactu' ); ?></th>
                <th class="text-right" style="width: 10%;"><?php esc_html_e( 'IVA %', 'freefactu' ); ?></th>
                <th class="text-right" style="width: 18%;"><?php esc_html_e( 'Total', 'freefactu' ); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php if ( ! empty( $freefactu_items ) ) : ?>
                <?php foreach ( $freefactu_items as $freefactu_item ) : ?>
                    <tr>
                        <td><?php echo esc_html( $freefactu_item['name'] ?? '' ); ?></td>
                        <td class="text-right"><?php echo esc_html( $freefactu_item['quantity'] ?? 1 ); ?></td>
                        <td class="text-right"><?php echo esc_html( freefactu_format_amount( $freefactu_item['unit_price'] ?? 0 ) ); ?> €</td>
                        <td class="text-right"><?php echo esc_html( $freefactu_item['tax_rate'] ?? 0 ); ?>%</td>
                        <td class="text-right"><?php echo esc_html( freefactu_format_amount( $freefactu_item['total'] ?? 0 ) ); ?> €</td>
                    </tr>
                <?php endforeach; ?>
            <?php else : ?>
                <tr>
                    <td colspan="5" style="text-align: center; color: #999;"><?php esc_html_e( 'No hay artículos', 'freefactu' ); ?></td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>

    <!-- Tax Breakdown -->
    <?php if ( ! empty( $freefactu_tax_breakdown ) ) : ?>
    <div class="tax-section">
        <div class="tax-title"><?php esc_html_e( 'Desglose de Impuestos', 'freefactu' ); ?></div>
        <table class="tax-table">
            <thead>
                <tr>
                    <th><?php esc_html_e( 'Tipo IVA', 'freefactu' ); ?></th>
                    <th class="text-right"><?php esc_html_e( 'Base Imponible', 'freefactu' ); ?></th>
                    <th class="text-right"><?php esc_html_e( 'Cuota IVA', 'freefactu' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ( $freefactu_tax_breakdown as $freefactu_tax ) : ?>
                    <tr>
                        <td><?php 
                            $freefactu_tax_rate = $freefactu_tax['rate'] ?? 0;
                            $freefactu_tax_name = $freefactu_tax['name'] ?? '';
                            if ( ! empty( $freefactu_tax_name ) ) {
                                echo esc_html( $freefactu_tax_name . ' (' . $freefactu_tax_rate . '%)' );
                            } else {
                                echo esc_html( $freefactu_tax_rate . '%' );
                            }
                        ?></td>
                        <td class="text-right"><?php echo esc_html( freefactu_format_amount( $freefactu_tax['base'] ?? 0 ) ); ?> €</td>
                        <td class="text-right"><?php echo esc_html( freefactu_format_amount( $freefactu_tax['amount'] ?? 0 ) ); ?> €</td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>

    <!-- Totals -->
    <div class="totals-section">
        <table class="totals-table">
            <tr>
                <td class="label"><?php esc_html_e( 'Subtotal:', 'freefactu' ); ?></td>
                <td class="value"><?php echo esc_html( freefactu_format_amount( $freefactu_totals['subtotal'] ?? 0 ) ); ?> €</td>
            </tr>
            <tr>
                <td class="label"><?php esc_html_e( 'IVA:', 'freefactu' ); ?></td>
                <td class="value"><?php echo esc_html( freefactu_format_amount( $freefactu_totals['tax_total'] ?? 0 ) ); ?> €</td>
            </tr>
            <tr class="total-row">
                <td class="label"><?php esc_html_e( 'TOTAL:', 'freefactu' ); ?></td>
                <td class="value"><?php echo esc_html( freefactu_format_amount( $freefactu_totals['total'] ?? 0 ) ); ?> €</td>
            </tr>
        </table>
    </div>

    <!-- Validity Notice -->
    <?php if ( ! empty( $freefactu_validity_date ) ) : ?>
    <div class="validity-notice">
        <?php echo esc_html( sprintf( 
            /* translators: 1: Validity date, 2: Validity days */
            __( 'Este presupuesto es válido hasta el %1$s (%2$d días desde la fecha de emisión).', 'freefactu' ), 
            $freefactu_validity_date, 
            $freefactu_validity_days 
        ) ); ?>
    </div>
    <?php endif; ?>

    <!-- Footer -->
    <div class="invoice-footer">
        <?php 
        if ( ! empty( $freefactu_pdf_footer ) ) {
            echo esc_html( $freefactu_pdf_footer );
        } else {
            echo esc_html( sprintf( 
                /* translators: %s: Company name */
                __( 'Presupuesto generado por %s', 'freefactu' ), 
                $freefactu_company['name'] ?? '' 
            ) );
        }
        ?>
    </div>
</body>
</html>
