import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; import '../api.dart'; class DepositScreen extends StatefulWidget { final Map order; const DepositScreen({super.key, required this.order}); @override State createState() => _DepositScreenState(); } class _DepositScreenState extends State { String status = "waiting_handshake"; Timer? timer; String? token; @override void initState() { super.initState(); _init(); } @override void dispose() { timer?.cancel(); super.dispose(); } Future _init() async { final prefs = await SharedPreferences.getInstance(); token = prefs.getString('api_token'); timer = Timer.periodic(const Duration(seconds: 3), (_) { fetchStatus(); }); } Future fetchStatus() async { if (token == null) return; try { final res = await http.get( Uri.parse( "https://taskmarket.store/api/agent/order-status.php?order_id=${widget.order['id']}&type=deposit" ), headers: {'Authorization': 'Bearer $token'}, ); final data = jsonDecode(res.body); if (!mounted) return; if (data['ok'] == true) { setState(() { status = data['status']; }); } } catch (e) { print("ERROR: $e"); } } Future post(String path) async { if (token == null) return; await http.post( Uri.parse(Api.post(path)), headers: {'Authorization': 'Bearer $token'}, body: {'order_id': widget.order['id'].toString()}, ); } @override Widget build(BuildContext context) { final order = widget.order; return Scaffold( appBar: AppBar(title: const Text("إيداع")), body: Padding( padding: const EdgeInsets.all(20), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("ربحك: ${order['profit']} IQD"), const SizedBox(height: 10), Text("المبلغ: ${order['amount']} IQD"), const SizedBox(height: 10), Text("الطريقة: ${order['method']}"), const SizedBox(height: 10), Text("رقم الحساب: ${order['account_number']}"), const SizedBox(height: 20), /// الحالات if (status == "waiting_handshake") const Text("بانتظار الزبون..."), if (status == "user_ready") ElevatedButton( onPressed: () => post("/agent/handshake.php"), child: const Text("أنا مستعد"), ), if (status == "waiting_proof") const Text("بانتظار إثبات الدفع..."), if (status == "proof_uploaded") Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ ElevatedButton( onPressed: () => post("/agent/confirm-received.php"), child: const Text("تأكيد الاستلام"), ), const SizedBox(height: 10), ElevatedButton( onPressed: () async { final confirm = await showDialog( context: context, builder: (_) => AlertDialog( title: const Text("تأكيد الإنكار"), content: const Text( "هل أنت متأكد من إنكار الاستلام؟"), actions: [ TextButton( onPressed: () => Navigator.pop(context, false), child: const Text("إلغاء"), ), ElevatedButton( onPressed: () => Navigator.pop(context, true), child: const Text("نعم"), ), ], ), ); if (confirm != true) return; await post("/agent/deny-received.php"); Navigator.pushReplacement( context, MaterialPageRoute( builder: (_) => Scaffold( appBar: AppBar(title: const Text("تم التحويل")), body: const Center( child: Text( "تم تحويل العملية إلى نزاع", textAlign: TextAlign.center, ), ), ), ), ); }, child: const Text("إنكار الاستلام"), ), ], ), if (status == "completed") const Center( child: Text( "تمت العملية بنجاح", style: TextStyle(fontSize: 18), ), ), if (status == "disputed") const Center( child: Text( "العملية قيد النزاع", style: TextStyle(fontSize: 18), ), ), ], ), ), ); } }