<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>مطابقة ثلاثية — <?= e($invoice['internal_number']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/procurement/invoices/<?= (int) $invoice['id'] ?>" 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
$matchStatusLabels = ['unmatched' => 'غير مطابق', 'matched' => 'مطابق', 'discrepancy' => 'يوجد اختلاف', 'tolerance_pass' => 'مطابق (ضمن التسامح)'];
$matchStatusColors = ['unmatched' => '#6B7280', 'matched' => '#059669', 'discrepancy' => '#DC2626', 'tolerance_pass' => '#D97706'];
$matchStatusBgs = ['unmatched' => '#F3F4F6', 'matched' => '#ECFDF5', 'discrepancy' => '#FEE2E2', 'tolerance_pass' => '#FFF7ED'];
$ms = $matchResult['status'] ?? 'unmatched';
?>

<!-- Match Summary -->
<div class="card" style="margin-bottom:20px;padding:20px;text-align:center;background:<?= $matchStatusBgs[$ms] ?? '#F3F4F6' ?>;">
    <div style="font-size:16px;font-weight:700;color:<?= $matchStatusColors[$ms] ?? '#6B7280' ?>;margin-bottom:6px;">
        <?= e($matchStatusLabels[$ms] ?? $ms) ?>
    </div>
    <div style="font-size:14px;color:#4B5563;"><?= e($matchResult['notes'] ?? '') ?></div>
</div>

<!-- Match Details -->
<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="git-compare" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">تفاصيل المطابقة (أمر شراء ↔ إذن استلام ↔ فاتورة)</h3>
    </div>
    <?php if (!empty($matchResult['details'])): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>كمية أمر الشراء</th>
                    <th>كمية المقبول (GRN)</th>
                    <th>كمية الفاتورة</th>
                    <th>كمية</th>
                    <th>سعر أمر الشراء</th>
                    <th>سعر الفاتورة</th>
                    <th>سعر</th>
                    <th>ملاحظات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($matchResult['details'] as $i => $d): ?>
                <tr>
                    <td><?= $i + 1 ?></td>
                    <td style="direction:ltr;text-align:left;"><?= number_format((float) ($d['po_qty'] ?? 0), 3) ?></td>
                    <td style="direction:ltr;text-align:left;"><?= number_format((float) ($d['grn_accepted_qty'] ?? 0), 3) ?></td>
                    <td style="direction:ltr;text-align:left;"><?= number_format((float) ($d['invoice_qty'] ?? 0), 3) ?></td>
                    <td>
                        <?php if ($d['qty_match'] ?? false): ?>
                        <span style="color:#059669;font-weight:600;"><i data-lucide="check" style="width:14px;height:14px;vertical-align:middle;"></i></span>
                        <?php else: ?>
                        <span style="color:#DC2626;font-weight:600;"><i data-lucide="x" style="width:14px;height:14px;vertical-align:middle;"></i></span>
                        <?php endif; ?>
                    </td>
                    <td style="direction:ltr;text-align:left;"><?= money($d['po_cost'] ?? 0) ?></td>
                    <td style="direction:ltr;text-align:left;"><?= money($d['invoice_cost'] ?? 0) ?></td>
                    <td>
                        <?php if ($d['cost_match'] ?? false): ?>
                        <span style="color:#059669;font-weight:600;"><i data-lucide="check" style="width:14px;height:14px;vertical-align:middle;"></i></span>
                        <?php else: ?>
                        <span style="color:#DC2626;font-weight:600;"><i data-lucide="x" style="width:14px;height:14px;vertical-align:middle;"></i></span>
                        <?php endif; ?>
                    </td>
                    <td style="font-size:12px;color:#6B7280;"><?= e(implode(' | ', $d['notes'] ?? [])) ?></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(); ?>
