import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/wode/model/piao_model.dart';
import 'package:liftmanager/internal/wode/presenter/piao_list_presenter.dart';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/res/colors.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/my_card.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';

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

  // final String index;

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

class PiaoListState extends BasePageState<PiaoList, PiaoListPresenter> {
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  int _page = 1;

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

  getCheckFlag(int type) {
    String str;
    if (type == 1) {
      str = "已开票";
    } else if (type == 2) {
      str = "已提交申请";
    } else if (type == 0) {
      str = "驳回";
    }
    return str;
  }

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

  @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: "开票历史",
            // actions:<Widget>[
            //   FlatButton(
            //     child: Text("发票抬头"),
            //     textColor: Colours.dark_text,
            //     highlightColor: Colors.transparent,
            //     onPressed: () {
            //       NavigatorUtils.push(context, WodeRouter.piaotou);

            //     },
            //   )
            // ],
          ),
          body: Container(
            color: Color(0xFFF6F6F6),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: Consumer<BaseListProvider<Records>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('piao_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        loadMore: _loadMore,
                        pageSize: 6,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return Container(
                            padding: EdgeInsets.only(
                                bottom: ScreenUtil().setWidth(15),
                                left: ScreenUtil().setWidth(10),
                                right: ScreenUtil().setWidth(10),
                                top: ScreenUtil().setWidth(15)),
                            // : EdgeInsets.only(
                            //     left: ScreenUtil().setWidth(10),
                            //     right: ScreenUtil().setWidth(10),
                            //     top: ScreenUtil().setWidth(15)),
                            margin: EdgeInsets.fromLTRB(10, 10, 10, 0),
                            decoration: new BoxDecoration(
                              //背景
                              color: Colors.white,
                              //设置四周圆角 角度
                              borderRadius:
                                  BorderRadius.all(Radius.circular(5.0)),
                            ),
                            child: InkWell(
                              child: Container(
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(5),
                                    right: ScreenUtil().setWidth(5),
                                    top: ScreenUtil().setWidth(5),
                                    bottom: ScreenUtil().setWidth(5)),
                                child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: <Widget>[
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: <Widget>[
                                          Row(
                                            // crossAxisAlignment:
                                            // CrossAxisAlignment.center,
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Container(
                                                padding:
                                                    EdgeInsets.only(bottom: 5),
                                                child: Icon(
                                                  const IconData(0xe6e0,
                                                      fontFamily: "Iconfont"),
                                                  size: 18.0,
                                                  color: Color(0xffcccccc),
                                                ),
                                              ),
                                              SizedBox(
                                                width: 10,
                                              ),
                                              Text(
                                                provider.list[index]
                                                            .createTime !=
                                                        null
                                                    ? DateUtils.instance
                                                        .getFormartData(
                                                            timeSamp: provider
                                                                .list[index]
                                                                .createTime,
                                                            format:
                                                                "yyyy-MM-dd HH:mm")
                                                    : "",
                                                textAlign: TextAlign.start,
                                                style: TextStyle(
                                                  fontSize:
                                                      ScreenUtil().setSp(16),
                                                  color: Color(0xff999999),
                                                ),
                                              ),
                                            ],
                                          ),
                                          Text(
                                            getCheckFlag(
                                                provider.list[index].checkFlag),
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(14),
                                                color: Colours.blue_app_main),
                                          ),
                                        ],
                                      ),
                                      SizedBox(
                                        height: 30,
                                      ),
                                      Row(
                                        children: <Widget>[
                                          Text(
                                            "服务 ",
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(16),
                                              color: Color(0xff000000),
                                            ),
                                          ),
                                          Container(
                                            padding:
                                                EdgeInsets.fromLTRB(5, 2, 5, 2),
                                            decoration: new BoxDecoration(
                                              //背景
                                              color: Colors.white,
                                              //设置四周圆角 角度
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(4.0)),
                                              //设置四周边框
                                              border: new Border.all(
                                                  width: 1,
                                                  color: Color(0xffcccccc)),
                                            ),
                                            child: Text(
                                              provider.list[index]
                                                          .invoiceType ==
                                                      '1'
                                                  ? '电子发票'
                                                  : '专用发票',
                                              style: TextStyle(
                                                  color: Color(0xffcccccc)),
                                            ),
                                          )
                                        ],
                                      ),

                                      SizedBox(height: 10),
                                      Container(
                                        child: Text(
                                          "发票代码:${provider.list[index].dm??''}",
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(14),
                                            color: Color(0xff999999),
                                          ),
                                        ),
                                      ),
                                      SizedBox(height: 10),
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: <Widget>[
                                          Text(
                                            "发票号码:${provider.list[index].hm??''}",
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(14),
                                              color: Color(0xff999999),
                                            ),
                                          ),
                                          Text(
                                            '￥${provider.list[index].hjje ?? "0"}',
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(16),
                                              color: Color(0xff333333),
                                            ),
                                          ),
                                        ],
                                      ),
                                      // Container(
                                      //   child: Text(
                                      //       '开票序号:${provider.list[index].hm??"0"}',
                                      //       textAlign: TextAlign.left,
                                      //       style: TextStyle(
                                      //         fontSize:ScreenUtil().setSp(14),
                                      //         color:Color(0xff999999),
                                      //       ),
                                      //     ),
                                      // ),
                                    ]),
                              ),
                              onTap: () {
                                NavigatorUtils.push(context,
                                    "${WodeRouter.piaoDetail}?id=${provider.list[index].id}");
                              },
                            ),
                          );
                        },
                      );
                    }))
              ],
            ),
          ),
        ));
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getWalletList(_page);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getWalletList(_page);
  }

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