
import 'package:liftmanager/mvp/base_page_presenter.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/net/dio_utils.dart';
import 'package:liftmanager/internal/search/model/search_entity.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:liftmanager/mvp/base_page_presenter.dart';
import 'package:liftmanager/internal/search/page/search_page2.dart';
import 'package:liftmanager/utils/log_util.dart';


class SearchPresenter extends BasePagePresenter<SearchPageState> {

  Future search(String text, int page, bool isShowDialog) async{

    Map<String, String> params = Map();
    params["q"] = text;
    params["page"] = page.toString();
    params["l"] = "Dart";
    await requestNetwork<SearchEntity>(Method.get,
        url: ApiUrl.account_smscode,
        queryParameters: params,
        isShow: isShowDialog,
        onSuccess: (data){
          if(data != null){
            /// 一页30条数据，等于30条认为有下一页
            /// 具体的处理逻辑根据具体的接口情况处理，这部分可以抽离出来
            view.provider.setHasMore(data.items.length == 30);
            if (page == 1){
              /// 刷新
              view.provider.list.clear();
              if (data.items.isEmpty){
                view.provider.setStateType(StateType.empty);
              }else{
                view.provider.addAll(data.items);
              }
            }else{
              view.provider.addAll(data.items);
            }
          }else{
            /// 加载失败
            view.provider.setHasMore(false);
            view.provider.setStateType(StateType.network);
          }
        },
        onError: (_, __){
          /// 加载失败
          view.provider.setHasMore(false);
          view.provider.setStateType(StateType.network);
        }
    );
  }


  
}

// class SearchIndexPresenter extends BasePagePresenter<SearchPageState> {

//   Future getWalletList(int page) async {
//     await NewApiService().getSearchIndex(page,6,1,onSuccess: (res) {
//       LogUtil.d(res);
//       LogUtil.d(666663);
//       view.closeProgress();
//       if (res != null) {
//         view.provider.setHasMore(res.records.length == 6);
//         if(page == 1){
//           view.provider.list.clear();
//           if (res.records.isEmpty) {
//             view.provider.setStateType(StateType.empty);
//           } else {
//             view.provider.addAll(res.records);
//           }
//         }else{
//           view.provider.addAll(res.records);
//         }

//       } else {
//         /// 加载失败
//         view.provider.setHasMore(false);
//         view.provider.setStateType(StateType.empty);
//       }
//     }, onError: (code, msg) {
//       view.closeProgress();
//       view.provider.setHasMore(false);
//       view.provider.setStateType(StateType.network);
//     });
//   }
// }