import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.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/my_refresh_list.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;

  @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);
  }

  @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 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();
  }
}
