<?php $this->start('body');
    $a = $this->data['asset'] ?? [];
    $dh = $this->data['dep_history'] ?? [];
    $nbv = (float)$a['purchase_cost'] - (float)$a['accumulated_depreciation'];
    $statusChip = [
        'IN_SERVICE'        => ['bg'=>'#E2E8F0','ink'=>'#334155','label'=>'In service'],
        'UNDER_MAINTENANCE' => ['bg'=>'#FEF3C7','ink'=>'#92400E','label'=>'Maintenance'],
        'DISPOSED'          => ['bg'=>'#F1F5F9','ink'=>'#64748B','label'=>'Disposed'],
        'LOST'              => ['bg'=>'#FEE2E2','ink'=>'#B91C1C','label'=>'Lost'],
    ];
    $chip = $statusChip[$a['status']] ?? ['bg'=>'#F1F5F9','ink'=>'#64748B','label'=>$a['status']];
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;color:#334155;"><i class="fas fa-cube me-2" style="color:#7A8BA1;"></i><?= htmlspecialchars($a['asset_no']); ?></h5>
        <div class="text-muted small mt-1" style="color:#64748B;"><?= htmlspecialchars($a['name']); ?> · <?= htmlspecialchars((string)$a['category_name']); ?></div>
    </div>
    <div class="d-flex align-items-center gap-2">
        <span class="badge" style="background:<?= $chip['bg']; ?>;color:<?= $chip['ink']; ?>;font-size:.68rem;padding:.35rem .7rem;"><?= $chip['label']; ?></span>
        <?php if ($a['status'] === 'IN_SERVICE' && UserGroups::can('Assets','dispose')): ?>
            <a href="<?= PROOT; ?>Assets/dispose/<?= (int)$a['c0d']; ?>" class="btn btn-sm" style="background:#C48579;border-color:#C48579;color:#fff;font-weight:600;"><i class="fas fa-hand-holding-usd me-1"></i>Dispose</a>
        <?php endif; ?>
        <a href="<?= PROOT; ?>Assets" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<?php $disposal = $this->data['disposal'] ?? null; if ($disposal): ?>
<div class="alert py-2 mb-3" style="background:#F1F5F9;border:1px solid #E2E8F0;color:#334155;font-size:.86rem;">
    <i class="fas fa-hand-holding-usd me-1" style="color:#7A8BA1;"></i>
    <strong>Disposed on <?= htmlspecialchars($disposal['disposal_date']); ?></strong> via <?= htmlspecialchars(strtolower(str_replace('_', ' ', $disposal['disposal_method']))); ?>
    <?php if ((float)$disposal['sale_price'] > 0): ?> · sale price TZS <?= number_format((float)$disposal['sale_price']); ?><?php endif; ?>
    <?php if (!empty($disposal['buyer_name'])): ?> · to <?= htmlspecialchars((string)$disposal['buyer_name']); ?><?php endif; ?>
    · NBV at disposal TZS <?= number_format((float)$disposal['net_book_value_at_disposal']); ?>
    · <?php $gl = (float)$disposal['gain_or_loss']; if ($gl > 0.01): ?><span style="color:#7A9E7E;font-weight:600;">gain TZS <?= number_format($gl); ?></span><?php elseif ($gl < -0.01): ?><span style="color:#C48579;font-weight:600;">loss TZS <?= number_format(abs($gl)); ?></span><?php else: ?>break-even<?php endif; ?>
    <?php if (!empty($disposal['je_no'])): ?> · <a href="<?= PROOT; ?>Journals?q=<?= urlencode((string)$disposal['je_no']); ?>" style="color:#7A8BA1;">JE#<?= htmlspecialchars((string)$disposal['je_no']); ?></a><?php endif; ?>
</div>
<?php endif; ?>

<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #7A8BA1 !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Purchase cost</div>
        <div style="font-weight:700;font-size:1.4rem;color:#334155;">TZS <?= number_format((float)$a['purchase_cost']); ?></div>
        <div class="text-muted small mt-1" style="color:#64748B;">on <?= htmlspecialchars($a['purchase_date']); ?></div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #C48579 !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Accumulated depreciation</div>
        <div style="font-weight:700;font-size:1.4rem;color:#C48579;">− TZS <?= number_format((float)$a['accumulated_depreciation']); ?></div>
        <div class="text-muted small mt-1" style="color:#64748B;"><?= count($dh); ?> month(s) posted</div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #334155 !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Net book value</div>
        <div style="font-weight:700;font-size:1.4rem;color:#334155;">TZS <?= number_format($nbv); ?></div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Method</div>
        <div style="font-weight:600;font-size:1.05rem;color:#334155;"><?= $a['depreciation_method'] === 'RB' ? 'Reducing balance' : 'Straight-line'; ?></div>
        <div class="text-muted small mt-1" style="color:#64748B;"><?= (int)$a['useful_life_months']; ?> months · salvage TZS <?= number_format((float)$a['salvage_value']); ?></div>
    </div></div></div>
</div>

<div class="row g-3">
    <div class="col-md-5">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white" style="border-bottom:1px solid #E2E8F0;"><strong style="color:#334155;"><i class="fas fa-info-circle me-2" style="color:#7A8BA1;"></i>Details</strong></div>
            <div class="card-body p-3">
                <dl class="row mb-0" style="font-size:.86rem;color:#334155;">
                    <dt class="col-5" style="color:#64748B;font-weight:500;">Serial no</dt>          <dd class="col-7"><?= htmlspecialchars((string)$a['serial_no']) ?: '—'; ?></dd>
                    <dt class="col-5" style="color:#64748B;font-weight:500;">Custodian</dt>          <dd class="col-7"><?= htmlspecialchars((string)$a['custodian_name']) ?: '—'; ?></dd>
                    <dt class="col-5" style="color:#64748B;font-weight:500;">Location</dt>           <dd class="col-7"><?= htmlspecialchars((string)$a['location']) ?: '—'; ?></dd>
                    <dt class="col-5" style="color:#64748B;font-weight:500;">Dep. start</dt>         <dd class="col-7"><?= htmlspecialchars((string)$a['depreciation_start_date']) ?: '—'; ?></dd>
                    <?php if (!empty($a['description'])): ?>
                        <dt class="col-5" style="color:#64748B;font-weight:500;">Description</dt><dd class="col-7"><?= nl2br(htmlspecialchars((string)$a['description'])); ?></dd>
                    <?php endif; ?>
                    <?php if (!empty($a['notes'])): ?>
                        <dt class="col-5" style="color:#64748B;font-weight:500;">Notes</dt><dd class="col-7"><?= nl2br(htmlspecialchars((string)$a['notes'])); ?></dd>
                    <?php endif; ?>
                </dl>
            </div>
        </div>
    </div>

    <div class="col-md-7">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white d-flex align-items-center justify-content-between" style="border-bottom:1px solid #E2E8F0;">
                <strong style="color:#334155;"><i class="fas fa-history me-2" style="color:#7A8BA1;"></i>Depreciation history</strong>
                <span class="text-muted small" style="color:#64748B;"><?= count($dh); ?> row(s)</span>
            </div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead style="background:#F1F5F9;">
                        <tr>
                            <th style="color:#334155;">Period</th>
                            <th class="text-end" style="color:#334155;">Depreciation</th>
                            <th class="text-end" style="color:#334155;">Book value after</th>
                            <th style="color:#334155;">JE</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($dh as $d): ?>
                            <tr>
                                <td style="color:#334155;"><?= sprintf('%04d-%02d', (int)$d['period_year'], (int)$d['period_month']); ?></td>
                                <td class="text-end" style="color:#C48579;">− <?= number_format((float)$d['depreciation_amount']); ?></td>
                                <td class="text-end" style="color:#334155;"><?= number_format((float)$d['book_value_after']); ?></td>
                                <td class="text-muted small" style="color:#64748B;">
                                    <?php if (!empty($d['je_no'])): ?><a href="<?= PROOT; ?>Journals?q=<?= urlencode((string)$d['je_no']); ?>" style="color:#7A8BA1;">JE#<?= htmlspecialchars((string)$d['je_no']); ?></a><?php else: ?>—<?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($dh)): ?>
                            <tr><td colspan="4" class="text-center py-3" style="color:#94A3B8;">No depreciation posted yet.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php $this->end(); ?>
