import 'package:flutter/material.dart';
import 'package:liftmanager/internal/message/message_router.dart';
import 'package:liftmanager/internal/message/model/message_list_entity.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:oktoast/oktoast.dart';

class MessageList extends StatefulWidget {
  const MessageList({Key key, @required this.index}) : super(key: key);

  final int index;

  @override
  _MessageListState createState() => _MessageListState();
}

class _MessageListState extends State<MessageList>
    with
        AutomaticKeepAliveClientMixin<MessageList>,
        SingleTickerProviderStateMixin {
  List<MessageListItem> _list = [];
  int _page = 1;
  int _maxPage = 999;
  StateType _stateType = StateType.loading;

  @override
  void initState() {
    super.initState();
    //Item数量
//    _maxPage = widget.index == 0 ? 1 : (widget.index == 1 ? 2 : 3);
    _onRefresh();
  }

  Future _onRefresh() async {
    _stateType = StateType.loading;
    _page = 1;
    _maxPage = 999;
    ApiService(context: context).messageList(_page, 10, widget.index,
        onSuccess: (MessageListEntity data) {
      _stateType = StateType.empty;
      if (data != null) {
        if (_page == 1) {
          _list.clear();
        }
        if (data.rows.length < 10) {
          _maxPage = _page;
        }
        if (data != null) {
          _list.addAll(data.rows);
        }
      }
      setState(() {});
    }, onError: (code, msg) {
      showToast(msg);
    });
  }

  Future _loadMore() async {
    _stateType = StateType.loading;
    _page +=1;
    ApiService(context: context).messageList(_page, 10, widget.index,
        onSuccess: (MessageListEntity data) {
      _stateType = StateType.empty;
      if (data.rows.length < 10) {
        _maxPage = _page;
      }
      _list.addAll(data.rows);
      _page++;
      setState(() {});
    }, onError: (code, msg) {
      showToast(msg);
    });
  }

  ///电梯列表
  void _liftList(id) {
//    NavigatorUtils.push(context, MessageRouter.heavyLiftListPage + "?id=${id}");
  }

  ///项目详情
  void _projectDetail(id, projectStatus) {
//    NavigatorUtils.push(context, "${MessageRouter.heavyDetailPage}?id=${id}&status=${projectStatus}");
  }

  void _startProject(id) {
    showAlert(
        context,
        "提示",
        "现在开工？",
        "确定",
        () {
          NavigatorUtils.goBack(context);
          showLoading(context, "正在加载");
          ApiService(context: context).capitalBegin(id, onSuccess: (res) {
            dismissLoading(context);
            _onRefresh();
          }, onError: (code, msg) {
            dismissLoading(context);
            toasts(msg);
          });
        },
        txt2: "取消",
        onPre2: () {
          NavigatorUtils.goBack(context);
        });
  }

  void _endProject(id) {
    showAlert(
        context,
        "提示",
        "现在开工？",
        "确定",
        () {
          ApiService(context: context).capitalBegin(id, onSuccess: (res) {
            if (res != null) {
              _onRefresh();
            }
          }, onError: (code, msg) {
            toasts(msg);
          });
        },
        txt2: "取消",
        onPre2: () {
          NavigatorUtils.goBack(context);
        });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return MyListView(
        itemCount: _list.length,
        stateType: _stateType,
        onRefresh: _onRefresh,
        loadMore: _loadMore,
        hasMore: _page < _maxPage,
        itemBuilder: (_, index) {
          return InkWell(
              onTap: () {
                NavigatorUtils.push(context,
                    "${MessageRouter.messageDetailPage}?id=" + _list[index].id);
              },
              child: Container(
                margin: EdgeInsets.fromLTRB(15, 5, 15, 5),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(6.0),
                ),
                padding: const EdgeInsets.only(top: 10, bottom: 10),
                child: Column(
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.only(left: 12,right: 12),
                      child: Row(
                        children: <Widget>[
                          Expanded(
                              flex: 1,
                              child: Text(
                                _list[index].content,
                                style: TextStyles.text15,
                                overflow: TextOverflow.ellipsis,
                                softWrap: false, //单行显示
                              )),
                          Text(
                            "${_list[index].viewFlag == 1 ? '已读' : ''}",
                            style: TextStyle(
                                fontSize: 11, color: Color(0xFF999999)),
                          ),
                        ],
                      ),
                    ),
                    lineTxt("", "${_list[index].createTime}"),
                  ],
                ),
              ));
        });
  }

  Widget lineTxt(title, value) {
    return Container(
      padding: EdgeInsets.only(left: 12, top: 5, right: 12),
      child: Row(
        children: <Widget>[
          Text("${title}",
              style: TextStyle(fontSize: 13, color: Colours.dark_text_gray)),
          Expanded(
            flex: 1,
            child: Text(
              "${value}",
              textAlign: TextAlign.right,
              style: TextStyle(fontSize: 13, color: Colours.dark_text_gray),
            ),
          )
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
