<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تحويل <?= e($transfer->transfer_number) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if ($transfer->status === 'pending_approval'): ?>
<form method="POST" action="/inventory/transfers/<?= (int) $transfer->id ?>/approve" 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 if ($transfer->status === 'approved' || $transfer->status === 'in_transit'): ?>
<form method="POST" action="/inventory/transfers/<?= (int) $transfer->id ?>/receive" style="display:inline;">
    <?= csrf_field() ?>
    <button type="submit" class="btn" style="background:#0D7377;color:#fff;border:none;" onclick="return confirm('هل أنت متأكد من تأكيد استلام هذا التحويل؟');">
        <i data-lucide="package-check" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> استلام
    </button>
</form>
<?php endif; ?>
<a href="/inventory/transfers" 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
$statusColors = [
    'draft'            => ['bg' => '#F3F4F6', 'color' => '#6B7280'],
    'pending_approval' => ['bg' => '#FFF7ED', 'color' => '#D97706'],
    'approved'         => ['bg' => '#F0FDFA', 'color' => '#0D7377'],
    'in_transit'       => ['bg' => '#EFF6FF', 'color' => '#2563EB'],
    'received'         => ['bg' => '#ECFDF5', 'color' => '#059669'],
    'cancelled'        => ['bg' => '#FEE2E2', 'color' => '#DC2626'],
];
$st = $transfer->status ?? 'draft';
$stColors = $statusColors[$st] ?? $statusColors['draft'];
$stLabel = $statuses[$st] ?? $st;
?>

<!-- Transfer Info Card -->
<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="repeat" 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($transfer->transfer_number) ?></code>
                    </td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">من المخزن</td>
                    <td style="padding:10px 0;font-weight:600;">
                        <?= e($fromWarehouse['name_ar'] ?? '') ?>
                        <?php if (!empty($fromWarehouse['code'])): ?>
                        <code style="font-size:11px;background:#F3F4F6;padding:1px 6px;border-radius:4px;margin-right:6px;"><?= e($fromWarehouse['code']) ?></code>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">إلى المخزن</td>
                    <td style="padding:10px 0;font-weight:600;">
                        <?= e($toWarehouse['name_ar'] ?? '') ?>
                        <?php if (!empty($toWarehouse['code'])): ?>
                        <code style="font-size:11px;background:#F3F4F6;padding:1px 6px;border-radius:4px;margin-right:6px;"><?= e($toWarehouse['code']) ?></code>
                        <?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;"><?= e($transfer->transfer_date ?? '—') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">الحالة</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>
                <?php if (!empty($transfer->approved_at)): ?>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">تاريخ الموافقة</td>
                    <td style="padding:10px 0;"><?= e($transfer->approved_at) ?></td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($transfer->received_at)): ?>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">تاريخ الاستلام</td>
                    <td style="padding:10px 0;"><?= e($transfer->received_at) ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

<!-- Transfer Items -->
<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>SKU</th>
                    <th>الوحدة</th>
                    <th>الكمية</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                <tr>
                    <td style="font-weight:600;"><?= e($item['item_name'] ?? '') ?></td>
                    <td>
                        <code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($item['sku'] ?? '—') ?></code>
                    </td>
                    <td><?= e($item['unit_of_measure'] ?? '—') ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:left;"><?= e($item['quantity'] ?? 0) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:40px;text-align:center;color:#9CA3AF;font-size:14px;">
        <i data-lucide="package-open" style="width:36px;height:36px;color:#D1D5DB;margin-bottom:10px;"></i>
        <div>لا توجد أصناف في هذا التحويل</div>
    </div>
    <?php endif; ?>
</div>

<!-- Notes -->
<?php if (!empty($transfer->notes)): ?>
<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="file-text" style="width:18px;height:18px;color:#6B7280;"></i>
        <h3 style="margin:0;color:#6B7280;font-size:15px;">ملاحظات</h3>
    </div>
    <div style="padding:20px;">
        <p style="margin:0;color:#374151;font-size:14px;line-height:1.7;white-space:pre-wrap;"><?= e($transfer->notes) ?></p>
    </div>
</div>
<?php endif; ?>

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