<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>طلبات الشراء<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/procurement/requisitions/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> طلب شراء جديد</a>
<?php $__template->endSection(); ?>

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

<?php
$statusColors = [
    'draft' => '#6B7280', 'submitted' => '#D97706', 'approved' => '#0D7377',
    'rejected' => '#DC2626', 'converted' => '#059669', 'cancelled' => '#9CA3AF',
];
$statusBgs = [
    'draft' => '#F3F4F6', 'submitted' => '#FFF7ED', 'approved' => '#F0FDFA',
    'rejected' => '#FEE2E2', 'converted' => '#ECFDF5', 'cancelled' => '#F3F4F6',
];
$urgencyColors = ['low' => '#6B7280', 'normal' => '#2563EB', 'high' => '#D97706', 'critical' => '#DC2626'];
$urgencyBgs = ['low' => '#F3F4F6', 'normal' => '#EFF6FF', 'high' => '#FFF7ED', 'critical' => '#FEE2E2'];
?>

<!-- Search & Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/procurement/requisitions" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($statuses as $val => $label): ?>
                <option value="<?= e($val) ?>" <?= ($filters['status'] ?? '') === $val ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الأولوية</label>
            <select name="urgency" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($urgencies as $val => $label): ?>
                <option value="<?= e($val) ?>" <?= ($filters['urgency'] ?? '') === $val ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">من</label>
            <input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">إلى</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to'] ?? '') ?>" class="form-input">
        </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/requisitions" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Requisitions Table -->
<?php if (!empty($requisitions)): ?>
<div class="card">
    <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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($requisitions as $r): ?>
                <?php
                $st = $r['status'] ?? 'draft';
                $stColor = $statusColors[$st] ?? '#6B7280';
                $stBg = $statusBgs[$st] ?? '#F3F4F6';
                $stLabel = $statuses[$st] ?? $st;
                $urg = $r['urgency'] ?? 'normal';
                $urgColor = $urgencyColors[$urg] ?? '#6B7280';
                $urgBg = $urgencyBgs[$urg] ?? '#F3F4F6';
                $urgLabel = $urgencies[$urg] ?? $urg;
                ?>
                <tr>
                    <td style="font-weight:600;">
                        <a href="/procurement/requisitions/<?= (int) $r['id'] ?>" style="color:#0D7377;text-decoration:none;">
                            <code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($r['pr_number']) ?></code>
                        </a>
                    </td>
                    <td style="font-weight:600;"><?= e($r['requester_name'] ?? '—') ?></td>
                    <td style="font-size:13px;"><?= e($r['department'] ?? '—') ?></td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $urgBg ?>;color:<?= $urgColor ?>;">
                            <?= e($urgLabel) ?>
                        </span>
                    </td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $stBg ?>;color:<?= $stColor ?>;">
                            <?= e($stLabel) ?>
                        </span>
                    </td>
                    <td style="font-weight:700;direction:ltr;text-align:left;"><?= money($r['estimated_total'] ?? 0) ?></td>
                    <td style="font-size:12px;"><?= e(substr($r['created_at'] ?? '', 0, 10)) ?></td>
                    <td>
                        <a href="/procurement/requisitions/<?= (int) $r['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>
</div>

<?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
<div style="padding:15px;">
    <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
</div>
<?php endif; ?>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="clipboard-list" 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 0 20px;">ابدأ بإنشاء طلب شراء جديد.</p>
    <a href="/procurement/requisitions/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> طلب شراء جديد</a>
</div>
<?php endif; ?>

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