90 lines
3.6 KiB
Dart
90 lines
3.6 KiB
Dart
// food_delivery_service.dart — طبقة الاتصال بـ backend/food (جهة السائق/الموصّل)
|
|
import '../functions/crud.dart';
|
|
import '../../constant/links.dart';
|
|
import 'food_delivery_models.dart';
|
|
|
|
class FoodDeliveryApiResult<T> {
|
|
final bool success;
|
|
final T? data;
|
|
final String message;
|
|
FoodDeliveryApiResult(this.success, this.data, this.message);
|
|
}
|
|
|
|
class FoodDeliveryService {
|
|
static String get _base => '${AppLink.server}/food';
|
|
|
|
static Future<FoodDeliveryApiResult<bool>> toggleAvailability(bool enabled) async {
|
|
final res = await CRUD().post(
|
|
link: '$_base/courier/toggle_availability.php',
|
|
payload: {'enabled': enabled.toString()},
|
|
);
|
|
if (res is Map && res['status'] == 'success') return FoodDeliveryApiResult(true, enabled, 'ok');
|
|
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
|
}
|
|
|
|
static Future<FoodDeliveryApiResult<List<FoodDeliveryTask>>> getActiveTasks() async {
|
|
final res = await CRUD().post(link: '$_base/courier/active.php');
|
|
if (res is Map && res['status'] == 'success') {
|
|
final msg = res['message'];
|
|
final list = (msg is Map && msg['orders'] is List)
|
|
? (msg['orders'] as List)
|
|
.map((o) => FoodDeliveryTask.fromJson(Map<String, dynamic>.from(o)))
|
|
.toList()
|
|
: <FoodDeliveryTask>[];
|
|
return FoodDeliveryApiResult(true, list, 'ok');
|
|
}
|
|
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
|
}
|
|
|
|
static Future<FoodDeliveryApiResult<List<FoodDeliveryOffer>>> getPendingOffers() async {
|
|
final res = await CRUD().post(link: '$_base/courier/pending_offers.php');
|
|
if (res is Map && res['status'] == 'success') {
|
|
final msg = res['message'];
|
|
final list = (msg is Map && msg['offers'] is List)
|
|
? (msg['offers'] as List)
|
|
.map((o) => FoodDeliveryOffer.fromJson(Map<String, dynamic>.from(o)))
|
|
.toList()
|
|
: <FoodDeliveryOffer>[];
|
|
return FoodDeliveryApiResult(true, list, 'ok');
|
|
}
|
|
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
|
}
|
|
|
|
static Future<FoodDeliveryApiResult<String>> respondToOffer(int orderId, bool accept) async {
|
|
final res = await CRUD().post(
|
|
link: '$_base/courier/offer_respond.php',
|
|
payload: {'order_id': orderId.toString(), 'response': accept ? 'accept' : 'reject'},
|
|
);
|
|
if (res is Map && res['status'] == 'success') {
|
|
final msg = res['message'];
|
|
return FoodDeliveryApiResult(true, (msg is Map ? msg['response']?.toString() : null) ?? '', 'ok');
|
|
}
|
|
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
|
}
|
|
|
|
static Future<FoodDeliveryApiResult<void>> markPickedUp(int orderId) async {
|
|
final res = await CRUD().post(
|
|
link: '$_base/courier/picked_up.php',
|
|
payload: {'order_id': orderId.toString()},
|
|
);
|
|
if (res is Map && res['status'] == 'success') return FoodDeliveryApiResult(true, null, 'ok');
|
|
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
|
}
|
|
|
|
static Future<FoodDeliveryApiResult<void>> markDelivered(int orderId) async {
|
|
final res = await CRUD().post(
|
|
link: '$_base/courier/delivered.php',
|
|
payload: {'order_id': orderId.toString()},
|
|
);
|
|
if (res is Map && res['status'] == 'success') return FoodDeliveryApiResult(true, null, 'ok');
|
|
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
|
}
|
|
|
|
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 'حدث خطأ، حاول مجدداً';
|
|
}
|
|
}
|