<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= e($supplier->name_ar) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/inventory/suppliers/<?= (int) $supplier->id ?>/edit" class="btn btn-outline"><i data-lucide="edit-3" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> تعديل</a>
<a href="/inventory/suppliers" 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'); ?>

<!-- Supplier Info Card -->
<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="truck" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">بيانات المورد</h3>
        <div style="margin-right:auto;">
            <?php if ((int) $supplier->is_active): ?>
            <span style="display:inline-block;padding:3px 12px;border-radius:10px;font-size:12px;font-weight:600;background:#ECFDF5;color:#059669;">نشط</span>
            <?php else: ?>
            <span style="display:inline-block;padding:3px 12px;border-radius:10px;font-size:12px;font-weight:600;background:#FEE2E2;color:#DC2626;">معطل</span>
            <?php endif; ?>
        </div>
    </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="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($supplier->code) ?></code>
                    </td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">الاسم بالعربي</td>
                    <td style="padding:10px 0;font-weight:600;"><?= e($supplier->name_ar) ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">الاسم بالإنجليزي</td>
                    <td style="padding:10px 0;"><?= e($supplier->name_en ?? '—') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">جهة الاتصال</td>
                    <td style="padding:10px 0;font-weight:600;"><?= e($supplier->contact_person ?? '—') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">الهاتف</td>
                    <td style="padding:10px 0;direction:ltr;text-align:right;"><?= e($supplier->phone ?? '—') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">البريد الإلكتروني</td>
                    <td style="padding:10px 0;direction:ltr;text-align:right;"><?= e($supplier->email ?? '—') ?></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;"><?= e($supplier->address ?? '—') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">الرقم الضريبي</td>
                    <td style="padding:10px 0;direction:ltr;text-align:right;"><?= e($supplier->tax_number ?? '—') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">شروط الدفع</td>
                    <td style="padding:10px 0;"><?= e($supplier->payment_terms ?? '—') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">التقييم</td>
                    <td style="padding:10px 0;">
                        <?php
                        $rating = (int) ($supplier->rating ?? 0);
                        if ($rating > 0):
                            for ($i = 1; $i <= 5; $i++):
                        ?>
                            <i data-lucide="star" style="width:16px;height:16px;vertical-align:middle;<?= $i <= $rating ? 'color:#D97706;fill:#D97706;' : 'color:#D1D5DB;' ?>"></i>
                        <?php
                            endfor;
                        else:
                        ?>
                            <span style="color:#9CA3AF;">—</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">ملاحظات</td>
                    <td style="padding:10px 0;"><?= e($supplier->notes ?? '—') ?></td>
                </tr>
            </table>
        </div>
    </div>
</div>

<!-- Performance & Financial Summary -->
<?php $perf = $performance ?? []; $ap = $apBalance ?? []; ?>
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;margin-bottom:6px;">إجمالي الطلبات</div>
        <div style="font-size:28px;font-weight:800;color:#0D7377;"><?= (int) ($perf['total_orders'] ?? 0) ?></div>
        <?php if (!empty($perf['last_order_date'])): ?>
        <div style="font-size:11px;color:#9CA3AF;margin-top:4px;">آخر طلب: <?= e($perf['last_order_date']) ?></div>
        <?php endif; ?>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;margin-bottom:6px;">معدل التسليم في الوقت</div>
        <div style="font-size:28px;font-weight:800;color:<?= ($perf['on_time_rate'] ?? 0) >= 80 ? '#059669' : (($perf['on_time_rate'] ?? 0) >= 50 ? '#D97706' : '#DC2626') ?>;"><?= $perf['on_time_rate'] ?? 0 ?>%</div>
        <div style="font-size:11px;color:#9CA3AF;margin-top:4px;"><?= (int) ($perf['on_time_delivery_count'] ?? 0) ?> من <?= (int) ($perf['total_orders'] ?? 0) ?></div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;margin-bottom:6px;">معدل جودة الاستلام</div>
        <div style="font-size:28px;font-weight:800;color:<?= ($perf['quality_rate'] ?? 0) >= 80 ? '#059669' : (($perf['quality_rate'] ?? 0) >= 50 ? '#D97706' : '#DC2626') ?>;"><?= $perf['quality_rate'] ?? 0 ?>%</div>
        <div style="font-size:11px;color:#9CA3AF;margin-top:4px;">متوسط المهلة: <?= $perf['average_lead_days'] ?? 0 ?> يوم</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:12px;color:#6B7280;margin-bottom:6px;">الرصيد المستحق (AP)</div>
        <div style="font-size:28px;font-weight:800;color:#DC2626;direction:ltr;"><?= money($ap['outstanding_balance'] ?? 0) ?></div>
        <div style="font-size:11px;color:#9CA3AF;margin-top:4px;"><?= (int) ($ap['invoice_count'] ?? 0) ?> فاتورة مفتوحة</div>
    </div>
</div>

<!-- Recent Purchase Orders -->
<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="shopping-cart" style="width:18px;height:18px;color:#D97706;"></i>
        <h3 style="margin:0;color:#D97706;font-size:15px;">آخر أوامر الشراء</h3>
    </div>
    <?php if (!empty($recentPOs)): ?>
    <?php
    $poStatusLabels = [
        'draft' => 'مسودة', 'submitted' => 'مقدم', 'approved' => 'معتمد',
        'partially_received' => 'استلام جزئي', 'received' => 'مستلم', 'cancelled' => 'ملغي',
    ];
    $poStatusColors = [
        'draft' => '#6B7280', 'submitted' => '#D97706', 'approved' => '#0D7377',
        'partially_received' => '#2563EB', 'received' => '#059669', 'cancelled' => '#DC2626',
    ];
    $poStatusBgs = [
        'draft' => '#F3F4F6', 'submitted' => '#FFF7ED', 'approved' => '#F0FDFA',
        'partially_received' => '#EFF6FF', 'received' => '#ECFDF5', 'cancelled' => '#FEE2E2',
    ];
    ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>رقم الأمر</th>
                    <th>الحالة</th>
                    <th>الإجمالي</th>
                    <th>التاريخ</th>
                    <th>الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recentPOs as $po): ?>
                <?php
                $poSt = $po['status'] ?? 'draft';
                $poColor = $poStatusColors[$poSt] ?? '#6B7280';
                $poBg = $poStatusBgs[$poSt] ?? '#F3F4F6';
                $poLabel = $poStatusLabels[$poSt] ?? $poSt;
                ?>
                <tr>
                    <td style="font-weight:600;">
                        <code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($po['po_number']) ?></code>
                    </td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $poBg ?>;color:<?= $poColor ?>;">
                            <?= e($poLabel) ?>
                        </span>
                    </td>
                    <td style="font-weight:700;direction:ltr;text-align:left;"><?= money($po['total_amount'] ?? 0) ?></td>
                    <td style="font-size:13px;"><?= e(substr($po['created_at'] ?? '', 0, 10)) ?></td>
                    <td>
                        <a href="/inventory/purchase-orders/<?= (int) $po['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>
    <?php else: ?>
    <div style="padding:40px 20px;text-align:center;color:#6B7280;">
        <i data-lucide="shopping-cart" style="width:36px;height:36px;color:#D1D5DB;margin-bottom:8px;"></i>
        <p style="margin:0;">لا توجد أوامر شراء لهذا المورد</p>
    </div>
    <?php endif; ?>
</div>

<!-- Metadata -->
<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="info" style="width:18px;height:18px;color:#6B7280;"></i>
        <h3 style="margin:0;color:#6B7280;font-size:15px;">معلومات النظام</h3>
    </div>
    <div style="padding:15px 20px;">
        <table style="width:100%;font-size:13px;">
            <tr>
                <td style="padding:6px 0;color:#6B7280;width:30%;">رقم السجل</td>
                <td style="padding:6px 0;font-weight:600;">#<?= (int) $supplier->id ?></td>
            </tr>
            <?php if ($supplier->created_at): ?>
            <tr>
                <td style="padding:6px 0;color:#6B7280;">تاريخ الإنشاء</td>
                <td style="padding:6px 0;"><?= e($supplier->created_at) ?></td>
            </tr>
            <?php endif; ?>
            <?php if ($supplier->updated_at): ?>
            <tr>
                <td style="padding:6px 0;color:#6B7280;">آخر تحديث</td>
                <td style="padding:6px 0;"><?= e($supplier->updated_at) ?></td>
            </tr>
            <?php endif; ?>
        </table>
    </div>
</div>

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