<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><span class="rs-swatch" style="background: <?= e($type['color'] ?? '#0f172a') ?>"></span><?= e($type['name']) ?></h5>
    <div class="d-flex gap-2">
        <a class="btn btn-sm btn-outline-primary" href="<?= url('/AssetTypes/edit/' . (int) $type['asset_type_id']) ?>"><i class="fas fa-pen me-1"></i>Edit Type</a>
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/AssetTypes') ?>"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<p class="text-muted small">Code: <code><?= e($type['code']) ?></code></p>

<div class="rs-card p-3 mb-3">
    <h6 class="fw-bold mb-2">Custom Fields</h6>
    <p class="text-muted small">These fields appear on the form when creating or editing an asset of this type.</p>
    <?php if (empty($fields)): ?>
        <p class="text-muted small mb-0">No fields yet. Add one below.</p>
    <?php else: ?>
        <table class="table table-sm table-hover mb-0">
            <thead><tr><th>Label</th><th>Key</th><th>Type</th><th>Unit</th><th>Section</th><th class="text-center">Req</th><th class="text-end">Order</th><th></th></tr></thead>
            <tbody>
                <?php foreach ($fields as $f): ?>
                    <tr>
                        <td><?= e($f['field_label']) ?></td>
                        <td><code><?= e($f['field_key']) ?></code></td>
                        <td class="small"><?= e($f['field_type']) ?></td>
                        <td class="small"><?= e($f['field_unit'] ?? '') ?></td>
                        <td class="small"><?= e($f['field_section']) ?></td>
                        <td class="text-center"><?= $f['is_required'] ? '<i class="fas fa-check text-success"></i>' : '' ?></td>
                        <td class="text-end"><?= (int) $f['sort_order'] ?></td>
                        <td class="text-end"><a href="<?= url('/AssetTypes/deleteField/' . (int) $f['field_id']) ?>" class="text-danger" onclick="return confirm('Delete?')"><i class="fas fa-times"></i></a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>

<div class="rs-card p-3">
    <h6 class="fw-bold mb-2">Add Field</h6>
    <form method="post" action="<?= url('/AssetTypes/saveField') ?>">
        <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
        <input type="hidden" name="asset_type_id" value="<?= (int) $type['asset_type_id'] ?>">
        <div class="row g-2">
            <div class="col-md-4"><label class="form-label">Label *</label><input type="text" class="form-control form-control-sm" name="field_label" required placeholder="e.g. kVA Rating"></div>
            <div class="col-md-3"><label class="form-label">Key *</label><input type="text" class="form-control form-control-sm" name="field_key" required placeholder="e.g. kva_rating"></div>
            <div class="col-md-2"><label class="form-label">Type</label>
                <select name="field_type" class="form-select form-select-sm">
                    <?php foreach (['text','number','date','datetime','select','multiselect','textarea','boolean'] as $t): ?>
                        <option value="<?= $t ?>"><?= $t ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2"><label class="form-label">Unit</label><input type="text" class="form-control form-control-sm" name="field_unit" placeholder="kVA, L, hrs"></div>
            <div class="col-md-1"><label class="form-label">Order</label><input type="number" class="form-control form-control-sm" name="sort_order" value="0"></div>
            <div class="col-md-4"><label class="form-label">Section</label><input type="text" class="form-control form-control-sm" name="field_section" value="General"></div>
            <div class="col-md-6"><label class="form-label">Options (JSON array for select)</label><input type="text" class="form-control form-control-sm" name="field_options" placeholder='["diesel","petrol"]'></div>
            <div class="col-md-2 d-flex align-items-end"><label class="form-check-label"><input type="checkbox" class="form-check-input me-1" name="is_required" value="1"> Required</label></div>
            <div class="col-md-6"><label class="form-label">Placeholder</label><input type="text" class="form-control form-control-sm" name="placeholder"></div>
            <div class="col-md-6"><label class="form-label">Help Text</label><input type="text" class="form-control form-control-sm" name="help_text"></div>
        </div>
        <div class="mt-3"><button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i>Add Field</button></div>
    </form>
</div>
