<?php
declare(strict_types=1);

function gf_stmt(mysqli $con, string $sql): mysqli_stmt
{
    $stmt = $con->prepare($sql);
    if (!$stmt) {
        throw new RuntimeException('Unable to prepare geofence query.');
    }
    return $stmt;
}

function gf_table_exists(mysqli $con, string $table): bool
{
    $stmt = gf_stmt($con, 'SELECT COUNT(*) AS total FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=?');
    $stmt->bind_param('s', $table);
    $stmt->execute();
    $exists = (int)($stmt->get_result()->fetch_assoc()['total'] ?? 0) > 0;
    $stmt->close();
    return $exists;
}

function gf_col_exists(mysqli $con, string $table, string $column): bool
{
    $stmt = gf_stmt($con, 'SELECT COUNT(*) AS total FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=? AND COLUMN_NAME=?');
    $stmt->bind_param('ss', $table, $column);
    $stmt->execute();
    $exists = (int)($stmt->get_result()->fetch_assoc()['total'] ?? 0) > 0;
    $stmt->close();
    return $exists;
}

function gf_columns(mysqli $con, string $table): array
{
    static $cache = [];
    if (isset($cache[$table])) {
        return $cache[$table];
    }

    $safeTable = gf_ident($table);
    $columns = [];
    $result = $con->query('SHOW COLUMNS FROM `' . $safeTable . '`');
    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $columns[] = (string)$row['Field'];
        }
        $result->free();
    }
    $cache[$table] = $columns;
    return $columns;
}

function gf_ident(string $value): string
{
    return preg_replace('/[^A-Za-z0-9_]/', '', $value);
}

function gf_pick(array $columns, array $candidates): string
{
    foreach ($candidates as $candidate) {
        if (in_array($candidate, $columns, true)) {
            return $candidate;
        }
    }
    return '';
}

function gf_find_table(mysqli $con): string
{
    $candidates = [
        'jos_app_geofence',
        'jos_app_geofence_area',
        'jos_app_geofencearea',
        'jos_geofence',
        'jos_geofence_area',
        'jos_geofencearea',
        'jos_payroll_geofence',
        'jos_payroll_geofence_area',
        'jos_app_locationgeofence',
        'jos_app_geofence_config',
    ];

    foreach ($candidates as $table) {
        if (gf_table_exists($con, $table)) {
            return $table;
        }
    }

    $tables = [];
    $result = $con->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=DATABASE() AND LOWER(TABLE_NAME) LIKE '%geofence%' ORDER BY TABLE_NAME ASC");
    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $tables[] = (string)$row['TABLE_NAME'];
        }
        $result->free();
    }
    foreach ($tables as $table) {
        $columns = gf_columns($con, $table);
        if (gf_pick($columns, ['id']) !== '' && gf_pick($columns, ['name', 'Name', 'geofence_name', 'geofencename', 'title']) !== '') {
            return $table;
        }
    }
    return '';
}

function gf_config(mysqli $con): array
{
    static $config = null;
    if ($config !== null) {
        return $config;
    }

    $table = gf_find_table($con);
    if ($table === '') {
        $config = ['table' => '', 'columns' => [], 'id' => '', 'key' => '', 'name' => '', 'lat' => '', 'lon' => '', 'radius' => '', 'status' => '', 'order' => ''];
        return $config;
    }

    $columns = gf_columns($con, $table);
    $config = [
        'table' => $table,
        'columns' => $columns,
        'id' => gf_pick($columns, ['id']),
        'key' => gf_pick($columns, ['GeofenceKey', 'geofencekey', 'geofence_key', 'geofenceKey', 'key', 'code']),
        'name' => gf_pick($columns, ['name', 'Name', 'geofence_name', 'geofencename', 'title']),
        'lat' => gf_pick($columns, ['latitude', 'Latitude', 'lat']),
        'lon' => gf_pick($columns, ['longitude', 'Longitude', 'lng', 'long', 'lon']),
        'radius' => gf_pick($columns, ['radius', 'Radius', 'range', 'distance']),
        'status' => gf_pick($columns, ['status', 'published', 'active', 'is_active']),
        'order' => gf_pick($columns, ['ordering', 'orderby', 'sort_order']),
    ];
    return $config;
}

function gf_ready(array $config): bool
{
    return $config['table'] !== '' && $config['id'] !== '' && $config['name'] !== '' && $config['lat'] !== '' && $config['lon'] !== '' && $config['radius'] !== '';
}

function gf_text(array $row, string $key, string $default = ''): string
{
    return array_key_exists($key, $row) && $row[$key] !== null ? (string)$row[$key] : $default;
}

function gf_flash(string $message): void
{
    $_SESSION['flash_geofence_area'] = $message;
}

function gf_current_flash(): ?string
{
    $flash = $_SESSION['flash_geofence_area'] ?? null;
    unset($_SESSION['flash_geofence_area']);
    return $flash ? (string)$flash : null;
}

function gf_menu_link(): string
{
    return 'organization/geofence-areas.php';
}

function gf_add_link(): string
{
    return '/organization/geofence-area-add.php';
}

function gf_list_link(array $extra = []): string
{
    $query = $extra ? '?' . http_build_query($extra) : '';
    return '/organization/geofence-areas.php' . $query;
}

function gf_edit_link(int $id): string
{
    return '/organization/geofence-area-edit.php?id=' . $id;
}

function gf_redirect(string $path): void
{
    header('Location: ' . $path);
    exit;
}

function gf_ensure_menu(mysqli $con): void
{
    if (!gf_table_exists($con, 'jos_admin_menus')) {
        return;
    }

    $menuColumnSql = [
        'parent_id' => 'ALTER TABLE jos_admin_menus ADD COLUMN parent_id INT UNSIGNED NOT NULL DEFAULT 0',
        'show_in_menu' => 'ALTER TABLE jos_admin_menus ADD COLUMN show_in_menu TINYINT(1) NOT NULL DEFAULT 1',
        'url' => 'ALTER TABLE jos_admin_menus ADD COLUMN url VARCHAR(255) NULL',
        'section' => 'ALTER TABLE jos_admin_menus ADD COLUMN section VARCHAR(100) NULL',
        'sort_order' => 'ALTER TABLE jos_admin_menus ADD COLUMN sort_order INT NOT NULL DEFAULT 0',
    ];
    foreach ($menuColumnSql as $column => $sql) {
        if (!gf_col_exists($con, 'jos_admin_menus', $column)) {
            $con->query($sql);
        }
    }

    $parentId = 0;
    $stmt = gf_stmt($con, "SELECT id FROM jos_admin_menus WHERE menu_link='#organisation-settings' OR url='#organisation-settings' OR menu_name IN ('Organisation Settings','Organization Settings') ORDER BY id ASC LIMIT 1");
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    if ($row) {
        $parentId = (int)$row['id'];
        $name = 'Organisation Settings';
        $link = '#organisation-settings';
        $section = 'D. Masters';
        $icon = 'fa-solid fa-building';
        $remarks = 'Company and organisation masters';
        $sort = 115;
        $stmt = gf_stmt($con, 'UPDATE jos_admin_menus SET menu_name=?, menu_link=?, url=?, parent_id=0, section=?, icon=?, remarks=?, status=1, show_in_menu=1, can_view=1, sort_order=?, orderby=? WHERE id=?');
        $stmt->bind_param('ssssssiii', $name, $link, $link, $section, $icon, $remarks, $sort, $sort, $parentId);
        $stmt->execute();
        $stmt->close();
    } else {
        $name = 'Organisation Settings';
        $link = '#organisation-settings';
        $section = 'D. Masters';
        $icon = 'fa-solid fa-building';
        $remarks = 'Company and organisation masters';
        $sort = 115;
        $stmt = gf_stmt($con, 'INSERT INTO jos_admin_menus (menu_name, menu_link, url, parent_id, section, icon, remarks, status, show_in_menu, can_view, sort_order, orderby) VALUES (?, ?, ?, 0, ?, ?, ?, 1, 1, 1, ?, ?)');
        $stmt->bind_param('ssssssii', $name, $link, $link, $section, $icon, $remarks, $sort, $sort);
        $stmt->execute();
        $parentId = (int)$con->insert_id;
        $stmt->close();
    }

    if ($parentId <= 0) {
        return;
    }

    $childId = 0;
    $link = gf_menu_link();
    $stmt = gf_stmt($con, "SELECT id FROM jos_admin_menus WHERE menu_link=? OR url=? OR menu_name IN ('Configuration Geofence Area','Geofence Area','Geofence Areas') OR menu_link LIKE '%layout=geofence%' OR url LIKE '%layout=geofence%' ORDER BY id ASC LIMIT 1");
    $stmt->bind_param('ss', $link, $link);
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    $name = 'Configuration Geofence Area';
    $section = 'Organisation Settings';
    $icon = 'fa-solid fa-draw-polygon';
    $remarks = 'Manage configuration geofence areas.';
    $sort = 123;
    if ($row) {
        $childId = (int)$row['id'];
        $stmt = gf_stmt($con, 'UPDATE jos_admin_menus SET menu_name=?, menu_link=?, url=?, parent_id=?, section=?, icon=?, remarks=?, status=1, show_in_menu=1, can_view=1, can_add=1, can_edit=1, can_delete=1, sort_order=?, orderby=? WHERE id=?');
        $stmt->bind_param('sssisssiii', $name, $link, $link, $parentId, $section, $icon, $remarks, $sort, $sort, $childId);
        $stmt->execute();
        $stmt->close();
    } else {
        $stmt = gf_stmt($con, 'INSERT INTO jos_admin_menus (menu_name, menu_link, url, parent_id, section, icon, remarks, status, show_in_menu, can_view, can_add, can_edit, can_delete, sort_order, orderby) VALUES (?, ?, ?, ?, ?, ?, ?, 1, 1, 1, 1, 1, 1, ?, ?)');
        $stmt->bind_param('sssisssii', $name, $link, $link, $parentId, $section, $icon, $remarks, $sort, $sort);
        $stmt->execute();
        $childId = (int)$con->insert_id;
        $stmt->close();
    }

    if ($childId <= 0 || !gf_table_exists($con, 'jos_admin_rolemenus')) {
        return;
    }

    $roleIds = [1];
    if (gf_table_exists($con, 'jos_admin_roles')) {
        $roleNameColumn = gf_col_exists($con, 'jos_admin_roles', 'role_name') ? 'role_name' : (gf_col_exists($con, 'jos_admin_roles', 'name') ? 'name' : '');
        if ($roleNameColumn !== '') {
            $statusSql = gf_col_exists($con, 'jos_admin_roles', 'status') ? ' AND status=1' : '';
            $result = $con->query("SELECT id FROM jos_admin_roles WHERE id=1 OR LOWER(`" . gf_ident($roleNameColumn) . "`) IN ('administrator','admin','super admin','super user')" . $statusSql);
            if ($result) {
                while ($role = $result->fetch_assoc()) {
                    $roleIds[] = (int)$role['id'];
                }
                $result->free();
            }
        }
    }

    $roleIds = array_values(array_unique(array_filter($roleIds)));
    foreach ($roleIds as $roleId) {
        $stmt = gf_stmt($con, 'SELECT id FROM jos_admin_rolemenus WHERE role_id=? AND menu_id=? LIMIT 1');
        $stmt->bind_param('ii', $roleId, $childId);
        $stmt->execute();
        $existing = $stmt->get_result()->fetch_assoc();
        $stmt->close();
        if ($existing) {
            $id = (int)$existing['id'];
            $stmt = gf_stmt($con, 'UPDATE jos_admin_rolemenus SET can_view=1, can_add=1, can_edit=1, can_delete=1, status=1 WHERE id=?');
            $stmt->bind_param('i', $id);
        } else {
            $stmt = gf_stmt($con, 'INSERT INTO jos_admin_rolemenus (role_id, menu_id, can_view, can_add, can_edit, can_delete, status) VALUES (?, ?, 1, 1, 1, 1, 1)');
            $stmt->bind_param('ii', $roleId, $childId);
        }
        $stmt->execute();
        $stmt->close();
    }
}

function gf_decimal_value(string $value): ?float
{
    if ($value === '') {
        return null;
    }
    $parsed = filter_var($value, FILTER_VALIDATE_FLOAT);
    if ($parsed === false || !is_finite((float)$parsed)) {
        return null;
    }
    return (float)$parsed;
}

function gf_validate(array $source, array &$errors): array
{
    $data = [
        'name' => trim((string)($source['name'] ?? '')),
        'latitude' => trim((string)($source['latitude'] ?? '')),
        'longitude' => trim((string)($source['longitude'] ?? '')),
        'radius' => trim((string)($source['radius'] ?? '')),
    ];

    if ($data['name'] === '') {
        $errors[] = 'Name is required.';
    } elseif (strlen($data['name']) > 150) {
        $errors[] = 'Name must be 150 characters or fewer.';
    }
    $latitude = gf_decimal_value($data['latitude']);
    $longitude = gf_decimal_value($data['longitude']);
    $radius = gf_decimal_value($data['radius']);
    if ($latitude === null || $latitude < -90 || $latitude > 90) {
        $errors[] = 'Latitude must be a number between -90 and 90.';
    }
    if ($longitude === null || $longitude < -180 || $longitude > 180) {
        $errors[] = 'Longitude must be a number between -180 and 180.';
    }
    if ($radius === null || $radius <= 0) {
        $errors[] = 'Radius must be a number greater than 0.';
    }

    return $data;
}

function gf_fetch(mysqli $con, array $config, int $id): ?array
{
    if (!gf_ready($config) || $id <= 0) {
        return null;
    }
    $table = gf_ident($config['table']);
    $idColumn = gf_ident($config['id']);
    $stmt = gf_stmt($con, 'SELECT * FROM `' . $table . '` WHERE `' . $idColumn . '`=? LIMIT 1');
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc() ?: null;
    $stmt->close();
    return $row;
}

function gf_duplicate(mysqli $con, array $config, string $name, int $excludeId = 0): bool
{
    if (!gf_ready($config)) {
        return false;
    }
    $table = gf_ident($config['table']);
    $nameColumn = gf_ident($config['name']);
    $idColumn = gf_ident($config['id']);
    if ($excludeId > 0) {
        $stmt = gf_stmt($con, 'SELECT `' . $idColumn . '` FROM `' . $table . '` WHERE LOWER(TRIM(`' . $nameColumn . '`))=LOWER(TRIM(?)) AND `' . $idColumn . '`<>? LIMIT 1');
        $stmt->bind_param('si', $name, $excludeId);
    } else {
        $stmt = gf_stmt($con, 'SELECT `' . $idColumn . '` FROM `' . $table . '` WHERE LOWER(TRIM(`' . $nameColumn . '`))=LOWER(TRIM(?)) LIMIT 1');
        $stmt->bind_param('s', $name);
    }
    $stmt->execute();
    $exists = (bool)$stmt->get_result()->fetch_assoc();
    $stmt->close();
    return $exists;
}

function gf_save(mysqli $con, array $config, int $id, array $data): int
{
    $table = gf_ident($config['table']);
    $idColumn = gf_ident($config['id']);
    $values = [
        $config['name'] => $data['name'],
        $config['lat'] => $data['latitude'],
        $config['lon'] => $data['longitude'],
        $config['radius'] => $data['radius'],
    ];
    if ($id === 0 && $config['status'] !== '') {
        $values[$config['status']] = '1';
    }

    if ($id > 0) {
        $sets = [];
        $types = '';
        $params = [];
        foreach ($values as $column => $value) {
            $sets[] = '`' . gf_ident($column) . '`=?';
            $types .= 's';
            $params[] = $value;
        }
        $types .= 'i';
        $params[] = $id;
        $stmt = gf_stmt($con, 'UPDATE `' . $table . '` SET ' . implode(', ', $sets) . ' WHERE `' . $idColumn . '`=?');
        $stmt->bind_param($types, ...$params);
        $stmt->execute();
        $stmt->close();
        return $id;
    }

    $columns = [];
    $placeholders = [];
    $types = '';
    $params = [];
    foreach ($values as $column => $value) {
        $columns[] = '`' . gf_ident($column) . '`';
        $placeholders[] = '?';
        $types .= 's';
        $params[] = $value;
    }
    $stmt = gf_stmt($con, 'INSERT INTO `' . $table . '` (' . implode(', ', $columns) . ') VALUES (' . implode(', ', $placeholders) . ')');
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    $savedId = (int)$con->insert_id;
    $stmt->close();
    return $savedId;
}

function gf_delete(mysqli $con, array $config, int $id): void
{
    $table = gf_ident($config['table']);
    $idColumn = gf_ident($config['id']);
    $stmt = gf_stmt($con, 'DELETE FROM `' . $table . '` WHERE `' . $idColumn . '`=?');
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $stmt->close();
}

function gf_toggle_status(mysqli $con, array $config, int $id, int $status): void
{
    if ($config['status'] === '') {
        return;
    }
    $table = gf_ident($config['table']);
    $idColumn = gf_ident($config['id']);
    $statusColumn = gf_ident($config['status']);
    $stmt = gf_stmt($con, 'UPDATE `' . $table . '` SET `' . $statusColumn . '`=? WHERE `' . $idColumn . '`=?');
    $stmt->bind_param('ii', $status, $id);
    $stmt->execute();
    $stmt->close();
}

function gf_list(mysqli $con, array $config, string $q, string $status, int $limit, int $offset): array
{
    if (!gf_ready($config)) {
        return [];
    }
    $table = gf_ident($config['table']);
    $where = [];
    $types = '';
    $params = [];
    if ($q !== '') {
        $like = '%' . $q . '%';
        $parts = ['`' . gf_ident($config['name']) . '` LIKE ?'];
        $types .= 's';
        $params[] = $like;
        if ($config['key'] !== '') {
            $parts[] = '`' . gf_ident($config['key']) . '` LIKE ?';
            $types .= 's';
            $params[] = $like;
        }
        $where[] = '(' . implode(' OR ', $parts) . ')';
    }
    if ($status !== '' && $config['status'] !== '') {
        $where[] = '`' . gf_ident($config['status']) . '`=?';
        $types .= 'i';
        $params[] = (int)$status;
    }
    $whereSql = $where ? implode(' AND ', $where) : '1=1';
    $orderColumn = $config['order'] !== '' ? gf_ident($config['order']) : gf_ident($config['id']);
    $orderDirection = $config['order'] !== '' ? 'ASC' : 'DESC';
    $stmt = gf_stmt($con, 'SELECT * FROM `' . $table . '` WHERE ' . $whereSql . ' ORDER BY `' . $orderColumn . '` ' . $orderDirection . ' LIMIT ? OFFSET ?');
    $types .= 'ii';
    $params[] = $limit;
    $params[] = $offset;
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    $rows = [];
    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
    $stmt->close();
    return $rows;
}

function gf_count(mysqli $con, array $config, string $q, string $status): int
{
    if (!gf_ready($config)) {
        return 0;
    }
    $table = gf_ident($config['table']);
    $where = [];
    $types = '';
    $params = [];
    if ($q !== '') {
        $like = '%' . $q . '%';
        $parts = ['`' . gf_ident($config['name']) . '` LIKE ?'];
        $types .= 's';
        $params[] = $like;
        if ($config['key'] !== '') {
            $parts[] = '`' . gf_ident($config['key']) . '` LIKE ?';
            $types .= 's';
            $params[] = $like;
        }
        $where[] = '(' . implode(' OR ', $parts) . ')';
    }
    if ($status !== '' && $config['status'] !== '') {
        $where[] = '`' . gf_ident($config['status']) . '`=?';
        $types .= 'i';
        $params[] = (int)$status;
    }
    $whereSql = $where ? implode(' AND ', $where) : '1=1';
    $stmt = gf_stmt($con, 'SELECT COUNT(*) AS total FROM `' . $table . '` WHERE ' . $whereSql);
    if ($types !== '') {
        $stmt->bind_param($types, ...$params);
    }
    $stmt->execute();
    $total = (int)($stmt->get_result()->fetch_assoc()['total'] ?? 0);
    $stmt->close();
    return $total;
}

function gf_breadcrumb(string $current = 'Configuration Geofence Area'): void
{
    echo '<div class="mb-6">';
    echo '<div class="mb-2 flex items-center gap-2 text-xs font-semibold text-slate-500"><a href="/dashboard.php">Dashboard</a><i class="fa-solid fa-chevron-right text-[9px]"></i><span>D. Masters</span><i class="fa-solid fa-chevron-right text-[9px]"></i><span>Organisation Settings</span><i class="fa-solid fa-chevron-right text-[9px]"></i><span class="text-brand-600">' . h($current) . '</span></div>';
    echo '<h1 class="text-2xl font-bold text-slate-900">Configuration Geofence Area</h1>';
    echo '<p class="mt-1 text-sm text-slate-500">Manage geofence names, latitude, longitude, and radius values.</p>';
    echo '</div>';
}

function gf_alerts(?string $flash, array $errors): void
{
    if ($flash) {
        echo '<div class="mb-5 rounded-xl border border-emerald-100 bg-emerald-50 px-5 py-4 text-sm font-semibold text-emerald-700"><i class="fa-solid fa-circle-check mr-2"></i>' . h($flash) . '</div>';
    }
    if ($errors) {
        echo '<div class="mb-5 rounded-xl border border-red-100 bg-red-50 px-5 py-4 text-sm font-semibold text-red-700"><div class="font-bold">Please fix the following</div><ul class="mt-2 list-inside list-disc">';
        foreach ($errors as $error) {
            echo '<li>' . h($error) . '</li>';
        }
        echo '</ul></div>';
    }
}

function gf_form_card(array $data, array $errors, string $title, string $submitLabel, int $id = 0): void
{
    $input = 'mt-1 w-full rounded-lg border border-slate-200 bg-white px-3 py-2.5 text-sm text-slate-800 outline-none focus:border-brand-500 focus:ring-2 focus:ring-brand-100';
    ?>
    <form method="post">
      <input type="hidden" name="_csrf" value="<?= h(csrf_token()) ?>">
      <input type="hidden" name="action" value="save">
      <input type="hidden" name="id" value="<?= (int)$id ?>">
      <section class="overflow-hidden rounded-2xl border border-slate-100 bg-white shadow-sm">
        <div class="flex flex-col gap-3 border-b border-slate-100 px-5 py-4 sm:flex-row sm:items-center sm:justify-between">
          <div class="flex items-center gap-3">
            <span class="flex h-10 w-10 items-center justify-center rounded-xl bg-brand-50 text-brand-600"><i class="fa-solid fa-draw-polygon"></i></span>
            <div>
              <h2 class="text-lg font-bold text-slate-800"><?= h($title) ?></h2>
              <p class="mt-1 text-sm font-medium text-slate-400">Fields marked with * are required.</p>
            </div>
          </div>
          <a href="<?= h(gf_list_link()) ?>" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-bold text-slate-600 hover:bg-slate-50"><i class="fa-solid fa-arrow-left mr-2"></i>Back to List</a>
        </div>
        <div class="grid gap-x-6 gap-y-5 p-5 md:grid-cols-2 xl:grid-cols-4">
          <label class="text-sm font-bold text-slate-700">Name *<input name="name" required maxlength="150" value="<?= h($data['name'] ?? '') ?>" class="<?= $input ?>" placeholder="Name"></label>
          <label class="text-sm font-bold text-slate-700">Latitude *<input name="latitude" required inputmode="decimal" value="<?= h($data['latitude'] ?? '') ?>" class="<?= $input ?>" placeholder="Latitude"></label>
          <label class="text-sm font-bold text-slate-700">Longitude *<input name="longitude" required inputmode="decimal" value="<?= h($data['longitude'] ?? '') ?>" class="<?= $input ?>" placeholder="Longitude"></label>
          <label class="text-sm font-bold text-slate-700">Radius *<input name="radius" required inputmode="decimal" value="<?= h($data['radius'] ?? '') ?>" class="<?= $input ?>" placeholder="Radius"></label>
        </div>
      </section>
      <div class="mt-5 flex justify-center gap-3">
        <button class="rounded-lg bg-brand-600 px-8 py-2.5 text-sm font-bold text-white shadow-sm hover:bg-brand-700"><i class="fa-solid fa-floppy-disk mr-2"></i><?= h($submitLabel) ?></button>
        <a href="<?= h(gf_list_link()) ?>" class="rounded-lg border border-slate-200 bg-white px-8 py-2.5 text-sm font-bold text-slate-600 hover:bg-slate-50">Cancel</a>
      </div>
    </form>
    <?php
}
