import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/bbs/model/error_model.dart';
import 'package:liftmanager/internal/means/means_router.dart';
import 'package:liftmanager/internal/means/presenter/error_list_presenter.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/res/iconfont.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/app_search_bar.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';

class ErrorList extends StatefulWidget {
  ErrorList(this.id);

  final String id;

  @override
  ErrorListState createState() => ErrorListState();
// State<StatefulWidget> createState() {
//   return ErrorListState();
// }
}

class ErrorListState
    extends BasePageState<ErrorList, ErrorListPresenterSeconds> {
  // NewsDetailItem item = NewsDetailItem();

  BaseListProvider<ErrorDetailModel> provider =
      BaseListProvider<ErrorDetailModel>();
  int _page = 1;

  String searchWord;

  ScrollController _scrollController = new ScrollController();

  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
  }

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

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;

    return ChangeNotifierProvider<BaseListProvider<ErrorDetailModel>>(
        create: (_) => provider,
        child: Scaffold(
            appBar: SearchAppBar2(
              onPressed: (text) {
                searchWord = text;
                _onRefresh();
              },
            ),
            body: Container(
                child: Column(
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: Consumer<BaseListProvider<ErrorDetailModel>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('error_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        loadMore: _loadMore,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return GestureDetector(
                            child: Container(
                              padding: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(10),
                                  right: ScreenUtil().setWidth(10),
                                  top: ScreenUtil().setHeight(10),
                                  bottom: ScreenUtil().setHeight(10)),
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                      width: 0.5, color: Colours.line),
                                ),
                              ),
                              child: Column(children: <Widget>[
                                Container(
                                  padding: EdgeInsets.only(
                                      bottom: ScreenUtil().setHeight(5)),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Container(
                                        padding: EdgeInsets.only(
                                            right: ScreenUtil().setWidth(10)),
                                        child: ClipRRect(
                                          borderRadius: BorderRadius.circular(
                                              ScreenUtil().setWidth(18)),
                                          child: Container(
                                            width: ScreenUtil().setWidth(36),
                                            height: ScreenUtil().setWidth(36),
                                            color: Color(0xffFAF7FA),
                                            // padding: EdgeInsets.only(right:10),
                                            child: Icon(
                                              Iconfont.guzhang,
                                              size: 18.0,
                                              color: Color(0xffFD9900),
                                            ),
                                          ),
                                        ),
                                      ),
                                      Expanded(
                                        child: Text(
                                          provider.list[index].code ?? '',
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: 14,
                                            color: Color(0xff333333),
                                          ),
                                        ),
                                      ),
                                      Icon(
                                        Iconfont.gengduo,
                                        size: 16,
                                        color: Color(0xffcccccc),
                                      ),
                                    ],
                                  ),
                                ),
                              ]),
                            ),
                            onTap: () {
                              NavigatorUtils.push(context,
                                  "${MeansRouter.errorDetail}?id=${provider.list[index].id.toString()}");
                            },
                          );
                        },
                      );
                    }))
              ],
            ))));
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getErrorList(int.parse(widget.id), _page,
        searchWord: searchWord);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getErrorList(int.parse(widget.id), _page,
        searchWord: searchWord);
  }

  @override
  ErrorListPresenterSeconds createPresenter() {
    return ErrorListPresenterSeconds();
  }
}
