master
Sneha 2 months ago
parent 5d2faaa9e3
commit 8cd6b1463a

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

Loading…
Cancel
Save