import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:mc_common_app/classes/app_state.dart'; import 'package:mc_common_app/extensions/int_extensions.dart'; import 'package:mc_common_app/extensions/string_extensions.dart'; import 'package:mc_common_app/models/general_models/m_response.dart'; import 'package:mc_common_app/models/subscriptions_models/branch_user_selection_model.dart'; import 'package:mc_common_app/models/subscriptions_models/subscription_model.dart'; import 'package:mc_common_app/theme/colors.dart'; import 'package:mc_common_app/utils/enums.dart'; import 'package:mc_common_app/utils/navigator.dart'; import 'package:mc_common_app/utils/utils.dart'; import 'package:mc_common_app/view_models/payment_view_model.dart'; import 'package:mc_common_app/view_models/subscriptions_view_model.dart'; import 'package:mc_common_app/views/payments/payment_methods_view.dart'; import 'package:mc_common_app/widgets/button/show_fill_button.dart'; import 'package:mc_common_app/widgets/extensions/extensions_widget.dart'; import 'package:provider/provider.dart'; class DowngradeDataSheet extends StatefulWidget { Subscription subscription; DowngradeDataSheet(this.subscription, {super.key}); @override State createState() => _DowngradeDataSheetState(); } class _DowngradeDataSheetState extends State { @override void initState() { super.initState(); context.read().getSPBranchUser_Get(); } @override Widget build(BuildContext context) { return SizedBox( width: double.infinity, height: MediaQuery.of(context).size.height / 1.2, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.all(12), child: Column( children: [ "Deactivate the branches & users".toText( fontSize: 19, isBold: true, ), 8.height, "To select this subscription you need to deactivate your branches and users as this package only supports 1 branch and 2 users per branch. You can reactivate these branches later after upgrading the package" .toText( fontSize: 14, color: MyColors.textColor, ), ], ), ), Expanded( child: Consumer( builder: (BuildContext context, model, Widget? child) { return Column( children: [ Expanded( child: model.branchSelectionList == null ? const Center(child: CircularProgressIndicator()) : ListView.separated( itemBuilder: (context, index) { return showBranchItem( model, model.branchSelectionList![index]); }, separatorBuilder: (context, index) { return 12.height; }, padding: const EdgeInsets.all(12), itemCount: model.branchSelectionList?.length ?? 0, ), ), ShowFillButton( title: "Next", maxWidth: double.infinity, margin: const EdgeInsets.all(12), onPressed: () async { List listOfBranches = []; List listOfUsers = []; for (var element in model.branchSelectionList!) { if (element.isSelected) { listOfBranches.add(element.branchId); } else { for (var element in element.usersList) { if (element.isSelected) { listOfUsers.add(element.userId); } } } } print("opoewww"); print(jsonEncode(listOfBranches)); if (listOfBranches.isNotEmpty || listOfUsers.isNotEmpty) { navigateTo( context, PaymentMethodsView( paymentType: PaymentTypes.subscription, onTempContinue: () async { Utils.showLoading(context); MResponse response = await model.payForSubscription( widget.subscription.id!, true, false, widget.subscription.price.toString(), isDegrade: true, listOfBranches: listOfBranches, listOfUsers: listOfUsers, ); Utils.hideLoading(context); if (response.messageStatus == 1) { if(response.message!.contains("Your current sub-users are not")){ Utils.showToast(response.message.toString()); context.read().getMySubscriptions( AppState() .getUser .data ?.userInfo ?.providerId .toString() ?? ""); pop(context); return false; }else{ context .read() .updateOrderProviderSubscriptionId( id: response.data); // Utils.showToast( // "Subscriptions is successfully updated"); return true; } } else { Utils.showToast(response.message.toString()); context.read().getMySubscriptions( AppState() .getUser .data ?.userInfo ?.providerId .toString() ?? ""); return false; } }, ), ); } }, ), ], ); }, ), ), ], ), ); } Widget showBranchItem(SubscriptionsVM viewModel, BranchSelectionModel model) { return Row( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ customCheckBox(model.isSelected).onPress(() { model.isSelected = !model.isSelected; if (model.isSelected) { for (var element in model.usersList) { element.isSelected = true; } } else { for (var element in model.usersList) { element.isSelected = false; } } model.isOpend = true; viewModel.setState(ViewState.idle); }), 12.width, Expanded( child: Column( mainAxisSize: MainAxisSize.min, children: [ SizedBox( width: double.infinity, child: Row( children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ model.branchName.toText( fontSize: 18, isBold: true, ), ("${model.usersList.length} Users").toText( fontSize: 10, color: MyColors.lightTextColor, ) ], ), ), Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon( model.isOpend ? Icons.keyboard_arrow_up_rounded : Icons.keyboard_arrow_down_rounded, ), ], ), ], ), ), if (model.isOpend) 12.height, if (model.isOpend) ListView.separated( itemBuilder: (context, index) { return Row( children: [ customCheckBox(model.usersList[index].isSelected), 8.width, model.usersList[index].userName.toText( fontSize: 14, ), ], ).onPress(() { // for (int i = 0; i < model.usersList.length; i++) { // if (i != index) { // model.usersList[index].isSelected = false; // } // } model.usersList[index].isSelected = !model.usersList[index].isSelected; viewModel.setState(ViewState.idle); }); }, separatorBuilder: (context, index) { return 12.height; }, physics: const NeverScrollableScrollPhysics(), shrinkWrap: true, itemCount: model.usersList.length, ), ], ).onPress(() { model.isOpend = !model.isOpend; viewModel.setState(ViewState.idle); }), ) ], ).toWhiteContainer( width: double.infinity, pading: const EdgeInsets.all(12), ); } Widget customCheckBox(bool isChecked) { return SizedBox( height: 18, width: 18, child: isChecked ? const Icon( Icons.done, color: Colors.white, size: 14, ) : const SizedBox(), ).toContainer( isEnabledBorder: true, paddingAll: 0, borderRadius: 0, borderWidget: 1, borderColor: MyColors.primaryColor, backgroundColor: isChecked ? MyColors.primaryColor : Colors.transparent, ); } }