<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<div class="page-header">
<div><h1><i class="bi bi-cpu me-2 text-primary"></i><?= esc($asset['name']) ?></h1><nav aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="<?= base_url('asset-register') ?>">Assets</a></li><li class="breadcrumb-item active"><?= esc($asset['asset_code']) ?></li></ol></nav></div>
<div class="d-flex gap-2"><span class="fm-badge badge-status-<?= esc($asset['status']) ?>"><?= ucfirst(str_replace('_',' ',$asset['status'])) ?></span><a href="<?= base_url('asset-register/edit/'.$asset['id']) ?>" class="btn btn-fm-outline btn-sm">Edit</a></div>
</div>
<div class="row g-3">
<div class="col-lg-8">
<div class="fm-form-section"><h6><i class="bi bi-info-circle"></i>Asset Details</h6>
<div class="row g-3">
<div class="col-md-4"><div class="small text-muted">Code</div><div class="fw-semibold"><?= esc($asset['asset_code']) ?></div></div>
<div class="col-md-4"><div class="small text-muted">Facility</div><div class="fw-semibold"><?= esc($asset['facility_name']) ?></div></div>
<div class="col-md-4"><div class="small text-muted">Category</div><span class="fm-badge" style="background:#e8f4fd;color:#1565C0;border:1px solid #90CAF9"><?= ucfirst(str_replace('_',' ',$asset['category'])) ?></span></div>
<div class="col-md-4"><div class="small text-muted">Brand</div><div><?= esc($asset['brand'] ?? '—') ?></div></div>
<div class="col-md-4"><div class="small text-muted">Model</div><div><?= esc($asset['model'] ?? '—') ?></div></div>
<div class="col-md-4"><div class="small text-muted">Serial No.</div><div><?= esc($asset['serial_number'] ?? '—') ?></div></div>
<div class="col-md-4"><div class="small text-muted">Purchase Date</div><div><?= $asset['purchase_date'] ? date('d M Y',strtotime($asset['purchase_date'])) : '—' ?></div></div>
<div class="col-md-4"><div class="small text-muted">Purchase Cost</div><div class="fw-semibold"><?= $asset['purchase_cost'] ? $currency.' '.number_format($asset['purchase_cost'],2) : '—' ?></div></div>
<div class="col-md-4"><div class="small text-muted">Warranty Expires</div><div class="<?= ($asset['warranty_expiry'] && strtotime($asset['warranty_expiry']) < time()) ? 'text-danger fw-bold' : '' ?>"><?= $asset['warranty_expiry'] ? date('d M Y',strtotime($asset['warranty_expiry'])) : '—' ?></div></div>
<div class="col-12"><div class="small text-muted">Health Score</div>
<?php $h=(int)$asset['health_score']; ?><div class="d-flex align-items-center gap-3 mt-1"><div class="health-bar flex-grow-1"><div class="health-bar-fill <?= $h>=80?'good':($h>=50?'warn':'bad') ?>" style="width:<?= $h ?>%"></div></div><span class="fw-bold fs-5"><?= $h ?>%</span></div></div>
</div></div>
<div class="fm-form-section"><h6><i class="bi bi-clock-history"></i>Work Order History</h6>
<table class="fm-table"><thead><tr><th>WO #</th><th>Title</th><th>Type</th><th>Status</th><th>Date</th></tr></thead><tbody>
<?php foreach($workOrders as $w): ?><tr><td><a href="<?= base_url('workorders/view/'.$w['id']) ?>" class="text-primary"><?= esc($w['wo_number']) ?></a></td><td class="small"><?= esc(substr($w['title'],0,40)) ?></td><td class="small"><?= ucfirst($w['type']) ?></td><td><span class="fm-badge badge-status-<?= esc($w['status']) ?>"><?= ucfirst(str_replace('_',' ',$w['status'])) ?></span></td><td class="small"><?= date('d M Y',strtotime($w['created_at'])) ?></td></tr><?php endforeach; ?>
<?php if(empty($workOrders)): ?><tr><td colspan="5" class="text-center text-muted py-3">No work orders for this asset</td></tr><?php endif; ?>
</tbody></table></div>
</div>
<div class="col-lg-4">
<div class="fm-form-section"><h6><i class="bi bi-calendar-check"></i>Maintenance Schedule</h6>
<div class="small mb-2"><span class="text-muted">Last Maintenance:</span> <strong><?= $asset['last_maintenance'] ? date('d M Y',strtotime($asset['last_maintenance'])) : '—' ?></strong></div>
<div class="small"><span class="text-muted">Next Maintenance:</span> <strong class="<?= ($asset['next_maintenance'] && strtotime($asset['next_maintenance']) < time()) ? 'text-danger' : '' ?>"><?= $asset['next_maintenance'] ? date('d M Y',strtotime($asset['next_maintenance'])) : '—' ?></strong></div>
<hr><a href="<?= base_url('workorders/create') ?>" class="btn btn-fm-primary btn-sm w-100">Create Work Order for Asset</a>
</div>
</div></div>
<?= $this->endSection() ?>
