<?php $this->start('body');
    $rows = $this->data['rows'] ?? [];
?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-shapes me-2 text-muted"></i>Asset Types
            <span class="text-muted ms-2" style="font-weight:400;font-size:.7rem;"><?= count($rows); ?> types</span>
        </h5>
        <div class="text-muted small mt-1" style="font-size:.72rem;">Master list of equipment classes maintained across all Services projects.</div>
    </div>
    <button type="button" class="btn btn-sm" id="btn_add" style="background:#7A9E7E;border-color:#7A9E7E;color:#fff;font-weight:600;"><i class="fas fa-plus me-1"></i>Add asset type</button>
</div>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light"><tr><th></th><th>Code</th><th>Name</th><th>Description</th><th class="text-end">Templates</th><th class="text-end">Packs</th><th class="text-end">Parts</th><th>Status</th><th></th></tr></thead>
            <tbody id="at_body">
                <?php foreach ($rows as $r): ?>
                    <tr data-id="<?= (int)$r['c0d']; ?>" data-json='<?= htmlspecialchars(json_encode($r), ENT_QUOTES); ?>'>
                        <td class="text-muted"><?php if (!empty($r['icon'])): ?><i class="fas <?= htmlspecialchars($r['icon']); ?>"></i><?php endif; ?></td>
                        <td class="small"><strong><?= htmlspecialchars($r['code']); ?></strong></td>
                        <td class="small"><?= htmlspecialchars($r['name']); ?></td>
                        <td class="small text-muted"><?= htmlspecialchars((string)($r['description'] ?? '')); ?></td>
                        <td class="text-end small"><?= (int)$r['template_count']; ?></td>
                        <td class="text-end small"><?= (int)$r['pack_count']; ?></td>
                        <td class="text-end small"><?= (int)$r['parts_count']; ?></td>
                        <td>
                            <?php if ((int)$r['is_active'] === 1): ?>
                                <span class="badge bg-success" style="font-size:.6rem;">Active</span>
                            <?php else: ?>
                                <span class="badge bg-light text-muted" style="font-size:.6rem;">Inactive</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-end">
                            <button type="button" class="btn btn-sm btn-link p-0 me-2 at_edit" title="Edit"><i class="fas fa-pen" style="color:#7A8BA1;"></i></button>
                            <button type="button" class="btn btn-sm btn-link p-0 at_del text-danger" title="Remove"><i class="fas fa-times"></i></button>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="9" class="text-center text-muted py-4">No asset types yet.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
(function () {
    var PROOT = '<?= PROOT; ?>';
    function esc(s) { return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) { return ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]); }); }
    function form(d) {
        d = d || {};
        return '<form id="at_form">' +
            '<input type="hidden" name="c0d" value="' + (d.c0d || '') + '">' +
            '<div class="row g-2 mb-2">' +
              '<div class="col-4"><label class="form-label small">Code *</label><input name="code" class="form-control form-control-sm" value="' + esc(d.code||'') + '" placeholder="e.g. GEN" required></div>' +
              '<div class="col-5"><label class="form-label small">Name *</label><input name="name" class="form-control form-control-sm" value="' + esc(d.name||'') + '" required></div>' +
              '<div class="col-3"><label class="form-label small">Icon (FA class)</label><input name="icon" class="form-control form-control-sm" value="' + esc(d.icon||'') + '" placeholder="fa-bolt"></div>' +
            '</div>' +
            '<div class="mb-2"><label class="form-label small">Description</label><input name="description" class="form-control form-control-sm" value="' + esc(d.description||'') + '"></div>' +
            '<div class="form-check form-check-inline"><input type="checkbox" name="is_active" class="form-check-input" ' + (parseInt(d.is_active||1)===1?'checked':'') + '><label class="form-check-label small">Active</label></div>' +
        '</form>';
    }
    function save(d) {
        $.confirm({
            title: d && d.c0d ? 'Edit asset type' : 'Add asset type', columnClass:'col-md-6', content: form(d), type:'green',
            buttons: {
                save: { text:'Save', btnClass:'btn-success', action: function () {
                    var f = document.getElementById('at_form');
                    var p = { c0d: f.c0d.value, code: f.code.value, name: f.name.value, icon: f.icon.value, description: f.description.value, is_active: f.is_active.checked ? 1 : 0 };
                    $.post(PROOT + 'AssetTypes/saveAj', p, function (r) {
                        if (r && r.ok) { rsToast('Saved.','success'); setTimeout(function(){location.reload();}, 400); }
                        else $.alert({title:'Failed', content:(r && r.msg) || 'Server error', type:'red'});
                    }, 'json');
                }},
                cancel: function () {}
            }
        });
    }
    document.getElementById('btn_add').addEventListener('click', function () { save({}); });
    document.getElementById('at_body').addEventListener('click', function (e) {
        var editB = e.target.closest('.at_edit'), delB = e.target.closest('.at_del');
        if (!editB && !delB) return;
        var tr = (editB || delB).closest('tr');
        var data = JSON.parse(tr.getAttribute('data-json'));
        if (editB) { save(data); return; }
        $.confirm({ title:'Remove asset type?', type:'red', content:'<code>' + esc(data.code) + '</code> will be deleted (only allowed if unreferenced).', buttons: {
            yes: { text:'Remove', btnClass:'btn-danger', action: function () {
                $.post(PROOT + 'AssetTypes/deleteAj', { id: data.c0d }, function (r) {
                    if (r && r.ok) { rsToast('Removed.','success'); setTimeout(function(){location.reload();}, 300); }
                    else $.alert({title:'Failed', content:(r && r.msg) || 'Server error', type:'red'});
                }, 'json');
            }},
            cancel: function () {}
        }});
    });
})();
</script>
<?php $this->end(); ?>
