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:liftmanager/widgets/bbs_content.dart';
import 'package:liftmanager/internal/wode/page/video/my_video.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:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/utils/url.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'dart:convert';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'package:liftmanager/utils/fast_notification.dart';
import 'package:liftmanager/internal/bbs/model/mix_model.dart';
import 'package:liftmanager/internal/bbs/presenter/mix_list_presenter.dart';
import 'package:liftmanager/common/common.dart';
import 'package:flustars/flustars.dart' as flustars;

class CollectPage extends StatefulWidget {
  
  String index = "1";
  
  @override
  CollectPageState createState() => CollectPageState();
}

class CollectPageState extends BasePageState<CollectPage,MixListPresenter> {
  // NewsDetailItem item = NewsDetailItem();
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  int checkIndex = 0;
  
  int _page = 1;
  List<String> titleList = [
    // "全部",
    "问答",
    "视频",
    "职位",
    "商品",
  ];

  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
    FastNotification.addListener("collectAction",(collectInit){
      if(mounted){
        setState(() {
        
        
        });
        _onRefresh();
        }
      
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print(12366);
  }

  @override
  void deactivate() {
    super.deactivate();
    print(123667);
  }


  @override
  Widget build(BuildContext context) {

    double width = MediaQuery.of(context).size.width;

    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "我的收藏",
      ),
      body:Container(
        child: Stack(
          children: <Widget>[  
            Positioned(
              child: Container(
                padding: EdgeInsets.only(bottom:5),
                // color:Color(0xffFAF7FA),
                decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(width: 0.5, color: Color(0xffeeeeee)),
                                ),
                                // color: Color(0xffffffff),
                              ),
                child:Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children:titleList.asMap().keys.map((i){
                    return TabThis(item:titleList[i],tabIndex:i,checkIndex: checkIndex,fun:(){
                      provider.list.clear();
                      setState(() {
                          checkIndex = i;
                        });
                      provider.setStateTypeNotNotify(StateType.loading);
                      _onRefresh();
                      print(checkIndex);
                    });
                  }).toList()
                )
              ),
            ),
            
            Container(
              child: ChangeNotifierProvider<BaseListProvider<Records>>(
                create: (_) => provider,
                child:  Column(
                  children: <Widget>[
                    SizedBox(
                      height:ScreenUtil().setWidth(50)
                    ),
                    Expanded(
                    flex: 1,
                    child: Consumer<BaseListProvider<Records>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('mix_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        loadMore: _loadMore,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              checkIndex == 0?
                          GestureDetector(
                            child: provider.list[index]!=null?Container(
                              padding: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(15),
                                  right: ScreenUtil().setWidth(15),
                                  top: ScreenUtil().setHeight(10),
                                  bottom: ScreenUtil().setHeight(10)),
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                      width: 0.5, color: Colours.line),
                                ),
                              ),
                              child: 
                              Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Text(
                                      "[${provider.list[index].brandName??""}]${provider.list[index].title??""}",
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                          fontSize: ScreenUtil().setSp(15)),
                                          maxLines: 1,
                overflow: TextOverflow.ellipsis,
                                    ),
                                    Container(
                                      padding: EdgeInsets.only(
                                          top: ScreenUtil().setHeight(10),
                                          bottom: ScreenUtil().setHeight(10)),
                                      child: Row(
                                        children: <Widget>[
                                          ClipRRect(
                                              borderRadius:
                                                  BorderRadius.circular(
                                                      ScreenUtil()
                                                          .setWidth(18)),
                                              child: Container(
                                                child: LoadNetworkImage(
                                                  provider.list[index].avatarUrl,
                                                  width:
                                                      ScreenUtil().setWidth(34),
                                                  height:
                                                      ScreenUtil().setWidth(34),
                                                ),
                                              )),
                                          Container(
                                            padding: EdgeInsets.only(
                                                left:
                                                    ScreenUtil().setWidth(10)),
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.start,
                                              children: <Widget>[
                                                Text(
                                                  provider.list[index].userName??"",
                                                  style: TextStyle(
                                                      fontSize: ScreenUtil()
                                                          .setSp(14),
                                                      color: Color(0xff333333)),
                                                ),
                                                Text(
                                                  provider
                                                              .list[index]
                                                              .createTime!=null?DateUtils.instance
                                                      .getFormartData(
                                                          timeSamp: provider
                                                              .list[index]
                                                              .createTime,
                                                          format: "yyyy-MM-dd"):"",
                                                  style: TextStyle(
                                                      fontSize: ScreenUtil()
                                                          .setSp(12),
                                                      color: Color(0xffaaaaaa)),
                                                ),
                                              ],
                                            ),
                                          )
                                        ],
                                      ),
                                    ),
                                    Text(
                                      provider.list[index].expression??"",
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: ScreenUtil().setSp(14),
                                        
                                      ),
                                      maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                                    ),
                                    Container(
                                      padding: EdgeInsets.only(
                                          top: ScreenUtil().setHeight(5),
                                          bottom: ScreenUtil().setHeight(5)),
                                      child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.start,
                                          children: provider.list[index].imgs !=
                                                      null &&
                                                  provider.list[index].imgs
                                                      .isNotEmpty
                                              ? List<Widget>.from(provider
                                                  .list[index].imgs
                                                  .split(",")
                                                  .asMap().keys.map((subindex) {
                                                  // print(item);
                                                  return Container(
                                                    padding:subindex<provider
                                                  .list[index].imgs.split(",").length-1?EdgeInsets.only(right:6):EdgeInsets.only(right:0),
                                                    // color:Colors.red,
                                                    // decoration: BoxDecoration(
                                                    //   borderRadius: BorderRadius.circular(20.0),
                                                    // ),
                                                    child: ClipRRect(
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              10),
                                                      child: LoadNetworkImage(
                                                        provider
                                                  .list[index].imgs.split(",")[subindex],
                                                        // height: width/375*75,
                                                        height: ScreenUtil()
                                                            .setWidth(80),
                                                        width: ScreenUtil()
                                                            .setWidth(110),
                                                        isWater: true,
                                                      ),
                                                    ),
                                                  );
                                                }).toList())
                                              : <Widget>[]),
                                    ),
                                    Text(
                                      "${provider.list[index].likeNum.toString()}人赞",
                                      textAlign: TextAlign.right,
                                      style: TextStyle(
                                        color: Color(0xff999999),
                                        fontSize: ScreenUtil().setSp(14),
                                      ),
                                    )
                                  ]),
                            ):Center(child: Text("加载中..."),),
                            onTap: () {
                              // print(item);
                              if(provider.list[index].statuz == 1){
                                  NavigatorUtils.push(context,
                                  "${BbsRouter.questionDetail}?id=${provider.list[index].id.toString()}");
                                }else {
                                  toasts("已下架");
                                }
                              
                            },
                          ):Container(child:null),
                          checkIndex == 1?
                          GestureDetector(
                            child: provider.list[index]!=null?
                            Container(
                              padding: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(15),
                                  // right: ScreenUtil().setWidth(15),
                                  top: ScreenUtil().setHeight(10),
                                  bottom: ScreenUtil().setHeight(10)),
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                      width: 0.5, color: Colours.line),
                                ),
                              ),
                              child: 
                              Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  Container(
                                    padding: EdgeInsets.only(top:ScreenUtil().setWidth(8),bottom:ScreenUtil().setWidth(8)),
                                    height: width*0.24,
                                    child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    Container(
                                      width:width*0.5,
                                      child: Text(
                                      "${provider.list[index].title??""}",
                                      style: TextStyle(
                                          fontWeight: FontWeight.w600,
                                          fontSize: ScreenUtil().setSp(17)),
                                      textAlign: TextAlign.start,
                                      maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                    ),
                                    ),
                                    Container(
                                      width:width*0.5,
                                      child: Text(
                                      "${provider.list[index].descr??""}",
                                      style: TextStyle(
                                          fontSize: ScreenUtil().setSp(15)),
                                      textAlign: TextAlign.start,
                                      maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                    ),
                                    ),
                                    Container(
                                      width: width*0.5,
                                      child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: <Widget>[
                                        Container(
                                          width:width*0.18,
                                          child: Text(
                                            "${provider.list[index].brandName??""}",
                                            style: TextStyle(
                                                color: Color(0xff999999),
                                                fontSize: ScreenUtil().setSp(14)),
                                            textAlign: TextAlign.start,
                                            maxLines: 1,
                                                overflow: TextOverflow.ellipsis,
                                          ),
                                        ),
                                        SizedBox(
                                          width:5
                                        ),
                                        Row(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Text(
                                              "${provider.list[index].likeNum??"0"}",
                                              style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize: ScreenUtil().setSp(14)),
                                              textAlign: TextAlign.start,
                                            ),
                                            SizedBox(
                                              width:3
                                            ),
                                            Icon(
                                              IconData(0xe7cd,
                                                  fontFamily: "myfont"),
                                              size: 14.0,
                                              color: Color(0xff999999),
                                            ),
                                          ],
                                        ),
                                        
                                        SizedBox(
                                          width:5
                                        ),
                                        Row(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Text(
                                              "${provider.list[index].browseNum??"0"}",
                                              style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize: ScreenUtil().setSp(14)),
                                              textAlign: TextAlign.start,
                                            ),
                                            SizedBox(
                                              width:3
                                            ),
                                            Icon(
                                              IconData(0xe610,
                                                  fontFamily: "myfont"),
                                              size: 14.0,
                                              color: Color(0xff999999),
                                            ),
                                          ],
                                        ),
                                        
                                        
                                      ],
                                    ),
                                    )
                                  ]),
                                  ),
                                  Container(
                                    padding: EdgeInsets.only(right:10),
                                        child: Stack(
                                      children: <Widget>[
                                        ClipRRect(
                                          borderRadius:
                                              BorderRadius.circular(1),
                                          child: Container(
                                            child: LoadNetworkImage(
                                              // imgFontUrl +
                                              //     provider.list[index].cover,
                                              provider.list[index].cover,
                                              width: width*0.38,
                                              height: width*0.24,
                                              isWater: true,
                                            ),
                                          ),
                                        ),
                                        Positioned(
                                          left: width*0.17,
                                          top: width*0.08,
                                          child: Icon(
                                            IconData(0xe607,
                                                fontFamily: "myfont"),
                                            size: 26.0,
                                            color: Colors.white,
                                          ),
                                        )
                                      ],
                                    )),
                                ],
                              )
                              
                            )
                            :Center(child:Text("加载中...")),
                            onTap: () {
                              if(provider.list[index].statuz == 1){
                                  NavigatorUtils.push(context,
                                "${BbsRouter.videoDetail}?id=${provider.list[index].id.toString()}");
                                }else {
                                  toasts("已下架");
                                }
                              
                            },
                          )
                          :Container(child:null),
                          checkIndex == 2?
                          GestureDetector(
                              child: provider.list[index]!=null?Container(
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(15),
                                    right: ScreenUtil().setWidth(15),
                                    top: ScreenUtil().setHeight(10),
                                    bottom: ScreenUtil().setHeight(10)),
                                decoration: BoxDecoration(
                                  border: Border(
                                    bottom: BorderSide(
                                        width: 0.5, color: Colours.line),
                                  ),
                                ),
                                child: Column(
                                    // crossAxisAlignment: CrossAxisAlignment.start,

                                    children: <Widget>[
                                      Container(
                                        padding: EdgeInsets.only(
                                            bottom: ScreenUtil().setHeight(5)),
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: <Widget>[
                                            Container(
                                              width: width*0.4,
                                              child: Text(
                                              provider.list[index].job ?? '',
                                              textAlign: TextAlign.left,
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(17),
                                                color: Color(0xff333333),
                                              ),
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                            ),
                                            Container(
                                              width:width*0.4,
                                              child:Text(
                                              // 'jiage',
                                              "￥" +
                                                  provider
                                                      .list[index].lowerSalary
                                                      .toString() +
                                                  "-" +
                                                  provider
                                                      .list[index].upperSalary
                                                      .toString(),
                                              textAlign: TextAlign.right,
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(17),
                                                color: Color(0xffff0000),
                                              ),
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                            )
                                          ],
                                        ),
                                      ),
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: <Widget>[
                                          Container(
                                            width: width*0.5,
                                            child: Text(
                                              provider.list[index].companyName!=null ?provider.list[index].companyName:'',
                                              textAlign: TextAlign.left,
                                              overflow: TextOverflow.ellipsis,
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(14),
                                                color: Color(0xff999999),
                                              ),
                                            ),
                                          ),
                                          provider.list[index]
                                                          .provinceName !=
                                                      null?Container(
                                            width: width*0.35,
                                            child: Text(
                                              (provider.list[index]
                                                          .provinceName !=
                                                      null
                                                  ? provider
                                                      .list[index].provinceName
                                                  : '') +
                                                      "-" +
                                                      (provider.list[index]
                                                          .cityName !=
                                                      null
                                                  ? provider
                                                      .list[index].cityName
                                                  : ''),
                                              textAlign: TextAlign.right,
                                              overflow: TextOverflow.ellipsis,
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(14),
                                                color: Color(0xff999999),
                                              ),
                                            ),
                                          ):Container(child:null),
                                        ],
                                      ),
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: <Widget>[
                                          Container(
                                            width: width * 0.65,
                                            child: Text(
                                              provider.list[index].info ?? '',
                                              textAlign: TextAlign.left,
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(14),
                                                color: Color(0xff999999),
                                              ),
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                          ),
                                          Text(
                                            provider
                                                    .list[index].createTime!=null?DateUtils.instance.getFormartData(
                                                timeSamp: provider
                                                    .list[index].createTime,
                                                format: "yyyy-MM-dd"):"",
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(14),
                                              color: Color(0xff999999),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ]),
                              ):Center(child: Text("加载中..."),),
                              onTap: () {
                                
                                if(provider.list[index].statuz == 1){
                                  NavigatorUtils.push(context,
                                    "${BbsRouter.positionDetail}?id=${provider.list[index].id.toString()}");
                                }else {
                                  toasts("已下架");
                                }
                              },
                            )
                          :Container(child:null),
                          checkIndex == 3?
                          InkWell(
                            child: provider.list[index]!=null?Container(
                              padding: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(15),
                                  right: ScreenUtil().setWidth(15),
                                  top: ScreenUtil().setHeight(10),
                                  bottom: ScreenUtil().setHeight(10)),
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                      width: 0.5, color: Colours.line),
                                ),
                              ),
                              child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    ClipRRect(
                                      borderRadius: BorderRadius.circular(5),
                                      child: Container(
                                        padding: EdgeInsets.only(
                                            right: ScreenUtil().setWidth(10)),
                                        child: LoadNetworkImage(
                                          provider.list[index].imgs.split(",")[0], 
                                          width: ScreenUtil().setWidth(90),
                                          height: ScreenUtil().setWidth(90),
                                          isWater: true,
                                        ),
                                      ),
                                    ),
                                    Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Text(
                                          provider.list[index].sname??"",
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(15),
                                            color: Color(0xff333333),
                                          ),
                                        ),
                                        Text(
                                          provider.list[index].bname??""
                                              .toString(),
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(14),
                                            color: Color(0xff666666),
                                          ),
                                        ),
                                        Text(
                                          "供应商：${provider.list[index].manufacturer}",
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(14),
                                            color: Color(0xff666666),
                                          ),
                                        ),
                                        Text(
                                          "Tel：${provider.list[index].telephone}",
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(14),
                                            color: Color(0xff666666),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ]),
                            ):Center(child: Text("加载中..."),),
                            onTap: () {
                              print(index);
                              if(provider.list[index].statuz == 1){
                                  NavigatorUtils.push(context,
                                  "${BbsRouter.productDetail}?id=${provider.list[index].id.toString()}");
                                }else {
                                  toasts("已下架");
                                }
                              
                            },
                          )
                          :Container(child:null),
                            ],
                          );
                          
                        },
                      );
                    }))
                  ],
                )
              
            ))
          ],
        )
      )
    );
  }

  Future _onRefresh() async {
    _page = 1;
    if(checkIndex==0){
      await presenter.getQuestionList(_page);
    }
    else if(checkIndex==1){
      await presenter.getVideoList(_page);
    }
    else if(checkIndex==2){
      await presenter.getPositionList(_page);
    }
    else if(checkIndex==3){
      await presenter.getShopList(_page);
    }
    
  }

  Future _loadMore() async {
    _page++;
    if(checkIndex==0){
      await presenter.getQuestionList(_page);
    }else if(checkIndex==1){
      await presenter.getVideoList(_page);
    }
    else if(checkIndex==2){
      await presenter.getPositionList(_page);
    }
    else if(checkIndex==3){
      await presenter.getShopList(_page);
    }
    
  }

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

}



class TabThis extends StatelessWidget {
  TabThis({Key key,this.item,this.tabIndex,this.fun,this.checkIndex}) : super(key: key);
  String item;
  int tabIndex;
  int checkIndex;
  Function fun;
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top:ScreenUtil().setWidth(10)),
      child: Container(
                      child:Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children:<Widget>[
                          GestureDetector(
                            onTap: (){
                              fun();
                            },
                            child: Container(
                              padding: EdgeInsets.only(bottom:6),
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(width: 2, color: checkIndex==tabIndex? Color(0xff02A0FD):Colors.transparent),
                                ),
                                // color: Color(0xff9FD1FE),
                              ),
                              child:Text(
                                              item,
                                              style: TextStyle(
                                                color:checkIndex==tabIndex? Color(0xff02A0FD):Color(0xff666666),
                                                fontSize:ScreenUtil().setSp(14)
                                              ),
                                              textAlign:TextAlign.center,
                                            ),
                            )
                          )
                        ]
                      )
                    ),
    );
  }
}
