<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>مقارنة عروض الأسعار<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/procurement/quotes" 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
$items = $comparison['items'] ?? [];
$suppliers = $comparison['suppliers'] ?? [];
?>

<!-- Requisition Info -->
<div class="card" style="margin-bottom:20px;padding:20px;">
    <div style="display:flex;gap:20px;align-items:center;">
        <div>
            <span style="font-size:12px;color:#6B7280;">رقم طلب الشراء</span>
            <div style="font-weight:700;font-size:16px;"><?= e($requisition['pr_number'] ?? '') ?></div>
        </div>
        <div>
            <span style="font-size:12px;color:#6B7280;">التاريخ</span>
            <div style="font-weight:600;"><?= e($requisition['created_at'] ?? '') ?></div>
        </div>
    </div>
</div>

<!-- Comparison Table -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:15px;"><i data-lucide="bar-chart-3" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;color:#0D7377;"></i> مقارنة الأسعار</h3>
    </div>
    <?php if (!empty($items) && !empty($suppliers)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الصنف</th>
                    <th>الكمية</th>
                    <?php foreach ($suppliers as $supplier): ?>
                    <th style="text-align:center;">
                        <div><?= e($supplier['name'] ?? '') ?></div>
                        <div style="font-size:11px;color:#6B7280;font-weight:normal;">مدة التوريد: <?= (int) ($supplier['delivery_days'] ?? 0) ?> يوم</div>
                    </th>
                    <?php endforeach; ?>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                <?php
                // Find lowest price for this item
                $prices = [];
                foreach ($suppliers as $supplier) {
                    $key = 'supplier_' . ($supplier['id'] ?? 0);
                    if (isset($item[$key]) && $item[$key] > 0) {
                        $prices[$supplier['id']] = (float) $item[$key];
                    }
                }
                $lowestPrice = !empty($prices) ? min($prices) : 0;
                ?>
                <tr>
                    <td style="font-weight:600;"><?= e($item['item_name'] ?? '') ?></td>
                    <td><?= (int) ($item['quantity'] ?? 0) ?></td>
                    <?php foreach ($suppliers as $supplier): ?>
                    <?php
                    $key = 'supplier_' . ($supplier['id'] ?? 0);
                    $price = isset($item[$key]) ? (float) $item[$key] : 0;
                    $isLowest = $price > 0 && $price === $lowestPrice;
                    ?>
                    <td style="text-align:center;direction:ltr;<?= $isLowest ? 'background:#D1FAE5;font-weight:800;color:#047857;' : '' ?>">
                        <?= $price > 0 ? money($price) : '—' ?>
                        <?php if ($isLowest): ?>
                        <i data-lucide="check-circle" style="width:14px;height:14px;color:#047857;vertical-align:middle;margin-right:4px;"></i>
                        <?php endif; ?>
                    </td>
                    <?php endforeach; ?>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:40px;text-align:center;color:#9CA3AF;">لا توجد بيانات للمقارنة</div>
    <?php endif; ?>
</div>

<!-- Winner Selection Form -->
<div class="card" style="padding:20px;">
    <h3 style="margin:0 0 15px;font-size:15px;"><i data-lucide="award" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;color:#D97706;"></i> اختيار العرض الفائز</h3>
    <form method="POST" action="/procurement/quotes/<?= (int) ($requisition['id'] ?? 0) ?>/select-winner">
        <?= csrf_field() ?>
        <div style="margin-bottom:15px;">
            <label class="form-label">المورد الفائز</label>
            <select name="winning_supplier_id" class="form-select" required>
                <option value="">-- اختر المورد --</option>
                <?php foreach ($suppliers as $supplier): ?>
                <option value="<?= (int) ($supplier['id'] ?? 0) ?>"><?= e($supplier['name'] ?? '') ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="margin-bottom:15px;">
            <label class="form-label">مبررات الاختيار</label>
            <textarea name="justification" class="form-control" rows="4" placeholder="اذكر أسباب اختيار هذا المورد..." required></textarea>
        </div>
        <button type="submit" class="btn btn-primary"><i data-lucide="check" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تأكيد الاختيار</button>
    </form>
</div>

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