import 'package:cpt_services/components/chooseAirConditionContent.dart';
import 'package:cpt_services/components/chooseAirConditionContent_vm.dart';
import 'package:cs_resources/generated/assets.dart';
import 'package:cs_resources/generated/l10n.dart';
import 'package:domain/entity/garage_sale_rent_entity.dart';
import 'package:domain/entity/service_order_detail_entity.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:plugin_basic/provider/app_config/app_config_service.dart';
import 'package:router/componentRouter/component_service_manager.dart';
import 'package:router/ext/auto_router_extensions.dart';
import 'package:shared/utils/color_utils.dart';
import 'package:shared/utils/ext_dart.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/screen_util.dart';
import 'package:widgets/load_state_layout.dart';
import 'package:widgets/my_button.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/my_appbar.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:widgets/utils/dark_theme_util.dart';
import 'package:widgets/widget_export.dart';

import '../../../components/chooseHouseCleanContent.dart';
import '../../../components/chooseVisitTimeContent_vm.dart';
import '../../../constants_services.dart';
import '../../../router/page/services_page_router.dart';
import 'clean_order_detail_state.dart';
import 'clean_order_detail_vm.dart';
import '../../../components/status_card_item.dart';

@RoutePage()
class CleanOrderDetailPage extends HookConsumerWidget {
  final int id; // 订单id
  final int? cleanServiceTypeId; // 订单类型

  const CleanOrderDetailPage({
    Key? key,
    @PathParam('id') required this.id,
    @PathParam('cleanServiceTypeId') this.cleanServiceTypeId,
  }) : super(key: key);

  //启动当前页面
  static void startInstance({BuildContext? context, required int id, int? cleanServiceTypeId }) {
    if (context != null) {
      context.router.push(CleanOrderDetailPageRoute(id: id, cleanServiceTypeId: cleanServiceTypeId,));
    } else {
      appRouter.push(CleanOrderDetailPageRoute(id: id, cleanServiceTypeId: cleanServiceTypeId,));
    }
  }



  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final vm = ref.read(cleanOrderDetailVmProvider.notifier);
    final state = ref.watch(cleanOrderDetailVmProvider);
    // final appConfigState = ref.watch(appConfigServiceProvider)

    // List<Map<String, dynamic>> actionBtnList = servicesConstants.servicesStatusActionBtnList[serviesStatusCode]!;
    // Log.d("actionBtnList   $actionBtnList");

    useEffect(() {
      vm.setInitPageData(id: id, cleanServiceTypeId: cleanServiceTypeId,);
      // 组件挂载时执行 - 执行接口请求
      Future.microtask(() => vm.fetchOrderDetailData(id));
      return () {
        // 组件卸载时执行
      };
    }, []);

    return Scaffold(
      appBar: MyAppBar.appBar(
        context,
        S.current.order_details,
        backgroundColor: context.appColors.whiteBG,
      ),
      backgroundColor: DarkThemeUtil.multiColors(context, ColorUtils.string2Color('#F2F3F6'), darkColor: Colors.black)?? Colors.black,
      body: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: EasyRefresh(
            key: ValueKey('CleanOrderDetail'),
            controller: vm.refreshController,
            // 上拉加载
            onLoad: null,
            // 下拉刷新
            onRefresh: null,
            child: LoadStateLayout(
              state: state.loadingState,
              errorMessage: state.errorMessage,
              errorRetry: () {
                vm.retryRequest(id);
              },
              successWidget: Column(
                children: [
                  Expanded(
                    child: Padding(
                      padding: const EdgeInsets.only(bottom: 10),
                      child: _buildBody(state, vm, context, ref),
                    ),
                  ),
                  // 底部联系信息
                  Visibility(
                    visible: state.loadingState == LoadState.State_Success,
                    child:
                    (state.datas != null && state.actionBtnList !=null && state.actionBtnList!.isNotEmpty) ? _buildActionSection(context, state, vm, ref, id, state.actionBtnList??[]) : Container(),
                  )
                ],
              ),
            ),
          ).marginOnly(left: 0, right: 0, top: 0, bottom: 0)),
    );
  }

  Widget _buildBody(CleanOrderDetailState state, CleanOrderDetailVm vm, BuildContext context, WidgetRef ref) {
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 评分
          Container(
            color: context.appColors.whiteBG,
            width: double.infinity,
            margin: const EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 0),
            padding: const EdgeInsets.only(left: 10, right: 10, top: 18.5, bottom: 18.5),
            child: _buildOrderScore(state, vm, context),
          ),
          // profile信息
          Container(
            color: context.appColors.whiteBG,
            width: double.infinity,
            margin: const EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 0),
            padding: const EdgeInsets.only(left: 10, right: 10, top: 18.5, bottom: 18.5),
            child: _buildOrderProfile(state, vm, context),
          ),
          // 订单 服务项目
          Container(
            color: context.appColors.whiteBG,
            width: double.infinity,
            margin: const EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 0),
            padding: const EdgeInsets.only(left: 10, right: 10, top: 18.5, bottom: 18.5),
            child: _buildOrderService(state, vm, context),
          ),
          // total amount
          Container(
            color: context.appColors.whiteBG,
            width: double.infinity,
            margin: const EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 0),
            padding: const EdgeInsets.only(left: 10, right: 10, top: 18.5, bottom: 18.5),
            child: _buildOrderTotalAmount(state, vm, context),
          ),
          // 订单 上门时间
          Container(
            color: context.appColors.whiteBG,
            width: double.infinity,
            margin: const EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 0),
            padding: const EdgeInsets.only(left: 10, right: 10, top: 18.5, bottom: 18.5),
            child: _buildOrderVisitTime(state, vm, context),
          ),
          // 订单 card 信息
          // Container(
          //   color: context.appColors.whiteBG,
          //   width: double.infinity,
          //   margin: const EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 0),
          //   padding: const EdgeInsets.only(left: 10, right: 10, top: 18.5, bottom: 18.5),
          //   child: _buildOrderPayCardInfo(state, vm, context),
          // ),

          // 订单备注
          Container(
            color: context.appColors.whiteBG,
            width: double.infinity,
            margin: const EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 0),
            padding: const EdgeInsets.only(left: 10, right: 10, top: 18.5, bottom: 18.5),
            child: _buildOrderRemark(state, vm, context, ref),
          ),
          // 订单 rules
          // Container(
          //   color: context.appColors.whiteBG,
          //   width: double.infinity,
          //   margin: const EdgeInsets.only(left: 0, right: 0, top: 5, bottom: 0),
          //   padding: const EdgeInsets.only(left: 10, right: 10, top: 18.5, bottom: 18.5),
          //   child: _buildOrderRules(state, vm, context),
          // ),
        ],
      ),
    );
  }

  Widget _buildOrderScore(CleanOrderDetailState state, CleanOrderDetailVm vm, BuildContext context) {
    //  评分
    return Column(
      children: [
        Container(
          width: double.infinity,
          color: context.appColors.whiteBG,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              MyTextView(
                state.datas?.orderService?.name ?? "-",
                textColor: context.appColors.textBlack,
                fontSize: 18,
                isFontBold: true,
                marginBottom: 5,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  AnimatedRatingStars(
                    initialRating: (state.datas?.orderService?.evaluationsAvgScore ?? 0.0).toDouble(),
                    onChanged: (rating) {},
                    readOnly: true,
                    displayRatingValue: true,
                    // Display the rating value
                    interactiveTooltips: true,
                    // Allow toggling half-star state
                    customFilledIcon: Icons.star,
                    customHalfFilledIcon: Icons.star_half,
                    customEmptyIcon: Icons.star_border,
                    filledColor: ColorUtils.string2Color("#FF0000"),
                    starSize: 16.5,
                    animationDuration: const Duration(milliseconds: 0),
                    animationCurve: Curves.easeInOut,
                  ),
                  MyTextView(
                    "${state.datas?.orderService?.evaluationsAvgScore ?? 0.0}",
                    textColor: context.appColors.textBlack,
                    fontSize: 16,
                    isFontMedium: true,
                    marginLeft: 15,
                  ),
                ],
              ),
              MyTextView(
                state.datas?.merchant?.name ?? "-",
                textColor: context.appColors.textDarkGray999,
                fontSize: 12,
                isFontRegular: true,
                marginTop: 10,
                marginBottom: 5,
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildOrderProfile(CleanOrderDetailState state, CleanOrderDetailVm vm, BuildContext context) {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const MyAssetImage(
              Assets.serviceLocationIcon,
              width: 15,
              height: 17.5,
            ),
            const SizedBox(
              width: 10,
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                MyTextView(
                  state.datas?.estateUnit?.address ?? "-",
                  fontSize: 17,
                  isFontBold: true,
                  textColor: context.appColors.textBlack,
                  marginBottom: 5,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    MyTextView(
                      state.datas?.account?.name ?? "-",
                      fontSize: 14,
                      textColor: context.appColors.textDarkGray999,
                      isFontRegular: true,
                    ),
                    MyTextView(
                      "+${state.datas?.account?.phone ?? "-"}",
                      fontSize: 14,
                      textColor: context.appColors.textDarkGray999,
                      isFontRegular: true,
                      marginLeft: 10,
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildOrderService(CleanOrderDetailState state, CleanOrderDetailVm vm, BuildContext context) {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const MyAssetImage(
              Assets.serviceServiceAvatorIcon,
              width: 15,
              height: 17.5,
            ),
            const SizedBox(
              width: 10,
            ),
            MyTextView(
              S.current.service,
              fontSize: 17,
              isFontBold: true,
              textColor: context.appColors.textBlack,
              marginBottom: 5,
            ),
          ],
        ),
        
        // if (cleanServiceTypeId == servicesConstants.paidServicesType['houseCleaning']!['id'])
        //   // 室内清理
        //   _buildCleanContent(context, state)
        // else if (cleanServiceTypeId == servicesConstants.paidServicesType['airConditioner']!['id'])
        //   // 空调清理
        //   _buildCleanContent(context, state)
        // else
        //   const SizedBox.shrink()

        // 根据 orderProducts 数组生成对应的 Widgets
        if (state.datas?.orderProducts != null && state.datas!.orderProducts!.isNotEmpty)
          ...state.datas!.orderProducts!.map((product) {
            return _buildCleanContent(product, context);
          }).toList()
        else
          const SizedBox.shrink(), //空
        
      ],
    );
  }

  Widget _buildCleanContent(ServiceOrderDetailOrderProducts item ,BuildContext context) {
    // final title = 'House Cleaning Services';
    // final areaSizeRange = '1 Bedroom';
    // final num = 1;
    // final price = 0;
    // bool isChecked = false;
    // bool disabled = true;
    // final isDisable = useState<bool>(disabled);

    return Container(
      padding: const EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
      margin: const EdgeInsets.only(left: 15, right: 15, top: 0, bottom: 0),
      color: DarkThemeUtil.multiColors(context, ColorUtils.string2Color('#F8F8F8'), darkColor: Colors.grey.withOpacity(0.1)),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    MyTextView(
                      item.productName?? '-',
                      fontSize: 16,
                      isFontMedium: true,
                      textColor: context.appColors.textBlack,
                    ),
                    MyTextView(
                      "x${item.quantity}",
                      fontSize: 15,
                      isFontRegular: true,
                      textColor: context.appColors.textDarkGray999,
                      marginTop: 5,
                    ),
                  ],
                ),
              ),
              // MyCartNum(onChange: (value){
              //   vm.handlerChangeNum(context, value, index);
              // })
              // MyButton(
              //   text: '\$$price',
              //   onPressed: (){
              //       // vm.handlerChangeNum(context, value, index);
              //   },
              //   fontSize: 19,
              //   fontWeight: FontWeight.w500,
              // )
              Container(
                child: MyButton(
                  text: '${servicesConstants.dollarUnit}${item.totalAmount??""}',
                  onPressed: null,
                  minWidth: 80,
                  minHeight: 40,
                  fontSize: 19,
                  fontWeight: FontWeight.w500,
                  // enable: !isDisable.value,
                  textColor: context.appColors.textPrimary,
                  backgroundColor: context.appColors.textWhite,
                  disabledBackgroundColor: context.appColors.disEnableGray,
                  disabledTextColor: context.appColors.textWhite,
                ),
              ),
            ],
          ),
          // Divider(),
        ],
      ),
    );
  }

  Widget _buildOrderTotalAmount(CleanOrderDetailState state, CleanOrderDetailVm vm, BuildContext context) {
    return Column(children: [
      Row(mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [
        const MyAssetImage(
          Assets.serviceMoneyIcon,
          width: 15,
          height: 17.5,
        ),
        const SizedBox(
          width: 10,
        ),
        MyTextView(
         S.current.total_amount,
          fontSize: 17,
          isFontBold: true,
          textColor: context.appColors.textBlack,
        ),
        MyTextView(
          '${servicesConstants.dollarUnit}${state.datas?.totalAmount ?? 0}',
          fontSize: 15,
          textColor: context.appColors.textBlack,
          isFontBold: true,
          textAlign: TextAlign.end,
          marginRight: 17,
        ).expanded(),
      ])
    ]);
  }

  Widget _buildOrderVisitTime(CleanOrderDetailState state, CleanOrderDetailVm vm, BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const MyAssetImage(
              Assets.serviceTimeIcon,
              width: 16,
              height: 16,
            ),
            const SizedBox(
              width: 10,
            ),
            MyTextView(
             S.current.visit_time,
              fontSize: 17,
              isFontBold: true,
              textColor: context.appColors.textBlack,
            ),
          ],
        ),
        Expanded(
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              MyTextView(
                state.datas?.orderProducts?[0].plannedServiceAt ?? '',
                fontSize: 15,
                textColor: context.appColors.textDarkGray999,
                isFontRegular: true,
                textAlign: TextAlign.end,
                marginRight: 17,
              ),
              // // 右箭头 icon
              // const MyAssetImage(
              //   Assets.serviceRightIcon,
              //   width: 17.5,
              //   height: 17.5,
              // ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildOrderPayCardInfo(CleanOrderDetailState state, CleanOrderDetailVm vm, BuildContext context) {
    return Container(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          MyTextView(
            S.current.card_caps,
            fontSize: 17,
            isFontMedium: true,
            textColor: context.appColors.textPrimary,
            marginBottom: 15,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Wrap(
                alignment: WrapAlignment.start,
                crossAxisAlignment: WrapCrossAlignment.center,
                children: [
                  const MyAssetImage(
                    Assets.serviceCardIcon,
                    width: 63.5,
                    height: 38.5,
                  ),
                  MyTextView(
                    'Ending 9423',
                    fontSize: 15,
                    isFontMedium: true,
                    textColor: context.appColors.textBlack,
                    marginLeft: 27.5,
                  ),
                ],
              ),
              MyButton(
                onPressed: () {
                  ComponentServiceManager().paymentService.startChooseCardPage();
                },
                text: S.current.change,
                minWidth: 80,
                minHeight: 30,
                textColor: context.appColors.textWhite,
                fontSize: 15,
                fontWeight: FontWeight.w400,
                backgroundColor: context.appColors.textPrimary,
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildOrderRemark(CleanOrderDetailState state, CleanOrderDetailVm vm, BuildContext context, WidgetRef ref) {
    //签名的说明文本
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            // const MyAssetImage(
            //   Assets.assetsYyHomeLogo,
            //   width: 15,
            //   height: 17.5,
            // ),
            // const SizedBox(
            //   width: 10,
            // ),
            MyTextView(
              S.current.remarks_information,
              fontSize: 17,
              isFontBold: true,
              textColor: context.appColors.textBlack,
              marginBottom: 15,
            ),
          ],
        ),
        Container(
          margin: const EdgeInsets.only(left: 10, right: 10, top: 0),
          width: double.infinity,
          padding: const EdgeInsets.all(15),
          decoration: BoxDecoration(
            color: DarkThemeUtil.multiColors(context, ColorUtils.string2Color("#F8F8F8"), darkColor: Colors.grey.withOpacity(0.1)),
            borderRadius: BorderRadius.circular(5),
            // boxShadow: [
            //   BoxShadow(
            //     color: Colors.grey.withOpacity(0.2),
            //     spreadRadius:1,
            //     blurRadius: 2,
            //     offset: const Offset(0.5, 0.5), // changes position of shadow
            //   ),
            // ]
          ),
          child: MyTextView(
            state.datas?.notes ?? "",
            fontSize: 16,
            isFontRegular: true,
            textColor: context.appColors.textBlack,
            marginBottom: 15,
          ),
        ),
      ],
    );
  }

  Widget _buildOrderRules(CleanOrderDetailState state, CleanOrderDetailVm vm, BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            MyTextView(
              S.current.service_rules,
              fontSize: 17,
              isFontBold: true,
              textColor: context.appColors.textBlack,
              marginBottom: 10,
            ),
          ],
        ),
        Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            MyTextView(
              'Dear user, in order to ensure the time efficiency of service personnel, if an order is cancelled within 60 minutes before the start of the service, a certain percentage of your order amount may be deducted. If a coupon is used for the order, it will be refunded to your account after cancellation. Please forgive me!If cancelled more than 60 minutes before the service, 100% of the payment amount will be refundedCancel within 0-60 minutes before the start of the service and the service personnel have accepted the order:If it is due to your personal reasons, the platform will deduct 20% of the payment amount (up to a maximum of 30 yuan), and the remaining amount will be refunded through the original payment methodIf it is due to the store\'s reasons, the platform will complete the verification within 24 hours. After verification is passed, the full amount will be refunded to the original route within 1-3 working daysAfter the service starts, if you have any disputes with the service personnel due to service quality issues, please contact customer service for resolution. Contact phone number: +65 66889955',
              fontSize: 14,
              textColor: context.appColors.textBlack,
              isFontRegular: true,
            ),
          ],
        )
      ],
    );
  }

  // 底部操作区域
  // Widget _buildActionSection(
  //     BuildContext context, state, vm, WidgetRef ref, int id,List<Map<String, dynamic>> actionBtnList) {
  //   return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
  //     final maxHeight = constraints.maxHeight;
  //     final minHeight = constraints.minHeight;
  //     final maxWidth = constraints.maxWidth;
  //     // Log.d("---maxHeight-----$maxHeight-- $minHeight  $maxWidth--");
  //     return Container(
  //       // height: 71.5,
  //       color: context.appColors.whiteBG,
  //       width: maxWidth,
  //       padding: const EdgeInsets.only(left: 10, right: 10),
  //       child: Wrap(
  //           // mainAxisAlignment: MainAxisAlignment.center,
  //           // mainAxisSize: MainAxisSize.max,
  //         alignment: WrapAlignment.center,
  //           spacing: 8.0, // 子项之间的水平间距
  //           runSpacing: 4.0, // 行与行之间的垂直间距
  //           children: List.generate(actionBtnList.length, (index) {
  //             var item = actionBtnList[index];
  //             return Expanded(
  //               child: _buildBtn(context, item, vm, ref),
  //             );
  //             // return _buildBtn(context, item, vm, ref);
  //           })),
  //     ).scrollable(
  //       scrollDirection: Axis.horizontal,
  //       physics: const BouncingScrollPhysics(),
  //     );
  //   });
  // }

  Widget _buildActionSection(
      BuildContext context, state, vm, WidgetRef ref, int id,List<Map<String, dynamic>> actionBtnList) {
    final state = ref.watch(cleanOrderDetailVmProvider);
    int? orderStatus = state.datas?.orderStatus;
    String? cancelStatus = state.datas?.cancelStatus;

    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
      final maxHeight = constraints.maxHeight;
      final minHeight = constraints.minHeight;
      final maxWidth = constraints.maxWidth;

      // Log.d("---maxHeight-----$maxHeight-- $minHeight  $maxWidth--");
      if (orderStatus!.toString() == '0') {
        // 未支付场景
        return Container(
          color: context.appColors.whiteBG,
          width: maxWidth,
          padding: const EdgeInsets.only(left: 10, right: 10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Row(
                children: actionBtnList
                    .asMap()
                    .map((index, item) {
                  final isLast = index == actionBtnList.length - 1;
                  if (isLast) return MapEntry(index, const SizedBox.shrink());
                  return MapEntry(
                    index,
                    Expanded(
                      child: _waitPaidBuildBtn(context, item, vm, ref, index),
                    ),
                  );
                }).values.toList(),
              ),
              if (actionBtnList.isNotEmpty && actionBtnList.last != null)
                Padding(
                  padding: const EdgeInsets.only(top: 8.0),
                  child: _waitPaidBuildBtn(
                    context,
                    actionBtnList.last,
                    vm,
                    ref,
                    actionBtnList.length - 1,
                  ),
                ),
            ],
          ),
        );
      }
      else {
        return Container(
          // height: 71.5,
          color: context.appColors.whiteBG,
          width: maxWidth,
          padding: const EdgeInsets.only(left: 10, right: 10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
              children: List.generate(actionBtnList.length, (index) {
                var item = actionBtnList[index];
                return Expanded(
                  child: _buildBtn(context, item, vm, ref,index),
                );
                // return _buildBtn(context, item, vm, ref);
              })),
        ).scrollable(
          scrollDirection: Axis.horizontal,
          physics: const BouncingScrollPhysics(),
        );
      }
    });
  }

  Widget _waitPaidBuildBtn(BuildContext context, Map<String, dynamic> btnItem, vm, WidgetRef ref, int btnIdx) {
    return MyButton(
      onPressed: () {
        vm.handlerClickActionBtn(context, btnItem);
      },
      text: btnItem['text'],
      radius: 10.0,
      textColor: context.appColors.textWhite,
      backgroundColor: ColorUtils.string2Color(btnItem['btnColor']),
      fontSize: 12,
      padding: const EdgeInsets.symmetric(horizontal: 5),
      minWidth: btnItem['btnWidth'].toDouble(),
      minHeight: btnItem['btnHeight']?.toDouble() + 5,
    ).marginOnly(left: 5);
  }


  Widget _buildBtn(BuildContext context, Map<String, dynamic> btnItem, vm, WidgetRef ref, int btnIdx) {
    // final btnCode = btnItem['code'];
    final state = ref.watch(cleanOrderDetailVmProvider);
    Log.d("8323  cancelStatus:${state.datas?.cancelStatus}  orderStatus:${state.datas?.orderStatus}");
    int? orderStatus = state.datas?.orderStatus;
    String? cancelStatus = state.datas?.cancelStatus;
    return MyButton(
      onPressed: () {
        vm.handlerClickActionBtn(context, btnItem);
      },
      text: btnItem['text'],
      radius: 10.0,
      textColor: context.appColors.textWhite,
      backgroundColor: ColorUtils.string2Color(btnItem['btnColor']),
      fontSize: 12,
      padding: const EdgeInsets.symmetric(horizontal: 5),
      minWidth: getBtnActionWidth(btnItem, orderStatus!, cancelStatus!, btnIdx),
      minHeight: btnItem['btnHeight']?.toDouble() + 5,
    ).marginOnly(left: 5);
  }

  double getBtnActionWidth(Map<String, dynamic> btnItem, int orderStatus, String cancelStatus, int btnIdx) {
    if (btnItem['btnWidth'] != null) {
      return btnItem['btnWidth']?.toDouble() + 5;
    }
    return 0;
  }
}
