/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-03-15 09:26:28
 * @LastEditTime : 2023-06-21 10:47:52
 * @Description  : Do not edit
 */
import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:app_package/src/view_models/notice_store.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class NoticeDetailPage extends StatelessWidget {
  const NoticeDetailPage({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> {
  late NoticeStore _store;

  @override
  Widget build(BuildContext context) {
    _store = Provider.of<NoticeStore>(context);
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      backgroundColor: Colors.white,
      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(
        onInit: () => _store.getDetail(),
        slivers: <Widget>[
          _buildContentComponent(),
        ],
      ),
    );
  }

  Widget _buildContentComponent() {
    String content = (_store.notice?.content ?? '')
        .replaceAllMapped(RegExp(r'<img src="/\w+/'), (match) {
      return '<img width="100%" src="${APPConfig.baseURL}';
    });
    content = content.replaceAllMapped(RegExp(r'&nbsp;'), (match) => ' ');
    final colorScheme = WTheme.of(context).colorScheme;
    return SliverToBoxAdapter(
      child: Wisdom.column(
        padding: EdgeInsets.fromLTRB(15.pt, 15.pt, 15.pt, 15.pt),
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              RichText(
                textAlign: TextAlign.center,
                maxLines: 10,
                text: TextSpan(children: [
                  if (_store.notice?.isStick == true) ...[
                    WidgetSpan(
                      child: WTag(
                        color: colorScheme.red,
                        child: WisText(
                          '置顶',
                        ),
                      ),
                    ),
                  ],
                  TextSpan(
                    text: _store.notice?.title ?? '--',
                    style: TextStyle(
                      fontSize: 18.pt,
                      color: colorScheme.mainTitle,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ]),
              ).asExpanded(),
            ],
          ),
          SizedBox(height: 10.pt),
          Wisdom.row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              WisText(
                '发布人:${_store.notice?.createdByName ?? '--'}',
                size: 12.pt,
                color: colorScheme.minor,
                weight: FontWeight.w300,
                overflow: TextOverflow.ellipsis,
              ),
              WisText(
                '发布时间:${_store.notice?.publishAt ?? '--'}',
                size: 12.pt,
                color: colorScheme.minor,
                weight: FontWeight.w300,
                overflow: TextOverflow.ellipsis,
              ),
            ],
          ),
          SizedBox(height: 10.pt),
          Wisdom.row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              WisText(
                '阅读数:${_store.notice?.readNum ?? '--'}',
                size: 12.pt,
                color: colorScheme.minor,
                weight: FontWeight.w300,
              ),
            ],
          ),
          // buildNoticeType(_store?.notice?.type ?? ''),
          Divider(
            height: 25.pt,
            color: colorScheme.placeholder,
          ),
          Container(
            child: HtmlWidget(
              content,
              textStyle: TextStyle(
                fontSize: 14.pt,
                color: colorScheme.mainTitle,
              ),
            ),
          ),
          if (_store.notice?.files?.length != null &&
              _store.notice!.files!.length > 0) ...[
            Divider(
              color: colorScheme.placeholder,
              height: 25.pt,
            ),
            WisText('附件上传:'),
            SizedBox(height: 10.pt),
            for (int i = 0; i < _store.notice!.files!.length; i++)
              _FileItem(itemData: _store.notice!.files![i]),
          ],
        ],
      ),
    );
  }
}

class _FileItem extends StatelessWidget {
  const _FileItem({Key? key, this.itemData}) : super(key: key);

  final itemData;

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<NoticeStore>(context);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final String baseUrl = APPConfig.baseURL;
    return Wisdom.row(
      margin: EdgeInsets.only(bottom: 5.pt),
      onTap: () => {
        if (['pdf', 'docx', 'xlsx', 'xls', 'doc', 'pptx', 'ppt']
            .contains(itemData.fileSuffix))
          {
            store.route2perview(
              baseUrl + 'file/download/' + itemData.fileId,
              itemData?.fileName,
            ),
          }
        else
          {
            store.onShowImage(itemData.fileId),
          }
      },
      // .pdf, .doc, .docx, .xls, .xlsx, .pptx, .ppt, image/*
      children: [
        WisText(
          itemData?.fileName,
          size: 14.pt,
          color: colorScheme.primary,
        ).asExpanded(),
      ],
    );
  }
}
