<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>فاتورة مورد <?= e($invoice['internal_number']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if ($invoice['status'] === 'draft'): ?>
<form method="POST" action="/procurement/invoices/<?= (int) $invoice['id'] ?>/verify" style="display:inline;">
    <?= csrf_field() ?>
    <button type="submit" class="btn" style="background:#D97706;color:#fff;border:none;">
        <i data-lucide="check-square" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> تحقق ومطابقة
    </button>
</form>
<?php endif; ?>
<?php if ($invoice['status'] === 'verified'): ?>
<form method="POST" action="/procurement/invoices/<?= (int) $invoice['id'] ?>/approve" style="display:inline;">
    <?= csrf_field() ?>
    <button type="submit" class="btn" style="background:#0D7377;color:#fff;border:none;" onclick="return confirm('هل أنت متأكد من اعتماد الفاتورة؟ سيتم تسجيل القيد المحاسبي.');">
        <i data-lucide="check-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> اعتماد
    </button>
</form>
<?php endif; ?>
<?php if (!empty($invoice['purchase_order_id'])): ?>
<a href="/procurement/invoices/<?= (int) $invoice['id'] ?>/match" class="btn btn-outline">
    <i data-lucide="git-compare" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> عرض المطابقة
</a>
<?php endif; ?>
<?php if (in_array($invoice['status'], ['draft', 'verified'])): ?>
<form method="POST" action="/procurement/invoices/<?= (int) $invoice['id'] ?>/cancel" style="display:inline;">
    <?= csrf_field() ?>
    <button type="submit" class="btn" style="background:#FEE2E2;color:#DC2626;border:none;" onclick="return confirm('هل أنت متأكد من إلغاء الفاتورة؟');">
        <i data-lucide="x-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> إلغاء
    </button>
</form>
<?php endif; ?>
<a href="/procurement/invoices" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<?php
$statusLabels = ['draft' => 'مسودة', 'verified' => 'تم التحقق', 'approved' => 'معتمدة', 'paid' => 'مدفوعة', 'partial_paid' => 'مدفوعة جزئياً', 'cancelled' => 'ملغاة'];
$statusColorMap = [
    'draft' => ['bg' => '#F3F4F6', 'color' => '#6B7280'], 'verified' => ['bg' => '#FFF7ED', 'color' => '#D97706'],
    'approved' => ['bg' => '#F0FDFA', 'color' => '#0D7377'], 'paid' => ['bg' => '#ECFDF5', 'color' => '#059669'],
    'partial_paid' => ['bg' => '#EFF6FF', 'color' => '#2563EB'], 'cancelled' => ['bg' => '#FEE2E2', 'color' => '#DC2626'],
];
$matchLabels = ['unmatched' => 'غير مطابق', 'matched' => 'مطابق', 'discrepancy' => 'يوجد اختلاف', 'tolerance_pass' => 'مطابق (تسامح)'];
$matchColorMap = ['unmatched' => ['bg' => '#F3F4F6', 'color' => '#6B7280'], 'matched' => ['bg' => '#ECFDF5', 'color' => '#059669'], 'discrepancy' => ['bg' => '#FEE2E2', 'color' => '#DC2626'], 'tolerance_pass' => ['bg' => '#FFF7ED', 'color' => '#D97706']];
$st = $invoice['status'] ?? 'draft';
$ms = $invoice['match_status'] ?? '';
$stColors = $statusColorMap[$st] ?? $statusColorMap['draft'];
$msColors = $matchColorMap[$ms] ?? ['bg' => '#F3F4F6', 'color' => '#6B7280'];
?>

<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="file-text" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">بيانات الفاتورة</h3>
    </div>
    <div style="padding:20px;">
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:0;">
            <table style="width:100%;font-size:14px;">
                <tr><td style="padding:10px 0;color:#6B7280;width:40%;">الرقم الداخلي</td><td style="padding:10px 0;font-weight:600;"><code style="background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($invoice['internal_number']) ?></code></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">رقم فاتورة المورد</td><td style="padding:10px 0;"><?= e($invoice['invoice_number'] ?? '—') ?></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">المورد</td><td style="padding:10px 0;font-weight:600;"><?= e($invoice['supplier_name'] ?? '') ?></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">أمر الشراء</td><td style="padding:10px 0;"><?php if (!empty($invoice['po_number'])): ?><a href="/inventory/purchase-orders/<?= (int) $invoice['purchase_order_id'] ?>" style="color:#0D7377;"><?= e($invoice['po_number']) ?></a><?php else: ?>—<?php endif; ?></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">إذن الاستلام</td><td style="padding:10px 0;"><?php if (!empty($invoice['grn_number'])): ?><a href="/procurement/grn/<?= (int) $invoice['grn_id'] ?>" style="color:#0D7377;"><?= e($invoice['grn_number']) ?></a><?php else: ?>—<?php endif; ?></td></tr>
            </table>
            <table style="width:100%;font-size:14px;">
                <tr><td style="padding:10px 0;color:#6B7280;width:40%;">الحالة</td><td style="padding:10px 0;"><span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $stColors['bg'] ?>;color:<?= $stColors['color'] ?>;"><?= e($statusLabels[$st] ?? $st) ?></span></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">المطابقة</td><td style="padding:10px 0;"><?php if ($ms): ?><span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $msColors['bg'] ?>;color:<?= $msColors['color'] ?>;"><?= e($matchLabels[$ms] ?? $ms) ?></span><?php else: ?>—<?php endif; ?></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">تاريخ الفاتورة</td><td style="padding:10px 0;"><?= e($invoice['invoice_date'] ?? '—') ?></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">تاريخ الاستحقاق</td><td style="padding:10px 0;"><?= e($invoice['due_date'] ?? '—') ?></td></tr>
            </table>
        </div>
    </div>
</div>

<!-- Totals -->
<div style="display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:10px;margin-bottom:20px;">
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;">المبلغ الأساسي</div>
        <div style="font-size:20px;font-weight:700;color:#4B5563;direction:ltr;"><?= money($invoice['subtotal'] ?? 0) ?></div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;">الضريبة</div>
        <div style="font-size:20px;font-weight:700;color:#D97706;direction:ltr;"><?= money($invoice['tax_amount'] ?? 0) ?></div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;">الخصم</div>
        <div style="font-size:20px;font-weight:700;color:#059669;direction:ltr;"><?= money($invoice['discount_amount'] ?? 0) ?></div>
    </div>
    <div class="card" style="padding:15px;text-align:center;background:linear-gradient(135deg, #0D737710, #0D737720);">
        <div style="font-size:12px;color:#6B7280;">الإجمالي</div>
        <div style="font-size:24px;font-weight:800;color:#0D7377;direction:ltr;"><?= money($invoice['total_amount'] ?? 0) ?></div>
    </div>
</div>

<!-- Items -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="package" style="width:18px;height:18px;color:#D97706;"></i>
        <h3 style="margin:0;color:#D97706;font-size:15px;">بنود الفاتورة</h3>
    </div>
    <?php if (!empty($items)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr><th>الصنف</th><th>الكمية</th><th>سعر الوحدة</th><th>الضريبة %</th><th>الضريبة</th><th>الإجمالي</th></tr></thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                <tr>
                    <td style="font-weight:600;"><?= e($item['item_name'] ?? $item['description'] ?? '—') ?></td>
                    <td style="direction:ltr;text-align:left;"><?= number_format((float) ($item['quantity'] ?? 0), 3) ?></td>
                    <td style="direction:ltr;text-align:left;"><?= money($item['unit_cost'] ?? 0) ?></td>
                    <td style="direction:ltr;text-align:left;"><?= number_format((float) ($item['tax_rate'] ?? 0), 2) ?>%</td>
                    <td style="direction:ltr;text-align:left;"><?= money($item['tax_amount'] ?? 0) ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:left;"><?= money($item['line_total'] ?? 0) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:40px 20px;text-align:center;color:#6B7280;"><p style="margin:0;">لا توجد بنود</p></div>
    <?php endif; ?>
</div>

<script>document.addEventListener('DOMContentLoaded', function() { if (typeof lucide !== 'undefined') lucide.createIcons(); });</script>
<?php $__template->endSection(); ?>
