<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
    body { font-family: 'DejaVu Sans', sans-serif; direction: rtl; font-size: 13px; color: #222; }
    table { width: 100%; border-collapse: collapse; }
    .header-table td { padding: 8px 0; vertical-align: top; }
    .company-name { font-size: 20px; font-weight: bold; }
    .muted { color: #666; font-size: 11px; }
    .status-badge { display:inline-block; padding:3px 10px; border-radius:10px; font-size:11px; }
    .items-table { margin-top: 20px; }
    .items-table th { background: #f0f0f0; padding: 8px; text-align: right; border-bottom: 2px solid #ccc; }
    .items-table td { padding: 8px; border-bottom: 1px solid #eee; }
    .totals { margin-top: 15px; width: 45%; float: left; }
    .totals td { padding: 6px 0; }
    .footer-note { margin-top: 60px; font-size: 10px; color: #999; border-top: 1px dashed #ccc; padding-top: 10px; }
    .payments-table th, .payments-table td { padding: 6px; font-size: 11px; }
</style>
</head>
<body>

<table class="header-table">
    <tr>
        <td style="width:15%;">
            <?php if (!empty($company['logo'])): ?>
                <img src="<?= base_url('uploads/logos/' . $company['logo']) ?>" style="max-height:60px;max-width:100%;">
            <?php endif; ?>
        </td>
        <td style="width:45%;">
            <div class="company-name"><?= esc($company['name']) ?></div>
            <div class="muted"><?= esc($company['address'] ?? '') ?></div>
            <div class="muted"><?= esc($company['phone'] ?? '') ?> <?= $company['email'] ? ' | ' . esc($company['email']) : '' ?></div>
            <?php if (!empty($company['tax_number'])): ?>
                <div class="muted">الرقم الضريبي: <?= esc($company['tax_number']) ?></div>
            <?php endif; ?>
        </td>
        <td style="width:40%; text-align:left;">
            <div style="font-size:16px;font-weight:bold;">فاتورة #<?= esc($invoice['invoice_number']) ?></div>
            <div class="muted">تاريخ الإصدار: <?= esc($invoice['issue_date']) ?></div>
            <div class="muted">تاريخ الاستحقاق: <?= esc($invoice['due_date']) ?></div>
            <div class="muted">الحالة: <?= lang('App.' . $invoice['status']) ?></div>
        </td>
    </tr>
</table>

<?php if (!empty($company['invoice_header_note'])): ?>
    <div style="background:#f7f7f7;padding:8px 12px;margin-top:8px;font-size:11px;color:#555;"><?= nl2br(esc($company['invoice_header_note'])) ?></div>
<?php endif; ?>

<hr>

<div><b>فاتورة إلى:</b> <?= esc($client['name'] ?? '-') ?> - <?= esc($client['email'] ?? '') ?> - <?= esc($client['phone'] ?? '') ?></div>

<table class="items-table">
    <thead><tr><th>الوصف</th><th style="text-align:left;">المبلغ</th></tr></thead>
    <tbody>
    <?php foreach ($items as $item): ?>
        <tr><td><?= esc($item['description']) ?></td><td style="text-align:left;"><?= number_format($item['amount'], 2) ?></td></tr>
    <?php endforeach; ?>
    </tbody>
</table>

<table class="totals">
    <tr><td>الإجمالي</td><td style="text-align:left;"><?= number_format($invoice['amount'], 2) ?> <?= esc($invoice['currency']) ?></td></tr>
    <tr><td>المدفوع</td><td style="text-align:left;"><?= number_format($totalPaid, 2) ?> <?= esc($invoice['currency']) ?></td></tr>
    <tr><td><b>المتبقي</b></td><td style="text-align:left;"><b><?= number_format($invoice['amount'] - $totalPaid, 2) ?> <?= esc($invoice['currency']) ?></b></td></tr>
</table>
<div style="clear:both;"></div>

<?php if (!empty($invoice['notes'])): ?>
    <p><b>ملاحظات:</b> <?= nl2br(esc($invoice['notes'])) ?></p>
<?php endif; ?>

<div class="footer-note">
    حرر بواسطة: <?= esc($creator['name'] ?? 'غير محدد') ?>
    — تاريخ الإنشاء: <?= esc(date('Y-m-d H:i', strtotime($invoice['created_at'] ?? 'now'))) ?>
</div>

<?php if (!empty($company['invoice_footer_note'])): ?>
    <div style="margin-top:15px;font-size:11px;color:#555;text-align:center;border-top:1px dashed #ccc;padding-top:10px;">
        <?= nl2br(esc($company['invoice_footer_note'])) ?>
    </div>
<?php endif; ?>

</body>
</html>
