import 'package:flutter/material.dart';
import 'package:sport/bean/notice.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/button_primary.dart';
import 'package:sport/widgets/dialog/request_dialog.dart';

class MessageNoticeDetailPage extends StatefulWidget {
  final Notice notice;

  MessageNoticeDetailPage(this.notice);

  @override
  State<StatefulWidget> createState() => _PageState();
}

class _PageState extends State<MessageNoticeDetailPage> with InjectApi {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: CustomScrollView(
          slivers: <Widget>[
            buildSliverAppBar(context, "通知详情"),
            SliverToBoxAdapter(
                child: Padding(
              padding: const EdgeInsets.all(12.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "${widget.notice.title}",
                    style: Theme.of(context).textTheme.headline3,
                  ),
                  SizedBox(
                    height: 12,
                  ),
                  Text(
                    "${widget.notice.createdAt}",
                    style: Theme.of(context).textTheme.bodyText2,
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Divider(),
                  SizedBox(
                    height: 5,
                  ),
                  Text("${widget.notice.content}", style: Theme.of(context).textTheme.bodyText2),
                  if (widget.notice.type == "receive")
                    Padding(
                      padding: const EdgeInsets.only(top: 40.0),
                      child: Text("注：邮件超过7天未查收将自动领取奖励", style: Theme.of(context).textTheme.bodyText2),
                    ),
                  if (widget.notice.type == "receive" && widget.notice.receiveDetail != null)
                    Column(
                      children: <Widget>[
                        SizedBox(
                          height: 10,
                        ),
                        Divider(
                          height: 32,
                        ),
                        Row(
                          children: <Widget>[
                            Expanded(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: (widget.notice.receiveDetail.thing ?? []).map((e) => Text("${e.type == "score" ? '积分' : '经验'} +${e.value}", style: Theme.of(context).textTheme.bodyText2.copyWith(color: Theme.of(context).accentColor))).toList(),
                              ),
                            ),
                            PrimaryButton(
                              width: 75,
                              height: 35,
                              content: widget.notice.isRead == 1 ? "已领取" : "领取",
                              callback: widget.notice.isRead == 1
                                  ? null
                                  : () async {
                                      await request(context, () async {
                                        await api.postNoticeRead(widget.notice.id).catchError((err) {});
                                        ToastUtil.show("领取成功");
                                      });
                                      Navigator.pop(context, true);
                                    },
                            )
                          ],
                        ),
                        Divider(
                          height: 32,
                        ),
                      ],
                    )
                ],
              ),
            )),
          ],
        ));
  }

  String _getName() {
    Notice _notice = widget.notice;
    if (_notice.type == "rank") {
      return "排名变更";
    } else if (_notice.type == "receive") {
      return "榜单通知";
    } else {
      return "系统通知";
    }
  }
}
