games_app/prediqo_mobile/lib/features/admin/presentation/options_screen.dart
2026-06-29 15:01:23 +02:00

199 lines
5.9 KiB
Dart

/// Admin presentation: options hub with network status and admin-only links.
library;
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import '../../../core/api/api_client.dart';
import '../../../core/theme/app_theme.dart';
import '../../auth/bloc/auth_bloc.dart';
/// Options tab; shows admin tools when [AuthState.isAdmin] is true.
class OptionsScreen extends StatelessWidget {
const OptionsScreen({super.key});
@override
Widget build(BuildContext context) {
final isAdmin = context.read<AuthBloc>().state.isAdmin;
return Scaffold(
appBar: AppBar(
title: const Text('Options'),
flexibleSpace: Container(
decoration: const BoxDecoration(
gradient: AppTheme.goldGradient,
),
),
),
body: ListView(
padding: const EdgeInsets.all(20),
children: [
if (isAdmin) ...[
const _NetworkBanner(),
const SizedBox(height: 12),
_OptionTile(
icon: Icons.people_rounded,
title: 'User Management',
subtitle: 'View and manage registered users, account status',
onTap: () => context.push('/admin/users'),
),
const SizedBox(height: 12),
_OptionTile(
icon: Icons.settings_applications_rounded,
title: 'Settings',
subtitle: 'Password policy, token expiration, and system configuration',
onTap: () => context.push('/options/settings'),
),
const SizedBox(height: 12),
],
if (!isAdmin)
const Padding(
padding: EdgeInsets.symmetric(vertical: 24),
child: Center(
child: Text(
'No administrative options available.',
style: TextStyle(color: Colors.grey),
),
),
),
],
),
);
}
}
/// Fetches and displays USDT (TRC20) network config from the health endpoint.
class _NetworkBanner extends StatefulWidget {
const _NetworkBanner();
@override
State<_NetworkBanner> createState() => _NetworkBannerState();
}
/// Loads network health data on init and on manual refresh.
class _NetworkBannerState extends State<_NetworkBanner> {
bool _loading = true;
String? _error;
Map<String, dynamic>? _data;
@override
void initState() {
super.initState();
_load();
}
/// Calls GET /health and updates the banner with network and withdrawal flags.
Future<void> _load() async {
setState(() {
_loading = true;
_error = null;
});
try {
final api = context.read<ApiClient>();
final res = await api.dio.get<Map<String, dynamic>>('/health');
if (!mounted) return;
setState(() {
_data = res.data ?? {};
_loading = false;
});
} catch (e) {
if (!mounted) return;
setState(() {
_error = e.toString().replaceFirst('Exception: ', '');
_loading = false;
});
}
}
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
if (_loading) {
return Card(
child: ListTile(
leading: const SizedBox(height: 22, width: 22, child: CircularProgressIndicator(strokeWidth: 2)),
title: const Text('Network'),
subtitle: const Text('Loading current network configuration…'),
),
);
}
if (_error != null) {
return Card(
color: scheme.errorContainer,
child: ListTile(
leading: Icon(Icons.error_outline_rounded, color: scheme.onErrorContainer),
title: Text('Network banner unavailable', style: TextStyle(color: scheme.onErrorContainer)),
subtitle: Text(_error!, style: TextStyle(color: scheme.onErrorContainer)),
trailing: TextButton(onPressed: _load, child: const Text('Retry')),
),
);
}
final network = (_data?['network'] ?? 'unknown').toString();
final withdrawalsEnabled = _data?['withdrawalsEnabled'] == true;
final ok = network == 'shasta' && withdrawalsEnabled == false;
final bg = ok ? scheme.secondaryContainer : scheme.tertiaryContainer;
final fg = ok ? scheme.onSecondaryContainer : scheme.onTertiaryContainer;
return Card(
color: bg,
child: ListTile(
leading: Icon(ok ? Icons.verified_rounded : Icons.warning_amber_rounded, color: fg),
title: Text('Network: USDT TRC20 · TRON $network', style: TextStyle(color: fg, fontWeight: FontWeight.w600)),
subtitle: Text(
'Withdrawals broadcasting: ${withdrawalsEnabled ? "ENABLED" : "DISABLED"}',
style: TextStyle(color: fg),
),
trailing: IconButton(
tooltip: 'Refresh',
onPressed: _load,
icon: Icon(Icons.refresh_rounded, color: fg),
),
),
);
}
}
/// Navigation tile for an admin option (user management, settings, etc.).
class _OptionTile extends StatelessWidget {
const _OptionTile({
required this.icon,
required this.title,
required this.subtitle,
required this.onTap,
});
final IconData icon;
final String title;
final String subtitle;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
leading: Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: AppColors.goldPrimary.withValues(alpha: 0.2),
borderRadius: BorderRadius.circular(12),
),
child: Icon(icon, color: AppColors.goldPrimary),
),
title: Text(
title,
style: const TextStyle(fontWeight: FontWeight.w600),
),
subtitle: Text(
subtitle,
style: Theme.of(context).textTheme.bodySmall,
),
trailing: const Icon(Icons.chevron_right_rounded),
onTap: onTap,
),
);
}
}