import 'package:cpt_form/modules/apply/vm/apply_view_model.dart';
import 'package:cs_resources/generated/l10n.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:domain/entity/form_content_entity.dart';
import 'package:domain/entity/form_list_entity.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_basic/widget/webview_page.dart';
import 'package:router/ext/auto_router_extensions.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/my_appbar.dart';
import 'package:widgets/my_button.dart';
import 'package:widgets/widget_export.dart';
import '../../router/page/form_page_router.dart';
import '../form/form_types.dart';

//协议页面，也是详情页面的第一个页面入口，内部可以请求详情数据和选项数据
@RoutePage()
class FormTermsPage extends HookConsumerWidget {
  final String type;
  final String estateFormId;
  final bool enableEdit;
  final FormListEntity? formItem;
  final FormContentEntity? formContent;
  final String? description;

  const FormTermsPage({
    Key? key,
    required this.type,
    required this.estateFormId,
    required this.enableEdit,
    this.formItem,
    this.formContent,
    this.description,
  }) : super(key: key);

  //启动当前页面
  static void startInstance({
    BuildContext? context,
    required String formType,
    required String estateFormId,
    required bool enableEdit,
    FormListEntity? formItem,
    FormContentEntity? formContent,
    String? description,
  }) {
    if (context != null) {
      context.router.push(FormTermsPageRoute(
        type: formType,
        formItem: formItem,
        enableEdit: enableEdit,
        formContent: formContent,
        estateFormId: estateFormId,
        description: description,
      ));
    } else {
      appRouter.push(FormTermsPageRoute(
        type: formType,
        formItem: formItem,
        enableEdit: enableEdit,
        formContent: formContent,
        estateFormId: estateFormId,
        description: description,
      ));
    }
  }

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

    useEffect(() {
      // 组件挂载时执行 - 保存参数
      Future.microtask(() => viewModel.setFormType(type, estateFormId, formItem, enableEdit, formContent));
      return () {
        // 组件卸载时执行
      };
    }, []);

    return Scaffold(
        appBar: MyAppBar.appBar(context, FormTypes.iconMap[type]?['name']),
        backgroundColor: context.appColors.backgroundWhite,
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SingleChildScrollView(
              scrollDirection: Axis.vertical,
              physics: const BouncingScrollPhysics(),
              child: Html(data: description ?? FormTypes.iconMap[type]?['name']),
            ).expanded(),

            //底部按钮
            MyButton(
              onPressed: () {
                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,
            ),
          ],
        ));
  }
}
