import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:intl/intl.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/sign/model/sign_list_entity.dart';
import 'package:liftmanager/internal/sign/presenter/SignListPresenter.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/calendar/flutter_calendar.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart' as p;

class SignListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SignListPageState();
  }
}

class SignListPageState extends BasePageState<SignListPage, SignListPresenter> {
  BaseListProvider<SignListItem> provider = BaseListProvider<SignListItem>();
  String _nowTimeStr = "";
  int _page = 1;
  Calendar mCalendar;

  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _nowTimeStr = DateTime.now().toString().split(" ")[0];
    _onRefresh();
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.signList(context, _page, 10, _nowTimeStr);
  }

  Future _loadMore() async {
    _page++;
    await presenter.signList(context, _page, 10, _nowTimeStr);
  }

  ///选择日期
  Future<void> _selectTime(Function callback) async {
    DatePicker.showDatePicker(context,
        showTitleActions: true, onChanged: (date) {}, onConfirm: (date) {
      callback("${date.toString().split(".")[0]}");
    }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }

  Widget calendarWidget() {
    if (mCalendar == null) {
      mCalendar = Calendar(
        showTodayAction: false,
        showChevronsToChangeRange: true,
        showCalendarPickerIcon: false,
        onSelectedRangeChange: (range) {
          // print("Range1 is ${range.item1}, ${range.item2}");
          // _pageDate = range.item1;
          // getCalenderDate();
        },
        onDateSelected: (res) async {
          await presenter.signList(
              context, _page, 10, DateFormat("yyyy-MM-dd").format(res));
          setState(() {});
          // selectedDate = res;
          // initHeaderTitle();
          // print(_pageController.page);
          // for(var i=0;i<mListView.length;i++){
          //   MaintenanceList widget = mListView[i];
          //   widget.date = res;
          // }
          // MaintenanceList widget = mListView[_pageController.page.toInt()];
          // widget.date = res;
          // if(widget.mState !=null){
          //   widget.mState.onRefresh();
          // }
          // setState(() {});
          // print("currentSelected is ${mCalendar.currentSelected()}");
          // print("Range2 is ${res}");
        },
        isExpandable: true,
      );
    }
    return mCalendar;
  }

  @override
  Widget build(BuildContext context) {
    return p.ChangeNotifierProvider<BaseListProvider<SignListItem>>(
        create: (_) => provider,
        child: WillPopScope(
            onWillPop: () {
              NavigatorUtils.goBackWithParams(context, true);
              return Future.value(false);
            },
            child: Scaffold(
                appBar: MyAppBar(
                  centerTitle: "考勤记录",
                  // actions: <Widget>[
                  //   FlatButton(
                  //     child: Text("选择日期"),
                  //     textColor: Colours.dark_text,
                  //     highlightColor: Colors.transparent,
                  //     onPressed: () {
                  //       _selectTime((String time) {
                  //         _nowTimeStr = "${time.split(" ")[0]}";
                  //         _onRefresh();
                  //       });
                  //     },
                  //   )
                  // ],
                ),
                body: p.Consumer<BaseListProvider<SignListItem>>(
                    builder: (_, provider, __) {
                  return Column(
                    children: [
                      calendarWidget(),
                      Container(
                          // padding: EdgeInsets.only(left: 10),
                          alignment: Alignment.centerLeft,
                          height: 45,
                          decoration: BoxDecoration(
                            border: Border(
                              bottom:
                                  BorderSide(width: 0.5, color: Colours.line),
                            ),
                          ),
                          child: Row(
                            children: [
                              Container(
                                // margin: EdgeInsets.only(left: 10),
                                height: 13, width: 2,
                                color: Colours.blue_app_main,
                              ),
                              SizedBox(
                                width: 10,
                              ),
                              Text(
                                "考勤记录",
                                style: TextStyle(
                                    fontSize: 14, color: Colours.text),
                              ),
                            ],
                          )),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          provider.list.length > 0
                              ? Container(
                                  color: Colors.white,
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 15.0, vertical: 12.0),
                                  alignment: Alignment.centerLeft,
                                  child: RichText(
                                      text: TextSpan(
                                          // style: DefaultTextStyle.of(context).style,
                                          children: <InlineSpan>[
                                        TextSpan(
                                            text:
                                                '${provider.list[0].createDate}',
                                            style:
                                                TextStyle(color: Colours.text)),
                                        TextSpan(
                                            text:
                                                '  ${provider.list[0].typeDesc}',
                                            style: TextStyle(
                                                color: Colours.blue_app_main)),
                                      ])),
                                )
                              : Container(),
                          provider.list.length > 1
                              ? Container(
                                  height: 22,
                                  width: 2,
                                  color: Colours.new_dark_line,
                                  margin: EdgeInsets.only(left: 90),
                                )
                              : Container(),
                          provider.list.length > 1
                              ? Container(
                                  color: Colors.white,
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 15.0, vertical: 12.0),
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    "${provider.list[1].createDate}  ${provider.list[1].typeDesc}",
                                    style: TextStyle(color: Colours.text_gray),
                                  ),
                                )
                              : Container()
                        ],
                      )
                    ],
                  );

//                   MyListView(
//                     itemCount: provider.list.length,
//                     stateType: provider.stateType,
//                     onRefresh: _onRefresh,
//                     loadMore: _loadMore,
// //                itemExtent: 67.0,
//                     hasMore: provider.hasMore,
//                     itemBuilder: (_, index) {
//                       return Container(
//                         padding: const EdgeInsets.symmetric(
//                             horizontal: 15.0, vertical: 12.0),
//                         alignment: Alignment.centerLeft,
//                         decoration: BoxDecoration(
//                             color: Colors.white,
//                             border: Border(
//                               bottom:
//                                   BorderSide(width: 0.5, color: Colours.line),
//                             )),
//                         child: Column(
//                           crossAxisAlignment: CrossAxisAlignment.start,
//                           mainAxisAlignment: MainAxisAlignment.start,
//                           children: <Widget>[
//                             Text("类型：${provider.list[index].typeDesc}"),
//                             Text("时间：${provider.list[index].createDate}"),
//                             Text("地址：${provider.list[index].address}"),
//                             Text("状态：${provider.list[index].statusDesc}")
//                           ],
//                         ),
//                       );
//                     },
//                   );
                }))));
  }

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