<?php
/**
 * Standard Quote Template - Clean SaaS Design
 * Clean, modern, and professional quote template
 */

$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>
/* Standard Quote Template - Simple and Clean */
.template-standard {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    max-width: 800px;
    margin: 0 auto;
    padding: 2rem;
    background: #fff;
    color: #333;
    line-height: 1.5;
}

/* Header Section */
.template-standard .quote-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 2rem;
    padding-bottom: 1rem;
    border-bottom: 2px solid #eee;
}

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

.template-standard .logo-image {
    max-height: 60px;
    max-width: 200px;
    margin-bottom: 1rem;
}

.template-standard .company-name {
    font-size: 1.5rem;
    font-weight: bold;
    margin-bottom: 0.5rem;
}

.template-standard .company-details {
    font-size: 0.9rem;
    color: #666;
}

.template-standard .quote-meta {
    text-align: right;
    min-width: 200px;
}

.template-standard .quote-title {
    font-size: 1.5rem;
    font-weight: bold;
    margin-bottom: 1rem;
    color: #2563eb;
}

.template-standard .meta-item {
    margin-bottom: 0.5rem;
}

.template-standard .meta-label {
    color: #666;
    font-size: 0.9rem;
}

.template-standard .meta-value {
    font-weight: 600;
}

/* Address Section */
.template-standard .address-section {
    margin-bottom: 2rem;
}

.template-standard .address-block {
    background: #f8f9fa;
    padding: 1rem;
    border-radius: 4px;
}

.template-standard .address-block h2 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    text-transform: uppercase;
}

.template-standard .client-name {
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.template-standard .address-content {
    font-size: 0.9rem;
    color: #666;
}

/* Quote Description */
.template-standard .quote-description {
    margin-bottom: 2rem;
}

.template-standard .description-content {
    color: #666;
    white-space: pre-wrap;
}

/* Quote Items Table */
.template-standard .quote-items {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 2rem;
    border: 1px solid #eee;
}

.template-standard .quote-items th {
    background: #2563eb;
    color: white;
    text-align: left;
    padding: 0.75rem;
    font-size: 0.9rem;
}

.template-standard .quote-items td {
    padding: 0.75rem;
    border-bottom: 1px solid #eee;
}

.template-standard .item-name .item-title {
    font-weight: normal;
}

.template-standard .item-description {
    font-size: 0.9rem;
    color: #666;
    margin-top: 0.25rem;
    font-weight: 300;
}

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

/* Quote Totals */
.template-standard .quote-totals {
    margin-left: auto;
    width: 300px;
    margin-bottom: 2rem;
}

.template-standard .quote-total-row {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    border-bottom: 1px solid #eee;
}

.template-standard .quote-total-row:last-child {
    border-bottom: none;
    font-weight: bold;
    font-size: 1.1rem;
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    border-top: 2px solid #2563eb;
}

/* Notes and Terms */
.template-standard .quote-notes,
.template-standard .quote-terms {
    margin-top: 2rem;
    padding: 1rem;
    background: #f8f9fa;
    border-radius: 4px;
}

.template-standard .quote-notes h3,
.template-standard .quote-terms h3 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.template-standard .notes-content,
.template-standard .terms-content {
    font-size: 0.9rem;
    color: #666;
}

/* Footer */
.template-standard .quote-footer {
    margin-top: 2rem;
    padding-top: 1rem;
    text-align: center;
    border-top: 1px solid #eee;
    color: #666;
    font-size: 0.9rem;
}

/* Responsive Design */
@media (max-width: 768px) {
    .template-standard {
        padding: 1rem;
    }

    .template-standard .quote-header {
        flex-direction: column;
    }

    .template-standard .quote-meta {
        text-align: left;
        margin-top: 1rem;
    }

    .template-standard .quote-totals {
        width: 100%;
    }
}
</style>

<div class="template template-standard">
    <!-- Header -->
    <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-meta">
            <div class="quote-title"><?php echo esc_html($quote->getTitle() ?: __('Quote', 'easy-invoice')); ?></div>
            <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>
    </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>

    <!-- Quote Description -->
    <?php if ($quote->getDescription()): ?>
    <div class="quote-description">
        <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>

    <!-- Notes -->
    <?php if ($quote->getNotes()): ?>
    <div class="quote-notes">
        <h3><?php _e('Notes:', 'easy-invoice'); ?></h3>
        <div class="notes-content">
            <?php echo wp_kses_post($quote->getNotes() ?: ''); ?>
        </div>
    </div>
    <?php endif; ?>

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

    <!-- Footer -->
    <div class="quote-footer">
        <?php
        $footer_text = $quote->getFooterText();
        if (!$footer_text) {
            $footer_text = \EasyInvoice\Controllers\SettingsController::getQuoteFooterText();
        }
        ?>
        <p><?php echo wp_kses_post($footer_text); ?></p>
    </div>
</div>
