import 'package:flutter/material.dart';
import 'package:liftmanager/internal/wode/wode_router.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/widgets/bbs_content.dart';
import 'package:liftmanager/internal/bbs/presenter/video_list_presenter.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/bbs/model/mix_model.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 'package:liftmanager/mvp/base_page_state.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/common/common.dart';
import 'package:flustars/flustars.dart' as flustars;
import 'package:liftmanager/utils/fast_notification.dart';

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

  // final String index;
  

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

class MyVideoState extends BasePageState<MyVideo,MyVideoListPresenter> 
with AutomaticKeepAliveClientMixin{
  // NewsDetailItem item = NewsDetailItem();
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  ScrollController _scrollController = new ScrollController();

  int _page = 1;
  String searchWord;
  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
    print(66666);

    FastNotification.addListener("initMyVideo",(initThisMyVideo){
      if(mounted){
        _onRefresh();
      }
    });
    
  }

  getCheckFlag(checkFlag){
    String str;
    if(checkFlag == 0){
      str = "已驳回";
    }else if (checkFlag == 1){
      str = "审核通过";
    }else if (checkFlag == 2){
      str = "待审核";
    }
    return str;
  }

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

  @override
  Widget build(BuildContext context) {

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

    return 
    ChangeNotifierProvider<BaseListProvider<Records>>(
        create: (_) => provider,
        child: Scaffold(
            appBar: SearchAppBar2(
              searchWidth:width*0.7,
              onPressed: (text) {
                searchWord = text;
                _onRefresh();
              },
              actions: <Widget>[
                FlatButton(
                  child: Text(
                    "上传",
                    style: TextStyle(
                        color: Colors.white, fontSize: ScreenUtil().setSp(15)),
                  ),
                  textColor: Colours.dark_text,
                  highlightColor: Colors.transparent,
                  onPressed: () {
                    // NavigatorUtils.push(context, BbsRouter.videoUpload);
                    NavigatorUtils.push(context, "${WodeRouter.videoUpload}?id=");
                  },
                )
              ],
            ),
            body: Container(
              child:Stack(
                children: <Widget>[
                  Column(
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: Consumer<BaseListProvider<Records>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('video_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        pageSize:10,
                        loadMore: _loadMore,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return GestureDetector(
                            child: 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(
                                    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,
                                          color: Color(0xff222222),
                                          fontSize: ScreenUtil().setSp(17)),
                                      textAlign: TextAlign.start,
                                      maxLines: 2,
                                            overflow: TextOverflow.ellipsis,
                                    ),
                                    ),
                                    Row(
                                      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
                                        ),
                                        Container(
                                          width: width*0.3,
                                          child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: <Widget>[
                                            Container(
                                              width: width*0.15,
                                              child: Text(
                                                "${provider.list[index].likeNum??"0"}人点赞",
                                                style: TextStyle(
                                                    color: Color(0xff999999),
                                                    fontSize: ScreenUtil().setSp(14)),
                                                textAlign: TextAlign.start,
                                                maxLines: 1,
                                                overflow: TextOverflow.ellipsis,
                                              ),
                                            ),
                                        
                                        Text(
                                          provider.list[index].checkFlag!=null?getCheckFlag(provider.list[index].checkFlag):"",
                                          style: TextStyle(
                                              color: Color(0xff999999),
                                              fontSize: ScreenUtil().setSp(14)),
                                          textAlign: TextAlign.start,
                                        ),
                                          ],
                                        ),
                                        )
                                      ],
                                    )
                                  ]),
                                  ),
                                  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,
                                          ),
                                        )
                                      ],
                                    )),
                                ],
                              )
                              
                            ),
                            onTap: () {
                              NavigatorUtils.push(context,
                                "${WodeRouter.videoDetail}?id=${provider.list[index].id.toString()}");
                            },
                          );
                        },
                      );
                    }))
              ],
            ),
                ],
              )
            )
            )
            );
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getVideoList( _page,int.parse(flustars.SpUtil.getString(Constant.userId)),searchWord);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getVideoList(_page,int.parse(flustars.SpUtil.getString(Constant.userId)),searchWord);
  }

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

  bool get wantKeepAlive => true;
}





