
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:responsive_builder/responsive_builder.dart';

import '../constants.dart';
import '../generated/l10n.dart';
import '../store/actions.dart';
import '../store/store.dart';
import '../utils/http_util.dart';
import '../widgets/desktop/desktop_minipos_learn_more.dart';
import '../widgets/general/bottom_nav.dart';
import '../widgets/general/breadcrumbs.dart';
import '../widgets/general/navigationbar.dart';
import '../widgets/mobile/MobileBottomNav.dart';
import '../widgets/mobile/mobile_minipos_learn_more.dart';

class MiniPosLearnMore extends StatefulWidget {
  const MiniPosLearnMore({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return MiniPosLearnMoreState();
  }
}

class MiniPosLearnMoreState extends State<MiniPosLearnMore> {
  final _scaffoldKey = GlobalKey<ScaffoldState>();
  Map<String, dynamic>? data;

  @override
  Widget build(BuildContext context) {
    store.dispatch(UpdateContext(context));
    if (data == null) {
      return Scaffold(
        body: Center(
          child: SpinKitWave(
            color: Colors.lightBlueAccent,
            size: 40.0,
          ),
        ),
      );
    }

    return ResponsiveBuilder(
      builder: (context, sizingInformation) => PopScope(
        canPop: true,
        onPopInvokedWithResult: (didPop, result) {
          if (didPop) return;
          // 可以在这里处理返回逻辑
        },
        child: Scaffold(
          key: _scaffoldKey,
          appBar: MiniNavigationBar(
            title: S.of(context).minipos,
            back: true,
            breadCrumbs: [
              BreadCrumb(S.of(context).minipos, null),
            ],
            breadCrumbHeight: sizingInformation.deviceScreenType == DeviceScreenType.mobile ? null : Constants.BREADCRUMB_HEIGHT,
          ),
          drawer: null,
          body: ScreenTypeLayout.builder(
            mobile: (context) => MobileMiniPosLearnMore(data!),
            tablet: (context) => DesktopMiniPosLearnMore(data!),
            desktop: (context) => DesktopMiniPosLearnMore(data!),
          ),
          bottomNavigationBar: ScreenTypeLayout.builder(
            mobile: (context) => MobileBottomNav(currentIndex: 0),
            tablet: (context) => BottomNav(),
            desktop: (context) => BottomNav(),
          ),
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  void _loadData() {
    HttpUtil.httpGet('v1/get-minipos-learn-more-page')
        .then((value) {
      print('$value');
      if (mounted) {
        setState(() {
          data = value;
        });
      }
    });
  }
}