import 'dart:async';

import 'package:odf/bean/search_bean.dart';
import 'package:odf/network/NetworkConfig.dart';
import 'package:odf/network/RequestCenter.dart';

import '../../bean/new_search_bean.dart';

class SearchModel {
  StreamController streamController = StreamController.broadcast();

  ///搜索
  Future<void> searchDevice(key, pageNum, pageSize) async {
    RequestCenter.instance.requestGet(NetworkConfig.search, {
      "key": key,
      "pageNum": pageNum,
      "pageSize": pageSize,
    }, (dataEntity) {
      if (dataEntity.code == 200) {
        SearchBean searchBean = SearchBean.fromJson(dataEntity.data);

        streamController.sink.add({
          'code': "searchDevice", //有数据
          'data': searchBean,
        });
      } else {
        streamController.sink.add({
          'code': "-1", //有数据
          'data': dataEntity.msg
        });
      }
    }, (errorEntity) {
      print("errorEntity==${errorEntity.msg}");
    });
  }

  ///新搜索
  Future<void> newSearch(key, pageNum, pageSize) async {
    RequestCenter.instance.requestGet(NetworkConfig.newSearch, {
      "key": key,
      "pageNum": pageNum,
      "pageSize": pageSize,
    }, (dataEntity) {
      if (dataEntity.code == 200) {
        NewSearchBean newSearchBean = NewSearchBean.fromJson(dataEntity.data);

        streamController.sink.add({
          'code': "newSearch", //有数据
          'data': newSearchBean,
        });
      } else {
        streamController.sink.add({
          'code': "-1", //有数据
          'data': dataEntity.msg
        });
      }
    }, (errorEntity) {
      print("errorEntity==${errorEntity.msg}");
    });
  }
}
