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/rule/cubit/rule_management_cubit.dart';
import 'package:smart_hotel_app/views/report/rule/cubit/rule_management_state.dart';
import 'package:smart_hotel_app/views/report/rule/widget/rule_card.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => RuleManagementCubit(),
      child: BlocBuilder<RuleManagementCubit, RuleManagementState>(
        builder: (context, state) {
          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.popRoute();
                },
              ),
              title: Text(
                '联动规则管理',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 32.sp,
                  fontWeight: FontWeight.bold,
                ),
              ),
              centerTitle: true,
            ),
            body: SingleChildScrollView(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 28.w, vertical: 20.h),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(24.r),
                  ),
                  child: ListView.separated(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    itemCount: state.rules.length,
                    separatorBuilder: (context, index) => Divider(
                      height: 1.h,
                      color: const Color.fromRGBO(229, 231, 235, 1),
                      indent: 28.w,
                      endIndent: 28.w,
                    ),
                    itemBuilder: (context, index) {
                      return RuleCard(
                        rule: state.rules[index],
                        onToggle: (id) {
                          context.read<RuleManagementCubit>().toggleRule(id);
                        },
                      );
                    },
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
