<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>فحص إذن استلام <?= e($grn['grn_number']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/procurement/grn/<?= (int) $grn['id'] ?>" 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'); ?>

<div class="card" style="margin-bottom:20px;padding:15px;">
    <div style="display:flex;gap:20px;flex-wrap:wrap;font-size:14px;">
        <div><strong>رقم الإذن:</strong> <code><?= e($grn['grn_number']) ?></code></div>
        <div><strong>أمر الشراء:</strong> <?= e($grn['po_number'] ?? '—') ?></div>
        <div><strong>المورد:</strong> <?= e($grn['supplier_name'] ?? '') ?></div>
        <div><strong>المستودع:</strong> <?= e($grn['warehouse_name'] ?? '') ?></div>
    </div>
</div>

<form method="POST" action="/procurement/grn/<?= (int) $grn['id'] ?>/inspect">
    <?= csrf_field() ?>

    <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="clipboard-check" style="width:18px;height:18px;color:#D97706;"></i>
            <h3 style="margin:0;color:#D97706;font-size:15px;">فحص الأصناف</h3>
        </div>
        <div class="table-responsive">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>الصنف</th>
                        <th>المستلم</th>
                        <th style="width:120px;">الكمية المقبولة</th>
                        <th style="width:120px;">الكمية المرفوضة</th>
                        <th style="min-width:200px;">سبب الرفض</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($items as $i => $item): ?>
                    <tr>
                        <td style="font-weight:600;">
                            <?= e($item['item_name'] ?? '') ?>
                            <?php if (!empty($item['sku'])): ?>
                            <br><code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($item['sku']) ?></code>
                            <?php endif; ?>
                            <?php if (!empty($item['batch_number'])): ?>
                            <br><span style="font-size:11px;color:#6B7280;">دفعة: <?= e($item['batch_number']) ?></span>
                            <?php endif; ?>
                        </td>
                        <td style="font-weight:600;direction:ltr;text-align:left;">
                            <?= number_format((float) ($item['quantity_received'] ?? 0), 3) ?>
                        </td>
                        <td>
                            <input type="hidden" name="grn_item_ids[<?= $i ?>]" value="<?= (int) $item['id'] ?>">
                            <input type="number" name="quantities_accepted[<?= $i ?>]" class="form-input"
                                   step="0.001" min="0" max="<?= (float) $item['quantity_received'] ?>"
                                   value="<?= (float) $item['quantity_received'] ?>"
                                   onchange="calcRejected(<?= $i ?>, <?= (float) $item['quantity_received'] ?>)">
                        </td>
                        <td>
                            <input type="number" name="quantities_rejected[<?= $i ?>]" class="form-input"
                                   step="0.001" min="0" value="0" id="rejected_<?= $i ?>"
                                   onchange="calcAccepted(<?= $i ?>, <?= (float) $item['quantity_received'] ?>)">
                        </td>
                        <td>
                            <input type="text" name="rejection_reasons[<?= $i ?>]" class="form-input" placeholder="سبب الرفض (اختياري)">
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="card" style="margin-bottom:20px;padding:15px;">
        <label class="form-label">ملاحظات الفحص</label>
        <textarea name="inspection_notes" class="form-input" rows="3" placeholder="ملاحظات عامة عن الفحص..."></textarea>
    </div>

    <div style="text-align:left;">
        <button type="submit" class="btn" style="background:#059669;color:#fff;border:none;padding:10px 30px;" onclick="return confirm('هل أنت متأكد من اعتماد نتائج الفحص؟ سيتم تحديث أرصدة المخزون.');">
            <i data-lucide="check-circle" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> اعتماد الفحص
        </button>
    </div>
</form>

<script>
function calcRejected(idx, total) {
    var accepted = parseFloat(document.querySelector('input[name="quantities_accepted['+idx+']"]').value) || 0;
    document.getElementById('rejected_' + idx).value = Math.max(0, total - accepted).toFixed(3);
}

function calcAccepted(idx, total) {
    var rejected = parseFloat(document.getElementById('rejected_' + idx).value) || 0;
    document.querySelector('input[name="quantities_accepted['+idx+']"]').value = Math.max(0, total - rejected).toFixed(3);
}

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