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

$text_settings = \EasyInvoice\Helpers\TemplateTextHelper::getInvoiceTextSettings();
$company_info = \EasyInvoice\Helpers\TemplateTextHelper::getCompanyInfo();
?>

<style>
/* Legacy Template Styles - Clean Minimalist Design */
.template-legacy {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    max-width: 800px;
    margin: 0 auto;
    padding: 40px;
    background: #ffffff;
    color: #000000;
    line-height: 1.6;
    font-weight: 400;
}

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

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

.template-legacy .company-logo {
    margin-bottom: 1rem;
}

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

.template-legacy .company-name {
    font-size: 22px;
    font-weight: 800;
    color: #2563eb;
    margin-bottom: 8px;
    letter-spacing: -0.025em;
}

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

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

.template-legacy .invoice-title {
    font-size: 20px;
    font-weight: 700;
    color: #111827;
    margin-bottom: 5px;
}

.template-legacy .invoice-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: 14px;
}

.template-legacy .meta-value {
    color: #111827;
    font-size: 14px;
}

.template-legacy .total-due {
    font-weight: 700;
    font-size: 16px;
    color: #111827;
}

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

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

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

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

.template-legacy .client-name {
    font-weight: 600;
    color: #111827;
    margin-bottom: 4px;
}

/* Description Section */
.template-legacy .description-section {
    margin-bottom: 30px;
    padding: 16px;
    background: #f9fafb;
    border-radius: 8px;
    border-left: 4px solid #e5e7eb;
}

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

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

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

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

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

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

.template-legacy .invoice-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 .invoice-items tbody tr:hover {
    background-color: #f9fafb;
}

.template-legacy .item-name .item-title {
    font-weight: 600;
    color: #111827;
    margin-bottom: 4px;
}

.template-legacy .item-name .item-description {
    color: #6b7280;
    font-size: 13px;
}

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

/* Invoice Totals */
.template-legacy .invoice-totals {
    margin-left: auto;
    width: 300px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    overflow: hidden;
    background: #ffffff;
}

.template-legacy .invoice-total-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid #e5e7eb;
}

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

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

.template-legacy .invoice-total-value {
    color: #111827;
    font-size: 14px;
}

.template-legacy .invoice-grand-total {
    background: #f9fafb;
    border-top: 1px solid #e5e7eb;
    margin-top: 0;
    padding-top: 12px;
}

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

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

.template-legacy .invoice-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="invoice-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="invoice-title-section">
            <div class="invoice-title"><?php echo esc_html($invoice->getTitle() ?: __('Invoice', 'easy-invoice')); ?></div>
            <div class="invoice-meta">
                <div class="meta-item">
                    <span class="meta-label"><?php echo esc_html($text_settings['invoice_number']); ?></span>
                    <span class="meta-value"><?php echo esc_html($invoice->getNumber()); ?></span>
                </div>
                <div class="meta-item">
                    <span class="meta-label"><?php echo esc_html($text_settings['invoice_date']); ?></span>
                    <span class="meta-value"><?php echo esc_html(\EasyInvoice\Controllers\SettingsController::formatDate($invoice->getIssueDate())); ?></span>
                </div>
                <div class="meta-item">
                    <span class="meta-label"><?php echo esc_html($text_settings['due_date']); ?></span>
                    <span class="meta-value"><?php echo esc_html(\EasyInvoice\Controllers\SettingsController::formatDate($invoice->getDueDate())); ?></span>
                </div>
                <div class="meta-item">
                    <span class="meta-label"><?php echo esc_html($text_settings['total_due']); ?></span>
                    <span class="meta-value total-due"><?php echo esc_html($formatter->format($invoice->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($invoice->getCustomerName()); ?></div>
                <?php if ($invoice->getCustomerAddress()): ?>
                    <?php echo nl2br(esc_html($invoice->getCustomerAddress())); ?><br>
                <?php endif; ?>
                <?php if ($invoice->getCustomerEmail()): ?>
                    <?php echo esc_html($invoice->getCustomerEmail()); ?><br>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Description Section -->
    <?php if ($invoice->getNotes()): ?>
    <div class="description-section">
        <div class="description-content">
            <?php echo nl2br(esc_html($invoice->getNotes())); ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Invoice Items -->
    <table class="invoice-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::shouldShowInvoiceAdjustField()): ?>
                <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 ($invoice->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::shouldShowInvoiceAdjustField()): ?>
                <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>

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

    <!-- Footer -->
    <div class="invoice-footer">
        <p>Payment is due within <?php echo esc_html($invoice->getDueDays() ?: 30); ?> days from date of invoice.</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>
