import 'dart:async';

import 'package:odf/bean/details_bean.dart';
import 'package:odf/bean/odf_details_bean.dart';
import 'package:odf/bean/racks_bean.dart';
import 'package:odf/network/NetworkConfig.dart';
import 'package:odf/network/RequestCenter.dart';

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

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

  ///地区列表
  Future<void> getRegion(deptId) async {
    RequestCenter.instance.requestGet(NetworkConfig.getRegion, {
      "deptId": deptId,
    }, (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': "getRegion", //有数据
          'data': data,
        });
      } else {
        streamController.sink.add({
          'code': "-1", //有数据
          'data': dataEntity.msg
        });
      }
    }, (errorEntity) {
      print("errorEntity==${errorEntity.msg}");
    });
  }

  ///机架列表
  Future<void> racksList(pageNum, pageSize, roomId) async {
    RequestCenter.instance.requestGet(NetworkConfig.racksList, {
      "pageNum": pageNum,
      "pageSize": pageSize,
      "roomId": roomId,
    }, (dataEntity) {
      if (dataEntity.code == 200) {
        RacksBean racksBean = RacksBean.fromJson(dataEntity.data);

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

  ///机架列表详情
  Future<void> mList(rackId) async {
    RequestCenter.instance.requestGet(NetworkConfig.mList, {
      "RackId": rackId,
    }, (dataEntity) {
      if (dataEntity.code == 200) {
        List<DetailsBean> data = (dataEntity.data as List<dynamic>).map((e) => DetailsBean.fromJson(e as Map<String, dynamic>)).toList();

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

  ///机架接口详情
  Future<void> odfDetails(id) async {
    RequestCenter.instance.requestGet(NetworkConfig.odfDetails + id, {}, (dataEntity) {
      if (dataEntity.code == 200) {
        OdfDetailsBean odfDetailsBean = OdfDetailsBean.fromJson(dataEntity.data);
        streamController.sink.add({
          'code': "odfDetails", //有数据
          'data': odfDetailsBean,
        });
      } else {
        streamController.sink.add({
          'code': "-1", //有数据
          'data': dataEntity.msg
        });
      }
    }, (errorEntity) {
      print("errorEntity==${errorEntity.msg}");
    });
  }

  ///机架接口信息保存
  Future<void> save(id, status, remarks, opticalAttenuation, historyRemarks, historyFault, opticalCableOffRemarks) async {
    RequestCenter.instance.request(NetworkConfig.save, {
      "Id": id,
      "Status": status,
      "Remarks": remarks,
      "OpticalAttenuation": opticalAttenuation,
      "HistoryRemarks": historyRemarks,
      "HistoryFault": historyFault,
      "OpticalCableOffRemarks": opticalCableOffRemarks,
    }, (dataEntity) {
      if (dataEntity.code == 200) {
        // OdfDetailsBean odfDetailsBean = OdfDetailsBean.fromJson(dataEntity.data);

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