/*
 * @Author       : WuWei
 * @LastEditors: ChenYaJin
 * @Date         : 2022-03-15 09:26:15
 * @LastEditTime: 2023-04-27 17:48:56
 * @Description  : Do not edit
 */
import 'package:app_package/src/view_models/notice_store.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class NoticeIndexPage extends StatelessWidget {
  const NoticeIndexPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => NoticeStore(context),
      child: _PageView(),
      lazy: false,
    );
  }
}

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

class _PageViewState extends State<_PageView> {
  NoticeStore get store => Provider.of(context, listen: false);

  // static const double TITLE_FONT_SIZE = 18.pt;

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        title: WisText('公告列表',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: WisScrollView(
        controller: store.viewController,
        key: store.scorllViewKey,
        onInit: () => store.onInit(),
        onLoad: () => store.getNoticeList(isLoad: true),
        slivers: <Widget>[
          Wisdom(
            color: Color(0xFFFFFFFF),
            margin: EdgeInsets.only(bottom: 10.pt),
            child: WSearch(
              hintText: '输入公告标题搜索',
              onSubmitted: (value) => store.toSearchList(value),
              onCancel: () => store.toSearchList(''),
              moonlight: false,
              padding: EdgeInsets.symmetric(vertical: 10.pt),
              isDark: true,
            ),
          ).asSliver(),
          _ListComponent(),
        ],
      ),
    );
  }
}

class _ListComponent extends StatelessWidget {
  const _ListComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<NoticeStore>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;
    return WisSliverList.childBuilder(
      childCount: store.noticeList.length,
      interval: 10.pt,
      builder: (_, i) {
        var item = store.noticeList[i];
        return InkWell(
          onTap: () => store.routeToDetail(item),
          child: Material(
            child: WisCard(
              color: Colors.white,
              margin: EdgeInsets.only(left: 15.pt, right: 15.pt),
              child: Wisdom.column(
                padding: EdgeInsets.all(15.pt),
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      textBaseline: TextBaseline.alphabetic,
                      children: <Widget>[
                        if (item.isStick == true) ...[
                          WTag(
                            color: colorScheme.red,
                            child: WisText(
                              '置顶',
                            ),
                          ),
                          SizedBox(
                            width: 10.pt,
                          ),
                        ],
                        Expanded(
                          child: WisText(
                            item?.title ?? '',
                            size: 15.pt,
                            bold: true,
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Wisdom.row(
                    margin: EdgeInsets.only(top: 10.pt),
                    children: [
                      WisText(
                        '公告类型:${item?.noticePublicationTypeName ?? '--'}',
                        size: 13.pt,
                      ),
                    ],
                  ),
                  Divider(
                    height: 20.pt,
                  ),
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        WisText(
                          '发布人:${item.createdByName ?? '--'}',
                          size: 13.pt,
                          color: colorScheme.minor,
                          weight: FontWeight.w300,
                          overflow: TextOverflow.ellipsis,
                        ).asFlexible(
                          flex: 1,
                        ),
                        WisText.c000(
                          '发布时间:${item.publishAt ?? '--'}',
                          size: 13.pt,
                          color: colorScheme.minor,
                          weight: FontWeight.w300,
                          overflow: TextOverflow.ellipsis,
                        ).asFlexible(
                          flex: 1,
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
