import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:intl/intl.dart'; import '../../core/theme/app_theme.dart'; import '../../core/widgets/status_badge.dart'; import 'tasks_provider.dart'; class SubtaskDetailScreen extends ConsumerWidget { final int taskId; final int subtaskId; const SubtaskDetailScreen({super.key, required this.taskId, required this.subtaskId}); @override Widget build(BuildContext context, WidgetRef ref) { final async = ref.watch(taskDetailProvider(subtaskId)); return async.when( loading: () => const Scaffold(body: Center(child: CircularProgressIndicator())), error: (e, _) => Scaffold(appBar: AppBar(), body: Center(child: Text(e.toString()))), data: (subtask) => Scaffold( appBar: AppBar(title: const Text('Detail Subtask')), body: ListView( padding: const EdgeInsets.all(16), children: [ // Parent ref Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: AppColors.blueLight, borderRadius: BorderRadius.circular(10), ), child: Row(children: [ const Icon(Icons.subdirectory_arrow_right, size: 15, color: AppColors.blue), const SizedBox(width: 8), Text('Subtask dari Task #$taskId', style: const TextStyle(fontSize: 12, color: AppColors.blue, fontWeight: FontWeight.w600)), const Spacer(), TextButton( onPressed: () => context.pop(), style: TextButton.styleFrom(padding: EdgeInsets.zero, minimumSize: const Size(0, 0)), child: const Text('Lihat Parent →', style: TextStyle(fontSize: 12)), ), ]), ), const SizedBox(height: 16), // Title + status Container( padding: const EdgeInsets.all(14), decoration: BoxDecoration( color: AppColors.surface, borderRadius: BorderRadius.circular(12), border: Border.all(color: AppColors.border), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row(children: [ Expanded(child: Text(subtask.title, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w700))), StatusBadge(subtask.status), ]), const SizedBox(height: 12), if (subtask.assignedUsername != null) _row(Icons.person_outline, 'Assignee', subtask.assignedUsername!), if (subtask.dueDate != null) _row(Icons.calendar_today_outlined, 'Deadline', DateFormat('d MMMM yyyy', 'id').format(subtask.dueDate!)), ], ), ), const SizedBox(height: 16), // Status buttons const Text('Update Status', style: TextStyle(fontWeight: FontWeight.w700, fontSize: 13)), const SizedBox(height: 10), ...['todo', 'in_progress', 'done'].map((s) => Padding( padding: const EdgeInsets.only(bottom: 8), child: OutlinedButton( style: OutlinedButton.styleFrom( backgroundColor: subtask.status == s ? AppColors.primary : null, foregroundColor: subtask.status == s ? Colors.white : AppColors.text, side: BorderSide(color: subtask.status == s ? AppColors.primary : AppColors.border), minimumSize: const Size(double.infinity, 46), ), onPressed: subtask.status == s ? null : () async { try { await updateTask(subtaskId, {'status': s}); ref.invalidate(taskDetailProvider(subtaskId)); ref.invalidate(taskDetailProvider(taskId)); } catch (e) { if (context.mounted) ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(e.toString()), backgroundColor: AppColors.danger)); } }, child: Text(s == 'in_progress' ? 'In Progress' : s[0].toUpperCase() + s.substring(1)), ), )), ], ), ), ); } Widget _row(IconData icon, String label, String value) => Padding( padding: const EdgeInsets.only(bottom: 8), child: Row(children: [ Icon(icon, size: 14, color: AppColors.textSub), const SizedBox(width: 6), Text('$label: ', style: const TextStyle(fontSize: 13, color: AppColors.textSub)), Text(value, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600)), ]), ); }