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

import '../../../../application/order/order_loader/order_loader_bloc.dart';
import '../../../../common/theme/theme.dart';
import '../../../components/button/button.dart';
import '../../../components/spaces/space.dart';
import '../../../router/app_router.gr.dart';
import 'order_information.dart';
import 'order_list.dart';
import 'order_list_payment.dart';
import 'order_payment_summary.dart';

class OrderRightPanel extends StatelessWidget {
  final OrderLoaderState state;
  const OrderRightPanel({super.key, required this.state});

  @override
  Widget build(BuildContext context) {
    if (state.selectedOrder == null) {
      return Center(
        child: Text(
          "Belum ada order yang dipilih.",
          style: AppStyle.lg.copyWith(fontWeight: FontWeight.bold),
        ),
      );
    }

    return Column(
      children: [
        Expanded(
          child: SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  OrderInformation(order: state.selectedOrder),
                  OrderList(order: state.selectedOrder),
                  const SpaceHeight(16),
                  OrderPaymentSummary(order: state.selectedOrder),
                  const SpaceHeight(16),
                  if (state.selectedOrder?.payments != null &&
                      state.selectedOrder?.payments.isNotEmpty == true) ...[
                    OrderListPayment(order: state.selectedOrder),
                    const SpaceHeight(20),
                  ],
                ],
              ),
            ),
          ),
        ),
        Container(
          padding: EdgeInsets.all(16),
          decoration: BoxDecoration(color: AppColor.white),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              AppElevatedButton.outlined(
                onPressed: () {
                  if (state.selectedOrder?.status == 'completed') {
                    // onPrintRecipt(
                    //   context,
                    //   order: orderDetail!,
                    //   paymentMethod:
                    //       orderDetail!.payments
                    //           ?.map((p) => p.paymentMethodName)
                    //           .join(', ') ??
                    //       "",
                    //   nominalBayar: orderDetail?.totalPaid ?? 0,
                    //   kembalian: 0,
                    //   productQuantity: orderDetail!.orderItems!
                    //       .toProductQuantities(),
                    // );
                  } else {
                    // onPrintBill(
                    //   context,
                    //   productQuantity: orderDetail!.orderItems!
                    //       .toProductQuantities(),
                    //   order: orderDetail!,
                    // );
                  }
                },
                label: 'Print Bill',
                icon: Icon(Icons.print),
              ),
              SpaceWidth(8),
              if (state.selectedOrder?.status == 'completed')
                if (state.selectedOrder?.isRefund == false)
                  AppElevatedButton.outlined(
                    onPressed: () {
                      context.router.push(
                        RefundRoute(order: state.selectedOrder!),
                      );
                    },
                    label: 'Refund',
                    icon: Icon(Icons.autorenew),
                  ),
              if (state.selectedOrder?.status == 'pending') ...[
                AppElevatedButton.outlined(
                  onPressed: () => context.router.push(
                    VoidRoute(order: state.selectedOrder!),
                  ),
                  label: 'Void',
                  icon: Icon(Icons.undo),
                ),
                SpaceWidth(8),
                AppElevatedButton.outlined(
                  onPressed: () {
                    context.router.push(
                      SplitBillRoute(order: state.selectedOrder!),
                    );
                  },
                  label: 'Split Bill',
                  icon: Icon(Icons.calculate_outlined),
                ),

                SpaceWidth(8),
                if (state.selectedOrder?.paymentStatus != 'partial')
                  AppElevatedButton.filled(
                    width: 120,
                    onPressed: () {
                      context.router.push(
                        PaymentRoute(order: state.selectedOrder!),
                      );
                    },
                    label: 'Bayar',
                    icon: Icon(Icons.payment, color: Colors.white),
                  ),
              ],
            ],
          ),
        ),
      ],
    );
  }
}
