<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>أصل <?= e($asset['asset_tag']) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/inventory/assets" 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
$statusMap = [
    'active'     => ['bg' => '#ECFDF5', 'color' => '#059669', 'label' => 'نشط'],
    'disposed'   => ['bg' => '#FEE2E2', 'color' => '#DC2626', 'label' => 'مستبعد'],
    'fully_depreciated' => ['bg' => '#FFF7ED', 'color' => '#D97706', 'label' => 'مهلك بالكامل'],
    'inactive'   => ['bg' => '#F3F4F6', 'color' => '#6B7280', 'label' => 'غير نشط'],
];
$depMethodLabels = [
    'straight_line'     => 'القسط الثابت',
    'declining_balance' => 'القسط المتناقص',
];
$asSt = $asset['status'] ?? 'active';
$asStInfo = $statusMap[$asSt] ?? $statusMap['active'];
$dm = $asset['depreciation_method'] ?? 'straight_line';
$dmLabel = $depMethodLabels[$dm] ?? $dm;

$purchaseCost = (float) ($asset['purchase_cost'] ?? 0);
$salvageValue = (float) ($asset['salvage_value'] ?? 0);
$depreciableAmount = $purchaseCost - $salvageValue;
$accumulatedDep = (float) ($asset['accumulated_depreciation'] ?? 0);
$bookValue = (float) ($asset['book_value'] ?? 0);
$depPercent = $depreciableAmount > 0 ? min(100, round(($accumulatedDep / $depreciableAmount) * 100, 1)) : 0;
?>

<!-- Asset 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="landmark" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">بيانات الأصل</h3>
        <div style="margin-right:auto;">
            <span style="display:inline-block;padding:3px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $asStInfo['bg'] ?>;color:<?= $asStInfo['color'] ?>;"><?= e($asStInfo['label']) ?></span>
        </div>
    </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($asset['asset_tag']) ?></code>
                    </td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">الصنف</td>
                    <td style="padding:10px 0;font-weight:600;"><?= e($asset['item_name'] ?? '') ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">SKU</td>
                    <td style="padding:10px 0;">
                        <code style="font-size:11px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($asset['sku'] ?? '—') ?></code>
                    </td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">المخزن</td>
                    <td style="padding:10px 0;font-weight:600;"><?= e($asset['warehouse_name'] ?? '') ?></td>
                </tr>
                <?php if (!empty($asset['serial_number'])): ?>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">الرقم التسلسلي</td>
                    <td style="padding:10px 0;font-family:monospace;direction:ltr;text-align:left;"><?= e($asset['serial_number']) ?></td>
                </tr>
                <?php endif; ?>
            </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($asset['purchase_date'] ?? '—') ?></td>
                </tr>
                <?php if (!empty($asset['warranty_expiry'])): ?>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">انتهاء الضمان</td>
                    <td style="padding:10px 0;"><?= e($asset['warranty_expiry']) ?></td>
                </tr>
                <?php endif; ?>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">طريقة الإهلاك</td>
                    <td style="padding:10px 0;font-weight:600;"><?= e($dmLabel) ?></td>
                </tr>
                <tr>
                    <td style="padding:10px 0;color:#6B7280;">العمر الافتراضي (شهور)</td>
                    <td style="padding:10px 0;font-weight:600;"><?= (int) ($asset['useful_life_months'] ?? 0) ?></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:<?= $asStInfo['bg'] ?>;color:<?= $asStInfo['color'] ?>;"><?= e($asStInfo['label']) ?></span>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</div>

<!-- Financial Summary -->
<div style="display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:13px;color:#6B7280;margin-bottom:6px;">تكلفة الشراء</div>
        <div style="font-size:22px;font-weight:800;color:#1A1A2E;direction:ltr;"><?= money($purchaseCost) ?></div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:13px;color:#6B7280;margin-bottom:6px;">قيمة الإنقاذ</div>
        <div style="font-size:22px;font-weight:800;color:#6B7280;direction:ltr;"><?= money($salvageValue) ?></div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:13px;color:#6B7280;margin-bottom:6px;">الإهلاك المتراكم</div>
        <div style="font-size:22px;font-weight:800;color:#D97706;direction:ltr;"><?= money($accumulatedDep) ?></div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:13px;color:#6B7280;margin-bottom:6px;">القيمة الدفترية</div>
        <div style="font-size:22px;font-weight:800;color:#0D7377;direction:ltr;"><?= money($bookValue) ?></div>
    </div>
</div>

<!-- Depreciation Progress Bar -->
<div class="card" style="margin-bottom:20px;padding:20px;">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:10px;">
        <span style="font-size:13px;font-weight:600;color:#374151;">نسبة الإهلاك</span>
        <span style="font-size:13px;font-weight:700;color:#D97706;"><?= $depPercent ?>%</span>
    </div>
    <div style="width:100%;height:16px;background:#F3F4F6;border-radius:8px;overflow:hidden;">
        <div style="width:<?= $depPercent ?>%;height:100%;background:linear-gradient(90deg, #0D7377, #059669);border-radius:8px;transition:width 0.5s ease;"></div>
    </div>
    <div style="display:flex;justify-content:space-between;margin-top:6px;font-size:11px;color:#9CA3AF;">
        <span>المبلغ القابل للإهلاك: <?= money($depreciableAmount) ?></span>
        <span>المتبقي: <?= money(max(0, $depreciableAmount - $accumulatedDep)) ?></span>
    </div>
</div>

<!-- Dispose Form (only for active assets) -->
<?php if ($asSt === 'active'): ?>
<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="trash-2" style="width:18px;height:18px;color:#DC2626;"></i>
        <h3 style="margin:0;color:#DC2626;font-size:15px;">استبعاد الأصل</h3>
    </div>
    <div style="padding:20px;">
        <form method="POST" action="/inventory/assets/<?= (int) $asset['id'] ?>/dispose" id="disposeForm" onsubmit="return confirm('هل أنت متأكد من استبعاد هذا الأصل؟ هذا الإجراء لا يمكن التراجع عنه.');">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:1fr 2fr;gap:20px;align-items:end;">
                <div class="form-group">
                    <label class="form-label">قيمة الاستبعاد</label>
                    <input type="number" name="disposal_value" value="0" class="form-input" step="0.01" min="0" placeholder="0.00" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">سبب الاستبعاد <span style="color:#DC2626;">*</span></label>
                    <input type="text" name="disposal_reason" class="form-input" required placeholder="مثال: تالف، نهاية العمر الافتراضي...">
                </div>
            </div>
            <div style="margin-top:15px;">
                <button type="submit" class="btn" style="background:#DC2626;color:#fff;border:none;padding:10px 24px;">
                    <i data-lucide="trash-2" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> استبعاد الأصل
                </button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

<!-- Depreciation History -->
<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="history" style="width:18px;height:18px;color:#D97706;"></i>
        <h3 style="margin:0;color:#D97706;font-size:15px;">سجل الإهلاك</h3>
    </div>
    <?php if (!empty($depreciationHistory)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الفترة</th>
                    <th>مبلغ الإهلاك</th>
                    <th>الإهلاك المتراكم</th>
                    <th>القيمة الدفترية</th>
                    <th>الطريقة</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($depreciationHistory as $dep): ?>
                <?php
                $depMethod = $dep['method_used'] ?? 'straight_line';
                $depMethodLabel = $depMethodLabels[$depMethod] ?? $depMethod;
                ?>
                <tr>
                    <td style="font-weight:600;white-space:nowrap;"><?= e($dep['period_month'] ?? '') ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:left;color:#DC2626;"><?= money($dep['depreciation_amount'] ?? 0) ?></td>
                    <td style="font-weight:600;direction:ltr;text-align:left;color:#D97706;"><?= money($dep['accumulated_total'] ?? 0) ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:left;color:#0D7377;"><?= money($dep['book_value_after'] ?? 0) ?></td>
                    <td style="font-size:13px;"><?= e($depMethodLabel) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:40px 20px;text-align:center;color:#6B7280;">
        <i data-lucide="calendar-x" style="width:36px;height:36px;color:#D1D5DB;margin-bottom:8px;"></i>
        <p style="margin:0;">لا يوجد سجل إهلاك لهذا الأصل بعد</p>
    </div>
    <?php endif; ?>
</div>

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