import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../core/api_service.dart'; import '../core/auth_service.dart'; import '../models/device.dart'; import '../widgets/status_badge.dart'; import 'device_detail_screen.dart'; import 'device_form_screen.dart'; class DevicesTab extends StatefulWidget { const DevicesTab({super.key}); @override State createState() => _DevicesTabState(); } class _DevicesTabState extends State { late Future> _future; @override void initState() { super.initState(); _future = _load(); } Future> _load() => context.read().devices(); Future _refresh() async { final future = _load(); setState(() => _future = future); await future; } Future _openForm({Device? device}) async { final saved = await Navigator.of(context).push( MaterialPageRoute(builder: (_) => DeviceFormScreen(device: device)), ); if (saved == true) _refresh(); } Future _delete(Device device) async { final confirmed = await showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Удалить устройство?'), content: Text('«${device.name}» будет удалено без возможности восстановления.'), actions: [ TextButton(onPressed: () => Navigator.pop(context, false), child: const Text('Отмена')), TextButton(onPressed: () => Navigator.pop(context, true), child: const Text('Удалить')), ], ), ); if (confirmed != true || !mounted) return; try { await context.read().deleteDevice(device.id); _refresh(); } catch (e) { if (mounted) ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('$e'))); } } @override Widget build(BuildContext context) { final isOwner = context.watch().user?.isOwner ?? false; return Scaffold( body: RefreshIndicator( onRefresh: _refresh, child: FutureBuilder>( future: _future, builder: (context, snapshot) { if (snapshot.connectionState != ConnectionState.done) { return const Center(child: CircularProgressIndicator()); } if (snapshot.hasError) { return _ErrorList(message: '${snapshot.error}', onRetry: _refresh); } final devices = snapshot.data!; if (devices.isEmpty) { return _ErrorList(message: 'Устройств пока нет.', onRetry: _refresh); } return ListView.separated( physics: const AlwaysScrollableScrollPhysics(), itemCount: devices.length, separatorBuilder: (context, index) => const Divider(height: 1), itemBuilder: (context, index) { final device = devices[index]; return ListTile( title: Text(device.name), subtitle: Text('${device.zone?.name ?? '—'} · ${device.deviceType?.code ?? device.protocol}'), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ StatusBadge(status: device.status), if (isOwner) PopupMenuButton( onSelected: (value) { if (value == 'edit') _openForm(device: device); if (value == 'delete') _delete(device); }, itemBuilder: (context) => const [ PopupMenuItem(value: 'edit', child: Text('Редактировать')), PopupMenuItem(value: 'delete', child: Text('Удалить')), ], ), ], ), onTap: () => Navigator.of(context).push( MaterialPageRoute(builder: (_) => DeviceDetailScreen(deviceId: device.id)), ), ); }, ); }, ), ), floatingActionButton: isOwner ? FloatingActionButton(onPressed: () => _openForm(), child: const Icon(Icons.add)) : null, ); } } class _ErrorList extends StatelessWidget { const _ErrorList({required this.message, required this.onRetry}); final String message; final Future Function() onRetry; @override Widget build(BuildContext context) { return ListView( physics: const AlwaysScrollableScrollPhysics(), children: [ Padding( padding: const EdgeInsets.symmetric(vertical: 64, horizontal: 24), child: Column( children: [ Text(message, textAlign: TextAlign.center), const SizedBox(height: 12), TextButton(onPressed: onRetry, child: const Text('Повторить')), ], ), ), ], ); } }