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

<div class="d-flex justify-content-between align-items-center mb-3">
    <h3>عرض سعر <?= esc($quote['quote_number']) ?></h3>
    <div>
        <a href="/admin/quotes/<?= $quote['id'] ?>/pdf" class="btn btn-dark btn-sm">⬇️ تحميل PDF</a>
        <button onclick="window.print()" class="btn btn-outline-secondary btn-sm">🖨️ طباعة</button>
        <?php if ($quote['status'] !== 'converted'): ?>
            <form method="post" action="/admin/quotes/<?= $quote['id'] ?>/convert" class="d-inline" onsubmit="return confirm('تحويل هذا العرض لفاتورة نهائية؟')">
                <?= csrf_field() ?><button class="btn btn-success btn-sm">تحويل لفاتورة</button>
            </form>
        <?php endif; ?>
        <a href="/admin/quotes" class="btn btn-light btn-sm">رجوع</a>
    </div>
</div>

<style>
    @media print { .navbar, .btn, nav { display: none !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-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:56px;width:56px;object-fit:contain;border-radius:10px;background:#fff;padding:4px;">
            <?php endif; ?>
            <div>
                <h4 class="mb-0"><?= esc($company['name']) ?></h4>
                <div class="small opacity-75">
                    <?= esc($company['address'] ?? '') ?>
                    <?= $company['phone'] ? ' | ' . esc($company['phone']) : '' ?>
                </div>
            </div>
        </div>
        <div class="text-end">
            <div class="fs-5 fw-bold">عرض سعر #<?= esc($quote['quote_number']) ?></div>
            <div class="small opacity-75">ساري حتى: <?= esc($quote['valid_until']) ?></div>
        </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="mb-4">
            <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>

        <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 d-flex justify-content-between fw-bold">
                    <span>الإجمالي</span><span><?= number_format($quote['amount'], 2) ?> <?= esc($quote['currency']) ?></span>
                </div>
            </div>
        </div>

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

        <div class="creator-note">
            حرر بواسطة: <b><?= esc($creator['name'] ?? 'غير محدد') ?></b>
        </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() ?>
