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

<?php $__template->section('page_actions'); ?>
<a href="/procurement" 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 if (!empty($suppliers)): ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="award" style="width:18px;height:18px;color:#D97706;"></i>
        <h3 style="margin:0;color:#D97706;font-size:15px;">أداء الموردين</h3>
    </div>
    <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 ($suppliers as $s): ?>
                <?php
                $total = (int) ($s['total_orders'] ?? 0);
                $onTime = (int) ($s['on_time_delivery_count'] ?? 0);
                $quality = (int) ($s['quality_acceptance_count'] ?? 0);
                $onTimeRate = $total > 0 ? round(($onTime / $total) * 100, 1) : 0;
                $qualityRate = $total > 0 ? round(($quality / $total) * 100, 1) : 0;
                $avgLead = round((float) ($s['average_lead_days'] ?? 0), 1);
                ?>
                <tr>
                    <td><code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($s['code'] ?? '') ?></code></td>
                    <td style="font-weight:600;"><a href="/inventory/suppliers/<?= (int) $s['id'] ?>" style="color:#0D7377;text-decoration:none;"><?= e($s['name_ar']) ?></a></td>
                    <td>
                        <?php
                        $rating = (int) ($s['rating'] ?? 0);
                        if ($rating > 0):
                            for ($i = 1; $i <= 5; $i++):
                        ?>
                            <i data-lucide="star" style="width:12px;height:12px;vertical-align:middle;<?= $i <= $rating ? 'color:#D97706;fill:#D97706;' : 'color:#D1D5DB;' ?>"></i>
                        <?php endfor; else: ?>
                            <span style="color:#9CA3AF;font-size:12px;">—</span>
                        <?php endif; ?>
                    </td>
                    <td style="font-weight:600;"><?= $total ?></td>
                    <td>
                        <div style="display:flex;align-items:center;gap:6px;">
                            <div style="flex:1;background:#E5E7EB;border-radius:4px;height:8px;overflow:hidden;">
                                <div style="background:<?= $onTimeRate >= 80 ? '#059669' : ($onTimeRate >= 50 ? '#D97706' : '#DC2626') ?>;height:100%;width:<?= $onTimeRate ?>%;border-radius:4px;"></div>
                            </div>
                            <span style="font-size:12px;font-weight:600;color:<?= $onTimeRate >= 80 ? '#059669' : ($onTimeRate >= 50 ? '#D97706' : '#DC2626') ?>;"><?= $onTimeRate ?>%</span>
                        </div>
                    </td>
                    <td>
                        <div style="display:flex;align-items:center;gap:6px;">
                            <div style="flex:1;background:#E5E7EB;border-radius:4px;height:8px;overflow:hidden;">
                                <div style="background:<?= $qualityRate >= 80 ? '#059669' : ($qualityRate >= 50 ? '#D97706' : '#DC2626') ?>;height:100%;width:<?= $qualityRate ?>%;border-radius:4px;"></div>
                            </div>
                            <span style="font-size:12px;font-weight:600;color:<?= $qualityRate >= 80 ? '#059669' : ($qualityRate >= 50 ? '#D97706' : '#DC2626') ?>;"><?= $qualityRate ?>%</span>
                        </div>
                    </td>
                    <td style="font-size:13px;"><?= $avgLead ?> يوم</td>
                    <td style="font-weight:700;direction:ltr;text-align:left;color:<?= bccomp((string) ($s['outstanding_balance'] ?? '0'), '0', 2) > 0 ? '#DC2626' : '#059669' ?>;"><?= money($s['outstanding_balance'] ?? 0) ?></td>
                    <td style="font-size:12px;color:#6B7280;"><?= e($s['last_order_date'] ?? '—') ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;"><i data-lucide="award" 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;">ستظهر بيانات الأداء بعد إتمام عمليات الشراء والاستلام.</p>
</div>
<?php endif; ?>

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