/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-06-09 16:15:01
 * @Description  : Do not edit
 */
part of 'index.dart';

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<_Store>(context, listen: true);
    if (store.list.length == 0 && store.isGetList == false) {
      store.scorllViewKey.currentState?.refreshTrigger();
    }
    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('${store.args['appBarTitle'] ?? '消息提醒'}',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: Column(
        children: [
          Wisdom.row(
            padding: EdgeInsets.all(15.pt),
            color: Colors.white,
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              StaticsItem(
                onTap: () => store.toDetailList('BILL', '收费通知'),
                name: '收费通知',
                image: Image.asset(
                  'lib/assets/main/icon_sftz.png',
                  package: 'app_package',
                  width: 44.pt,
                ),
                count: store.billCount,
              ),
              StaticsItem(
                onTap: () => store.toDetailList('REPORT_AND_REPAIR', '报事报修'),
                name: '报事报修',
                image: Image.asset(
                  'lib/assets/main/icon_bsbxtz.png',
                  package: 'app_package',
                  width: 44.pt,
                ),
                count: store.repairCount,
              ),
              StaticsItem(
                onTap: () => store.toDetailList('TASK', '任务通知'),
                name: '任务通知',
                image: Image.asset(
                  'lib/assets/main/icon_rwtz.png',
                  package: 'app_package',
                  width: 44.pt,
                ),
                count: store.taskCount,
              ),
              StaticsItem(
                onTap: () => store.toDetailList('FACE_PASS', '人脸通行'),
                name: '人脸通行',
                image: Image.asset(
                  'lib/assets/main/icon_rltx.png',
                  package: 'app_package',
                  width: 44.pt,
                ),
                count: store.faceCount,
              ),
            ],
          ),
          WisScrollView(
            key: store.scorllViewKey,
            onInit: () => store.initPage(),
            onRefresh: () => store.initPage(),
            // onLoad: () => store.getList(isLoad: true),
            slivers: <Widget>[
              Wisdom.column(
                margin: EdgeInsets.symmetric(
                  vertical: 20.pt,
                ),
                // color:
                children: [
                  if (store.showUpdate == true) ...[
                    NoticeListItem(
                      onTap: () => UpdateAppStore.instance.checkUpdate(),
                      type: 'update',
                      title: '有新版本啦，点击更新',
                      content: store.data!.updateRecord ?? '',
                      time: '${store.data!.releaseDate ?? '刚刚'}',
                    ),
                  ],
                  if (store.list.length > 0) ...[
                    for (var item in store.list)
                      NoticeListItem(
                        onTap: () => store.toDetailDialog(item),
                        type: 'dialog',
                        title: '${item.title}',
                        content: '${item.msg}',
                        time: '${item.time}',
                        count: item.read == false ? item.count! : 0,
                      ),
                  ] else ...[
                    WEmptyWidget(
                      text: Text('暂无消息'),
                    ),
                  ],
                ],
              ).asSliver(),
            ],
          ).asFlexible(flex: 1),
        ],
      ),
    );
  }
}

class NoticeListItem extends StatelessWidget {
  const NoticeListItem({
    Key? key,
    required this.type,
    required this.title,
    required this.content,
    required this.time,
    this.count,
    this.onTap,
  }) : super(key: key);
  final String type;
  final String title;
  final String content;
  final String time;
  final num? count;
  final Function()? onTap;

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return WisCard(
        onTap: () => onTap!(),
        margin: EdgeInsets.fromLTRB(
          15.pt,
          0,
          15.pt,
          10.pt,
        ),
        child: Wisdom.row(
          crossAxisAlignment: CrossAxisAlignment.center,
          padding: EdgeInsets.all(15.pt),
          children: [
            if (type == 'update') ...[
              Image.asset(
                'lib/assets/main/icon_bbgx.png',
                package: 'app_package',
                width: 44.pt,
              ),
            ] else ...[
              Image.asset(
                'lib/assets/main/iocn_txzwt.png',
                package: 'app_package',
                width: 44.pt,
              ),
            ],
            Wisdom.column(
              margin: EdgeInsets.symmetric(horizontal: 15.pt),
              children: [
                Wisdom.row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    WisText(
                      '$title',
                      size: 16.pt,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ).asExpanded(),
                    WisText('$time', size: 13.pt, color: colorScheme.grey),
                  ],
                ),
                Wisdom.row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  margin: EdgeInsets.only(top: 5.pt),
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    WisText(
                      '$content',
                      size: 14.pt,
                      color: colorScheme.grey,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ).asExpanded(),
                    if (count != null && count! > 0) ...[
                      Wisdom(
                        border: Border.all(
                          color: Colors.white,
                        ),
                        padding: EdgeInsets.symmetric(
                            horizontal: 5.pt, vertical: 2.pt),
                        borderRadius:
                            BorderRadius.circular(count! > 99 ? 9.5.pt : 20.pt),
                        color: colorScheme.red,
                        child: WisText(
                          '${count! > 99 ? '99+' : count}',
                          size: 12.pt,
                          color: Colors.white,
                        ),
                      ),
                    ]
                  ],
                ),
              ],
            ).asFlexible(flex: 1),
          ],
        ));
  }
}

class StaticsItem extends StatelessWidget {
  const StaticsItem({
    Key? key,
    required this.name,
    required this.image,
    required this.count,
    this.onTap,
  }) : super(key: key);
  final String name;
  final Widget image;
  final num count;
  final Function()? onTap;

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return Wisdom.column(
      onTap: () => onTap!(),
      children: [
        Stack(
          clipBehavior: Clip.none,
          children: [
            image,
            if (count != 0) ...[
              Positioned(
                right: -15.pt,
                top: -8.pt,
                child: Wisdom(
                  border: Border.all(
                    color: Colors.white,
                  ),
                  padding: EdgeInsets.symmetric(
                      horizontal: count > 9 ? 5.pt : 10.pt, vertical: 2.pt),
                  borderRadius:
                      BorderRadius.circular(count > 99 ? 9.5.pt : 20.pt),
                  color: colorScheme.red,
                  child: WisText(
                    '${count > 99 ? '99+' : count}',
                    color: Colors.white,
                  ),
                ),
              ),
            ],
          ],
        ),
        SizedBox(height: 5.pt),
        WisText(
          '$name',
          size: 13.pt,
          color: colorScheme.mainTitle,
        ),
      ],
    );
  }
}
