import 'package:domain/entity/response/device_list_entity.dart';
import 'package:domain/entity/response/fiance_report_entity.dart';
import 'package:domain/entity/response/labour_report_entity.dart';
import 'package:domain/entity/response/staff_request_report_entity.dart';
import 'package:get/get.dart';
import 'package:plugin_platform/platform_export.dart';
import 'package:plugin_platform/http/http_provider.dart';
import 'package:plugin_platform/http/http_result.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';

import '../constants/api_constants.dart';
import '../entity/response/labour_report_v_n_entity.dart';
import '../entity/response/staff_report_v_n_entity.dart';

/// 报表与其他的相关数据
class OtherRepository extends GetxService {
  HttpProvider httpProvider;

  OtherRepository({required this.httpProvider});

  // 雇主的设备列表
  Future<HttpResult<DeviceListEntity>> fetchDeviceList({
    required int curPage,
    String? deviceName,
    String? isOnline,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = "10";

    if (Utils.isNotEmpty(deviceName)) {
      params["device_name"] = deviceName!;
    }
    if (Utils.isNotEmpty(isOnline)) {
      params["is_online"] = isOnline!;
    }

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiDeviceList,
      params: params,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = DeviceListEntity.fromJson(json!);
      //重新赋值data或list
      return result.convert<DeviceListEntity>(data: data);
    }
    return result.convert();
  }

  //财务报表
  Future<HttpResult<FianceReportEntity>> fetchReportFiance(
    String? startDate,
    String? endDate, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (Utils.isNotEmpty(startDate)) {
      params["start_date"] = startDate!;
    }
    if (Utils.isNotEmpty(endDate)) {
      params["end_date"] = endDate!;
    }

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiReportFiance,
      params: params,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = FianceReportEntity.fromJson(json!);
      //重新赋值data或list
      return result.convert<FianceReportEntity>(data: data);
    }
    return result.convert();
  }

  //用工报表
  Future<HttpResult<LabourReportEntity>> fetchReportLabour(
    String? startDate,
    String? endDate, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (Utils.isNotEmpty(startDate)) {
      params["start_date"] = startDate!;
    }
    if (Utils.isNotEmpty(endDate)) {
      params["end_date"] = endDate!;
    }

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiReportLabour,
      params: params,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = LabourReportEntity.fromJson(json!);
      //重新赋值data或list
      return result.convert<LabourReportEntity>(data: data);
    }
    return result.convert();
  }

  //员工请求报表
  Future<HttpResult<StaffRequestReportEntity?>> fetchReportStaffRequest(
    String? startDate,
    String? endDate, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (Utils.isNotEmpty(startDate)) {
      params["start_date"] = startDate!;
    }
    if (Utils.isNotEmpty(endDate)) {
      params["end_date"] = endDate!;
    }

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiReportStaffRequest,
      params: params,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      var jsonList = result.getListJson();

      var list = jsonList?.map((e) {
        if (e is Map<String, dynamic>) {
          return StaffRequestReportEntity.fromJson(e);
        } else {
          return null;
        }
      }).toList();

      return result.convert<StaffRequestReportEntity?>(list: list);
    }

    return result.convert<StaffRequestReportEntity>();

  }

  /// 越南的员工报表
  Future<HttpResult<StaffReportVNEntity>> fetchStaffReportVN(
      String? startDate,
      String? endDate, {
        CancelToken? cancelToken,
      }) async {
    //参数
    Map<String, String> params = {};
    if (Utils.isNotEmpty(startDate)) {
      params["start_date"] = startDate!;
    }
    if (Utils.isNotEmpty(endDate)) {
      params["end_date"] = endDate!;
    }

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiReportStaffRequest,
      params: params,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = StaffReportVNEntity.fromJson(json!);
      //重新赋值data或list
      return result.convert<StaffReportVNEntity>(data: data);
    }
    return result.convert();
  }

  /// 越南的用工报表
  Future<HttpResult<LabourReportVNEntity>> fetchReportLabourVN(
      String? startDate,
      String? endDate, {
        CancelToken? cancelToken,
      }) async {
    //参数
    Map<String, String> params = {};
    if (Utils.isNotEmpty(startDate)) {
      params["start_date"] = startDate!;
    }
    if (Utils.isNotEmpty(endDate)) {
      params["end_date"] = endDate!;
    }

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiReportLabour,
      params: params,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = LabourReportVNEntity.fromJson(json!);
      //重新赋值data或list
      return result.convert<LabourReportVNEntity>(data: data);
    }
    return result.convert();
  }

}
