import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/wode/model/wallet_model.dart';
import 'package:liftmanager/internal/wode/presenter/wallet_list_presenter.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/utils/time_format.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';

class SmallMoney extends StatefulWidget {
  // QuestionList(this.index);

  // final String index;

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

class SmallMoneyState
    extends BasePageState<SmallMoney, WalletListPresenterSeconds>
    with AutomaticKeepAliveClientMixin {
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  ScrollController _scrollController = new ScrollController();

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

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

  int currentYear;
  int currentMonth;

  String incomeStr = '0';
  String outcomeStr = '0';

  String currentDate = '全部';

  List<int> selecteds = [];

  getBusinessType(int type) {
    String str;
    if (type == 1) {
      str = "充值会员";
    } else if (type == 2) {
      str = "打赏";
    } else if (type == 3) {
      str = "充值";
    } else if (type == 4) {
      str = "诊单业务";
    } else if (type == 5) {
      str = "提现";
    } else if (type == 6) {
      str = "提现失败退款";
    } else if (type == 7) {
      str = "余额消费";
    } else if (type == 8) {
      str = "拒绝接单退款";
    }else if (type == 9) {
      str = "专家惩罚";
    }else if (type == 10) {
      str = "申诉退款";
    }else if (type == 11) {
      str = "粉丝充值返利";
    }else if (type == 12) {
      str = "现金支付";
    }else if (type == 13) {
      str = "差旅费";
    }
    return str;
  }

  void selectTimePeriod() {
    List pickerdata = [];
    int minYear = 2016;
    DateTime now = new DateTime.now();
    int maxYear = now.year;
    int lastMonth = now.month;
    print(lastMonth);

    for (int i = minYear; i <= maxYear; i++) {
      List<int> month = [];
      int monthLength = (i == maxYear) ? lastMonth : 12;
      for (int j = 1; j <= monthLength; j++) {
        month.add(j);
      }
      pickerdata.add({i.toString(): month});
    }

    new Picker(
      adapter: PickerDataAdapter<String>(
        pickerdata: pickerdata,
      ),
      selecteds: selecteds,
      changeToFirst: true,
      hideHeader: false,
      cancelText:"取消",
      confirmText:"确认",
      onConfirm: (Picker picker, List value) {
        print(picker.getSelectedValues());
        selecteds = value;
        currentYear = int.parse(picker.getSelectedValues()[0]);
        currentMonth = int.parse(picker.getSelectedValues()[1]);

        currentDate = picker.getSelectedValues()[0] +
            '年' +
            picker.getSelectedValues()[1] +
            '月';

        _onRefresh();
        setState(() {});
      },
    ).showModal(this.context);
  }

  int _page = 1;

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

    return ChangeNotifierProvider<BaseListProvider<Records>>(
      create: (_) => provider,
      child: Scaffold(
        resizeToAvoidBottomPadding: true,
        appBar: MyAppBar(centerTitle: "零钱明细"),
        body: Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                width: width,
                height: 60,
                padding: EdgeInsets.only(
                  left: ScreenUtil().setWidth(15),
                  right: ScreenUtil().setWidth(15),
                ),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(width: 0.5, color: Colours.line),
                  ),
                  color: Color(0xffEFEFEF),
                ),
                // padding: EdgeInsets.only(left:15,right:15),
                // color: Color(0xFFF1F4FC),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    GestureDetector(
                      onTap: () {
                        selectTimePeriod();
                      },
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(20),
                        child: Container(
                          // height: ,
                          padding: EdgeInsets.only(
                              left: 10, right: 3, top: 2, bottom: 2),
                          color: Colors.white,
                          child: Row(
                            children: <Widget>[
                              Text(
                                currentDate,
                                style: TextStyle(
                                    color: Color(0xff333333),
                                    fontSize: ScreenUtil().setSp(14)),
                                textAlign: TextAlign.center,
                              ),
                              Container(
                                padding: EdgeInsets.only(top: 3),
                                child: Icon(
                                  Icons.keyboard_arrow_down,
                                  size: 20.0,
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(top: 10),
                      child: Column(
                        children: <Widget>[
                          Row(children: <Widget>[
                            Text(
                              "支出：",
                              style: TextStyle(
                                  color: Color(0xff333333),
                                  fontSize: ScreenUtil().setSp(14)),
                              textAlign: TextAlign.start,
                            ),
                            Text(
                              "￥" + outcomeStr,
                              style: TextStyle(
                                  color: Color(0xff333333),
                                  fontSize: ScreenUtil().setSp(14)),
                              textAlign: TextAlign.start,
                            ),
                          ]),
                          Row(children: <Widget>[
                            Text(
                              "收入：",
                              style: TextStyle(
                                  color: Color(0xff333333),
                                  fontSize: ScreenUtil().setSp(14)),
                              textAlign: TextAlign.start,
                            ),
                            Text(
                              "￥" + incomeStr,
                              style: TextStyle(
                                  color: Color(0xff333333),
                                  fontSize: ScreenUtil().setSp(14)),
                              textAlign: TextAlign.start,
                            ),
                          ]),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                flex: 1,
                child: Consumer<BaseListProvider<Records>>(
                  builder: (_, provider, __) {
                    return MyListView(
                      key: Key('wallet_list'),
                      itemCount: provider.list.length,
                      stateType: provider.stateType,
                      onRefresh: _onRefresh,
                      loadMore: _loadMore,
                      pageSize: 8,
                      hasMore: provider.hasMore,
                      itemBuilder: (_, index) {
                        return InkWell(
                          child: Container(
                            padding: EdgeInsets.only(
                                left: ScreenUtil().setWidth(15),
                                right: ScreenUtil().setWidth(15),
                                top: ScreenUtil().setWidth(15),
                                bottom: ScreenUtil().setWidth(15)),
                            decoration: BoxDecoration(
                              border: Border(
                                bottom:
                                    BorderSide(width: 0.5, color: Colours.line),
                              ),
                              color: Colors.white,
                            ),
                            child: Column(
                              // crossAxisAlignment: CrossAxisAlignment.start,

                              children: <Widget>[
                                Container(
                                  padding: EdgeInsets.only(bottom: 5),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Text(
                                        getBusinessType(provider.list[index].businessType),
                                        textAlign: TextAlign.left,
                                        style: TextStyle(
                                          fontSize: ScreenUtil().setSp(16),
                                          color: Color(0xff333333),
                                        ),
                                      ),
                                      Text(
                                        (provider.list[index].type == 1
                                                ? "-"
                                                : "+") +
                                            provider.list[index].amount
                                                .toString(),
                                        textAlign: TextAlign.left,
                                        style: TextStyle(
                                          fontSize: ScreenUtil().setSp(16),
                                          color: Colors.red,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    Text(
                                      provider.list[index].descr ?? "",
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(13),
                                        color: Color(0xff999999),
                                      ),
                                    ),
                                    Text(
                                      DateUtils.instance.getFormartData(
                                          timeSamp:
                                              provider.list[index].createTime,
                                          format: "yyyy-MM-dd"),
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                        fontSize: ScreenUtil().setSp(13),
                                        color: Color(0xff999999),
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                          onTap: () {
                            // print(i);
                            // NavigatorUtils.push(context, BbsRouter.positionDetail);
                          },
                        );
                      },
                    );
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getWalletList(_page,
        currentYear: currentYear,
        currentMonth: currentMonth, complete: (income, outcome) {
      setState(() {
        incomeStr = income.toString();
        outcomeStr = outcome.toString();
      });
    });
  }

  Future _loadMore() async {
    _page++;
    await presenter.getWalletList(_page,
        currentYear: currentYear,
        currentMonth: currentMonth, complete: (income, outcome) {
      setState(() {
        incomeStr = income.toString();
        outcomeStr = outcome.toString();
      });
    });
  }

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

  bool get wantKeepAlive => true;
}
