<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>فواتير الموردين<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('procurement.invoice.create')): ?>
<a href="/procurement/invoices/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> فاتورة جديدة</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<?php
$statusColors = ['draft' => '#6B7280', 'verified' => '#D97706', 'approved' => '#0D7377', 'paid' => '#059669', 'partial_paid' => '#2563EB', 'cancelled' => '#DC2626'];
$statusBgs = ['draft' => '#F3F4F6', 'verified' => '#FFF7ED', 'approved' => '#F0FDFA', 'paid' => '#ECFDF5', 'partial_paid' => '#EFF6FF', 'cancelled' => '#FEE2E2'];
$matchColors = ['unmatched' => '#6B7280', 'matched' => '#059669', 'discrepancy' => '#DC2626', 'tolerance_pass' => '#D97706'];
$matchBgs = ['unmatched' => '#F3F4F6', 'matched' => '#ECFDF5', 'discrepancy' => '#FEE2E2', 'tolerance_pass' => '#FFF7ED'];
?>

<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/procurement/invoices" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($statuses as $val => $label): ?>
                <option value="<?= e($val) ?>" <?= ($filters['status'] ?? '') === $val ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">المطابقة</label>
            <select name="match_status" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($matchStatuses as $val => $label): ?>
                <option value="<?= e($val) ?>" <?= ($filters['match_status'] ?? '') === $val ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:160px;">
            <label class="form-label" style="font-size:12px;">المورد</label>
            <select name="supplier_id" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($suppliers as $sup): ?>
                <option value="<?= (int) $sup['id'] ?>" <?= ($filters['supplier_id'] ?? '') == $sup['id'] ? 'selected' : '' ?>><?= e($sup['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">من</label>
            <input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">إلى</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to'] ?? '') ?>" class="form-input">
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/procurement/invoices" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<?php if (!empty($invoices)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الرقم الداخلي</th>
                    <th>رقم الفاتورة</th>
                    <th>المورد</th>
                    <th>أمر الشراء</th>
                    <th>الحالة</th>
                    <th>المطابقة</th>
                    <th>الإجمالي</th>
                    <th>الاستحقاق</th>
                    <th>الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($invoices as $inv): ?>
                <?php
                $st = $inv['status'] ?? 'draft';
                $ms = $inv['match_status'] ?? '';
                ?>
                <tr>
                    <td style="font-weight:600;">
                        <a href="/procurement/invoices/<?= (int) $inv['id'] ?>" style="color:#0D7377;text-decoration:none;">
                            <code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($inv['internal_number']) ?></code>
                        </a>
                    </td>
                    <td style="font-size:13px;"><?= e($inv['invoice_number'] ?? '—') ?></td>
                    <td style="font-weight:600;"><?= e($inv['supplier_name'] ?? '—') ?></td>
                    <td>
                        <?php if (!empty($inv['po_number'])): ?>
                        <code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($inv['po_number']) ?></code>
                        <?php else: ?>—<?php endif; ?>
                    </td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $statusBgs[$st] ?? '#F3F4F6' ?>;color:<?= $statusColors[$st] ?? '#6B7280' ?>;">
                            <?= e($statuses[$st] ?? $st) ?>
                        </span>
                    </td>
                    <td>
                        <?php if ($ms): ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $matchBgs[$ms] ?? '#F3F4F6' ?>;color:<?= $matchColors[$ms] ?? '#6B7280' ?>;">
                            <?= e($matchStatuses[$ms] ?? $ms) ?>
                        </span>
                        <?php else: ?>—<?php endif; ?>
                    </td>
                    <td style="font-weight:700;direction:ltr;text-align:left;"><?= money($inv['total_amount'] ?? 0) ?></td>
                    <td style="font-size:12px;"><?= e($inv['due_date'] ?? '—') ?></td>
                    <td>
                        <a href="/procurement/invoices/<?= (int) $inv['id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> عرض
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
<div style="padding:15px;"><?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?></div>
<?php endif; ?>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;"><i data-lucide="file-text" style="width:48px;height:48px;color:#D1D5DB;"></i></div>
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد فواتير موردين</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0 0 20px;">ابدأ بإنشاء فاتورة مورد جديدة.</p>
    <a href="/procurement/invoices/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> فاتورة جديدة</a>
</div>
<?php endif; ?>

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