<?php $this->start('body');
    $rows = $this->data['rows'] ?? [];
    $cats = $this->data['categories'] ?? [];
    $f    = $this->data['filter'] ?? [];
    $q    = 'status=' . urlencode((string)($f['status'] ?? '')) . '&category_id=' . (int)($f['category_id'] ?? 0) . '&q=' . urlencode((string)($f['q'] ?? ''));

    $totalCost = 0; $totalAcc = 0; $totalNbv = 0;
    foreach ($rows as $a) {
        $totalCost += (float)$a['purchase_cost']; $totalAcc += (float)$a['accumulated_depreciation'];
        $totalNbv += ((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'],
    ];
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;color:#334155;"><i class="fas fa-boxes-stacked me-2" style="color:#7A8BA1;"></i>Fixed Assets · Register</h5>
    <div class="d-flex gap-2">
        <a href="<?= PROOT; ?>Assets/xlsx?<?= $q; ?>" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-file-excel me-1" style="color:#7A9E7E;"></i>Excel</a>
        <a href="<?= PROOT; ?>Assets/pdf?<?= $q; ?>"  class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-file-pdf   me-1" style="color:#C48579;"></i>PDF</a>
        <?php if (UserGroups::can('Assets','runDepreciation')): ?>
        <a href="<?= PROOT; ?>Assets/runDepreciation" class="btn btn-sm" style="background:#F1F5F9;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-calculator me-1" style="color:#7A8BA1;"></i>Run monthly depreciation</a>
        <?php endif; ?>
        <?php if (UserGroups::can('Assets','add')): ?>
        <a href="<?= PROOT; ?>Assets/add" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-plus me-1"></i>Add asset</a>
        <?php endif; ?>
    </div>
</div>

<!-- ── KPIs ─────────────────────────────────────────────────────────────── -->
<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;">Total assets</div>
        <div style="font-weight:700;font-size:1.4rem;color:#334155;"><?= count($rows); ?></div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #7A9E7E !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Total cost</div>
        <div style="font-weight:700;font-size:1.4rem;color:#334155;">TZS <?= number_format($totalCost); ?></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($totalAcc); ?></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($totalNbv); ?></div>
    </div></div></div>
</div>

<!-- ── Filters ─────────────────────────────────────────────────────────── -->
<form method="get" action="<?= PROOT; ?>Assets" class="card border-0 shadow-sm mb-3" style="background:#F8FAFC;">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label" style="color:#334155;">Search</label>
                <input type="text" name="q" value="<?= htmlspecialchars((string)($f['q'] ?? '')); ?>" placeholder="asset #, name, serial, custodian…" class="form-control form-control-sm"></div>
            <div class="col-md-3"><label class="form-label" style="color:#334155;">Category</label>
                <select name="category_id" class="form-select form-select-sm">
                    <option value="0">All categories</option>
                    <?php foreach ($cats as $c): ?>
                        <option value="<?= (int)$c['c0d']; ?>" <?= (int)($f['category_id']??0) === (int)$c['c0d'] ? 'selected' : ''; ?>><?= htmlspecialchars($c['name']); ?></option>
                    <?php endforeach; ?>
                </select></div>
            <div class="col-md-3"><label class="form-label" style="color:#334155;">Status</label>
                <select name="status" class="form-select form-select-sm">
                    <?php foreach (['','IN_SERVICE','UNDER_MAINTENANCE','DISPOSED','LOST'] as $s): ?>
                        <option value="<?= $s; ?>" <?= ($f['status']??'') === $s ? 'selected' : ''; ?>><?= $s === '' ? 'All statuses' : ucfirst(strtolower(str_replace('_',' ',$s))); ?></option>
                    <?php endforeach; ?>
                </select></div>
            <div class="col-md-3 text-end">
                <button type="submit" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-filter me-1"></i>Filter</button>
            </div>
        </div>
    </div>
</form>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead style="background:#F1F5F9;">
                <tr>
                    <th style="color:#334155;">Asset #</th>
                    <th style="color:#334155;">Name</th>
                    <th style="color:#334155;">Category</th>
                    <th style="color:#334155;">Purchased</th>
                    <th class="text-end" style="color:#334155;">Cost</th>
                    <th class="text-end" style="color:#334155;">Acc. dep.</th>
                    <th class="text-end" style="color:#334155;">NBV</th>
                    <th style="color:#334155;">Status</th>
                    <th style="color:#334155;">Custodian</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $a):
                    $nbv = (float)$a['purchase_cost'] - (float)$a['accumulated_depreciation'];
                    $chip = $statusChip[$a['status']] ?? ['bg'=>'#F1F5F9','ink'=>'#64748B','label'=>$a['status']];
                ?>
                    <tr>
                        <td style="color:#334155;font-weight:500;"><?= htmlspecialchars($a['asset_no']); ?></td>
                        <td style="color:#334155;"><?= htmlspecialchars($a['name']); ?>
                            <?php if (!empty($a['serial_no'])): ?><div class="small" style="color:#94A3B8;font-size:.68rem;">SN: <?= htmlspecialchars((string)$a['serial_no']); ?></div><?php endif; ?>
                        </td>
                        <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$a['category_name']); ?></td>
                        <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars($a['purchase_date']); ?></td>
                        <td class="text-end" style="color:#334155;"><?= number_format((float)$a['purchase_cost']); ?></td>
                        <td class="text-end" style="color:#C48579;"><?= number_format((float)$a['accumulated_depreciation']); ?></td>
                        <td class="text-end" style="font-weight:600;color:#334155;"><?= number_format($nbv); ?></td>
                        <td><span class="badge" style="background:<?= $chip['bg']; ?>;color:<?= $chip['ink']; ?>;font-size:.62rem;text-transform:capitalize;"><?= $chip['label']; ?></span></td>
                        <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$a['custodian_name']); ?></td>
                        <td class="text-end">
                            <a href="<?= PROOT; ?>Assets/view/<?= (int)$a['c0d']; ?>" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-eye me-1" style="color:#7A8BA1;"></i>View</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="10" class="text-center py-4" style="color:#94A3B8;">
                        No assets. <a href="<?= PROOT; ?>Assets/add" style="color:#7A8BA1;font-weight:600;">Add your first asset</a>.
                    </td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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