<?php
$meta = !empty($asset['meta_json']) ? (json_decode($asset['meta_json'], true) ?: []) : [];
$sections = [];
foreach ($fields as $f) { $sections[$f['field_section'] ?: 'General'][] = $f; }
?>
<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><?= $asset ? 'Edit' : 'New' ?> <?= e($type['name']) ?></h5>
    <a href="<?= url('/Assets') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<form method="post" action="<?= url('/Assets/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="asset_id" value="<?= (int) ($asset['asset_id'] ?? 0) ?>">
    <input type="hidden" name="asset_type_id" value="<?= (int) $type['asset_type_id'] ?>">

    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2">Basics</h6>
        <div class="row g-2">
            <div class="col-md-6"><label class="form-label">Name *</label><input type="text" class="form-control form-control-sm" name="name" required value="<?= e($asset['name'] ?? '') ?>"></div>
            <div class="col-md-3"><label class="form-label">Asset Tag</label><input type="text" class="form-control form-control-sm" name="asset_tag" value="<?= e($asset['asset_tag'] ?? '') ?>"></div>
            <div class="col-md-3"><label class="form-label">Serial</label><input type="text" class="form-control form-control-sm" name="serial_number" value="<?= e($asset['serial_number'] ?? '') ?>"></div>
            <div class="col-md-3"><label class="form-label">Code</label><input type="text" class="form-control form-control-sm" name="code" value="<?= e($asset['code'] ?? '') ?>"></div>
            <div class="col-md-3"><label class="form-label">Site</label>
                <select name="site_id" class="form-select form-select-sm">
                    <option value="">— none —</option>
                    <?php foreach ($sites as $s): ?><option value="<?= (int) $s['site_id'] ?>" <?= (int) ($asset['site_id'] ?? 0) === (int) $s['site_id'] ? 'selected' : '' ?>><?= e($s['name']) ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3"><label class="form-label">Parent Asset</label>
                <select name="parent_asset_id" class="form-select form-select-sm">
                    <option value="">— none —</option>
                    <?php foreach ($parents as $p): ?>
                        <?php $prefix = str_repeat('— ', (int) ($p['depth'] ?? 0)); ?>
                        <option value="<?= (int) $p['asset_id'] ?>" <?= (int) ($asset['parent_asset_id'] ?? 0) === (int) $p['asset_id'] ? 'selected' : '' ?>><?= e($prefix . $p['name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3"><label class="form-label">Status</label>
                <select name="status" class="form-select form-select-sm">
                    <?php foreach (['active','inactive','scrapped','in_transit'] as $s): ?><option <?= ($asset['status'] ?? 'active') === $s ? 'selected' : '' ?>><?= $s ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3"><label class="form-label">Criticality</label>
                <select name="criticality" class="form-select form-select-sm" title="A = critical (drives PM every week), B = important, C = standard">
                    <?php foreach (['A' => 'A · Critical', 'B' => 'B · Important', 'C' => 'C · Standard'] as $val => $lbl): ?>
                        <option value="<?= $val ?>" <?= ($asset['criticality'] ?? 'C') === $val ? 'selected' : '' ?>><?= $lbl ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3"><label class="form-label">Installed On</label><input type="date" class="form-control form-control-sm" name="installed_at" value="<?= e($asset['installed_at'] ?? '') ?>"></div>
            <div class="col-md-3"><label class="form-label">Warranty Expires</label><input type="date" class="form-control form-control-sm" name="warranty_expires_at" value="<?= e($asset['warranty_expires_at'] ?? '') ?>"></div>
            <div class="col-12"><label class="form-label">Notes</label><textarea class="form-control form-control-sm" name="notes" rows="2"><?= e($asset['notes'] ?? '') ?></textarea></div>
        </div>
    </div>

    <?php foreach ($sections as $sectionName => $sectionFields): ?>
        <div class="rs-card p-3 mb-3">
            <h6 class="fw-bold mb-2"><?= e($sectionName) ?></h6>
            <div class="row g-2">
                <?php foreach ($sectionFields as $f):
                    $val = $meta[$f['field_key']] ?? '';
                    $req = $f['is_required'] ? ' *' : '';
                    $reqAttr = $f['is_required'] ? 'required' : '';
                    $inputName = 'meta[' . $f['field_key'] . ']';
                    $options = !empty($f['field_options']) ? (json_decode($f['field_options'], true) ?: []) : [];
                ?>
                    <div class="col-md-4">
                        <label class="form-label"><?= e($f['field_label']) ?><?= $req ?><?php if ($f['field_unit']): ?> <span class="text-muted">(<?= e($f['field_unit']) ?>)</span><?php endif; ?></label>
                        <?php if ($f['field_type'] === 'textarea'): ?>
                            <textarea class="form-control form-control-sm" name="<?= $inputName ?>" rows="2" <?= $reqAttr ?>><?= e((string) $val) ?></textarea>
                        <?php elseif ($f['field_type'] === 'select'): ?>
                            <select class="form-select form-select-sm" name="<?= $inputName ?>" <?= $reqAttr ?>>
                                <option value="">— none —</option>
                                <?php foreach ($options as $opt): ?><option value="<?= e((string) $opt) ?>" <?= (string) $val === (string) $opt ? 'selected' : '' ?>><?= e((string) $opt) ?></option><?php endforeach; ?>
                            </select>
                        <?php elseif ($f['field_type'] === 'boolean'): ?>
                            <div class="form-check"><input type="checkbox" class="form-check-input" name="<?= $inputName ?>" value="1" <?= !empty($val) ? 'checked' : '' ?>></div>
                        <?php elseif ($f['field_type'] === 'date'): ?>
                            <input type="date" class="form-control form-control-sm" name="<?= $inputName ?>" value="<?= e((string) $val) ?>" <?= $reqAttr ?>>
                        <?php elseif ($f['field_type'] === 'datetime'): ?>
                            <input type="datetime-local" class="form-control form-control-sm" name="<?= $inputName ?>" value="<?= e((string) $val) ?>" <?= $reqAttr ?>>
                        <?php elseif ($f['field_type'] === 'number'): ?>
                            <input type="number" step="any" class="form-control form-control-sm" name="<?= $inputName ?>" value="<?= e((string) $val) ?>" placeholder="<?= e($f['placeholder'] ?? '') ?>" <?= $reqAttr ?>>
                        <?php else: ?>
                            <input type="text" class="form-control form-control-sm" name="<?= $inputName ?>" value="<?= e((string) $val) ?>" placeholder="<?= e($f['placeholder'] ?? '') ?>" <?= $reqAttr ?>>
                        <?php endif; ?>
                        <?php if (!empty($f['help_text'])): ?><small class="text-muted"><?= e($f['help_text']) ?></small><?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endforeach; ?>

    <div class="d-flex gap-2">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
        <?php if (!empty($asset['asset_id'])): ?>
            <a class="btn btn-sm btn-outline-danger" href="<?= url('/Assets/delete/' . (int) $asset['asset_id']) ?>" onclick="return confirm('Delete this asset?')"><i class="fas fa-trash me-1"></i>Delete</a>
        <?php endif; ?>
    </div>
</form>
