<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-map-marker-alt me-2 text-primary"></i><?= $site ? 'Edit Site' : 'New Site' ?></h5>
    <a href="<?= url('/Sites') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="rs-card p-3">
<form method="post" action="<?= url('/Sites/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="site_id" value="<?= (int) ($site['site_id'] ?? 0) ?>">

    <div class="row g-3">
        <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($site['name'] ?? '') ?>"></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($site['code'] ?? '') ?>"></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','decommissioned'] as $s): ?>
                    <option <?= ($site['status'] ?? 'active') === $s ? 'selected' : '' ?>><?= $s ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-6"><label class="form-label">Parent Site</label>
            <select name="parent_site_id" class="form-select form-select-sm">
                <option value="">— none —</option>
                <?php foreach ($parents as $p): ?>
                    <option value="<?= (int) $p['site_id'] ?>" <?= (int) ($site['parent_site_id'] ?? 0) === (int) $p['site_id'] ? 'selected' : '' ?>><?= e($p['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-12"><label class="form-label">Description</label><textarea class="form-control form-control-sm" name="description" rows="2"><?= e($site['description'] ?? '') ?></textarea></div>
        <div class="col-md-6"><label class="form-label">Address</label><input type="text" class="form-control form-control-sm" name="address" value="<?= e($site['address'] ?? '') ?>"></div>
        <div class="col-md-3"><label class="form-label">City</label><input type="text" class="form-control form-control-sm" name="city" value="<?= e($site['city'] ?? '') ?>"></div>
        <div class="col-md-3"><label class="form-label">Region</label><input type="text" class="form-control form-control-sm" name="region" value="<?= e($site['region'] ?? '') ?>"></div>
        <div class="col-md-4"><label class="form-label">Country</label><input type="text" class="form-control form-control-sm" name="country" value="<?= e($site['country'] ?? 'Tanzania') ?>"></div>
        <div class="col-md-4"><label class="form-label">Latitude</label><input type="number" step="0.0000001" class="form-control form-control-sm" name="latitude" value="<?= e($site['latitude'] ?? '') ?>"></div>
        <div class="col-md-4"><label class="form-label">Longitude</label><input type="number" step="0.0000001" class="form-control form-control-sm" name="longitude" value="<?= e($site['longitude'] ?? '') ?>"></div>
    </div>

    <div class="mt-3 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($site['site_id'])): ?>
            <a class="btn btn-sm btn-outline-danger" href="<?= url('/Sites/delete/' . (int) $site['site_id']) ?>" onclick="return confirm('Delete this site?')"><i class="fas fa-trash me-1"></i>Delete</a>
        <?php endif; ?>
    </div>
</form>
</div>
