import 'dart:async';

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

import '../../bean/company_bean.dart';
import '../../bean/device_list_bean.dart';
import '../../bean/update_bean.dart';

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

  ///机房列表
  Future<void> roomList(pageNum, pageSize, deptId) async {
    RequestCenter.instance.requestGet(NetworkConfig.roomList, {
      "pageNum": pageNum,
      "pageSize": pageSize,
      "deptId": deptId,
    }, (dataEntity) {
      if (dataEntity.code == 200) {
        RoomListBean roomListBean = RoomListBean.fromJson(dataEntity.data);

        streamController.sink.add({
          'code': "roomList", //有数据
          'data': roomListBean,
        });
      }
    }, (errorEntity) {
      print("errorEntity==${errorEntity.msg}");
    });
  }

  ///公司列表
  Future<void> getCompany() async {
    RequestCenter.instance.requestGet(NetworkConfig.getCompany, {}, (dataEntity) {
      if (dataEntity.code == 200) {
        List<CompanyBean> data = (dataEntity.data as List<dynamic>).map((e) => CompanyBean.fromJson(e as Map<String, dynamic>)).toList();

        streamController.sink.add({
          'code': "getCompany", //有数据
          'data': data,
        });
      }
    }, (errorEntity) {
      print("errorEntity==${errorEntity.msg}");
    });
  }

  ///设备列表
  Future<void> odfPortsUnitType() async {
    RequestCenter.instance.requestGet(NetworkConfig.odfPortsUnitType, {}, (dataEntity) {
      if (dataEntity.code == 200) {
        List<DeviceListBean> data = (dataEntity.data as List<dynamic>).map((e) => DeviceListBean.fromJson(e as Map<String, dynamic>)).toList();

        for (var element in data) {
          NetworkConfig.diverItems.add("${element.dictValue}");
        }
      }
    }, (errorEntity) {
      print("errorEntity==${errorEntity.msg}");
    });
  }

  ///业务类型
  Future<void> odfPortsBusinessType() async {
    RequestCenter.instance.requestGet(NetworkConfig.odfPortsBusinessType, {}, (dataEntity) {
      if (dataEntity.code == 200) {
        List<DeviceListBean> data = (dataEntity.data as List<dynamic>).map((e) => DeviceListBean.fromJson(e as Map<String, dynamic>)).toList();

        for (var element in data) {
          NetworkConfig.businessItems.add("${element.dictValue}");
        }
      }
    }, (errorEntity) {
      print("errorEntity==${errorEntity.msg}");
    });
  }

  ///业务类型
  Future<void> checkAppVersion(version) async {
    RequestCenter.instance.requestGet(NetworkConfig.checkAppVersion, {"version": version}, (dataEntity) {
      if (dataEntity.code == 200) {
        UpdateBean updateBean = UpdateBean.fromJson(dataEntity.data);

        streamController.sink.add({
          'code': "checkAppVersion", //有数据
          'data': updateBean,
        });
      }
    }, (errorEntity) {
      print("errorEntity==${errorEntity.msg}");
    });
  }
}
