import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/search/search_router.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/bbs_content.dart';
import 'package:liftmanager/widgets/search_app_bar.dart';

class SearchPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SearchPageState();
  }
}

class SearchPageState extends State<SearchPage> {
  String searchText;
  bool _hasData = false;
  String noText = "";
  List<dynamic> searchListOne = [];
  List<dynamic> searchListTwo = [];
  List<dynamic> searchListThree = [];
  List<dynamic> searchListFour = [];

  Future getSearchList(text) async {
    await NewApiService().getSearchIndex(text, onSuccess: (res) {
      //Uri.encodeComponent(_keyword)
      // res.records.forEach((item){
      //   questionsList.addAll(item);
      // });
      if(res.length>0){
        res.forEach((item){
        if(item.code=="1"){
          searchListOne = item.infoList.take(2).toList();
        }
        else if(item.code=="2"){
          searchListTwo = item.infoList.take(3).toList();
        }
        else if(item.code=="3"){
          searchListThree = item.infoList.take(2).toList();
        }
        else if(item.code=="4"){
          searchListFour = item.infoList.take(3).toList();
        }
      });
      print(JsonEncoder().convert(res));
      print(JsonEncoder().convert(searchListOne));
      print("searchListOne");
      print(JsonEncoder().convert(searchListTwo));
      print("searchListTwo");
      print(JsonEncoder().convert(searchListThree));
      print("searchListThree");
      print(JsonEncoder().convert(searchListFour));
      print(searchListFour.length);
      print("searchListFour");
      print(898988);
      setState(() {});
      _hasData = true;
      }else {
        print(123);
        setState(() {
          noText = "暂无数据";
        });
        print(noText);
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  void initState() {
    /// 默认为加载中状态，本页面场景默认为空
    super.initState();
    searchListOne = [];
    searchListTwo = [];
    searchListThree = [];
    searchListFour = [];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: SearchAppBar(
          hintText: "请输入需要搜索的内容",
          onPressed: (text){
            if (text.isEmpty){
              toasts("搜索关键字不能为空！");
              return;
            }
            FocusScope.of(context).unfocus();
            print(text);
            print(22222);
            searchText = text;
            searchListOne = [];
            searchListTwo = [];
            searchListThree = [];
            searchListFour = [];
            getSearchList(text);
          },
        ),
        body: _hasData?ListView(
          children: <Widget>[
            searchListOne != null && searchListOne.length >=1?LableTitle(
                  title: "问题",
                  userTap: () {
                    
                    NavigatorUtils.push(context, "${SearchRouter.searchIndex}?type=1&text=${Uri.encodeComponent(searchText)}");
                  },
                ):Container(child:null),
                searchListOne != null && searchListOne.length >=1?Container(
                    child: HotQuestion(initList: searchListOne)
                        ):Container(child:null),
                // Container(height: 5, color: Color(0xffeeeeee)),
                searchListTwo != null && searchListTwo.length >=1?LableTitle(
                  title: "学堂",
                  userTap: () {
                    NavigatorUtils.push(context, "${SearchRouter.searchIndex}?type=2&text=${Uri.encodeComponent(searchText)}");
                  },
                ):Container(child:null),
                searchListTwo != null && searchListTwo.length >=1
                    ? Container(
                      decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(width: 0.5, color: Colours.line),
                          ),
                        ),
                        height: ScreenUtil().setWidth(210),
                        padding:
                            EdgeInsets.only(top: ScreenUtil().setWidth(10)),
                        child: HotClass(initList: searchListTwo),
                      )
                    : Container(child: null),
                // Container(height: 5, color: Color(0xffeeeeee)),
                searchListThree != null && searchListThree.length >=1?LableTitle(
                  title: "商品",
                  userTap: () {
                    NavigatorUtils.push(context, "${SearchRouter.searchIndex}?type=3&text=${Uri.encodeComponent(searchText)}");
                  },
                ):Container(child:null),
                searchListThree != null && searchListThree.length >=1?Container(
                    child:HotProduct(productList: searchListThree)
                        ):Container(child:null),
                // Container(height: 5, color: Color(0xffeeeeee)),
                searchListFour != null && searchListFour.length >=1?LableTitle(
                  title: "职位",
                  userTap: () {
                    NavigatorUtils.push(context, "${SearchRouter.searchIndex}?type=4&text=${Uri.encodeComponent(searchText)}");
                  },
                ):Container(child:null),
               searchListFour != null && searchListFour.length >=1? Container(
                    color: ThemeUtils.getTabsBg(context),
                    padding: EdgeInsets.only(bottom: 20),
                    child:HotPosition(positionList: searchListFour)
                        ):Container(child:null),
          ],
        ):Center(child:Text(noText))
      );
  }
}
