import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:syncrow_web/pages/device_managment/water_heater/bloc/water_heater_bloc.dart'; import 'package:syncrow_web/pages/device_managment/water_heater/models/schedule_model.dart'; import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/constants/assets.dart'; import 'package:syncrow_web/utils/extension/build_context_x.dart'; import 'package:syncrow_web/utils/format_date_time.dart'; import '../helper/add_schedule_dialog_helper.dart'; class ScheduleTableWidget extends StatelessWidget { final WaterHeaterDeviceStatusLoaded state; const ScheduleTableWidget({ super.key, required this.state, }); @override Widget build(BuildContext context) { return Column( children: [ Table( border: TableBorder.all( color: ColorsManager.graysColor, borderRadius: const BorderRadius.only( topLeft: Radius.circular(20), topRight: Radius.circular(20)), ), children: [ TableRow( decoration: const BoxDecoration( color: ColorsManager.boxColor, borderRadius: BorderRadius.only( topLeft: Radius.circular(20), topRight: Radius.circular(20), ), ), children: [ _buildTableHeader('Active'), _buildTableHeader('Days'), _buildTableHeader('Time'), _buildTableHeader('Function'), _buildTableHeader('Action'), ], ), ], ), BlocBuilder( builder: (context, state) { if (state is ScheduleLoadingState) { return const SizedBox( height: 200, child: Center(child: CircularProgressIndicator())); } if (state is WaterHeaterDeviceStatusLoaded && state.schedules.isEmpty) { return _buildEmptyState(context); } else if (state is WaterHeaterDeviceStatusLoaded) { return Container( height: 200, decoration: BoxDecoration( border: Border.all(color: ColorsManager.graysColor), borderRadius: const BorderRadius.only( bottomLeft: Radius.circular(20), bottomRight: Radius.circular(20)), ), child: _buildTableBody(state, context)); } return const SizedBox(); }, ), ], ); } Widget _buildEmptyState(BuildContext context) { return Container( height: 200, decoration: BoxDecoration( border: Border.all(color: ColorsManager.graysColor), borderRadius: const BorderRadius.only( bottomLeft: Radius.circular(20), bottomRight: Radius.circular(20)), ), child: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ SvgPicture.asset(Assets.emptyRecords, width: 40, height: 40), const SizedBox(height: 8), Padding( padding: const EdgeInsets.all(8.0), child: Text( 'No schedules added yet', style: context.textTheme.bodySmall!.copyWith( fontSize: 13, color: ColorsManager.grayColor, ), ), ), ], ), ), ); } Widget _buildTableBody( WaterHeaterDeviceStatusLoaded state, BuildContext context) { return SingleChildScrollView( child: Table( border: TableBorder.all(color: ColorsManager.graysColor), defaultVerticalAlignment: TableCellVerticalAlignment.middle, children: [ for (int i = 0; i < state.schedules.length; i++) _buildScheduleRow(state.schedules[i], i, context), ], ), ); } Widget _buildTableHeader(String label) { return TableCell( child: Padding( padding: const EdgeInsets.all(12), child: Text( label, style: const TextStyle( fontSize: 13, color: ColorsManager.grayColor, ), ), ), ); } TableRow _buildScheduleRow( ScheduleModel schedule, int index, BuildContext context) { return TableRow( children: [ Center( child: schedule.function.value ? const Icon(Icons.radio_button_checked, color: ColorsManager.blueColor) : const Icon(Icons.radio_button_unchecked)), Center( child: Text(_getSelectedDays( ScheduleModel.parseSelectedDays(schedule.days)))), Center(child: Text(formatIsoStringToTime(schedule.time))), Center(child: Text(schedule.function.value ? 'On' : 'Off')), Center( child: Wrap( runAlignment: WrapAlignment.center, children: [ TextButton( style: TextButton.styleFrom(padding: EdgeInsets.zero), onPressed: () { ScheduleDialogHelper.showAddScheduleDialog(context, schedule: schedule, index: index, isEdit: true); }, child: Text( 'Edit', style: context.textTheme.bodySmall! .copyWith(color: ColorsManager.blueColor), ), ), TextButton( style: TextButton.styleFrom(padding: EdgeInsets.zero), onPressed: () { context.read().add(DeleteScheduleEvent( index: index, scheduleId: schedule.scheduleId, )); }, child: Text( 'Delete', style: context.textTheme.bodySmall! .copyWith(color: ColorsManager.blueColor), ), ), ], ), ), ], ); } String _getSelectedDays(List selectedDays) { final days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; List selectedDaysStr = []; for (int i = 0; i < selectedDays.length; i++) { if (selectedDays[i]) { selectedDaysStr.add(days[i]); } } return selectedDaysStr.join(', '); } }