Update: 2026-08-08 12:58:16
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
// fuel_admin_service.dart — طبقة الاتصال بـ backend/Admin/fuel
|
||||
//
|
||||
// محفظة الوقود: السائق يتزوّد بلا نقد، والقيمة تُقيَّد ديناً يُخصم من
|
||||
// أرباحه. هذه الشاشة تدير المحطات الشريكة وتشغّل المنتج أو توقفه.
|
||||
import '../functions/crud.dart';
|
||||
import '../../constant/links.dart';
|
||||
import 'fuel_admin_models.dart';
|
||||
|
||||
class FuelApiResult<T> {
|
||||
final bool success;
|
||||
final T? data;
|
||||
final String message;
|
||||
FuelApiResult(this.success, this.data, this.message);
|
||||
}
|
||||
|
||||
class FuelAdminService {
|
||||
static String get _base => '${AppLink.server}/Admin/fuel';
|
||||
|
||||
static String _errMsg(dynamic res) {
|
||||
if (res == 'no_internet') return 'تحقق من اتصالك بالإنترنت';
|
||||
if (res == 'token_expired') return 'انتهت الجلسة، حاول مجدداً';
|
||||
if (res is Map && res['message'] is String) return res['message'];
|
||||
return 'حدث خطأ، حاول مجدداً';
|
||||
}
|
||||
|
||||
static Future<FuelApiResult<FuelStationsPage>> listStations({
|
||||
String? search,
|
||||
String? city,
|
||||
bool onlyActive = false,
|
||||
}) async {
|
||||
final payload = <String, dynamic>{};
|
||||
if (search != null && search.isNotEmpty) payload['search'] = search;
|
||||
if (city != null && city.isNotEmpty) payload['city'] = city;
|
||||
if (onlyActive) payload['only_active'] = '1';
|
||||
|
||||
final res = await CRUD().post(link: '$_base/list.php', payload: payload);
|
||||
|
||||
if (res is Map && res['status'] == 'success' && res['message'] is Map) {
|
||||
return FuelApiResult(
|
||||
true,
|
||||
FuelStationsPage.fromJson(Map<String, dynamic>.from(res['message'])),
|
||||
'ok',
|
||||
);
|
||||
}
|
||||
return FuelApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
/// يُرجع `api_key` عند الإنشاء فقط — مخزَّن مُهشَّراً على الخادم، فلا
|
||||
/// سبيل لعرضه ثانيةً. الشاشة مسؤولة عن إظهاره فوراً وبوضوح.
|
||||
static Future<FuelApiResult<Map<String, dynamic>>> saveStation({
|
||||
int? stationId,
|
||||
required String nameAr,
|
||||
String? chainName,
|
||||
String? city,
|
||||
String? address,
|
||||
double? lat,
|
||||
double? lng,
|
||||
String? phone,
|
||||
required double discountPercent,
|
||||
required bool isActive,
|
||||
}) async {
|
||||
final res = await CRUD().post(link: '$_base/save.php', payload: {
|
||||
if (stationId != null) 'station_id': stationId.toString(),
|
||||
'name_ar': nameAr,
|
||||
if (chainName != null && chainName.isNotEmpty) 'chain_name': chainName,
|
||||
if (city != null && city.isNotEmpty) 'city': city,
|
||||
if (address != null && address.isNotEmpty) 'address': address,
|
||||
if (lat != null) 'lat': lat.toString(),
|
||||
if (lng != null) 'lng': lng.toString(),
|
||||
if (phone != null && phone.isNotEmpty) 'phone': phone,
|
||||
'discount_percent': discountPercent.toString(),
|
||||
'is_active': isActive ? '1' : '0',
|
||||
});
|
||||
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
return FuelApiResult(
|
||||
true,
|
||||
Map<String, dynamic>.from(res['message'] ?? {}),
|
||||
res['message'] is Map ? 'ok' : (res['message']?.toString() ?? 'ok'),
|
||||
);
|
||||
}
|
||||
return FuelApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<FuelApiResult<Map<String, dynamic>>> rotateKey(int stationId) async {
|
||||
final res = await CRUD().post(
|
||||
link: '$_base/rotate_key.php',
|
||||
payload: {'station_id': stationId.toString()},
|
||||
);
|
||||
if (res is Map && res['status'] == 'success' && res['message'] is Map) {
|
||||
return FuelApiResult(true, Map<String, dynamic>.from(res['message']), 'ok');
|
||||
}
|
||||
return FuelApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<FuelApiResult<bool>> toggleProduct(bool isActive) async {
|
||||
final res = await CRUD().post(
|
||||
link: '$_base/toggle_product.php',
|
||||
payload: {'is_active': isActive ? '1' : '0'},
|
||||
);
|
||||
if (res is Map && res['status'] == 'success' && res['message'] is Map) {
|
||||
return FuelApiResult(
|
||||
true, res['message']['product_active'] == true, 'ok');
|
||||
}
|
||||
return FuelApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<FuelApiResult<FuelDrawdownsPage>> listDrawdowns({
|
||||
int? stationId,
|
||||
String? status,
|
||||
}) async {
|
||||
final payload = <String, dynamic>{};
|
||||
if (stationId != null) payload['station_id'] = stationId.toString();
|
||||
if (status != null && status.isNotEmpty) payload['status'] = status;
|
||||
|
||||
final res = await CRUD().post(link: '$_base/drawdowns.php', payload: payload);
|
||||
|
||||
if (res is Map && res['status'] == 'success' && res['message'] is Map) {
|
||||
return FuelApiResult(
|
||||
true,
|
||||
FuelDrawdownsPage.fromJson(Map<String, dynamic>.from(res['message'])),
|
||||
'ok',
|
||||
);
|
||||
}
|
||||
return FuelApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user