<?php
/**
 * Offer (Angebot) PDF Template - BOLD
 * Clone of invoice-pdf-bold.php for offers: title ANGEBOT, validity, acceptance
 * block instead of payment terms. Reads $offer (not $invoice).
 */

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

// Format date as "DD MMM YYYY"
$months_en = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
$fmt_date = function ($ymd) use ($months_en) {
    if (empty($ymd)) return '';
    $p = explode('-', $ymd);
    if (count($p) !== 3) return $ymd;
    return $p[2] . ' ' . ($months_en[(int)$p[1] - 1] ?? '') . ' ' . $p[0];
};
$formatted_date = $fmt_date($offer['date_issued']);
$formatted_valid_until = $fmt_date($offer['valid_until'] ?? '');

// Format currency with 2 decimals and comma separator (German format)
if (!function_exists('mk_format_currency_bold')) {
    function mk_format_currency_bold($amount) {
        return number_format((float)$amount, 2, ',', '.') . ' EUR';
    }
}

// Theme colors
$theme = $settings['invoice_theme'] ?? 'light';
if ($theme === 'dark') {
    $bg_color = '#0a0a0b';
    $text_primary = '#ffffff';
    $text_secondary = '#a1a1aa';
    $text_muted = '#71717a';
    $border_color = '#ffffff';
} else {
    $bg_color = '#ffffff';
    $text_primary = '#000000';
    $text_secondary = '#444444';
    $text_muted = '#888888';
    $border_color = '#000000';
}

$page_padding = '50px';
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        @page { margin: 0; size: A4; }
        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            /* DejaVu Sans is bundled with DomPDF and gets embedded — keeps the offer
               visually identical to the invoice and PDF/A-3-safe. */
            font-family: "DejaVu Sans", sans-serif;
            font-size: 9pt;
            line-height: 1.4;
            color: <?php echo $text_primary; ?>;
            background: <?php echo $bg_color; ?>;
            padding: <?php echo $page_padding; ?>;
        }

        .invoice-container { width: 100%; }

        .header { padding-bottom: 25px; margin-bottom: 35px; }
        .header-row { display: table; width: 100%; }
        .header-left { display: table-cell; vertical-align: top; }
        .header-right { display: table-cell; text-align: right; vertical-align: top; }
        .invoice-title { font-size: 42pt; font-weight: 900; text-transform: uppercase; letter-spacing: -2px; line-height: 1; color: <?php echo $text_primary; ?>; }
        .invoice-number { font-size: 10pt; font-weight: 700; text-transform: uppercase; color: <?php echo $text_muted; ?>; padding-top: 5px; }

        .meta-section { margin-bottom: 50px; }
        .meta-row { display: table; width: 100%; }
        .meta-col { display: table-cell; vertical-align: top; width: 50%; }
        .meta-col:last-child { text-align: right; }
        .meta-label { font-size: 8pt; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; color: <?php echo $text_muted; ?>; margin-bottom: 5px; }
        .meta-value { font-size: 11pt; font-weight: 700; text-transform: uppercase; color: <?php echo $text_primary; ?>; }
        .client-address { font-size: 9pt; font-weight: 400; text-transform: none; color: <?php echo $text_secondary; ?>; margin-top: 3px; }

        .items-section { margin-bottom: 25px; }
        .items-header { display: table; width: 100%; padding-bottom: 8px; margin-bottom: 20px; border-bottom: 0.5px solid <?php echo $text_muted; ?>; }
        .items-header-col { display: table-cell; font-size: 8pt; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; color: <?php echo $text_muted; ?>; }
        .items-header-col:last-child { text-align: right; }
        .item-row { display: table; width: 100%; padding: 15px 0; }
        .item-content { display: table-cell; vertical-align: top; width: 75%; }
        .item-amount { display: table-cell; vertical-align: top; text-align: right; width: 25%; font-size: 11pt; font-weight: 700; color: <?php echo $text_primary; ?>; }
        .item-title { font-size: 12pt; font-weight: 700; text-transform: uppercase; color: <?php echo $text_primary; ?>; margin-bottom: 8px; }
        .item-period { font-size: 8pt; color: <?php echo $text_muted; ?>; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 10px; }
        .item-bullets { list-style: none; padding: 0; margin: 0; }
        .item-bullets li { font-size: 9pt; color: <?php echo $text_secondary; ?>; margin-bottom: 4px; padding-left: 15px; text-indent: -15px; }
        .item-bullets__dot { color: <?php echo $text_muted; ?>; padding-right: 7px; }

        .totals-section { border-top: 0.5px solid <?php echo $text_muted; ?>; padding-top: 15px; margin-bottom: 40px; }
        .totals-row { display: table; width: 100%; margin-bottom: 5px; }
        .totals-label { display: table-cell; font-size: 9pt; text-transform: uppercase; letter-spacing: 0.5px; color: <?php echo $text_muted; ?>; }
        .totals-value { display: table-cell; text-align: right; font-size: 9pt; color: <?php echo $text_secondary; ?>; }
        .totals-row-main { margin-top: 12px; padding-top: 12px; }
        .totals-row-main .totals-label { font-size: 12pt; font-weight: 700; color: <?php echo $text_primary; ?>; }
        .totals-row-main .totals-value { font-size: 18pt; font-weight: 900; color: <?php echo $text_primary; ?>; }

        .footer { border-top: 0.5px solid <?php echo $text_muted; ?>; padding-top: 25px; position: fixed; bottom: <?php echo $page_padding; ?>; left: <?php echo $page_padding; ?>; right: <?php echo $page_padding; ?>; }
        .footer-row { display: table; width: 100%; table-layout: fixed; }
        .footer-col { display: table-cell; vertical-align: top; text-align: left; }
        .footer-col:first-child { width: 33%; }
        .footer-col:nth-child(2) { width: 33%; }
        .footer-col:last-child { width: 34%; padding-left: 60px; }
        .footer-headline { font-size: 8pt; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; color: <?php echo $text_muted; ?>; margin-bottom: 10px; }
        .company-brand { font-size: 14pt; font-weight: 900; text-transform: uppercase; letter-spacing: -0.5px; color: <?php echo $text_primary; ?>; margin-bottom: 8px; }
        .footer-label { font-size: 7pt; text-transform: uppercase; letter-spacing: 0.5px; color: <?php echo $text_muted; ?>; margin-bottom: 1px; }
        .footer-value { font-size: 8pt; color: <?php echo $text_secondary; ?>; margin-bottom: 6px; }
        .legal-text { font-size: 7pt; color: <?php echo $text_muted; ?>; line-height: 1.4; }
    </style>
</head>
<body>
    <div class="invoice-container">
        <!-- Header -->
        <div class="header">
            <div class="header-row">
                <div class="header-left">
                    <div class="invoice-title">ANGEBOT</div>
                </div>
                <div class="header-right">
                    <div class="invoice-number"><?php echo esc_html($offer['offer_number']); ?></div>
                </div>
            </div>
        </div>

        <!-- Client & Date -->
        <div class="meta-section">
            <div class="meta-row">
                <div class="meta-col">
                    <div class="meta-label">Angebotsempfänger</div>
                    <div class="meta-value"><?php echo esc_html($client['company_name']); ?></div>
                    <?php if (!empty($client['address_line1']) || !empty($client['city'])): ?>
                    <div class="client-address">
                        <?php echo esc_html($client['address_line1'] ?? ''); ?><?php if (!empty($client['postal_code'])): ?>, <?php echo esc_html($client['postal_code']); ?><?php endif; ?>
                        <?php if (!empty($client['city'])): ?><br><?php echo esc_html($client['city']); ?><?php if ($client_country_label !== ''): ?>, <?php echo esc_html($client_country_label); ?><?php endif; ?><?php endif; ?>
                    </div>
                    <?php endif; ?>
                </div>
                <div class="meta-col">
                    <div class="meta-label">Angebotsdatum</div>
                    <div class="meta-value"><?php echo esc_html($formatted_date); ?></div>
                    <?php if ($formatted_valid_until !== ''): ?>
                    <div class="meta-label" style="margin-top: 14px;">Gültig bis</div>
                    <div class="meta-value"><?php echo esc_html($formatted_valid_until); ?></div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Items -->
        <div class="items-section">
            <div class="items-header">
                <div class="items-header-col">Pos.</div>
                <div class="items-header-col">Betrag</div>
            </div>

            <?php foreach ($items as $item): ?>
            <div class="item-row">
                <div class="item-content">
                    <div class="item-title"><?php echo esc_html($item['description']); ?></div>
                    <?php if (!empty($item['details'])): ?>
                    <ul class="item-bullets">
                        <?php
                        $details_lines = explode("\n", $item['details']);
                        foreach ($details_lines as $line):
                            $line = trim($line);
                            if (!empty($line)):
                                $line = ltrim($line, '•-* ');
                        ?>
                        <li><span class="item-bullets__dot">&#8226;</span><?php echo esc_html($line); ?></li>
                        <?php
                            endif;
                        endforeach;
                        ?>
                    </ul>
                    <?php endif; ?>
                </div>
                <div class="item-amount"><?php echo mk_format_currency_bold($item['total']); ?></div>
            </div>
            <?php endforeach; ?>
        </div>

        <!-- Totals -->
        <div class="totals-section">
            <div class="totals-row">
                <div class="totals-label">Zwischensumme</div>
                <div class="totals-value"><?php echo mk_format_currency_bold($offer['subtotal'] ?? $offer['total']); ?></div>
            </div>
            <div class="totals-row">
                <div class="totals-label">MwSt. (0%)</div>
                <div class="totals-value">0,00 EUR</div>
            </div>
            <div class="totals-row totals-row-main">
                <div class="totals-label">Angebotssumme</div>
                <div class="totals-value"><?php echo mk_format_currency_bold($offer['total']); ?></div>
            </div>
        </div>

        <!-- Footer with Company Branding -->
        <div class="footer">
            <div class="footer-row">
                <div class="footer-col">
                    <?php if (!empty($logo_data_uri)): $logo_h = array('small' => 26, 'medium' => 36, 'large' => 50); ?>
                    <img src="<?php echo $logo_data_uri; ?>" alt="" style="height: <?php echo ($logo_h[$logo_size] ?? 36); ?>px; width: auto; max-width: 180px; display: block; margin-bottom: 6px;" />
                    <?php endif; ?>
                    <div class="company-brand"><?php echo esc_html($settings['company_name'] ?? ''); ?></div>
                    <?php if (!empty($settings['steuernummer'])): ?><div class="footer-label">St.-Nr. <?php echo esc_html($settings['steuernummer']); ?></div><?php endif; ?>
                    <div class="footer-label"><?php echo esc_html($settings['company_email'] ?? ''); ?></div>
                    <div class="footer-label"><?php echo esc_html($settings['company_phone'] ?? ''); ?></div>
                </div>
                <div class="footer-col">
                    <div class="footer-headline">Angebot</div>
                    <div class="footer-value">Bei Annahme erstellen wir eine Rechnung gemäß § 14 UStG.</div>
                    <div class="legal-text">
                        <?php echo esc_html($settings['kleinunternehmer_text'] ?? 'Gemäß § 19 UStG. Abs. 1 wird keine Umsatzsteuer berechnet.'); ?>
                    </div>
                </div>
                <div class="footer-col">
                    <div class="footer-headline">Bankverbindung</div>
                    <div class="footer-label">Bank</div>
                    <div class="footer-value"><?php echo esc_html($settings['bank_name'] ?? ''); ?></div>
                    <div class="footer-label">IBAN</div>
                    <div class="footer-value"><?php echo esc_html($settings['bank_iban'] ?? ''); ?></div>
                    <div class="footer-label">BIC</div>
                    <div class="footer-value"><?php echo esc_html($settings['bank_bic'] ?? ''); ?></div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
