/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-02-09 10:59:31
 * @LastEditTime : 2023-05-16 09:57:37
 * @Description  : 个性化菜单页
 */
import 'dart:io';

import 'package:app_package/src/api/root.dart';
import 'package:app_package/src/components/menu_grid.dart';
import 'package:app_package/src/components/menu_list.dart';
import 'package:app_package/src/models/app_page_setting_detail.dart';
import 'package:app_package/src/models/form_menu_id_detail.dart';
import 'package:flutter/material.dart';
import 'package:app_package/app_package.dart';
// import 'package:mobx/mobx.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
part './store.dart';

class DynamicMenu extends StatelessWidget {
  const DynamicMenu({Key? key, this.menuItem});
  final menuItem;
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => _Store(context),
      child: _PageView(menuItem: menuItem),
      lazy: false, //取消懒加载，懒加载可能会导致获取路由传参不准确
    );
  }
}

class _PageView extends StatelessWidget {
  const _PageView({Key? key, this.menuItem}) : super(key: key);
  final menuItem;
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<_Store>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        title: WisText(
          menuItem.name,
          size: 17.pt,
        ),
      ),
      body: WisScrollView(
        key: store.scrollViewKey,
        onInit: () => store.onInit(menuItem),
        onRefresh: () => store.onInit(menuItem),
        slivers: [
          if (store.detail!.groupData!.length > 0) ...[
            /// 九宫格样式
            if (store.detail!.displayMode == 'gridLayout') ...[
              for (var item in store.detail!.groupData!)
                DynamicMenuGrid(
                  title: item.name!,
                  list: item.menus!,
                  type: item.type!,
                  toRoutes: (item, type) => store.toRoutes(item, type),
                ),
            ] else ...[
              /// 列表样式
              for (var item in store.detail!.groupData!)
                SliverToBoxAdapter(
                  child: DynamicMenuList(
                    title: item.name!,
                    list: item.menus!,
                    type: item.type!,
                    toRoutes: (item, type) => store.toRoutes(item, type),
                  ),
                ),
            ],
          ] else ...[
            SliverToBoxAdapter(
              child: WEmptyWidget(
                text: WisText('空空如也~'),
              ),
            )
          ],
        ],
      ),
    );
  }
}
