import 'package:flutter/material.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/internal/search/search_router.dart';
import 'package:liftmanager/widgets/app_search_bar.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/internal/bbs/bbs_router.dart';
import 'package:liftmanager/internal/means/means_router.dart';
// import 'package:amap_all_fluttify/amap_all_fluttify.dart';
import 'package:provider/provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:async';
import 'package:liftmanager/widgets/my_card.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/internal/bbs/model/coupon_model_page.dart';
import 'package:liftmanager/internal/wode/presenter/red_list_presenter.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';

import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/utils/log_util.dart';

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

class CouponPageState extends BasePageState<CouponPage,CoupontListPresenter> 
with AutomaticKeepAliveClientMixin{
  // NewsDetailItem item = NewsDetailItem();
  BaseListProvider<Records> provider =
      BaseListProvider<Records>();
  ScrollController _scrollController = new ScrollController();

  int _page = 1;
  int usedFlag = 1;//0失效1可使用
  int checkIndex = 0;
  
  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
    print(66666);
    
  }
  
  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

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

  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",
    },
  ];

  @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:ScreenUtil().setWidth(15),left:ScreenUtil().setWidth(15),right:ScreenUtil().setWidth(15)),
                          child: Stack(
                            children: <Widget>[
                              Positioned(
                                top:0,
                                left:0,
                                child: Container(
                                  width:width-ScreenUtil().setWidth(30),
                                  height:ScreenUtil().setWidth(104),
                                  child:LoadAssetImage(
                                    // image: AssetImage(i['img']),
                                    checkIndex == 0?"wode/bg_front":"wode/bg_back",
                                    fit: BoxFit.cover,
                                    // alignment: Alignment.centerLeft,
                                  ),
                                )
                              ),
                              Container(
                                // decoration: BoxDecoration(
                                //   border:Border.all(
                                //     width:2,
                                //     color:Colors.red
                                //   )
                                // ),
                                width:width-ScreenUtil().setWidth(30),
                                height:ScreenUtil().setWidth(100),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      padding: EdgeInsets.only(right:ScreenUtil().setWidth(40),left:ScreenUtil().setWidth(10)),
                                      child:Column(
                                      crossAxisAlignment: CrossAxisAlignment.center,
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      children:<Widget>[
                                        Row(
                                          crossAxisAlignment: CrossAxisAlignment.center,
                                          children: <Widget>[
                                            Text(
                                                  '￥',
                                                  textAlign: TextAlign.center,
                                                  style: TextStyle(
                                                    fontSize:ScreenUtil().setSp(16),
                                                    color:checkIndex==0?Colors.white:Color(0xff999999),
                                                  ),
                                              ),
                                            Text(
                                                  provider.list[index].coupon?.discount?.toString(),
                                                  textAlign: TextAlign.center,
                                                  style: TextStyle(
                                                    fontSize:ScreenUtil().setSp(28),
                                                    color:checkIndex==0?Colors.white:Color(0xff999999),
                                                  ),
                                              ),
                                          ],
                                        ),
                                        Text(
                                            getScenario(provider.list[index].coupon?.scenario??0),
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize:ScreenUtil().setSp(12),
                                              color:checkIndex==0?Colors.white:Color(0xff999999),
                                            ),
                                        ),
                                      ]
                                    ),
                                    ),
                                    Container(
                                      padding: EdgeInsets.only(top:ScreenUtil().setWidth(10)),
                                      child:Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children:<Widget>[
                                        Text(
                                            provider.list[index].coupon?.name??"",
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize:ScreenUtil().setSp(16),
                                              color:Color(0xff333333),
                                            ),
                                        ),
                                        Text(
                                            DateUtils.instance
                                                      .getFormartData(
                                                          timeSamp: provider.list[index].coupon?.validity,
                                                          format: "yyyy-MM-dd HH:mm:ss")+'过期',
                                            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(
                                        IconData(
                                          0xe6d9,
                                          fontFamily:"myfont"
                                        ),
                                        size: 50.0,
                                        color:Color(0xff999999),
                                      ),
                                    ):Container(child:null)
                              )
                            ],
                          ),
                        ),
                        Container()
                      ]
                    )
                  )
                                
                              ],
                            ),
                          );
                        },
                      );
                    })
                    )
              ],
            ),
          ),
        ));
    
    
  }

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

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

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

  bool get wantKeepAlive => true;
}




