import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../../application/checkout/checkout_form/checkout_form_bloc.dart';
import '../../../../../application/printer/print_struck/print_struck_bloc.dart';
import '../../../../../common/theme/theme.dart';
import '../../../../components/spaces/space.dart';
import '../../../../components/toast/flushbar.dart';
import 'widgets/success_add_item_order_left_panel.dart';
import 'widgets/success_add_item_order_right_panel.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocListener<PrintStruckBloc, PrintStruckState>(
      listenWhen: (p, c) => p.failureOrPrintStruck != c.failureOrPrintStruck,
      listener: (context, state) {
        state.failureOrPrintStruck.fold(
          () {},
          (either) => either.fold(
            (f) => AppFlushbar.showPrinterFailureToast(context, f),
            (success) {
              AppFlushbar.showSuccess(context, "Struck berhasil dicetak");
            },
          ),
        );
      },
      child: Scaffold(
        backgroundColor: AppColor.background,
        body: SafeArea(
          child: BlocBuilder<CheckoutFormBloc, CheckoutFormState>(
            builder: (context, checkoutState) {
              return Padding(
                padding: const EdgeInsets.all(24.0),
                child: Row(
                  children: [
                    Expanded(
                      flex: 35,
                      child: SuccessAddItemOrderLeftPanel(
                        checkoutState: checkoutState,
                      ),
                    ),
                    const SpaceWidth(16),
                    Expanded(
                      flex: 65,
                      child: SuccessAddItemOrderRightPanel(
                        checkoutState: checkoutState,
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}
