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

<?php $__template->section('page_actions'); ?>
<button onclick="window.print()" class="btn btn-outline"><i data-lucide="printer" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> طباعة</button>
<?php $__template->endSection(); ?>

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

<?php
$statusLabels = [
    'draft'              => ['label' => 'مسودة',    'bg' => '#F3F4F6', 'color' => '#6B7280'],
    'pending_approval'   => ['label' => 'بانتظار الموافقة', 'bg' => '#FEF3C7', 'color' => '#D97706'],
    'approved'           => ['label' => 'معتمد',    'bg' => '#DBEAFE', 'color' => '#2563EB'],
    'partially_received' => ['label' => 'استلام جزئي', 'bg' => '#E0E7FF', 'color' => '#4F46E5'],
    'received'           => ['label' => 'مستلم',    'bg' => '#ECFDF5', 'color' => '#059669'],
    'cancelled'          => ['label' => 'ملغى',     'bg' => '#FEE2E2', 'color' => '#DC2626'],
];
?>

<!-- Filter -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/inventory/reports/supplier-history" style="display:flex;gap:10px;align-items:end;">
        <div style="min-width:250px;">
            <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'] ?>" <?= $supplierId == $sup['id'] ? 'selected' : '' ?>>
                    <?= e($sup['name_ar']) ?>
                    <?php if (!empty($sup['code'])): ?> (<?= e($sup['code']) ?>)<?php endif; ?>
                </option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-primary"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> عرض</button>
    </form>
</div>

<?php if ($supplierId && !empty($orders)): ?>

<?php
    $totalAmount = 0;
    foreach ($orders as $o) {
        $totalAmount = bcadd((string) $totalAmount, (string) ($o['total_amount'] ?? 0), 2);
    }
?>

<!-- Summary -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;background:#F0FDF4;border:1px solid #BBF7D0;">
        <div style="font-size:28px;font-weight:700;color:#059669;"><?= count($orders) ?></div>
        <div style="color:#6B7280;font-size:14px;">عدد أوامر الشراء</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;background:#EFF6FF;border:1px solid #BFDBFE;">
        <div style="font-size:28px;font-weight:700;color:#2563EB;"><?= money($totalAmount) ?></div>
        <div style="color:#6B7280;font-size:14px;">إجمالي المشتريات</div>
    </div>
</div>

<!-- Orders Table -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="file-text" style="width:20px;height:20px;color:#1e3a5f;"></i>
        <h3 style="margin:0;color:#1e3a5f;">أوامر الشراء</h3>
    </div>
    <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 ($orders as $order): ?>
                <?php
                    $st = $statusLabels[$order['status'] ?? ''] ?? ['label' => $order['status'] ?? '—', 'bg' => '#F3F4F6', 'color' => '#6B7280'];
                ?>
                <tr>
                    <td style="font-family:monospace;font-weight:600;">
                        <a href="/inventory/purchase-orders/<?= (int) ($order['id'] ?? 0) ?>" style="color:#1e3a5f;text-decoration:none;"><?= e($order['po_number'] ?? '—') ?></a>
                    </td>
                    <td><?= e($order['warehouse_name'] ?? '—') ?></td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $st['bg'] ?>;color:<?= $st['color'] ?>;">
                            <?= e($st['label']) ?>
                        </span>
                    </td>
                    <td style="font-weight:700;direction:ltr;text-align:right;"><?= money($order['total_amount'] ?? 0) ?></td>
                    <td style="font-size:13px;color:#6B7280;white-space:nowrap;"><?= e($order['created_at'] ?? '—') ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="background:#F0FDF4;font-weight:700;">
                    <td colspan="3" style="padding:12px 15px;">الإجمالي</td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;font-size:16px;color:#059669;"><?= money($totalAmount) ?></td>
                    <td></td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>

<?php elseif ($supplierId && empty($orders)): ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="inbox" 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 else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="truck" 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(); ?>
