<?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'); ?>

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/procurement/reports/po-balance" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="min-width:180px;">
            <label class="form-label" style="font-size:12px;">المورد</label>
            <select name="supplier_id" class="form-select">
                <option value="">جميع الموردين</option>
                <?php foreach ($suppliers as $s): ?>
                <option value="<?= (int) $s['id'] ?>" <?= ($supplier_id ?? '') == $s['id'] ? 'selected' : '' ?>><?= e($s['name'] ?? '') ?></option>
                <?php endforeach; ?>
            </select>
        </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/reports/po-balance" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- PO Balance Table -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="package" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;font-size:15px;">رصيد أوامر الشراء</h3>
    </div>
    <?php if (!empty($orders)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>رقم أمر الشراء</th>
                    <th>المورد</th>
                    <th>الكمية المطلوبة</th>
                    <th>الكمية المستلمة</th>
                    <th>الكمية المتبقية</th>
                    <th>نسبة التسليم</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($orders as $order): ?>
                <?php
                $totalOrdered = (int) ($order['total_qty_ordered'] ?? 0);
                $totalReceived = (int) ($order['total_qty_received'] ?? 0);
                $totalRemaining = (int) ($order['total_qty_remaining'] ?? ($totalOrdered - $totalReceived));
                $percentage = $totalOrdered > 0 ? round(($totalReceived / $totalOrdered) * 100) : 0;
                $barColor = $percentage >= 100 ? '#059669' : ($percentage >= 50 ? '#D97706' : '#DC2626');
                ?>
                <tr>
                    <td style="font-weight:600;">
                        <code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($order['po_number'] ?? '') ?></code>
                    </td>
                    <td style="font-weight:600;"><?= e($order['supplier_name'] ?? '—') ?></td>
                    <td style="text-align:center;"><?= $totalOrdered ?></td>
                    <td style="text-align:center;color:#059669;font-weight:600;"><?= $totalReceived ?></td>
                    <td style="text-align:center;color:#DC2626;font-weight:600;"><?= $totalRemaining ?></td>
                    <td style="min-width:160px;">
                        <div style="display:flex;align-items:center;gap:8px;">
                            <div style="flex:1;background:#E5E7EB;border-radius:4px;height:8px;overflow:hidden;">
                                <div style="width:<?= $percentage ?>%;height:100%;background:<?= $barColor ?>;border-radius:4px;transition:width 0.3s;"></div>
                            </div>
                            <span style="font-size:12px;font-weight:700;color:<?= $barColor ?>;min-width:40px;text-align:left;direction:ltr;"><?= $percentage ?>%</span>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:60px 20px;text-align:center;">
        <div style="margin-bottom:15px;"><i data-lucide="package" 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; ?>
</div>

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