import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/bbs/model/coupon_model_page.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/wode/presenter/red_list_presenter.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';
// import 'package:amap_all_fluttify/amap_all_fluttify.dart';
import 'package:provider/provider.dart';

class CouponPage extends StatefulWidget {
  @override
  CouponPageState createState() => CouponPageState();
}

class CouponPageState extends BasePageState<CouponPage, CoupontListPresenter>
    with AutomaticKeepAliveClientMixin {
  static const staticList = [
    {
      "price": "5",
      "time": "2019-12-30",
    },
    {
      "price": "5",
      "time": "2019-12-30",
    },
    {
      "price": "5",
      "time": "2019-12-30",
    },
    {
      "price": "5",
      "time": "2019-12-30",
    },
    {
      "price": "5",
      "time": "2019-12-30",
    },
    {
      "price": "5",
      "time": "2019-12-30",
    },
    {
      "price": "5",
      "time": "2019-12-30",
    },
    {
      "price": "5",
      "time": "2019-12-30",
    },
    {
      "price": "5",
      "time": "2019-12-30",
    },
  ];

  int checkIndex = 0;
  // NewsDetailItem item = NewsDetailItem();
  BaseListProvider<Records> provider = BaseListProvider<Records>();

  int usedFlag = 1; //0失效1可使用

  int _page = 1;
  ScrollController _scrollController = new ScrollController();

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

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

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

  // getScenario(int type) {
  //   String str;
  //   if (type == 0) {
  //     str = "全场通用";
  //   } else if (type == 1) {
  //     str = "仅限出诊问诊服务";
  //   } else if (type == 2) {
  //     str = "仅限题库";
  //   } else if (type == 3) {
  //     str = "仅限视频";
  //   }
  //   return str;
  // }
  getScenario(int type) {
    String str;
    if (type == 0) {
      str = "全场通用";
    } else if (type == 1) {
      str = "出诊问诊服务";
    } else if (type == 2) {
      str = "题库";
    } else if (type == 3) {
      str = "视频";
    }
    return str;
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getRedList(_page, usedFlag);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getRedList(_page, usedFlag);
  }

  bool get wantKeepAlive => true;

  @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(
            // color: Color(0xFFF1F4FC),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                    padding: EdgeInsets.only(bottom: 5),
                    // color:Color(0xffFAF7FA),
                    decoration: BoxDecoration(
                      border: Border(
                        bottom:
                            BorderSide(width: 0.5, color: Color(0xffeeeeee)),
                      ),
                      // color: Color(0xff9FD1FE),
                    ),
                    child: Row(children: <Widget>[
                      Container(
                          width: width / 2,
                          padding: EdgeInsets.only(top: 10),
                          child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                GestureDetector(
                                    onTap: () {
                                      print(6663);
                                      provider.list.clear();
                                      setState(() {
                                        checkIndex = 0;

                                        usedFlag = 1;
                                      });

                                      _onRefresh();
                                    },
                                    child: Container(
                                      padding: EdgeInsets.only(bottom: 6),
                                      decoration: BoxDecoration(
                                        border: Border(
                                          bottom: BorderSide(
                                              width: 2,
                                              color: checkIndex == 0
                                                  ? Color(0xff02A0FD)
                                                  : Colors.white),
                                        ),
                                        // color: Color(0xff9FD1FE),
                                      ),
                                      child: Text(
                                        '可使用',
                                        style: TextStyle(
                                          color: checkIndex == 0
                                              ? Color(0xff02A0FD)
                                              : Color(0xff666666),
                                          // fontSize:ScreenUtil().setSp(14)
                                        ),
                                        textAlign: TextAlign.center,
                                      ),
                                    ))
                              ])),
                      Container(
                          width: width / 2,
                          padding:
                              EdgeInsets.only(top: ScreenUtil().setWidth(10)),
                          child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                GestureDetector(
                                    onTap: () {
                                      // Provider.of<BaseListProvider<Records>>(context,listen: false).list.clear();
                                      provider.list.clear();
                                      setState(() {
                                        checkIndex = 1;

                                        usedFlag = 0;
                                      });

                                      _onRefresh();
                                    },
                                    child: Container(
                                      padding: EdgeInsets.only(bottom: 6),
                                      decoration: BoxDecoration(
                                        border: Border(
                                          bottom: BorderSide(
                                              width: 2,
                                              color: checkIndex == 1
                                                  ? Color(0xff02A0FD)
                                                  : Colors.transparent),
                                        ),
                                      ),
                                      child: Text(
                                        '已失效',
                                        style: TextStyle(
                                          color: checkIndex == 1
                                              ? Color(0xff02A0FD)
                                              : Color(0xff666666),
                                          // fontSize:ScreenUtil().setSp(14)
                                        ),
                                        textAlign: TextAlign.center,
                                      ),
                                    ))
                              ])),
                    ])),
                Expanded(
                    flex: 1,
                    child: Consumer<BaseListProvider<Records>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('coupon_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        loadMore: _loadMore,
                        pageSize: 8,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return Container(
                            child: Column(
                              children: <Widget>[
                                Container(
                                    child: Row(children: <Widget>[
                                  Container(
                                    padding: EdgeInsets.only(
                                      top: 10,
                                    ),
                                    child: Stack(
                                      children: <Widget>[
                                        Positioned(
                                            top: 0,
                                            left: -10,
                                            // right: -30,
                                            child: Container(
                                              width: width + 10,
                                              height: 122,
                                              child: LoadAssetImage(
                                                // image: AssetImage(i['img']),
                                                checkIndex == 0
                                                    ? "wode/bg_front"
                                                    : "wode/bg_back",
                                                fit: BoxFit.fill,
                                                // alignment: Alignment.centerLeft,
                                              ),
                                            )),
                                        Container(
                                            // decoration: BoxDecoration(
                                            //   border:Border.all(
                                            //     width:2,
                                            //     color:Colors.red
                                            //   )
                                            // ),
                                            width: width,
                                            height: 120,
                                            child: Row(
                                              children: <Widget>[
                                                Container(
                                                  padding: EdgeInsets.only(
                                                      right: 10, left: 15),
                                                  child: Column(
                                                      crossAxisAlignment:
                                                          CrossAxisAlignment
                                                              .start,
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .center,
                                                      children: <Widget>[
                                                        Container(
                                                          width: 120,
                                                          child: Row(
                                                            crossAxisAlignment:
                                                                CrossAxisAlignment
                                                                    .end,
                                                            mainAxisAlignment:
                                                                MainAxisAlignment
                                                                    .center,
                                                            children: <Widget>[
                                                              Text(
                                                                '￥',
                                                                textAlign:
                                                                    TextAlign
                                                                        .center,
                                                                style: TextStyle(
                                                                    fontSize: ScreenUtil()
                                                                        .setSp(
                                                                            16),
                                                                    color: Colors
                                                                        .white),
                                                              ),
                                                              Text(
                                                                // '999.0',
                                                                provider
                                                                    .list[index]
                                                                    .coupon
                                                                    ?.discount
                                                                    ?.toString(),
                                                                textAlign:
                                                                    TextAlign
                                                                        .left,
                                                                style: TextStyle(
                                                                    fontSize: ScreenUtil()
                                                                        .setSp(
                                                                            28),
                                                                    color: Colors
                                                                        .white,
                                                                    fontWeight:
                                                                        FontWeight
                                                                            .bold),
                                                              ),
                                                              SizedBox(
                                                                width: 10,
                                                              )
                                                            ],
                                                          ),
                                                        ),
                                                        Container(
                                                          margin:
                                                              EdgeInsets.only(
                                                                  left: 30),
                                                          padding: EdgeInsets
                                                              .fromLTRB(
                                                                  5, 1, 5, 1),
                                                          child: Text(
                                                            // getScenario(provider
                                                            //         .list[index]
                                                            //         .coupon
                                                            //         ?.scenario ??
                                                            //     0),
                                                            '代金劵',
                                                            textAlign: TextAlign
                                                                .center,
                                                            style: TextStyle(
                                                                fontSize:
                                                                    ScreenUtil()
                                                                        .setSp(
                                                                            11),
                                                                color: Colors
                                                                    .white),
                                                          ),
                                                          decoration:
                                                              new BoxDecoration(
                                                            //背景
                                                            // color: Colors.white,
                                                            //设置四周圆角 角度
                                                            borderRadius:
                                                                BorderRadius
                                                                    .all(Radius
                                                                        .circular(
                                                                            4.0)),
                                                            //设置四周边框
                                                            border: Border.all(
                                                                width: 1,
                                                                color: Colors
                                                                    .white),
                                                          ),
                                                        ),
                                                      ]),
                                                ),
                                                Container(
                                                    padding: EdgeInsets.only(
                                                        top: 30),
                                                    child: Column(
                                                        crossAxisAlignment:
                                                            CrossAxisAlignment
                                                                .start,
                                                        children: <Widget>[
                                                          Text(
                                                            '${getScenario(provider.list[index].coupon?.scenario ?? 0)}${provider.list[index].coupon?.name ?? ""}',
                                                            textAlign:
                                                                TextAlign.left,
                                                            style: TextStyle(
                                                              fontSize:
                                                                  ScreenUtil()
                                                                      .setSp(
                                                                          17),
                                                              color: Color(
                                                                  0xff333333),
                                                            ),
                                                          ),
                                                          Text(
                                                            '有效时间: ' +
                                                                DateUtils.instance.getFormartData(
                                                                    timeSamp: provider
                                                                        .list[
                                                                            index]
                                                                        .coupon
                                                                        ?.validity,
                                                                    format:
                                                                        "yyyy.MM.dd HH:mm"),
                                                            textAlign:
                                                                TextAlign.left,
                                                            style: TextStyle(
                                                              fontSize:
                                                                  ScreenUtil()
                                                                      .setSp(
                                                                          13),
                                                              color: Color(
                                                                  0xff9F9C9F),
                                                            ),
                                                          ),
                                                          SizedBox(height: 20),
                                                          Container(
                                                            width: width * 0.5,
                                                            height: 0.5,
                                                            color: Color(
                                                                0xffeeeeee),
                                                            child: null,
                                                          ),
                                                          SizedBox(height: 3),
                                                          Text(
                                                            provider
                                                                    .list[index]
                                                                    .coupon
                                                                    ?.remark ??
                                                                "",
                                                            textAlign:
                                                                TextAlign.left,
                                                            style: TextStyle(
                                                              fontSize:
                                                                  ScreenUtil()
                                                                      .setSp(
                                                                          13),
                                                              color: Color(
                                                                  0xff9F9C9F),
                                                            ),
                                                          ),
                                                        ])),
                                              ],
                                            )),

                                        // Positioned(
                                        //     top: 0,
                                        //     right: 5,
                                        //     child: checkIndex == 1
                                        //         ? Container(
                                        //             child: Icon(
                                        //               const IconData(0xe6d9,
                                        //                   fontFamily: "myfont"),
                                        //               size: 50.0,
                                        //               color: Color(0xff999999),
                                        //             ),
                                        //           )
                                        //         : Container(child: null))
                                      ],
                                    ),
                                  ),
                                  Container()
                                ]))
                              ],
                            ),
                          );
                        },
                      );
                    }))
              ],
            ),
          ),
        ));
  }
}
