<?php
/**
 * Offer (Angebot) PDF Template — classic
 * Clone of invoice-pdf.php: title ANGEBOT, validity instead of due date,
 * 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')) {
    function mk_format_currency($amount) {
        return number_format((float)$amount, 2, ',', '.') . '€';
    }
}

// Theme colors
$theme = $settings['invoice_theme'] ?? 'light';
if ($theme === 'dark') {
    $bg_color = '#0a0a0b';
    $text_primary = '#f4f4f5';
    $text_secondary = '#a1a1aa';
    $text_muted = '#71717a';
} else {
    $bg_color = '#ffffff';
    $text_primary = '#1a1a1a';
    $text_secondary = '#555555';
    $text_muted = '#666666';
}

$page_margin = '30mm';
$content_padding = '80px';
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        @page { margin: <?php echo $page_margin; ?>; size: A4; }
        * { margin: 0; padding: 0; box-sizing: border-box; }

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

        .invoice-container { width: 100%; max-width: 100%; }

        .company-info { margin-bottom: 25px; }
        .company-name { font-weight: bold; font-size: 11pt; margin-bottom: 2px; color: <?php echo $text_primary; ?>; }
        .company-details { font-size: 10pt; line-height: 1.4; color: <?php echo $text_secondary; ?>; }

        .invoice-section { margin-bottom: 25px; }
        .invoice-label { font-weight: bold; font-size: 11pt; margin-bottom: 2px; color: <?php echo $text_primary; ?>; }
        .invoice-number, .invoice-date { font-size: 10pt; color: <?php echo $text_secondary; ?>; }
        .invoice-validity { font-size: 10pt; color: <?php echo $text_secondary; ?>; margin-top: 4px; }

        .client-section { margin-bottom: 120px; }
        .client-name { font-weight: bold; font-size: 11pt; margin-bottom: 2px; color: <?php echo $text_primary; ?>; }
        .client-address { font-size: 10pt; line-height: 1.4; color: <?php echo $text_secondary; ?>; }

        .items-table { width: 100%; border-collapse: collapse; margin-bottom: 60px; }
        .items-table th { text-align: left; font-weight: bold; font-size: 10pt; padding-bottom: 30px; color: <?php echo $text_primary; ?>; }
        .items-table th.cost-header { text-align: right; }
        .items-table td { vertical-align: top; padding-bottom: 10px; }
        .items-table td.item-description { text-align: left; font-size: 10pt; padding-right: 20px; }
        .items-table td.item-cost { text-align: right; font-weight: bold; font-size: 11pt; white-space: nowrap; color: <?php echo $text_primary; ?>; }

        .item-title { font-size: 11pt; font-weight: bold; color: <?php echo $text_primary; ?>; margin-bottom: 2px; }
        .item-period { font-size: 9pt; color: <?php echo $text_muted; ?>; margin-bottom: 8px; }
        .item-bullets { list-style: none; margin: 0; padding: 0; }
        .item-bullets li { font-size: 10pt; margin-bottom: 4px; color: <?php echo $text_secondary; ?>; padding-left: 16px; text-indent: -16px; }
        .item-bullets__dot { color: <?php echo $text_muted; ?>; padding-right: 8px; }
        .item-spacer { height: 25px; }

        .totals-section { margin-top: 60px; margin-bottom: 30px; line-height: 1.4; color: <?php echo $text_secondary; ?>; }
        .total-line { font-size: 11pt; font-weight: bold; color: <?php echo $text_primary; ?>; margin-bottom: 6px; }
        .acceptance-terms { font-size: 10pt; margin-bottom: 0; }
        .kleinunternehmer { font-size: 9pt; color: <?php echo $text_muted; ?>; margin-bottom: 0; }

        .bank-section { position: fixed; bottom: 80px; left: 80px; }
        .bank-name { font-weight: bold; font-size: 10pt; margin-bottom: 2px; color: <?php echo $text_primary; ?>; }
        .bank-details { font-size: 10pt; line-height: 1.4; color: <?php echo $text_secondary; ?>; }
    </style>
</head>
<body>
    <div class="invoice-container">
        <!-- Company Info -->
        <div class="company-info">
            <?php if (!empty($logo_data_uri)): $logo_h = array('small' => 34, 'medium' => 48, 'large' => 66); ?>
            <img src="<?php echo $logo_data_uri; ?>" alt="" style="height: <?php echo ($logo_h[$logo_size] ?? 48); ?>px; width: auto; max-width: 240px; display: block; margin-bottom: 8px;" />
            <?php endif; ?>
            <div class="company-name"><?php echo esc_html($settings['company_name'] ?? ''); ?></div>
            <div class="company-details">
                <?php if (!empty($settings['steuernummer'])): ?>Steuernummer: <?php echo esc_html($settings['steuernummer']); ?><br><br><?php endif; ?>
                <?php echo esc_html($settings['company_email'] ?? ''); ?><br>
                <?php echo esc_html($settings['company_phone'] ?? ''); ?>
            </div>
        </div>

        <!-- Offer Info -->
        <div class="invoice-section">
            <div class="invoice-label">ANGEBOT</div>
            <div class="invoice-number"><?php echo esc_html($offer['offer_number']); ?></div>
            <div class="invoice-date"><?php echo esc_html($formatted_date); ?></div>
            <?php if ($formatted_valid_until !== ''): ?>
            <div class="invoice-validity">Gültig bis: <?php echo esc_html($formatted_valid_until); ?></div>
            <?php endif; ?>
        </div>

        <!-- Client Info -->
        <div class="client-section">
            <div class="client-name"><?php echo esc_html($client['company_name']); ?></div>
            <div class="client-address">
                <?php if (!empty($client['address_line1'])): ?>
                    <?php echo esc_html($client['address_line1']); ?>, <?php echo esc_html($client['postal_code'] ?? ''); ?><br>
                <?php endif; ?>
                <?php echo esc_html($client['city'] ?? ''); ?><?php if (!empty($client['city']) && $client_country_label !== ''): ?>, <?php echo esc_html($client_country_label); ?><?php endif; ?>
            </div>
        </div>

        <!-- Items Table -->
        <table class="items-table">
            <thead>
                <tr>
                    <th>Pos.</th>
                    <th class="cost-header">Betrag</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $item_count = count($items);
                $current_item = 0;
                foreach ($items as $item):
                $current_item++;
                ?>
                <tr>
                    <td class="item-description">
                        <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; ?>
                        <?php if ($current_item < $item_count): ?>
                        <div class="item-spacer"></div>
                        <?php endif; ?>
                    </td>
                    <td class="item-cost"><?php echo mk_format_currency($item['total']); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <!-- Totals + Acceptance -->
        <div class="totals-section">
            <div class="total-line">Angebotssumme: <?php echo mk_format_currency($offer['total']); ?></div>
            <div class="acceptance-terms">Bei Annahme dieses Angebots erstellen wir Ihnen eine Rechnung gemäß § 14 UStG.</div>
            <div class="kleinunternehmer"><?php echo esc_html($settings['kleinunternehmer_text'] ?? 'Gemäß § 19 UStG. Abs. 1 wird keine Umsatzsteuer berechnet.'); ?></div>
        </div>

        <!-- Bank Info -->
        <div class="bank-section">
            <div class="bank-name"><?php echo esc_html($settings['bank_name'] ?? ''); ?></div>
            <div class="bank-details">
                <?php echo esc_html($settings['company_owner'] ?? ''); ?><br>
                IBAN: <?php echo esc_html($settings['bank_iban'] ?? ''); ?><br>
                BIC: <?php echo esc_html($settings['bank_bic'] ?? ''); ?>
            </div>
        </div>
    </div>
</body>
</html>
