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/image/image_nine_grid.dart';
import 'package:plugin_platform/engine/toast/toast_engine.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/my_text_view.dart';

import '../../router/page/form_page_router.dart';
import 'vm/apply_view_model.dart';

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

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

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

    useEffect(() {
      //赋值State的值
      Future.microtask(() {});
      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.attachments,
                          fontSize: 17,
                          marginTop: 30,
                          marginBottom: 13,
                          isFontMedium: true,
                          textColor: context.appColors.textBlack,
                        ),

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

                        //选择图片九宫格
                        ImageNineGrid(
                          isSelectEnable: state.enableEdit,
                          maxImages: 10,
                          spacing: 10,
                          aspectRatio: 108 / 80,
                          initialImages: state.formContentDetail.attachments ?? [],
                          onImagesChanged: (list) {
                            viewModel.updateFormContentDetail((content) {
                              content.attachments = list;
                            });
                          },
                        ),
                      ],
                    ),
                  ),
                ).expanded(),

                //底部按钮
                MyButton(
                  onPressed: () {

                    if (state.enableEdit){
                      final attachments = state.formContentDetail.attachments;

                      if (attachments == null || attachments.isEmpty) {
                        ToastEngine.show("Select attachments");
                        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;
        });
  }
}
