Files
Siro/siro_admin/lib/controller/admin/kazan_controller.dart
2026-06-26 17:36:57 +03:00

85 lines
2.6 KiB
Dart

import 'dart:convert';
import 'package:get/get.dart';
import '../../constant/links.dart';
import '../../views/widgets/snackbar.dart';
import '../functions/crud.dart';
class KazanController extends GetxController {
var kazanData = {}.obs;
var isLoading = false.obs;
var selectedCountry = 'سوريا'.obs;
final CRUD _crud = CRUD();
final List<Map<String, String>> countries = [
{'code': 'syria', 'name': 'سوريا', 'flag': '🇸🇾'},
{'code': 'jordan', 'name': 'الأردن', 'flag': '🇯🇴'},
{'code': 'egypt', 'name': 'مصر', 'flag': '🇪🇬'},
];
String get selectedCountryCode =>
countries.firstWhere(
(c) => c['name'] == selectedCountry.value,
orElse: () => countries.first,
)['code']!;
@override
void onInit() {
super.onInit();
getKazan();
}
void setCountry(String countryName) {
selectedCountry.value = countryName;
getKazan();
}
Future<void> getKazan() async {
isLoading.value = true;
try {
final countryParam = selectedCountryCode;
var response = await _crud.get(link: "${AppLink.getKazanPercent}?country=$countryParam");
if (response != null && response != 'failure' && response != 'token_expired') {
var decoded = response is String ? jsonDecode(response) : response;
if (decoded['status'] == "success") {
var message = decoded['message'];
if (message is List && message.isNotEmpty) {
kazanData.value = Map<String, dynamic>.from(message[0]);
kazanData['country'] = selectedCountry.value;
} else {
kazanData.value = {'country': selectedCountry.value};
}
}
}
} catch (e) {
mySnackbarError('فشل جلب بيانات التسعير: $e');
} finally {
isLoading.value = false;
}
}
Future<bool> updateKazan(Map<String, dynamic> data) async {
isLoading.value = true;
try {
data['country'] = selectedCountry.value;
final String link = data.containsKey('id') ? AppLink.updateKazanPercent : AppLink.addKazanPercent;
Map<String, String> payload = {};
data.forEach((key, value) {
payload[key] = value.toString();
});
var response = await _crud.post(link: link, payload: payload);
if (response != null && response is Map && response['status'] == "success") {
await getKazan();
return true;
}
return false;
} catch (e) {
mySnackbarError('فشل تحديث التسعير: $e');
return false;
} finally {
isLoading.value = false;
}
}
}