import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/bbs/bbs_router.dart';
import 'package:liftmanager/internal/bbs/model/mix_model.dart';
import 'package:liftmanager/internal/bbs/presenter/mix_search_list_presenter.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';

class SearchIndex extends StatefulWidget {
  
  SearchIndex(this.type,this.text);

  final String type;
  final String text;
  
  @override
  SearchIndexState createState() => SearchIndexState();
}

class SearchIndexState extends BasePageState<SearchIndex,MixListPresenter> {
  // NewsDetailItem item = NewsDetailItem();
  BaseListProvider<Records> provider = BaseListProvider<Records>();

  
  int _page = 1;


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



  @override
  Widget build(BuildContext context) {

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

    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "搜索列表",
      ),
      body:Container(
        child: Stack(
          children: <Widget>[  
            
            Container(
              child: ChangeNotifierProvider<BaseListProvider<Records>>(
                create: (_) => provider,
                child:  Column(
                  children: <Widget>[
                    Expanded(
                    flex: 1,
                    child: Consumer<BaseListProvider<Records>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('mix_search_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        loadMore: _loadMore,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return Column(
                            children: <Widget>[
                              widget.type == "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: 
                              Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Text(
                                      "[${provider.list[index].brandName}]${provider.list[index].title}",
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                          fontSize: ScreenUtil().setSp(15)),
                                    ),
                                    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,
                                                  // fit: BoxFit.fitWidth,
                                                  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(
                                                  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),
                                                      ),
                                                    ),
                                                  );
                                                }).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);
                              NavigatorUtils.push(context,
                                  "${BbsRouter.questionDetail}?id=${provider.list[index].id.toString()}");
                            },
                          ):Container(child:null),
                          widget.type == "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: 
                              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
                                        ),
                                        Text(
                                          "${provider.list[index].likeNum??"0"}人点赞",
                                          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,
                                            ),
                                          ),
                                        ),
                                        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: () {
                              NavigatorUtils.push(context,
                                "${WodeRouter.videoDetail}?id=${provider.list[index].id.toString()}");
                            },
                          )
                          :Container(child:null),
                          widget.type == "4"?
                          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>[
                                            Text(
                                              provider.list[index].job ?? '',
                                              textAlign: TextAlign.left,
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(17),
                                                color: Color(0xff333333),
                                              ),
                                            ),
                                            Text(
                                              // 'jiage',
                                              "￥" +
                                                  provider
                                                      .list[index].lowerSalary
                                                      .toString() +
                                                  "-" +
                                                  provider
                                                      .list[index].upperSalary
                                                      .toString(),
                                              textAlign: TextAlign.left,
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(17),
                                                color: Color(0xffff0000),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: <Widget>[
                                          Text(
                                            provider.list[index].company?.name ??
                                                '',
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(14),
                                              color: Color(0xff999999),
                                            ),
                                          ),
                                          Text(
                                            provider.list[index].address ?? '',
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(14),
                                              color: Color(0xff999999),
                                            ),
                                          ),
                                        ],
                                      ),
                                      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(
                                            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: () {
                                NavigatorUtils.push(context,
                                    "${BbsRouter.positionDetail}?id=${provider.list[index].id.toString()}");
                              },
                            )
                          :Container(child:null),
                          widget.type == "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,
                                          width: ScreenUtil().setWidth(90),
                                          height: ScreenUtil().setWidth(90),
                                        ),
                                      ),
                                    ),
                                    Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Text(
                                          provider.list[index].name??"",
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(15),
                                            color: Color(0xff333333),
                                          ),
                                        ),
                                        Text(
                                          provider.list[index].brandId
                                              .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);
                              NavigatorUtils.push(context,
                                  "${BbsRouter.productDetail}?id=${provider.list[index].id.toString()}");
                            },
                          )
                          :Container(child:null),
                            ],
                          );
                          
                        },
                      );
                    }))
                  ],
                )
              
            ))
          ],
        )
      )
    );
  }

  Future _onRefresh() async {
    _page = 1;
    if(widget.type=="1"){
      await presenter.getQuestionList(_page,widget.text);
    }
    else if(widget.type=="2"){
      await presenter.getVideoList(_page,widget.text);
    }
    else if(widget.type=="3"){
      await presenter.getShopList(_page,widget.text);
    }
    else if(widget.type=="4"){
      await presenter.getPositionList(_page,widget.text);
    }
    
  }

  Future _loadMore() async {
    _page++;
    if(widget.type=="1"){
      await presenter.getQuestionList(_page,widget.text);
    }
    else if(widget.type=="2"){
      await presenter.getVideoList(_page,widget.text);
    }
    else if(widget.type=="3"){
      await presenter.getShopList(_page,widget.text);
    }
    else if(widget.type=="4"){
      await presenter.getPositionList(_page,widget.text);
    }
    
  }

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

}

