import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/views/report/energy/cubit/energy_cubit.dart';
import 'package:smart_hotel_app/views/report/energy/cubit/energy_state.dart';
import 'package:smart_hotel_app/views/report/energy/widget/energy_stats_card_block.dart';
import 'package:smart_hotel_app/views/report/energy/widget/hourly_chart_block.dart';
import 'package:smart_hotel_app/views/report/energy/widget/zone_chart_block.dart';

@RoutePage()
class ReportEnergyDetailView extends StatelessWidget {
  const ReportEnergyDetailView({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => EnergyCubit(),
      child: Scaffold(
        backgroundColor: const Color.fromRGBO(245, 245, 247, 1),
        appBar: AppBar(
          backgroundColor: Colors.white,
          elevation: 0,
          leading: IconButton(
            icon: const Icon(Icons.arrow_back_ios, color: Colors.black87),
            onPressed: () {
              context.popRoute();
            },
          ),
          title: Text(
            '能耗分析',
            style: TextStyle(
              color: Colors.black87,
              fontSize: 32.sp,
              fontWeight: FontWeight.bold,
            ),
          ),
          centerTitle: true,
        ),
        body: BlocBuilder<EnergyCubit, EnergyState>(
          builder: (context, state) {
            if (state.isLoading) {
              return const Center(child: CircularProgressIndicator());
            }
            if (state.error != null) {
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '加载失败: ${state.error}',
                      style: TextStyle(
                        fontSize: 28.sp,
                        color: const Color.fromRGBO(100, 116, 139, 1),
                      ),
                    ),
                    SizedBox(height: 16.h),
                    ElevatedButton(
                      onPressed: () => context.read<EnergyCubit>().loadData(),
                      child: const Text('重试'),
                    ),
                  ],
                ),
              );
            }
            return SingleChildScrollView(
              child: Column(
                children: [
                  SizedBox(height: 16.h),
                  EnergyStatsCardBlock(
                    todayEnergy: state.todayEnergy,
                    todayEnergyChange: state.todayEnergyChange,
                    monthEnergy: state.monthEnergy,
                    monthEnergyChange: state.monthEnergyChange,
                    monthBill: state.monthBill,
                  ),
                  SizedBox(height: 16.h),
                  HourlyChartBlock(
                    hourlyData: state.hourlyData,
                    maxValue: state.hourlyMaxValue,
                  ),
                  SizedBox(height: 16.h),
                  ZoneChartBlock(zoneData: state.zoneData),
                  SizedBox(height: 20.h),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
