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

<?php $__template->section('page_actions'); ?>
<?php if (in_array($grn['status'], ['draft', 'inspecting'])): ?>
<a href="/procurement/grn/<?= (int) $grn['id'] ?>/inspect" class="btn" style="background:#D97706;color:#fff;border:none;">
    <i data-lucide="clipboard-check" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> فحص واعتماد
</a>
<form method="POST" action="/procurement/grn/<?= (int) $grn['id'] ?>/cancel" style="display:inline;">
    <?= csrf_field() ?>
    <button type="submit" class="btn" style="background:#FEE2E2;color:#DC2626;border:none;" onclick="return confirm('هل أنت متأكد من إلغاء إذن الاستلام؟');">
        <i data-lucide="x-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> إلغاء
    </button>
</form>
<?php endif; ?>
<a href="/procurement/grn" 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
$statusLabels = ['draft' => 'مسودة', 'inspecting' => 'تحت الفحص', 'accepted' => 'مقبول', 'partial_accept' => 'قبول جزئي', 'cancelled' => 'ملغي'];
$statusColorMap = [
    'draft' => ['bg' => '#F3F4F6', 'color' => '#6B7280'],
    'inspecting' => ['bg' => '#FFF7ED', 'color' => '#D97706'],
    'accepted' => ['bg' => '#ECFDF5', 'color' => '#059669'],
    'partial_accept' => ['bg' => '#EFF6FF', 'color' => '#2563EB'],
    'cancelled' => ['bg' => '#FEE2E2', 'color' => '#DC2626'],
];
$st = $grn['status'] ?? 'draft';
$stColors = $statusColorMap[$st] ?? $statusColorMap['draft'];
$stLabel = $statusLabels[$st] ?? $st;
?>

<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="package-check" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">بيانات إذن الاستلام</h3>
    </div>
    <div style="padding:20px;">
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:0;">
            <table style="width:100%;font-size:14px;">
                <tr>
                    <td style="padding:10px 0;color:#6B7280;width:40%;">رقم الإذن</td>
                    <td style="padding:10px 0;font-weight:600;"><code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($grn['grn_number']) ?></code></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">أمر الشراء</td>
                    <td style="padding:10px 0;">
                        <?php if (!empty($grn['po_number'])): ?>
                        <a href="/inventory/purchase-orders/<?= (int) $grn['purchase_order_id'] ?>" style="color:#0D7377;font-weight:600;text-decoration:none;"><?= e($grn['po_number']) ?></a>
                        <?php else: ?>—<?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">المورد</td>
                    <td style="padding:10px 0;font-weight:600;"><?= e($grn['supplier_name'] ?? '') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">المستودع</td>
                    <td style="padding:10px 0;"><?= e($grn['warehouse_name'] ?? '') ?></td>
                </tr>
            </table>
            <table style="width:100%;font-size:14px;">
                <tr>
                    <td style="padding:10px 0;color:#6B7280;width:40%;">الحالة</td>
                    <td style="padding:10px 0;">
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $stColors['bg'] ?>;color:<?= $stColors['color'] ?>;"><?= e($stLabel) ?></span>
                    </td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">تاريخ الاستلام</td>
                    <td style="padding:10px 0;"><?= e($grn['received_date'] ?? '—') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">المستلم</td>
                    <td style="padding:10px 0;"><?= e($grn['receiver_name'] ?? '—') ?></td>
                </tr>
            </table>
        </div>
        <?php if (!empty($grn['inspection_notes'])): ?>
        <div style="margin-top:20px;padding-top:15px;border-top:1px solid #F3F4F6;">
            <div style="font-size:12px;color:#6B7280;margin-bottom:4px;">ملاحظات الفحص</div>
            <div style="font-size:14px;color:#4B5563;line-height:1.7;"><?= e($grn['inspection_notes']) ?></div>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Totals -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;background:linear-gradient(135deg, #D9770610, #D9770620);">
        <div style="font-size:14px;color:#6B7280;margin-bottom:6px;">قيمة المستلم</div>
        <div style="font-size:28px;font-weight:800;color:#D97706;direction:ltr;"><?= money($grn['total_received_value'] ?? 0) ?></div>
    </div>
    <div class="card" style="padding:20px;text-align:center;background:linear-gradient(135deg, #05966910, #05966920);">
        <div style="font-size:14px;color:#6B7280;margin-bottom:6px;">قيمة المقبول</div>
        <div style="font-size:28px;font-weight:800;color:#059669;direction:ltr;"><?= money($grn['total_accepted_value'] ?? 0) ?></div>
    </div>
</div>

<!-- Items Table -->
<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="package" style="width:18px;height:18px;color:#D97706;"></i>
        <h3 style="margin:0;color:#D97706;font-size:15px;">الأصناف</h3>
    </div>
    <?php if (!empty($items)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الصنف</th>
                    <th>المستلم</th>
                    <th>المقبول</th>
                    <th>المرفوض</th>
                    <th>الدفعة</th>
                    <th>سعر الوحدة</th>
                    <th>الإجمالي</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $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; ?>
                    </td>
                    <td style="font-weight:600;direction:ltr;text-align:left;"><?= number_format((float) ($item['quantity_received'] ?? 0), 3) ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;color:#059669;"><?= number_format((float) ($item['quantity_accepted'] ?? 0), 3) ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;color:<?= (float) ($item['quantity_rejected'] ?? 0) > 0 ? '#DC2626' : '#6B7280' ?>;">
                        <?= number_format((float) ($item['quantity_rejected'] ?? 0), 3) ?>
                        <?php if (!empty($item['rejection_reason'])): ?>
                        <br><span style="font-size:11px;color:#DC2626;font-weight:400;"><?= e($item['rejection_reason']) ?></span>
                        <?php endif; ?>
                    </td>
                    <td style="font-size:12px;"><?= e($item['batch_number'] ?? '—') ?></td>
                    <td style="direction:ltr;text-align:left;"><?= money($item['unit_cost'] ?? 0) ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:left;"><?= money($item['line_total'] ?? 0) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:40px 20px;text-align:center;color:#6B7280;">
        <p style="margin:0;">لا توجد أصناف</p>
    </div>
    <?php endif; ?>
</div>

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