diff --git a/lib/set_rates/set_rates.dart b/lib/set_rates/set_rates.dart index 52705df..a06e851 100644 --- a/lib/set_rates/set_rates.dart +++ b/lib/set_rates/set_rates.dart @@ -49,21 +49,20 @@ class _SetRatesScreenState extends State // 🧭 Group data and create controllers for (final item in data) { - final category = item.type_of_water; - final size = item.capacity; - final price = item.price.toString(); + final rawCategory = item.type_of_water ?? ""; + final categoryKey = normalizeType(rawCategory); + final categoryLabel = displayType(rawCategory); - if (!tankerGroups.containsKey(category)) { - tankerGroups[category] = []; - } + final size = normalizeCap(item.capacity); + final price = item.price.toString(); - // avoid duplicate sizes for same category - if (!tankerGroups[category]!.contains(size)) { - tankerGroups[category]!.add(size); + tankerGroups.putIfAbsent(categoryKey, () => []); + if (!tankerGroups[categoryKey]!.contains(size)) { + tankerGroups[categoryKey]!.add(size); } - final controllerKey = "$category-$size"; - controllers[controllerKey] = TextEditingController(text: price); + controllers["$categoryKey-$size"] = + TextEditingController(text: price); } final capacities = tankersList.map((t) => normalizeCap(t.capacity)).toSet(); for (final cap in capacities) { @@ -172,6 +171,17 @@ class _SetRatesScreenState extends State ); } + String normalizeType(String type) { + return type.trim().toLowerCase(); + } + + String displayType(String type) { + final t = normalizeType(type); + if (t.contains("drink")) return "Drinking Water"; + if (t.contains("bore")) return "Bore Water"; + return type.trim(); + } + Widget WaterCharges() { return isLoading ? const Center(child: CircularProgressIndicator()):SingleChildScrollView( @@ -187,9 +197,8 @@ class _SetRatesScreenState extends State children: [ for (final entry in tankerGroups.entries) ...[ Text( - entry.key, - style: - fontTextStyle(10, const Color(0XFF2D2E30), FontWeight.w600), + displayType(entry.key), // 👈 shows clean label + style: fontTextStyle(10, const Color(0XFF2D2E30), FontWeight.w600), ), const SizedBox(height: 8), for (final size in entry.value) @@ -221,27 +230,18 @@ class _SetRatesScreenState extends State onPressed: () async { final List> tankersPayload = []; - tankerGroups.forEach((category, sizes) { - for (final size in sizes) { - final key = "$category-$size"; - final amount = controllers[key]?.text.trim() ?? "0"; - - // find the tanker in tankersList by capacity (size) - final matchedTanker = tankersList.firstWhere( - (t) => t.capacity == size, - orElse: () => TankersModel(), - ); + for (final tanker in tankersList) { + final categoryKey = normalizeType(tanker.type_of_water); + final size = normalizeCap(tanker.capacity); - final tankerName = matchedTanker.tanker_name.isNotEmpty - ? matchedTanker.tanker_name - : "$category $size L"; // fallback if not found + final controllerKey = "$categoryKey-$size"; + final amount = controllers[controllerKey]?.text.trim() ?? "0"; - tankersPayload.add({ - "tankerName": tankerName, - "amount": amount.isEmpty ? "0" : amount, - }); - } - }); + tankersPayload.add({ + "tankerName": tanker.tanker_name, + "amount": amount.isEmpty ? "0" : amount, + }); + } final payload = { "price_type": "price", @@ -250,18 +250,13 @@ class _SetRatesScreenState extends State print(payload); - try { - final ok = await AppSettings.setRatesDaily(payload); - if (ok) { - AppSettings.longSuccessToast("Prices updated successfully"); - _fetchTankers(); - } else { - AppSettings.longFailedToast("Update failed"); - } - } catch (e) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text("Error: $e")), - ); + final ok = await AppSettings.setRatesDaily(payload); + + if (ok) { + AppSettings.longSuccessToast("Prices updated successfully"); + _fetchTankers(); + } else { + AppSettings.longFailedToast("Update failed"); } }, child: Text(