<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<div class="page-header"><div><h1><i class="bi bi-cpu me-2 text-primary"></i>Asset Management</h1></div><a href="<?= base_url('asset-register/create') ?>" class="btn btn-fm-primary btn-sm"><i class="bi bi-plus-lg me-1"></i>Add Asset</a></div>
<div class="fm-card mb-3">
  <?= form_open('assets',['method'=>'get','class'=>'fm-card-body']) ?>
  <div class="row g-2 align-items-end">
    <div class="col-md-4"><input type="text" name="search" class="form-control form-control-sm" placeholder="Search name or code..." value="<?= esc($filters['search']) ?>"></div>
    <div class="col-md-2"><select name="category" class="form-select form-select-sm"><option value="">All Categories</option><?php foreach(['hvac','elevator','electrical','plumbing','fire_safety','security','it','civil','other'] as $c): ?><option <?= $filters['category']==$c?'selected':'' ?> value="<?= $c ?>"><?= ucfirst(str_replace('_',' ',$c)) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-2"><select name="status" class="form-select form-select-sm"><option value="">All Statuses</option><?php foreach(['active','under_maintenance','retired','disposed'] as $s): ?><option <?= $filters['status']==$s?'selected':'' ?> value="<?= $s ?>"><?= ucfirst(str_replace('_',' ',$s)) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-2"><select name="facility" class="form-select form-select-sm"><option value="">All Facilities</option><?php foreach($facilities as $f): ?><option <?= $filters['facility']==$f['id']?'selected':'' ?> value="<?= $f['id'] ?>"><?= esc($f['name']) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-2"><button type="submit" class="btn btn-fm-primary btn-sm w-100"><i class="bi bi-search me-1"></i>Filter</button></div>
  </div>
  <?= form_close() ?>
</div>
<div class="fm-card"><div class="fm-card-body p-0"><div class="table-responsive">
<table class="fm-table"><thead><tr><th>Code</th><th>Name</th><th>Category</th><th>Facility</th><th>Brand/Model</th><th>Status</th><th>Health</th><th>Next PM</th><th>Warranty</th><th>Actions</th></tr></thead><tbody>
<?php foreach($assets as $a): ?><tr>
  <td class="fw-semibold"><a href="<?= base_url('asset-register/view/'.$a['id']) ?>" class="text-primary"><?= esc($a['asset_code']) ?></a></td>
  <td class="small"><?= esc($a['name']) ?></td>
  <td><span class="fm-badge badge-status-active" style="font-size:.65rem"><?= ucfirst(str_replace('_',' ',$a['category'])) ?></span></td>
  <td class="small text-muted"><?= esc($a['facility_name']) ?></td>
  <td class="small text-muted"><?= esc(($a['brand']??'').' '.($a['model']??'')) ?></td>
  <td><span class="fm-badge badge-status-<?= esc($a['status']) ?>"><?= ucfirst(str_replace('_',' ',$a['status'])) ?></span></td>
  <td style="min-width:100px"><?php $h=(int)$a['health_score']; ?><div class="d-flex align-items-center gap-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="x-small"><?= $h ?>%</span></div></td>
  <td class="small <?= ($a['next_maintenance']&&strtotime($a['next_maintenance'])<time())?'text-danger fw-bold':'' ?>"><?= $a['next_maintenance']?date('d M Y',strtotime($a['next_maintenance'])):'—' ?></td>
  <td class="small <?= ($a['warranty_expiry']&&strtotime($a['warranty_expiry'])<time())?'text-danger':'' ?>"><?= $a['warranty_expiry']?date('d M Y',strtotime($a['warranty_expiry'])):'—' ?></td>
  <td><div class="d-flex gap-1">
    <a href="<?= base_url('asset-register/view/'.$a['id']) ?>" class="btn-action bg-primary bg-opacity-10 text-primary"><i class="bi bi-eye"></i></a>
    <a href="<?= base_url('asset-register/edit/'.$a['id']) ?>" class="btn-action bg-warning bg-opacity-10 text-warning"><i class="bi bi-pencil"></i></a>
  </div></td>
</tr><?php endforeach; ?>
<?php if(empty($assets)): ?><tr><td colspan="10" class="text-center py-5 text-muted"><i class="bi bi-cpu d-block mb-2 fs-2"></i>No assets found</td></tr><?php endif; ?>
</tbody></table></div></div></div>
<?= $this->endSection() ?>
