<?php
/**
 * Legacy Quote Template
 * Clean, minimalist design with traditional business layout
 */

$text_settings = \EasyInvoice\Helpers\TemplateTextHelper::getQuoteTextSettings();
$company_info = \EasyInvoice\Helpers\TemplateTextHelper::getCompanyInfo();
// Ensure a formatter is available for currency formatting
if (!isset($formatter) || !$formatter) {
    $formatter = new \EasyInvoice\Helpers\QuoteFormatter($quote ?? null);
}
?>

<style>
/* Legacy Template Styles - Clean Minimalist Design (Older Version Style) */
.template-legacy {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    max-width: 800px;
    margin: 0 auto;
    padding: 40px;
    background: #ffffff;
    color: #000000;
    line-height: 1.5;
}

/* Header Section */
.template-legacy .quote-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.template-legacy .company-info {
    flex: 1;
}

.template-legacy .company-logo {
    margin-bottom: 5px;
}

.template-legacy .logo-image {
    max-height: 60px;
    max-width: 200px;
    object-fit: contain;
}

.template-legacy .company-name {
    font-size: 20px;
    font-weight: 700;
    color: #2563eb;
    margin-bottom: 8px;
}

.template-legacy .company-details {
    color: #333333;
    font-size: 14px;
    line-height: 1.4;
}

.template-legacy .quote-title-section {
    text-align: right;
    flex: 1;
}

.template-legacy .quote-title {
    font-size: 20px;
    font-weight: 700;
    color: #000000;
    margin-bottom: 20px;
}

.template-legacy .quote-meta {
    border: 1px solid #d1d5db;
    background: #ffffff;
    width: 280px;
    margin-left: auto;
}

.template-legacy .meta-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid #d1d5db;
}

.template-legacy .meta-item:last-child {
    border-bottom: none;
}

.template-legacy .meta-label {
    font-weight: 600;
    color: #000000;
    font-size: 13px;
}

.template-legacy .meta-value {
    color: #000000;
    font-size: 13px;
    font-weight: 500;
}

.template-legacy .total-due {
    font-weight: 700;
    color: #000000;
}

/* Address Section */
.template-legacy .address-section {
    margin-bottom: 20px;
}

.template-legacy .address-block {
    margin-bottom: 20x;
}

.template-legacy .address-block h2 {
    font-size: 16px;
    font-weight: 700;
    color: #000000;
    margin-bottom: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.template-legacy .address-content {
    color: #333333;
    font-size: 14px;
    line-height: 1.4;
}

.template-legacy .client-name {
    font-weight: 600;
    color: #2563eb;
    margin-bottom: 6px;
    text-decoration: underline;
}

/* Description Section */
.template-legacy .description-section {
    margin-bottom: 20px;
    padding: 20px;
    background: #f8f9fa;
    border-radius: 6px;
    border-left: 4px solid #e5e7eb;
}

.template-legacy .description-content {
    color: #333333;
    font-size: 14px;
    line-height: 1.5;
}

/* Quote Items Table */
.template-legacy .quote-items {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 20px;
    border: 1px solid #d1d5db;
}

.template-legacy .quote-items thead {
    background: #f3f4f6;
}

.template-legacy .quote-items th {
    padding: 16px 20px;
    text-align: left;
    font-weight: 700;
    color: #000000;
    font-size: 13px;
    border: 1px solid #d1d5db;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.template-legacy .quote-items td {
    padding: 16px 20px;
    border: 1px solid #d1d5db;
    font-size: 14px;
    vertical-align: top;
    color: #000000;
}

/* PDF-specific border fixes to prevent 2px borders */
.template-legacy .quote-items th,
.template-legacy .quote-items td {
    border-width: 1px !important;
    border-style: solid !important;
    border-color: #d1d5db !important;
}

.template-legacy .quote-meta {
    border-width: 1px !important;
    border-style: solid !important;
    border-color: #d1d5db !important;
}

.template-legacy .meta-item {
    border-bottom-width: 1px !important;
    border-bottom-style: solid !important;
    border-bottom-color: #d1d5db !important;
}

.template-legacy .quote-items tr:last-child td {
    border-bottom: none;
}

.template-legacy .quote-items tbody tr:hover {
    background-color: #f8f9fa;
}

.template-legacy .item-name .item-title {
    font-weight: 700;
    color: #000000;
    margin-bottom: 6px;
}

.template-legacy .item-name .item-description {
    color: #666666;
    font-size: 13px;
    line-height: 1.4;
}

.template-legacy .item-quantity,
.template-legacy .item-price,
.template-legacy .item-adjust,
.template-legacy .item-total {
    text-align: right;
}

/* Quote Totals */
.template-legacy .quote-totals {
    margin-left: auto;
    width: 320px;
    border: 1px solid #dddddd;
    border-radius: 4px;
    overflow: hidden;
    background: #ffffff;
}

.template-legacy .quote-total-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px;
    border-bottom: 1px solid #dddddd;
}

.template-legacy .quote-total-row:last-child {
    border-bottom: none;
}

.template-legacy .quote-total-label {
    font-weight: 600;
    color: #333333;
    font-size: 14px;
}

.template-legacy .quote-total-value {
    color: #000000;
    font-size: 14px;
    font-weight: 500;
}

.template-legacy .quote-grand-total {
    background: #f8f9fa;
    border-top: 1px solid #dddddd;
    margin-top: 0;
    padding-top: 14px;
}

.template-legacy .quote-grand-total .quote-total-label,
.template-legacy .quote-grand-total .quote-total-value {
    font-weight: 700;
    font-size: 16px;
    color: #000000;
}

/* Terms Section */
.template-legacy .terms-section {
    margin-top: 20px;
    padding: 20px;
    background: #f9fafb;
    border-radius: 8px;
    border-left: 4px solid #e5e7eb;
}

.template-legacy .terms-section h3 {
    font-size: 16px;
    font-weight: 600;
    color: #374151;
    margin-bottom: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.template-legacy .terms-content {
    color: #6b7280;
    font-size: 14px;
    line-height: 1.6;
}

/* Footer */
.template-legacy .quote-footer {
    margin-top: 20px;
    border-top: 1px solid #e5e7eb;
    text-align: center;
}

.template-legacy .quote-footer p {
    color: #6b7280;
    font-size: 14px;
    margin-bottom: 16px;
}

.template-legacy .footer-company {
    color: #9ca3af;
    font-size: 13px;
}
</style>

<div class="template-legacy">
    <!-- Header Section -->
    <div class="quote-header">
        <div class="company-info">
            <div class="company-logo">
                <?php if (!empty($company_info['logo'])): ?>
                    <img src="<?php echo esc_url($company_info['logo']); ?>" alt="<?php echo esc_attr($company_info['name']); ?>" class="logo-image">
                <?php endif; ?>
            </div>
            <div class="company-name"><?php echo esc_html($company_info['name']); ?></div>
            <div class="company-details">
                <?php echo \EasyInvoice\Helpers\TemplateTextHelper::generateFromSection($company_info, false, 'details-only'); ?>
            </div>
        </div>
        
        <div class="quote-title-section">
            <div class="quote-title"><?php echo esc_html($quote->getTitle() ?: __('Quote', 'easy-invoice')); ?></div>
            <div class="quote-meta">
                <div class="meta-item">
                    <span class="meta-label"><?php echo esc_html($text_settings['quote_number']); ?></span>
                    <span class="meta-value"><?php echo esc_html($quote->getNumber()); ?></span>
                </div>
                <div class="meta-item">
                    <span class="meta-label"><?php echo esc_html($text_settings['quote_date']); ?></span>
                    <span class="meta-value"><?php echo esc_html(\EasyInvoice\Controllers\SettingsController::formatDate($quote->getIssueDate())); ?></span>
                </div>
                <div class="meta-item">
                    <span class="meta-label"><?php echo esc_html($text_settings['valid_until']); ?></span>
                    <span class="meta-value"><?php echo esc_html(\EasyInvoice\Controllers\SettingsController::formatDate($quote->getExpiryDate())); ?></span>
                </div>
                <div class="meta-item">
                    <span class="meta-label"><?php echo esc_html($text_settings['total']); ?></span>
                    <span class="meta-value total-due"><?php echo esc_html($formatter->format($quote->getTotal())); ?></span>
                </div>
            </div>
        </div>
    </div>

    <!-- Address Section -->
    <div class="address-section">
        <div class="address-block">
            <h2><?php echo esc_html($text_settings['to']); ?></h2>
            <div class="address-content">
                <div class="client-name"><?php echo esc_html($quote->getCustomerName()); ?></div>
                <?php if ($quote->getCustomerAddress()): ?>
                    <?php echo nl2br(esc_html($quote->getCustomerAddress())); ?><br>
                <?php endif; ?>
                <?php if ($quote->getCustomerEmail()): ?>
                    <?php echo esc_html($quote->getCustomerEmail()); ?><br>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Description Section -->
    <?php if ($quote->getDescription()): ?>
    <div class="description-section">
        <div class="description-content"><?php echo wp_kses_post($quote->getDescription()); ?></div>
    </div>
    <?php endif; ?>

    <!-- Quote Items -->
    <table class="quote-items">
        <thead>
            <tr>
                <th><?php echo esc_html($text_settings['service']); ?></th>
                <th><?php echo esc_html($text_settings['qty']); ?></th>
                <th><?php echo esc_html($text_settings['rate_price']); ?></th>
                <?php if (\EasyInvoice\Controllers\SettingsController::shouldShowQuoteAdjustField()): ?>
                <th><?php echo esc_html($text_settings['adjust']); ?> (%)</th>
                <?php endif; ?>
                <th><?php echo esc_html($text_settings['total']); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($quote->getItems() as $item): ?>
            <tr>
                <td class="item-name">
                    <div class="item-title"><?php echo esc_html($item->getName()); ?></div>
                    <?php if ($item->getDescription()): ?>
                        <div class="item-description"><?php echo esc_html($item->getDescription()); ?></div>
                    <?php endif; ?>
                </td>
                <td class="item-quantity"><?php echo esc_html($item->getQuantity() ?: '0'); ?></td>
                <td class="item-price"><?php echo esc_html($formatter->format($item->getPrice())); ?></td>
                <?php if (\EasyInvoice\Controllers\SettingsController::shouldShowQuoteAdjustField()): ?>
                <td class="item-adjust"><?php 
                    $adjust_percentage = $item->getAdjustPercentage();
                    if ($adjust_percentage != 0) {
                        echo esc_html($adjust_percentage > 0 ? '+' : '') . esc_html($adjust_percentage) . '%';
                    } else {
                        echo esc_html__('—', 'easy-invoice');
                    }
                ?></td>
                <?php endif; ?>
                <td class="item-total"><?php echo esc_html($formatter->format($item->getAmount())); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- Quote Totals -->
    <div class="quote-totals">
        <?php echo \EasyInvoice\Helpers\TemplateTextHelper::generateTotalsSection($quote, $text_settings, $formatter, 'quote'); ?>
    </div>

    <!-- Terms Section -->
    <?php 
    $terms_conditions = $quote->getTerms() ?: \EasyInvoice\Controllers\SettingsController::getQuoteTermsConditions();
    if ($terms_conditions): 
    ?>
    <div class="terms-section">
        <h3>Terms & Conditions</h3>
        <div class="terms-content">
            <?php echo wp_kses_post($terms_conditions); ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Footer -->
    <div class="quote-footer">
        <p>This quote is valid for 30 days from the date of issue.</p>
        <div class="footer-company">
            Thanks for choosing <?php echo esc_html($company_info['name']); ?> | <?php echo esc_html($company_info['email'] ?? 'temp@example.com'); ?>
        </div>
    </div>
</div>
