<?php
defined('ABSPATH') || exit;
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body style="font-family: DejaVu Sans, Arial, sans-serif; font-size: 11px; color: #333; margin: 0; padding: 30px;">
<div style="display: table; width: 100%; margin-bottom: 30px;">
    <div style="display: table-cell; vertical-align: top; width: 50%;">
        <div style="font-size: 16px; font-weight: bold; color: #2c3e50;"><?php echo esc_html($company['company_name'] ?? ''); ?></div>
        <div style="margin: 15px 0; line-height: 1.6;">
            <?php echo esc_html($company['address'] ?? ''); ?><br>
            <?php echo esc_html($company['city'] ?? ''); ?> <?php echo esc_html($company['postcode'] ?? ''); ?><br>
            <?php if (!empty($company['company_email'])) : ?>
                <?php echo esc_html($company['company_email']); ?><br>
            <?php endif; ?>
            <?php if (!empty($company['company_phone'])) : ?>
                <?php echo esc_html($company['company_phone']); ?>
            <?php endif; ?>
        </div>
        <?php if (!empty($company['company_number'])) : ?>
            <div><?php echo esc_html__('Company No:', 'comptaflow-uk-by-meetempo'); ?> <?php echo esc_html($company['company_number']); ?></div>
        <?php endif; ?>
        <?php if (!empty($company['vat_number'])) : ?>
            <div><?php echo esc_html__('VAT No:', 'comptaflow-uk-by-meetempo'); ?> <?php echo esc_html($company['vat_number']); ?></div>
        <?php endif; ?>
    </div>
    <div style="display: table-cell; vertical-align: top; width: 50%; text-align: right;">
        <h1 style="font-size: 22px; color: #2c3e50; margin: 0 0 5px 0;"><?php echo esc_html__('INVOICE', 'comptaflow-uk-by-meetempo'); ?></h1>
        <div style="font-size: 14px; color: #7f8c8d;"><?php echo esc_html($invoice['invoice_number']); ?></div>
    </div>
</div>

<div style="margin: 15px 0; line-height: 1.6; background: #f8f9fa; padding: 12px; border-left: 4px solid #2c3e50;">
    <strong><?php echo esc_html__('Bill To:', 'comptaflow-uk-by-meetempo'); ?></strong><br>
    <?php if (!empty($client)) : ?>
        <?php echo esc_html(!empty($client['company_name']) ? $client['company_name'] : ($client['contact_name'] ?? '')); ?><br>
        <?php if (!empty($client['address_line_1'])) : ?><?php echo esc_html($client['address_line_1']); ?><br><?php endif; ?>
        <?php if (!empty($client['city'])) : ?><?php echo esc_html($client['city']); ?> <?php endif; ?>
        <?php echo esc_html($client['postcode'] ?? ''); ?><br>
        <?php if (!empty($client['vat_number'])) : ?>
            <?php echo esc_html__('VAT No:', 'comptaflow-uk-by-meetempo'); ?> <?php echo esc_html($client['vat_number']); ?>
        <?php endif; ?>
    <?php endif; ?>
</div>

<table style="width: 100%; margin-bottom: 20px;">
<tr><td style="padding: 4px 8px; font-weight: bold; width: 140px; background: #f8f9fa;"><?php echo esc_html__('Invoice Date:', 'comptaflow-uk-by-meetempo'); ?></td><td style="padding: 4px 8px;"><?php echo esc_html(date_i18n('d/m/Y', strtotime($invoice['issue_date']))); ?></td></tr>
<?php if (!empty($invoice['due_date'])) : ?>
<tr><td style="padding: 4px 8px; font-weight: bold; width: 140px; background: #f8f9fa;"><?php echo esc_html__('Due Date:', 'comptaflow-uk-by-meetempo'); ?></td><td style="padding: 4px 8px;"><?php echo esc_html(date_i18n('d/m/Y', strtotime($invoice['due_date']))); ?></td></tr>
<?php endif; ?>
<tr><td style="padding: 4px 8px; font-weight: bold; width: 140px; background: #f8f9fa;"><?php echo esc_html__('Payment Terms:', 'comptaflow-uk-by-meetempo'); ?></td><td style="padding: 4px 8px;"><?php echo esc_html($invoice['payment_terms'] ?? 30); ?> <?php echo esc_html__('days', 'comptaflow-uk-by-meetempo'); ?></td></tr>
</table>

<table style="width: 100%; border-collapse: collapse; margin-bottom: 20px;">
<thead>
<tr>
    <th style="width: 40%; background: #2c3e50; color: white; padding: 8px; text-align: left; font-size: 10px;"><?php echo esc_html__('Description', 'comptaflow-uk-by-meetempo'); ?></th>
    <th style="background: #2c3e50; color: white; padding: 8px; text-align: right; font-size: 10px;"><?php echo esc_html__('Qty', 'comptaflow-uk-by-meetempo'); ?></th>
    <th style="background: #2c3e50; color: white; padding: 8px; text-align: right; font-size: 10px;"><?php echo esc_html__('Unit Price', 'comptaflow-uk-by-meetempo'); ?></th>
    <th style="background: #2c3e50; color: white; padding: 8px; text-align: right; font-size: 10px;"><?php echo esc_html__('VAT %', 'comptaflow-uk-by-meetempo'); ?></th>
    <th style="background: #2c3e50; color: white; padding: 8px; text-align: right; font-size: 10px;"><?php echo esc_html__('Net', 'comptaflow-uk-by-meetempo'); ?></th>
    <th style="background: #2c3e50; color: white; padding: 8px; text-align: right; font-size: 10px;"><?php echo esc_html__('VAT', 'comptaflow-uk-by-meetempo'); ?></th>
    <th style="background: #2c3e50; color: white; padding: 8px; text-align: right; font-size: 10px;"><?php echo esc_html__('Gross', 'comptaflow-uk-by-meetempo'); ?></th>
</tr>
</thead>
<tbody>
<?php $line_index = 0; foreach ($lines as $line) : $line_bg = ($line_index % 2 === 1) ? ' background: #f8f9fa;' : ''; ?>
<tr>
    <td style="padding: 8px; border-bottom: 1px solid #eee;<?php echo esc_attr($line_bg); ?>"><?php echo esc_html($line['description']); ?></td>
    <td style="padding: 8px; border-bottom: 1px solid #eee; text-align: right;<?php echo esc_attr($line_bg); ?>"><?php echo esc_html(number_format(floatval($line['quantity']), 2)); ?></td>
    <td style="padding: 8px; border-bottom: 1px solid #eee; text-align: right;<?php echo esc_attr($line_bg); ?>">&pound;<?php echo esc_html(number_format(floatval($line['unit_price']), 2)); ?></td>
    <td style="padding: 8px; border-bottom: 1px solid #eee; text-align: right;<?php echo esc_attr($line_bg); ?>"><?php echo esc_html(number_format(floatval($line['vat_rate']), 1)); ?>%</td>
    <td style="padding: 8px; border-bottom: 1px solid #eee; text-align: right;<?php echo esc_attr($line_bg); ?>">&pound;<?php echo esc_html(number_format(floatval($line['line_total']), 2)); ?></td>
    <td style="padding: 8px; border-bottom: 1px solid #eee; text-align: right;<?php echo esc_attr($line_bg); ?>">&pound;<?php echo esc_html(number_format(floatval($line['vat_amount']), 2)); ?></td>
    <td style="padding: 8px; border-bottom: 1px solid #eee; text-align: right;<?php echo esc_attr($line_bg); ?>">&pound;<?php echo esc_html(number_format(floatval($line['gross_total']), 2)); ?></td>
</tr>
<?php $line_index++; endforeach; ?>
</tbody>
</table>

<table style="width: 300px; margin-left: auto;">
<tr><td style="padding: 6px 8px;"><?php echo esc_html__('Subtotal:', 'comptaflow-uk-by-meetempo'); ?></td><td style="padding: 6px 8px; text-align: right;">&pound;<?php echo esc_html(number_format(floatval($invoice['subtotal']), 2)); ?></td></tr>
<tr><td style="padding: 6px 8px;"><?php echo esc_html__('Total VAT:', 'comptaflow-uk-by-meetempo'); ?></td><td style="padding: 6px 8px; text-align: right;">&pound;<?php echo esc_html(number_format(floatval($invoice['total_vat']), 2)); ?></td></tr>
<tr style="font-size: 14px; font-weight: bold; background: #2c3e50; color: white;"><td style="padding: 8px;"><?php echo esc_html__('Total Due:', 'comptaflow-uk-by-meetempo'); ?></td><td style="padding: 8px; text-align: right;">&pound;<?php echo esc_html(number_format(floatval($invoice['total']), 2)); ?></td></tr>
</table>

<?php if (!empty($company['sort_code']) || !empty($company['account_number'])) : ?>
<div style="background: #f0f7ff; border: 1px solid #c3d9ed; padding: 12px; margin-top: 15px; border-radius: 4px;">
    <strong><?php echo esc_html__('Payment Details:', 'comptaflow-uk-by-meetempo'); ?></strong><br>
    <?php if (!empty($company['sort_code'])) : ?>
        <?php echo esc_html__('Sort Code:', 'comptaflow-uk-by-meetempo'); ?> <?php echo esc_html($company['sort_code']); ?><br>
    <?php endif; ?>
    <?php if (!empty($company['account_number'])) : ?>
        <?php echo esc_html__('Account Number:', 'comptaflow-uk-by-meetempo'); ?> <?php echo esc_html($company['account_number']); ?><br>
    <?php endif; ?>
    <?php if (!empty($company['iban'])) : ?>
        <?php echo esc_html__('IBAN:', 'comptaflow-uk-by-meetempo'); ?> <?php echo esc_html($company['iban']); ?>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php if (!empty($invoice['notes'])) : ?>
<div style="margin-top: 15px;"><strong><?php echo esc_html__('Notes:', 'comptaflow-uk-by-meetempo'); ?></strong><br><?php echo esc_html($invoice['notes']); ?></div>
<?php endif; ?>

<?php if (!empty($invoice['terms'])) : ?>
<div style="margin-top: 10px;"><strong><?php echo esc_html__('Terms & Conditions:', 'comptaflow-uk-by-meetempo'); ?></strong><br><?php echo esc_html($invoice['terms']); ?></div>
<?php endif; ?>

<div style="margin-top: 30px; padding-top: 15px; border-top: 1px solid #ddd; font-size: 9px; color: #7f8c8d;">
    <?php echo esc_html($company['company_name'] ?? ''); ?>
    <?php if (!empty($company['registered_office'])) : ?> | <?php echo esc_html($company['registered_office']); ?><?php endif; ?>
    <?php if (!empty($company['company_number'])) : ?> | <?php echo esc_html__('Company No:', 'comptaflow-uk-by-meetempo'); ?> <?php echo esc_html($company['company_number']); ?><?php endif; ?>
    <?php if (!empty($company['vat_number'])) : ?> | <?php echo esc_html__('VAT No:', 'comptaflow-uk-by-meetempo'); ?> <?php echo esc_html($company['vat_number']); ?><?php endif; ?>
</div>
</body>
</html>
