import 'package:domain/entity/response/u_k_attendance_entity.dart';
import 'package:domain/entity/response/u_k_security_attendance_entity.dart';
import 'package:domain/entity/response/u_k_security_attendance_option_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';

/// UK设备与考勤相关的数据仓库
class UKAttendanceRepository extends GetxService {
  HttpProvider httpProvider;

  UKAttendanceRepository({required this.httpProvider});

  /// 获取当前门卫的考勤筛选选项
  Future<HttpResult<UKSecurityAttendanceOptionEntity>> fetchSecurityAttendanceOptions({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiSecurityAttendanceOptionUK,
      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<UKSecurityAttendanceEntity>> fetchSecurityAttendanceList({
    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.apiSecurityAttendanceListUK,
      params: params,
      cancelToken: cancelToken,
    );

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

  /// 门卫的考勤的编辑提交
  Future<HttpResult> submitSecurityAttendanceEdit({
    required String? appliedId,
    required String? formView,
    required String? securityIn,
    required String? securityOut,
    String? reason,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['applied_id'] = appliedId ?? "";
    params['from_view'] = formView ?? "";
    params['security_in'] = securityIn ?? "";
    params['security_out'] = securityOut ?? "";

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiSecurityAttendanceEditUK,
      method: HttpMethod.POST,
      params: params,
      networkDebounce: true,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

    //根据返回的结果，封装原始数据为Bean/Entity对象
    if (result.isSuccess) {
      //重新赋值data或list
      return result.convert();
    }
    return result.convert();
  }

  /// 获取工作地考勤筛选选项
  Future<HttpResult<UKSecurityAttendanceOptionEntity>> fetchAttendanceOptions({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiEAttendanceOptionUK,
      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<UKAttendanceEntity>> fetchAttendanceList({
    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.apiEAttendanceListUK,
      params: params,
      cancelToken: cancelToken,
    );

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

}
