import 'package:flutter/material.dart';
import 'package:liftmanager/internal/wode/model/message_model.dart';
import 'package:liftmanager/internal/wode/page/notice_list.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/iconfont.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';

class MessageCenter extends StatefulWidget {
  @override
  MessageCenterState createState() => MessageCenterState();
}

class MessageCenterState extends State<MessageCenter> {
  MessageOverview overview;

  @override
  void initState() {
    super.initState();

    getMessageOverview();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // resizeToAvoidBottomPadding: true,
        appBar: MyAppBar(
          centerTitle: "消息中心",
        ),
        body: Container(
          padding: EdgeInsets.symmetric(horizontal: 10),
          child: Column(
            children: [
              GestureDetector(
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      child: Stack(
                        overflow: Overflow.visible,
                        children: [
                          ClipRRect(
                            borderRadius: BorderRadius.circular(20),
                            child: Container(
                              width: 40,
                              height: 40,
                              decoration: BoxDecoration(
                                color: Color(0xffFDAF2C),
                              ),
                              // padding: EdgeInsets.only(right:10),
                              child: Icon(
                                Iconfont.tongzhi,
                                size: 17.0,
                                color: Colors.white,
                              ),
                            ),
                          ),
                          if (overview != null &&
                              overview.systemMessageUnreadCount > 0)
                            Positioned(
                              right: -5,
                              top: -1,
                              child: Container(
                                height: 14,
                                padding: EdgeInsets.symmetric(horizontal: 5),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  color: Color(0xffF94F45),
                                  borderRadius: BorderRadius.circular(7),
                                ),
                                child: Text(
                                  '${overview.systemMessageUnreadCount}',
                                  style: TextStyle(
                                      color: Color(0xffffffff), fontSize: 10),
                                ),
                              ),
                            )
                        ],
                      ),
                    ),
                    Expanded(
                      child: Column(
                        children: <Widget>[
                          SizedBox(
                            height: 20,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              SizedBox(
                                width: 10,
                              ),
                              Text(
                                "系统消息",
                                textAlign: TextAlign.left,
                                style: TextStyle(
                                  fontSize: 14,
                                  color: Color(0xff333333),
                                ),
                              ),
                              Spacer(),
                              Text(
                                "${overview?.systemMessageModel?.createTime ?? ''}",
                                textAlign: TextAlign.left,
                                style: TextStyle(
                                  fontSize: 12,
                                  color: Color(0xffCCCCCC),
                                ),
                              ),
                            ],
                          ),
                          SizedBox(
                            height: 5,
                          ),
                          Row(
                            children: <Widget>[
                              SizedBox(
                                width: 10,
                              ),
                              Container(
                                child: Text(
                                  "${overview?.systemMessageModel?.content ?? ''}",
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                    fontSize: 13,
                                    color: Color(0xff999999),
                                  ),
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Divider(),
              SizedBox(
                height: 20,
              ),
              GestureDetector(
                onTap: () async {
                  await Navigator.push(context,
                      MaterialPageRoute(builder: (context) => NoticeList()));

                  getMessageOverview();
                },
                child: Container(
                  color: Colors.white,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        child: Stack(
                          overflow: Overflow.visible,
                          children: [
                            ClipRRect(
                              borderRadius: BorderRadius.circular(20),
                              child: Container(
                                width: 40,
                                height: 40,
                                decoration: BoxDecoration(
                                  color: Color(0xff5589FF),
                                ),
                                // padding: EdgeInsets.only(right:10),
                                child: Icon(
                                  Iconfont.tongzhi,
                                  size: 17.0,
                                  color: Colors.white,
                                ),
                              ),
                            ),
                            if (overview != null &&
                                overview.notificationMessageUnreadCount > 0)
                              Positioned(
                                right: -5,
                                top: -1,
                                child: Container(
                                  height: 14,
                                  padding: EdgeInsets.symmetric(horizontal: 5),
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                    color: Color(0xffF94F45),
                                    borderRadius: BorderRadius.circular(7),
                                  ),
                                  child: Text(
                                    '${overview.notificationMessageUnreadCount}',
                                    style: TextStyle(
                                        color: Color(0xffffffff), fontSize: 10),
                                  ),
                                ),
                              )
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                SizedBox(
                                  width: 10,
                                ),
                                Text(
                                  "通知消息",
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                    fontSize: 14,
                                    color: Color(0xff333333),
                                  ),
                                ),
                                Spacer(),
                                Text(
                                  "${overview?.notificationMessageModel?.createTime ?? ''}",
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                    fontSize: 12,
                                    color: Color(0xffCCCCCC),
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Row(
                              children: <Widget>[
                                SizedBox(
                                  width: 10,
                                ),
                                Container(
                                  child: Text(
                                    "${overview?.notificationMessageModel?.content ?? ''}",
                                    textAlign: TextAlign.left,
                                    style: TextStyle(
                                      fontSize: 13,
                                      color: Color(0xff999999),
                                    ),
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Divider(),
            ],
          ),
        ));
  }

  getMessageOverview() {
    NewApiService().getMessageOverview(onSuccess: (res) {
      setState(() {
        overview = res;
      });
    }, onError: (code, msg) {
      toasts(msg);
    });
  }
}
