/*
 * @Author: XianKaiQun
 * @Date: 2020-07-07 11:10:46
 * @LastEditors: wuwei
 * @LastEditTime: 2024-04-02 15:49:59
 * @Description: 
 */
import 'package:app_package/app_package.dart';
import 'package:app_package/src/assets.gen.dart';
import 'package:app_package/src/view_models/message_store.dart';
import 'package:flutter/material.dart';
// import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
part 'components/swiper.dart';
part 'components/grid.dart';

class MessagePage extends StatelessWidget {
  const MessagePage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (providerContext) =>
          PrivateMessageStore(context, providerContext),
      child: _PageView(),
      lazy: false,
    );
  }
}

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);
  @override
  _PageViewState createState() => _PageViewState();
}

class _PageViewState extends State<_PageView> {
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateMessageStore>(context, listen: true);
    final appGlobalStore = GetIt.I.get<AppGlobalStore>();
    final colorScheme = WTheme.of(context).colorScheme;
    // final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        backgroundColor: Color(0xffDDEEFE),
        toolbarHeight: 0,
        automaticallyImplyLeading: false,
      ),
      body: Stack(children: [
        Image.asset(
          'lib/assets/main/bg.png',
          package: 'app_package',
          width: MediaQuery.of(context).size.width,
        ),
        Column(children: [
          Wisdom.row(
            margin: EdgeInsets.only(top: 15.pt, bottom: 3.5.pt),
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              WisText(
                '房管数据',
                size: 19.pt,
                weight: FontWeight.bold,
                color: colorScheme.mainTitle,
              ),
            ],
          ),
          if (appGlobalStore.isPageSet != true &&
              [460101, 460106].contains(store.entCode)) ...[
            WSearch(
              hintText: '输入功能名称搜索',
              onSubmitted: (value) => store.onNameInput(value),
              onCancel: () => store.onNameInput(''),
              isStatic: true,
              moonlight: false,
            ),
          ] else ...[
            SizedBox(height: 15.pt),
          ],
          WisScrollView(
            key: store.scrollViewKey,
            onInit: () => store.onInit(),
            onRefresh: () => store.onInit(),
            slivers: <Widget>[
              /// 去除个性化
              SliverToBoxAdapter(
                child: Wisdom.column(
                  // padding: EdgeInsets.only(
                  //     bottom: MediaQuery.of(context).viewInsets.bottom),
                  children: [
                    if (store.searchValue == null ||
                        store.searchValue == '') ...[
                      _BuildMenuBlock(
                          title: '常用信息', list: store.menuList, type: 'static'),
                      if (store.stateMenuAllList.length > 0) ...[
                        for (int i = 0; i < store.stateMenuAllList.length; i++)
                          _BuildMenuBlock(
                            title: store.stateMenuAllList[i]
                                    .intelligentFormGroupName ??
                                '',
                            list: store.stateMenuAllList[i].forms,
                            type: 'state',
                          ),
                      ]
                    ],
                    if (store.searchValue != null &&
                        store.searchValue != '' &&
                        store.stateMenuSearchList.length > 0) ...[
                      Wisdom.column(color: Colors.white, children: <Widget>[
                        Wisdom(
                          height: 15.pt,
                        ),
                        for (int i = 0;
                            i < store.stateMenuSearchList.length;
                            i++)
                          _BuildMenuItem(
                            item: store.stateMenuSearchList[i],
                          ),
                      ]),
                    ],
                    if (store.searchValue != null &&
                        store.searchValue != '' &&
                        store.stateMenuSearchList.length == 0) ...[
                      WEmptyWidget(
                        text: WisText('暂无数据'),
                      )
                    ],
                  ],
                ),
              ),

              SizedBox(height: 30.pt).asSliver(),
            ],
          ).asFlexible(flex: 1),
          Opacity(
            opacity: 1,
            child: Container(
              color: Colors.transparent,
              height: 50.pt,
            ),
          ),
        ]),
      ]),
    );
  }
}

class _BuildMenuBlock extends StatelessWidget {
  const _BuildMenuBlock({
    Key? key,
    required this.title,
    required this.list,
    required this.type,
  }) : super(key: key);
  final String? title;
  final List? list;
  final String? type;
  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return WisCard(
      margin: EdgeInsets.only(
        bottom: 15.pt,
        left: 15.pt,
        right: 15.pt,
      ),
      child: Wisdom.column(color: Colors.white, children: <Widget>[
        Wisdom.row(
            border: Border(
              bottom: BorderSide(
                color: Color(0xffE6E6E6),
              ),
            ),
            padding: EdgeInsets.symmetric(vertical: 15.pt),
            children: [
              Wisdom(
                height: 20.pt,
                width: 3.pt,
                color: colorScheme.primary,
                margin: EdgeInsets.only(
                  right: 12.pt,
                ),
              ),
              WisText(
                '$title',
                color: colorScheme.mainTitle,
                weight: FontWeight.w600,
                size: 17.pt,
              ),
            ]),
        if (list != null && list!.length > 0) ...[
          for (int i = 0; i < list!.length; i++)
            _BuildMenuItem(
              item: list![i],
              type: type,
              isLast: i == list!.length - 1 ? true : false,
            ),
        ] else ...[
          WisText('暂无数据', color: colorScheme.minor, size: 14.pt),
          SizedBox(height: 15.pt),
        ]
      ]),
    );
  }
}

class _BuildMenuItem extends StatelessWidget {
  const _BuildMenuItem({Key? key, this.item, this.type, this.isLast})
      : super(key: key);
  final item;
  final String? type;
  final bool? isLast;

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<PrivateMessageStore>(context, listen: false);
    return Wisdom.row(
      onTap: () => store.toRoutes(item, type),
      padding: EdgeInsets.only(
        left: 15.pt,
      ),
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            WImage.network(
              AppParseUtil.image(item.icon),
              height: 39.pt,
              width: 39.pt,
            ),
            Wisdom(width: 12.5.pt),
          ],
        ),
        Wisdom.row(
          padding: EdgeInsets.only(
            top: 22.pt,
            bottom: 22.pt,
          ),
          border: Border(
            bottom: BorderSide(
              color: isLast == true ? Colors.transparent : colorScheme.divider,
            ),
          ),
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            WisText(
              item?.name ?? '--',
              color: colorScheme.mainTitle,
              size: 16.pt,
              overflow: TextOverflow.ellipsis,
            ).asFlexible(flex: 1),
            Row(
              children: [
                Image(
                  image: AssetList.$tz_png_image,
                  width: 6.pt,
                  height: 12.pt,
                ),
                SizedBox(width: 15.pt),
              ],
            )
          ],
        ).asFlexible(flex: 1),
      ],
    );
  }
}
