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/repositories/room_repository.dart';
import 'package:smart_hotel_app/services/room_service.dart';
import 'package:smart_hotel_app/views/report/room/cubit/room_report_cubit.dart';
import 'package:smart_hotel_app/views/report/room/cubit/room_report_state.dart';
import 'package:smart_hotel_app/views/report/room/widget/floor_selector.dart';
import 'package:smart_hotel_app/views/report/room/widget/room_grid.dart';
import 'package:smart_hotel_app/views/report/room/widget/status_stats.dart';
import 'package:smart_hotel_app/views/report/room/widget/room_detail_card.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => RoomReportCubit(
        service: RoomService(repository: RoomRepository()),
      ),
      child: BlocBuilder<RoomReportCubit, RoomReportState>(
        builder: (context, state) {
          if (state.isLoading && state.floors.isEmpty) {
            return Scaffold(
              backgroundColor: const Color.fromRGBO(242, 243, 245, 1),
              appBar: AppBar(
                backgroundColor: Colors.white,
                elevation: 0,
                leading: IconButton(
                  icon: const Icon(Icons.arrow_back_ios, color: Colors.black),
                  onPressed: () => context.maybePop(),
                ),
                title: Text(
                  '客房状态总览',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 32.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                centerTitle: true,
              ),
              body: const Center(child: CircularProgressIndicator()),
            );
          }

          if (state.error != null && state.floors.isEmpty) {
            return Scaffold(
              backgroundColor: const Color.fromRGBO(242, 243, 245, 1),
              appBar: AppBar(
                backgroundColor: Colors.white,
                elevation: 0,
                leading: IconButton(
                  icon: const Icon(Icons.arrow_back_ios, color: Colors.black),
                  onPressed: () => context.maybePop(),
                ),
                title: Text(
                  '客房状态总览',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 32.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                centerTitle: true,
              ),
              body: Center(child: Text('加载失败: ${state.error}')),
            );
          }

          return Scaffold(
            backgroundColor: const Color.fromRGBO(242, 243, 245, 1),
            appBar: AppBar(
              backgroundColor: Colors.white,
              elevation: 0,
              leading: IconButton(
                icon: const Icon(Icons.arrow_back_ios, color: Colors.black),
                onPressed: () {
                  context.maybePop();
                },
              ),
              title: Text(
                '客房状态总览',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 32.sp,
                  fontWeight: FontWeight.bold,
                ),
              ),
              centerTitle: true,
            ),
            body: SingleChildScrollView(
              child: Column(
                children: [
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 28.w),
                    child: Column(
                      children: [
                        SizedBox(height: 10.h),
                        if (state.floors.isNotEmpty)
                          Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.vertical(top: Radius.circular(24.r)),
                            ),
                            child: FloorSelector(
                              floorNames: state.floors.map((f) => f.floorName).toList(),
                              selectedIndex: state.selectedFloorIndex,
                            ),
                          ),
                        if (state.floors.isNotEmpty && state.selectedFloorIndex < state.floors.length)
                          Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.vertical(bottom: Radius.circular(24.r)),
                            ),
                            padding: EdgeInsets.only(bottom: 28.w),
                            child: RoomGrid(rooms: state.roomGrid),
                          ),
                        SizedBox(height: 10.h),
                        StatusStats(summary: state.summary),
                        SizedBox(height: 10.h),
                        if (state.isLoading)
                          const Padding(
                            padding: EdgeInsets.all(16.0),
                            child: Center(child: CircularProgressIndicator()),
                          ),
                        ...state.detailRooms.map((room) {
                          return Padding(
                            padding: EdgeInsets.only(bottom: 10.h),
                            child: RoomDetailCard(room: room),
                          );
                        }).toList(),
                        SizedBox(height: 10.h),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}