<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>مرتجع مورد <?= e($rtv['rtv_number']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if ($rtv['status'] === 'draft'): ?>
<form method="POST" action="/procurement/rtv/<?= (int) $rtv['id'] ?>/submit" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn" style="background:#D97706;color:#fff;border:none;"><i data-lucide="send" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> تقديم</button></form>
<?php endif; ?>
<?php if ($rtv['status'] === 'submitted'): ?>
<?php if (can('procurement.rtv.approve')): ?>
<form method="POST" action="/procurement/rtv/<?= (int) $rtv['id'] ?>/approve" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn" style="background:#1e3a5f;color:#fff;border:none;"><i data-lucide="check-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> اعتماد</button></form>
<?php endif; ?>
<?php endif; ?>
<?php if ($rtv['status'] === 'approved'): ?>
<?php if (can('procurement.rtv.approve')): ?>
<form method="POST" action="/procurement/rtv/<?= (int) $rtv['id'] ?>/ship" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn" style="background:#2563EB;color:#fff;border:none;"><i data-lucide="truck" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> شحن</button></form>
<?php endif; ?>
<?php endif; ?>
<?php if ($rtv['status'] === 'shipped'): ?>
<?php if (can('procurement.rtv.approve')): ?>
<form method="POST" action="/procurement/rtv/<?= (int) $rtv['id'] ?>/complete" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn" style="background:#059669;color:#fff;border:none;" onclick="return confirm('هل أنت متأكد؟ سيتم خصم المخزون وتسجيل القيد المحاسبي.');"><i data-lucide="check-circle" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> إتمام</button></form>
<?php endif; ?>
<?php endif; ?>
<?php if (in_array($rtv['status'], ['draft', 'submitted'])): ?>
<form method="POST" action="/procurement/rtv/<?= (int) $rtv['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/rtv" 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' => 'مسودة', 'submitted' => 'مُقدّم', 'approved' => 'معتمد', 'shipped' => 'تم الشحن', 'completed' => 'مكتمل', 'cancelled' => 'ملغي'];
$statusColorMap = ['draft' => ['bg' => '#F3F4F6', 'color' => '#6B7280'], 'submitted' => ['bg' => '#FFF7ED', 'color' => '#D97706'], 'approved' => ['bg' => '#F0FDFA', 'color' => '#1e3a5f'], 'shipped' => ['bg' => '#EFF6FF', 'color' => '#2563EB'], 'completed' => ['bg' => '#ECFDF5', 'color' => '#059669'], 'cancelled' => ['bg' => '#FEE2E2', 'color' => '#DC2626']];
$st = $rtv['status'] ?? 'draft';
$stColors = $statusColorMap[$st] ?? $statusColorMap['draft'];
?>

<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="undo-2" style="width:18px;height:18px;color:#1e3a5f;"></i>
        <h3 style="margin:0;color:#1e3a5f;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="background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($rtv['rtv_number']) ?></code></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">المورد</td><td style="padding:10px 0;font-weight:600;"><?= e($rtv['supplier_name'] ?? '') ?></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">المستودع</td><td style="padding:10px 0;"><?= e($rtv['warehouse_name'] ?? '') ?></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">إذن الاستلام</td><td style="padding:10px 0;"><?php if (!empty($rtv['grn_number'])): ?><a href="/procurement/grn/<?= (int) $rtv['grn_id'] ?>" style="color:#1e3a5f;"><?= e($rtv['grn_number']) ?></a><?php else: ?>—<?php endif; ?></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($statusLabels[$st] ?? $st) ?></span></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">تاريخ الإرجاع</td><td style="padding:10px 0;"><?= e($rtv['return_date'] ?? '—') ?></td></tr>
                <tr><td style="padding:10px 0;color:#6B7280;">تاريخ الإنشاء</td><td style="padding:10px 0;"><?= e($rtv['created_at'] ?? '—') ?></td></tr>
            </table>
        </div>
        <?php if (!empty($rtv['reason'])): ?>
        <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($rtv['reason']) ?></div>
        </div>
        <?php endif; ?>
    </div>
</div>

<div class="card" style="margin-bottom:20px;padding:20px;text-align:center;background:linear-gradient(135deg, #DC262610, #DC262620);">
    <div style="font-size:14px;color:#6B7280;margin-bottom:6px;">قيمة المرتجع</div>
    <div style="font-size:36px;font-weight:800;color:#DC2626;direction:ltr;"><?= money($rtv['total_amount'] ?? 0) ?></div>
</div>

<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></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="direction:ltr;text-align:left;font-weight:600;"><?= number_format((float) ($item['quantity'] ?? 0), 3) ?></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>
                    <td style="font-size:12px;"><?= e($item['batch_number'] ?? '—') ?></td>
                    <td style="font-size:12px;"><?= e($item['reason'] ?? '—') ?></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(); ?>
