import 'package:domain/entity/response/fiance_report_entity.dart';
import 'package:domain/entity/response/u_k_report_attendance_entity.dart';
import 'package:domain/entity/response/u_k_report_working_hours_entity.dart';
import 'package:get/get.dart';
import 'package:plugin_platform/http/http_provider.dart';
import 'package:plugin_platform/http/http_result.dart';
import 'package:plugin_platform/platform_export.dart';
import 'package:shared/utils/util.dart';

import '../constants/api_constants.dart';
import '../entity/response/u_k_report_labour_entity.dart';
import '../entity/response/u_k_security_attendance_option_entity.dart';

/// UK报表与统计相关的数据仓库
class UKReportRepository extends GetxService {
  HttpProvider httpProvider;

  UKReportRepository({required this.httpProvider});

  /// 获取报表的筛选选项
  Future<HttpResult<UKSecurityAttendanceOptionEntity>> fetchReportOptions({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiReportOptionUK,
      cancelToken: cancelToken,
    );

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

  /// 获取报表的用工数据列表
  Future<HttpResult<UKReportLabourEntity>> fetchReportLabourList({
    String? keyword,
    String? startDate,
    String? endDate,
    String? departmentId,
    int curPage = 1,
    int pageSize = 10,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = pageSize.toString();

    if (!Utils.isEmpty(keyword)) {
      params["job_title"] = keyword!;
    }
    if (!Utils.isEmpty(departmentId)) {
      params["co_department_id"] = departmentId!;
    }
    if (!Utils.isEmpty(startDate)) {
      params["start_date"] = startDate!;
    }
    if (!Utils.isEmpty(endDate)) {
      params["end_date"] = endDate!;
    }

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

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = UKReportLabourEntity.fromJson(json!);
      return result.convert<UKReportLabourEntity>(data: data);
    }
    return result.convert();
  }

  /// 获取报表的电子考勤数据列表
  Future<HttpResult<UKReportAttendanceEntity>> fetchReportAttendanceList({
    String? keyword,
    String? startDate,
    String? endDate,
    String? departmentId,
    int curPage = 1,
    int pageSize = 10,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = pageSize.toString();

    if (!Utils.isEmpty(keyword)) {
      params["staff_name"] = keyword!;
    }
    if (!Utils.isEmpty(departmentId)) {
      params["co_department_id"] = departmentId!;
    }
    if (!Utils.isEmpty(startDate)) {
      params["start_date"] = startDate!;
    }
    if (!Utils.isEmpty(endDate)) {
      params["end_date"] = endDate!;
    }

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

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = UKReportAttendanceEntity.fromJson(json!);
      return result.convert<UKReportAttendanceEntity>(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.apiReportFianceUK,
      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<UKReportWorkingHoursEntity>> fetchReportWorkingHoursList({
    String? startDate,
    String? endDate,
    String? departmentId,
    int curPage = 1,
    int pageSize = 10,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = pageSize.toString();

    if (!Utils.isEmpty(departmentId)) {
      params["co_department_id"] = departmentId!;
    }
    if (!Utils.isEmpty(startDate)) {
      params["start_date"] = startDate!;
    }
    if (!Utils.isEmpty(endDate)) {
      params["end_date"] = endDate!;
    }

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

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = UKReportWorkingHoursEntity.fromJson(json!);
      return result.convert<UKReportWorkingHoursEntity>(data: data);
    }
    return result.convert();
  }

}
