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:flutter_hooks/flutter_hooks.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/util.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/my_appbar.dart';
import 'package:widgets/my_button.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/widget_export.dart';
import '../../router/page/form_page_router.dart';
import 'vm/apply_view_model.dart';

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

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

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

    // 使用 useState 来持久化 TextEditingController 和 FocusNode
    final textEditingController = useTextEditingController();
    final focusNode = useFocusNode();

    useEffect(() {
      //赋值State的值
      Future.microtask(() {
        textEditingController.text = state.formContentDetail.notesToManagement ?? "";
      });

      return () {};
    }, []);

    return WillPopScope(
        child: Scaffold(
            appBar: MyAppBar.appBar(context, state.detailPage?['title']),
            backgroundColor: context.appColors.backgroundWhite,
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  physics: const BouncingScrollPhysics(),
                  child: Container(
                    margin: const EdgeInsets.symmetric(horizontal: 15),
                    width: double.infinity,
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        MyTextView(
                          S.current.notes_to_management,
                          fontSize: 17,
                          marginTop: 30,
                          marginBottom: 13,
                          isFontMedium: true,
                          textColor: context.appColors.textBlack,
                        ),

                        //Note to Management 的文本
                        MyTextView(
                          state.detailPage?['detail_data']['note_management_txt'],
                          fontSize: 15,
                          marginBottom: 18,
                          isFontRegular: true,
                          textColor: context.appColors.textBlack,
                        ),

                        //文本框
                        IgnoreKeyboardDismiss(
                          child: Container(
                            height: 190,
                            padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 15),
                            decoration: BoxDecoration(
                              color: context.appColors.authFiledBG,
                              borderRadius: const BorderRadius.all(Radius.circular(5)),
                            ),
                            child: TextField(
                              cursorColor: context.appColors.authFiledText,
                              cursorWidth: 1.5,
                              autofocus: false,
                              enabled: state.enableEdit,
                              focusNode: focusNode,
                              controller: textEditingController,
                              decoration: InputDecoration(
                                isDense: true,
                                isCollapsed: true,
                                border: InputBorder.none,
                                hintText: S.current.type_here,
                                hintStyle: TextStyle(
                                  color: context.appColors.authFiledHint,
                                  fontSize: 15.0,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                              style: TextStyle(
                                color: context.appColors.authFiledText,
                                fontSize: 15.0,
                                fontWeight: FontWeight.w500,
                              ),
                              textInputAction: TextInputAction.done,
                              onSubmitted: (value) {
                                FocusScope.of(context).unfocus();
                              },
                              maxLines: null,
                              expands: true,
                              onChanged: (text) {
                                // 当文本改变时
                              },
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ).expanded(),

                //底部按钮
                MyButton(
                  onPressed: () {
                    focusNode.unfocus();

                    if (state.enableEdit) {
                      String? notesToManagement = textEditingController.text;

                      if (Utils.isEmpty(notesToManagement)) {
                        ToastEngine.show("Enter Notes To Management");
                        return;
                      }

                      viewModel.updateFormContentDetail((content) {
                        content.notesToManagement = notesToManagement;
                      });
                    }

                    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;
        });
  }
}
