Update: 2026-08-03 11:32:12
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
import 'dart:convert';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:siro_admin/constant/links.dart';
|
||||
import 'package:siro_admin/controller/functions/crud.dart';
|
||||
import 'package:siro_admin/views/widgets/snackbar.dart';
|
||||
import '../../print.dart';
|
||||
|
||||
/// إدارة المطاعم (وحدة الطعام).
|
||||
/// الباك إند كان جاهزاً بالكامل في backend/food/admin/ منذ البداية
|
||||
/// (merchants / merchant_approve / payouts) دون أي واجهة في اللوحة.
|
||||
class FoodMerchantsController extends GetxController {
|
||||
bool isLoading = true;
|
||||
List<dynamic> merchants = [];
|
||||
String filter = 'all';
|
||||
|
||||
/// المطعم قيد المعالجة — لتعطيل أزراره أثناء الطلب
|
||||
int? busyId;
|
||||
|
||||
/// الحالات كما تقبلها backend/food/admin/merchants.php حرفياً
|
||||
static const statuses = <String, String>{
|
||||
'all': 'الكل',
|
||||
'pending_approval': 'بانتظار الاعتماد',
|
||||
'active': 'نشط',
|
||||
'paused': 'موقوف مؤقتاً',
|
||||
'suspended': 'معلّق',
|
||||
'rejected': 'مرفوض',
|
||||
};
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
fetchMerchants();
|
||||
}
|
||||
|
||||
Future<void> changeFilter(String next) async {
|
||||
if (filter == next) return;
|
||||
filter = next;
|
||||
await fetchMerchants();
|
||||
}
|
||||
|
||||
Future<void> fetchMerchants() async {
|
||||
isLoading = true;
|
||||
update();
|
||||
|
||||
try {
|
||||
var res = await CRUD()
|
||||
.post(link: AppLink.foodMerchants, payload: {'status': filter});
|
||||
if (res != 'failure' && res != null) {
|
||||
var d = res is String ? jsonDecode(res) : res;
|
||||
if (d['status'] == 'success') {
|
||||
// jsonSuccess يغلّف الحمولة في message لا data
|
||||
final payload = d['message'] ?? d['data'];
|
||||
if (payload is Map && payload['merchants'] is List) {
|
||||
merchants = payload['merchants'];
|
||||
} else {
|
||||
merchants = [];
|
||||
}
|
||||
} else {
|
||||
mySnackbarError('${d['message'] ?? 'تعذّر جلب المطاعم'}');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print('Error fetching merchants: $e');
|
||||
mySnackbarError('$e');
|
||||
}
|
||||
|
||||
isLoading = false;
|
||||
update();
|
||||
}
|
||||
|
||||
/// approve أو reject — الخادم يقبلهما فقط لمطعم بحالة pending_approval.
|
||||
Future<void> decide(int merchantId, String action) async {
|
||||
busyId = merchantId;
|
||||
update();
|
||||
|
||||
try {
|
||||
var res = await CRUD().post(
|
||||
link: AppLink.foodMerchantApprove,
|
||||
payload: {
|
||||
'merchant_id': merchantId.toString(),
|
||||
'action': action,
|
||||
},
|
||||
);
|
||||
if (res != 'failure' && res != null) {
|
||||
var d = res is String ? jsonDecode(res) : res;
|
||||
if (d['status'] == 'success') {
|
||||
mySnackbarSuccess(
|
||||
action == 'approve' ? 'تم اعتماد المطعم' : 'تم رفض المطعم');
|
||||
busyId = null;
|
||||
await fetchMerchants();
|
||||
return;
|
||||
}
|
||||
mySnackbarError('${d['message'] ?? 'تعذّر تنفيذ الإجراء'}');
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print('Error deciding merchant: $e');
|
||||
mySnackbarError('$e');
|
||||
}
|
||||
|
||||
busyId = null;
|
||||
update();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user