<?= $this->extend('admin/layout/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h3>فاتورة <?= esc($invoice['invoice_number']) ?></h3>
    <div>
        <a href="/admin/invoices/<?= $invoice['id'] ?>/pdf" class="btn btn-dark btn-sm">⬇️ تحميل PDF</a>
        <button onclick="window.print()" class="btn btn-outline-secondary btn-sm">🖨️ طباعة</button>
        <a href="/admin/invoices/<?= $invoice['id'] ?>/edit" class="btn btn-outline-primary btn-sm"><?= lang('App.edit') ?></a>
        <a href="/admin/invoices" class="btn btn-light btn-sm">رجوع</a>
    </div>
</div>

<style>
    @media print {
        .navbar, .sidebar, .btn, nav { display: none !important; }
        body { background: #fff !important; }
    }
    .invoice-card {
        background: #fff;
        border-radius: 12px;
        box-shadow: 0 2px 12px rgba(0,0,0,.08);
        overflow: hidden;
    }
    .invoice-header {
        background: linear-gradient(135deg, #1e1e2d 0%, #2b2b40 100%);
        color: #fff;
        padding: 32px 40px;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .invoice-header .logo-box {
        width: 64px; height: 64px; border-radius: 12px;
        background: rgba(255,255,255,.15);
        display: flex; align-items: center; justify-content: center;
        font-size: 28px; font-weight: bold;
    }
    .invoice-status {
        display: inline-block; padding: 6px 18px; border-radius: 20px;
        font-weight: bold; font-size: .85rem;
    }
    .status-paid { background: #d1f7e0; color: #0a7a3d; }
    .status-sent { background: #fff3cd; color: #856404; }
    .status-overdue { background: #f8d7da; color: #842029; }
    .status-draft, .status-cancelled { background: #e9ecef; color: #495057; }
    .invoice-body { padding: 40px; }
    .items-table th { background: #f8f9fa; font-weight: 600; font-size: .85rem; }
    .totals-box { background: #f8f9fa; border-radius: 10px; padding: 20px 24px; }
    .creator-note { font-size: .8rem; color: #999; border-top: 1px dashed #ddd; padding-top: 16px; margin-top: 24px; }
</style>

<div class="invoice-card">
    <div class="invoice-header">
        <div class="d-flex align-items-center gap-3">
            <?php if (!empty($company['logo'])): ?>
                <img src="<?= base_url('uploads/logos/' . $company['logo']) ?>" alt="شعار الشركة" style="height:64px;width:64px;object-fit:contain;border-radius:12px;background:#fff;padding:4px;">
            <?php else: ?>
                <div class="logo-box">🏢</div>
            <?php endif; ?>
            <div>
                <h4 class="mb-0"><?= esc($company['name']) ?></h4>
                <div class="small opacity-75">
                    <?= esc($company['address'] ?? '') ?>
                    <?= $company['phone'] ? ' | ' . esc($company['phone']) : '' ?>
                    <?= $company['email'] ? ' | ' . esc($company['email']) : '' ?>
                </div>
                <?php if (!empty($company['tax_number'])): ?>
                    <div class="small opacity-75">الرقم الضريبي: <?= esc($company['tax_number']) ?></div>
                <?php endif; ?>
            </div>
        </div>
        <div class="text-end">
            <div class="fs-4 fw-bold">#<?= esc($invoice['invoice_number']) ?></div>
            <span class="invoice-status status-<?= esc($invoice['status']) ?>"><?= lang('App.' . $invoice['status']) ?></span>
        </div>
    </div>

    <?php if (!empty($company['invoice_header_note'])): ?>
        <div class="px-4 py-2 small text-muted border-bottom bg-light"><?= nl2br(esc($company['invoice_header_note'])) ?></div>
    <?php endif; ?>

    <div class="invoice-body">
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="text-muted small mb-1">فاتورة إلى</div>
                <div class="fw-bold fs-5"><?= esc($client['name'] ?? '-') ?></div>
                <div class="text-muted small"><?= esc($client['email'] ?? '') ?></div>
                <div class="text-muted small"><?= esc($client['phone'] ?? '') ?></div>
            </div>
            <div class="col-md-6 text-md-end">
                <div class="text-muted small">تاريخ الإصدار: <b><?= esc($invoice['issue_date']) ?></b></div>
                <div class="text-muted small">تاريخ الاستحقاق: <b><?= esc($invoice['due_date']) ?></b></div>
                <div class="text-muted small">العملة: <b><?= esc($invoice['currency']) ?></b></div>
            </div>
        </div>

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

        <div class="row justify-content-end">
            <div class="col-md-5">
                <div class="totals-box">
                    <div class="d-flex justify-content-between mb-2">
                        <span>الإجمالي</span><b><?= number_format($invoice['amount'], 2) ?> <?= esc($invoice['currency']) ?></b>
                    </div>
                    <div class="d-flex justify-content-between mb-2">
                        <span>المدفوع</span><span><?= number_format($totalPaid, 2) ?> <?= esc($invoice['currency']) ?></span>
                    </div>
                    <div class="d-flex justify-content-between fw-bold border-top pt-2">
                        <span>المتبقي</span><span><?= number_format($invoice['amount'] - $totalPaid, 2) ?> <?= esc($invoice['currency']) ?></span>
                    </div>
                </div>
            </div>
        </div>

        <?php if (!empty($invoice['notes'])): ?>
            <div class="mt-4"><div class="text-muted small mb-1">ملاحظات</div><div><?= nl2br(esc($invoice['notes'])) ?></div></div>
        <?php endif; ?>

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

        <?php if (!empty($company['invoice_footer_note'])): ?>
            <div class="mt-3 small text-muted text-center border-top pt-3"><?= nl2br(esc($company['invoice_footer_note'])) ?></div>
        <?php endif; ?>
    </div>
</div>

<?= $this->endSection() ?>
