diff --git a/web/add_provider.php b/web/add_provider.php index 1c3d785..20674da 100644 --- a/web/add_provider.php +++ b/web/add_provider.php @@ -28,6 +28,26 @@ function callApi(string $method, string $path, $body = null) return json_decode($response, true); } +// Fetch rate options setting +$rateOptionsSetting = callApi('GET', '/settings/rate_options'); +$rateOptions = []; +if ($rateOptionsSetting && isset($rateOptionsSetting['value'])) { + $parsed = json_decode($rateOptionsSetting['value'], true); + if (is_array($parsed)) { + $rateOptions = $parsed; + } +} + +// Update form handling to process selected rates +$selectedRates = $_POST['rates'] ?? []; +$ratesArray = []; +foreach ($selectedRates as $item) { + $parts = explode(':', $item, 2); + if (count($parts) == 2) { + $ratesArray[] = [$parts[0] => $parts[1]]; + } +} + $created = null; $errors = []; @@ -37,26 +57,26 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $phone = trim($_POST['phone'] ?? ''); $age = trim($_POST['age'] ?? ''); $photo = trim($_POST['photo'] ?? ''); - $rates = trim($_POST['rates'] ?? ''); $private = isset($_POST['private']) ? true : false; // Basic validation if ($name === '') { $errors[] = 'Name is required.'; } if ($age !== '' && !is_numeric($age)) { $errors[] = 'Age must be a number.'; } - if ($rates !== '' && !is_numeric($rates)) { $errors[] = 'Rates must be a number.'; } - + // Rates now processed from checkboxes if (empty($errors)) { $body = [ 'name' => $name, 'phone' => $phone, 'age' => $age === '' ? null : (int)$age, 'photo' => $photo === '' ? null : $photo, - 'rates' => $rates === '' ? null : (float)$rates, + 'rates' => empty($ratesArray) ? null : $ratesArray, 'private'=> $private ]; $result = callApi('POST', '/providers', $body); if ($result) { - $created = $result; // API returns created provider object + // On successful creation, redirect to the list providers page + header('Location: list_providers.php'); + exit; } else { $errors[] = 'Failed to create provider. API error.'; } @@ -88,20 +108,26 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { - -
-

Provider created successfully with ID .

-
-
- - - + + + +
+
+ + diff --git a/web/edit_provider.php b/web/edit_provider.php new file mode 100644 index 0000000..047187d --- /dev/null +++ b/web/edit_provider.php @@ -0,0 +1,100 @@ + +Error' . + '

Provider ID is missing or invalid.

'; + exit; +} + +// API base URL +$api_base = 'http://172.25.1.105:5000'; + +// Handle form submission +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + // Collect submitted data + $payload = [ + 'name' => $_POST['name'] ?? '', + 'age' => (int)($_POST['age'] ?? 0), + 'photo' => $_POST['photo'] ?? '', + 'phone' => $_POST['phone'] ?? '', + 'rates' => json_decode($_POST['rates'] ?? 'null', true) ?? [], + 'private' => isset($_POST['private']) && $_POST['private'] === 'on', + ]; + + // Send PUT request to API + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $api_base . '/providers/' . $provider_id); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($httpCode >= 400) { + $error = json_decode($response, true)['error'] ?? 'Update failed'; + echo '

Error

' . htmlspecialchars($error) . '

'; + } else { + // Success: redirect back to provider list + header('Location: list_providers.php'); + exit; + } +} + +// Fetch current provider details for the form (GET request) +$ch = curl_init(); +curl_setopt($ch, CURLOPT_URL, $api_base . '/providers?id=' . $provider_id); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +$response = curl_exec($ch); +$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); +curl_close($ch); + +if ($httpCode >= 400) { + echo '

Error

Could not load provider details.

'; + exit; +} +$provider = json_decode($response, true); +// API may return array for GET by id +if (is_array($provider) && count($provider) > 0 && isset($provider[0])) { + $provider = $provider[0]; +} + +?> + + + + + Edit Provider + + + + +
+

Edit Provider

+
+ + + + + + + + +
+
+ + diff --git a/web/list_providers.php b/web/list_providers.php index 16992cc..a157c3a 100644 --- a/web/list_providers.php +++ b/web/list_providers.php @@ -39,18 +39,21 @@ $providers = $providers ?? []; Providers -
+

Providers

+ Add Provider +
@@ -62,6 +65,7 @@ $providers = $providers ?? []; + @@ -84,6 +88,7 @@ function renderTable(data) { + `; tbody.appendChild(tr); }); @@ -118,6 +123,7 @@ renderTable(providers); document.querySelectorAll('#providers-table th').forEach(th => { th.addEventListener('click', () => { const field = th.getAttribute('data-field'); + if (!field) return; sortData(field); }); }); @@ -135,3 +141,5 @@ document.getElementById('search').addEventListener('input', e => { + + diff --git a/web/settings.php b/web/settings.php new file mode 100644 index 0000000..9bca49b --- /dev/null +++ b/web/settings.php @@ -0,0 +1,127 @@ += 400) { + return null; // Could also throw an exception if preferred + } + + return json_decode($response, true); +} + +$messages = []; + +// Handle form submission – update existing settings and/or create a new one +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + // Update existing settings + if (isset($_POST['orig_name'], $_POST['value'])) { + foreach ($_POST['orig_name'] as $idx => $orig) { + $newVal = $_POST['value'][$idx] ?? ''; + if ($newVal === '') continue; // skip empty values + $res = callApi('PUT', '/settings/' . urlencode($orig), ['value' => $newVal]); + if ($res === null) { + $messages[] = "Failed to update setting '" . htmlspecialchars($orig) . "'."; + } else { + $messages[] = "Updated setting '" . htmlspecialchars($orig) . "'."; + } + } + } + + // Create a single new setting + $newName = $_POST['new_name'] ?? ''; + $newValue = $_POST['new_value'] ?? ''; + if ($newName !== '' && $newValue !== '') { + $res = callApi('POST', '/settings', ['name' => $newName, 'value' => $newValue]); + if ($res === null) { + $messages[] = "Failed to create new setting '" . htmlspecialchars($newName) . "'."; + } else { + $messages[] = "Created new setting '" . htmlspecialchars($newName) . "'."; + } + } elseif ($newName !== '' || $newValue !== '') { + $messages[] = "Both name and value are required to create a new setting."; + } +} + +// Retrieve current settings to display +$settings = callApi('GET', '/settings'); +$settings = $settings ?? []; +?> + + + + + Settings + + + + +
+

Settings

+ +
+ +
+ +
+ +
+
Photo Rates PrivateActions
${p.photo ? `Photo` : ''} ${p.rates} ${p.private ? 'Yes' : 'No'}Edit
+ + + + + + + + + + + + + + + + + + + + +
NameValue
+ + +
+ + +
+ + + +