165 lines
5.5 KiB
Dart
165 lines
5.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:intaleq_rider/constant/box_name.dart';
|
|
import 'package:intaleq_rider/constant/links.dart';
|
|
import 'package:intaleq_rider/main.dart';
|
|
import 'package:intaleq_rider/controller/functions/crud.dart';
|
|
import 'package:intaleq_rider/views/widgets/error_snakbar.dart';
|
|
|
|
class PrimeController extends GetxController {
|
|
// ── State ──────────────────────────────────────────────────
|
|
bool isLoading = false;
|
|
bool isPrime = false;
|
|
String? expireAt;
|
|
double currentWalletBalance = 0.0;
|
|
|
|
// ── Pricing map (mirrors backend) ─────────────────────────
|
|
static Map<String, Map<String, dynamic>> pricingMap = {
|
|
'Jordan': {'amount': 1.50, 'currency': 'JOD', 'symbol': 'JOD'},
|
|
'Egypt': {'amount': 150.00, 'currency': 'EGP', 'symbol': 'EGP'},
|
|
'Syria': {'amount': 50000.0, 'currency': 'SYP', 'symbol': 'SYP'},
|
|
};
|
|
|
|
String get country => box.read(BoxName.countryCode) ?? 'Jordan';
|
|
|
|
double get primePrice {
|
|
return (pricingMap[country]?['amount'] ?? 1.50) as double;
|
|
}
|
|
|
|
String get primeCurrency {
|
|
return pricingMap[country]?['symbol'] ?? 'JOD';
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
fetchPrimeStatus();
|
|
}
|
|
|
|
// ── Fetch Prime Status ────────────────────────────────────
|
|
Future<void> fetchPrimeStatus() async {
|
|
isLoading = true;
|
|
update();
|
|
|
|
try {
|
|
final res = await CRUD().post(link: AppLink.getPrimeStatus, payload: {});
|
|
if (res != 'failure' && res['status'] == 'success') {
|
|
isPrime = res['data']['is_prime'] == true;
|
|
expireAt = res['data']['expire_at'];
|
|
}
|
|
} catch (e) {
|
|
// Non-critical — default is false
|
|
}
|
|
|
|
isLoading = false;
|
|
update();
|
|
}
|
|
|
|
// ── Subscribe to Prime ────────────────────────────────────
|
|
Future<void> subscribePrime(BuildContext context) async {
|
|
isLoading = true;
|
|
update();
|
|
|
|
try {
|
|
final res = await CRUD().post(
|
|
link: AppLink.initiatePrime,
|
|
payload: {'country': country},
|
|
);
|
|
|
|
if (res == 'failure') {
|
|
mySnackbarError('Connection error. Please try again.'.tr);
|
|
isLoading = false;
|
|
update();
|
|
return;
|
|
}
|
|
|
|
final status = res['status'] ?? '';
|
|
|
|
if (status == 'success') {
|
|
// ✅ تم الاشتراك بنجاح
|
|
isPrime = true;
|
|
expireAt = res['data']['expire_at'];
|
|
box.write(BoxName.isPrime, true);
|
|
update();
|
|
_showSuccessDialog(context);
|
|
|
|
} else if (status == 'insufficient_balance') {
|
|
// 💳 رصيد غير كافٍ
|
|
final required = res['required_amount'] ?? primePrice;
|
|
final current = res['current_balance'] ?? 0.0;
|
|
_showInsufficientBalanceDialog(context, current: current.toDouble(), required: required.toDouble());
|
|
|
|
} else {
|
|
mySnackbarError((res['message'] ?? 'Subscription failed.').toString().tr);
|
|
}
|
|
} catch (e) {
|
|
mySnackbarError('An error occurred. Please try again.'.tr);
|
|
}
|
|
|
|
isLoading = false;
|
|
update();
|
|
}
|
|
|
|
// ── Dialogs ───────────────────────────────────────────────
|
|
void _showSuccessDialog(BuildContext context) {
|
|
Get.defaultDialog(
|
|
title: '👑 Intaleq Prime!',
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(Icons.verified, color: Colors.amber, size: 48),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
'You are now a Intaleq Prime member! Enjoy no surge pricing for 30 days.'.tr,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
if (expireAt != null) ...[
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
'${'Valid until'.tr}: $expireAt',
|
|
style: const TextStyle(color: Colors.grey, fontSize: 12),
|
|
),
|
|
]
|
|
],
|
|
),
|
|
textConfirm: 'Great!'.tr,
|
|
onConfirm: () => Get.back(),
|
|
);
|
|
}
|
|
|
|
void _showInsufficientBalanceDialog(BuildContext context, {required double current, required double required}) {
|
|
Get.defaultDialog(
|
|
title: 'Insufficient Balance'.tr,
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(Icons.account_balance_wallet_outlined, color: Colors.orange, size: 48),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
'${'Your current balance'.tr}: ${current.toStringAsFixed(2)} $primeCurrency',
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
'${'Required'.tr}: ${required.toStringAsFixed(2)} $primeCurrency',
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
'Please top up your wallet to subscribe to Intaleq Prime.'.tr,
|
|
textAlign: TextAlign.center,
|
|
style: const TextStyle(color: Colors.grey),
|
|
),
|
|
],
|
|
),
|
|
textConfirm: 'Top Up Wallet'.tr,
|
|
textCancel: 'Cancel'.tr,
|
|
onConfirm: () {
|
|
Get.back(); // close dialog
|
|
Get.back(); // close Prime page → return to wallet page to top up
|
|
},
|
|
);
|
|
}
|
|
}
|