import 'package:cs_resources/generated/l10n.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:router/ext/auto_router_extensions.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/my_appbar.dart';
import 'package:widgets/my_button.dart';
import 'package:widgets/shatter/picker_container.dart';
import 'package:widgets/my_text_view.dart';
import '../../router/page/form_page_router.dart';
import 'vm/apply_view_model.dart';

@RoutePage()
class TypeOfApplicationPage extends HookConsumerWidget {
  const TypeOfApplicationPage({Key? key}) : super(key: key);

  //启动当前页面
  static void startInstance({BuildContext? context}) {
    if (context != null) {
      context.router.push(const TypeOfApplicationPageRoute());
    } else {
      appRouter.push(const TypeOfApplicationPageRoute());
    }
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final viewModel = ref.watch(applyViewModelProvider.notifier);
    final state = ref.watch(applyViewModelProvider);

    return WillPopScope(
        child: Scaffold(
            appBar: MyAppBar.appBar(context, state.detailPage?['title']),
            backgroundColor: context.appColors.backgroundWhite,
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                MyTextView(
                  S.current.type_of_application,
                  fontSize: 17,
                  marginTop: 30,
                  marginLeft: 15,
                  marginBottom: 16,
                  isFontMedium: true,
                  textColor: context.appColors.textBlack,
                ),

                PickerContainer(
                  hint: S.current.choose_an_option,
                  enable: state.enableEdit,
                  content: state.formContentDetail.typeOfApplication,
                  onClick: (context) {
                    viewModel.pickOption(true);
                  },
                ).marginOnly(left: 15, right: 15),

                const Spacer(),

                //底部按钮
                MyButton(
                  onPressed: (){
                    if (state.enableEdit){
                      //校验数据
                      final typeOfApplication = state.formContentDetail.typeOfApplication;

                      if (Utils.isEmpty(typeOfApplication)) {
                        ToastEngine.show("Select Type Of Application");
                        return;
                      }
                    }

                    viewModel.gotoNextPage();
                  },
                  text: S.current.next,
                  textColor: Colors.white,
                  backgroundColor: context.appColors.btnBgDefault,
                  fontWeight: FontWeight.w500,
                  type: ClickType.throttle,
                  fontSize: 16,
                  minHeight: 50,
                  radius: 0,
                ),
              ],
            )),
        onWillPop: () async {
          viewModel.handlePopAction();
          return true;
        });
  }
}
