import 'package:domain/entity/response/t_h_o_a_attachment_entity.dart';
import 'package:domain/entity/response/t_h_o_a_labour_detail_entity.dart';
import 'package:domain/entity/response/t_h_o_a_labour_review_table_entity.dart';
import 'package:domain/entity/response/t_h_o_a_labour_table_entity.dart';
import 'package:domain/entity/response/t_h_upload_file_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/labour_request_index_entity.dart';
import '../entity/response/labour_request_work_flow_entity.dart';
import '../entity/response/labour_review_status_entity.dart';

/// 泰国的 OA 的数据仓库
class THOARepository extends GetxService {
  HttpProvider httpProvider;

  THOARepository({required this.httpProvider});

  /// 获取OA用工请求的筛选选项
  Future<HttpResult<LabourRequestIndexEntity>> fetchLabourRequestIndex({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiOALabourRequestOptionTH,
      cancelToken: cancelToken,
    );

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

  /// 获取OA用工请求的列表
  Future<HttpResult<THOALabourTableEntity>> fetchLabourRequestTable({
    required int curPage,
    String? divisionId,
    String? outletId,
    String? status,
    String? startDate,
    String? endDate,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['cur_page'] = curPage.toString();
    params['cur_page'] = curPage.toString();
    if (Utils.isNotEmpty(divisionId)) {
      params['division_id'] = divisionId!;
    }
    if (Utils.isNotEmpty(outletId)) {
      params['outlet_id'] = outletId!;
    }
    if (Utils.isNotEmpty(status)) {
      params['status'] = status!;
    }
    if (Utils.isNotEmpty(startDate)) {
      params['start_date'] = startDate!;
    }
    if (Utils.isNotEmpty(endDate)) {
      params['end_date'] = endDate!;
    }

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

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

  /// 根据ID获取主列表的Item数据，用于刷新Item
  Future<HttpResult<THOALabourTableEntity>> fetchItemByRequestId(
    String? requestId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = "1";
    params["page_size"] = "1";

    if (!Utils.isEmpty(requestId)) {
      params["request_id"] = requestId!;
    }

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiOALabourRequestTableTH,
      params: params,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

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

  /// 撤回用工请求
  Future<HttpResult> recallLabourRequest(
    String? requestId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['request_id'] = requestId ?? "";

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

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  /// 删除用工请求
  Future<HttpResult> deleteLabourRequest(
    String? requestId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['request_id'] = requestId ?? "";

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

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  ///  用工的审核详情工作流列表
  Future<HttpResult<LabourRequestWorkFlowEntity>> fetchLabourRequestWorkFlow(
    String? requestId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(requestId)) {
      params["request_id"] = requestId!;
    }

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

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

  /// 用工请求 Add Option
  Future<HttpResult<THOALabourDetailEntity>> fetchLabourRequestAddOption({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiOALabourRequestAddOptionTH,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

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

  /// 用工请求 Edit Detail
  Future<HttpResult<THOALabourDetailEntity>> fetchLabourRequestEditDetail(
    String? requestId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['request_id'] = requestId ?? "";

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiOALabourRequestDetailTH,
      params: params,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

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

  /// 添加用工请求提交
  Future<HttpResult> addLabourRequestSubmit({
    required String? jobTitleId,
    required String? startTime,
    required String? endTime,
    String? repeatStart,
    String? repeatEnd,
    required String? outletId,
    int? sexLimit,
    String? needNum,
    String? maleLimit,
    String? femaleLimit,
    String? description,
    String? employmentType,
    String? eventName,
    String? eventType,
    String? passengers,
    String? estRevenue,
    String? position,
    String? estCost,
    String? attUrl,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['job_title_id'] = jobTitleId ?? "";
    params['start_time'] = startTime ?? "";
    params['end_time'] = endTime ?? "";
    params['outlet_id'] = outletId ?? "";
    params['repeat_start'] = repeatStart ?? "";
    params['repeat_end'] = repeatEnd ?? "";

    params['sex_limit'] = sexLimit?.toString() ?? "0";
    if (sexLimit == 1) {
      params['male_limit'] = maleLimit ?? "0";
      params['female_limit'] = femaleLimit ?? "0";
    } else {
      params['need_num'] = needNum ?? "0";
    }

    params['description'] = description ?? "";
    params['employment_type'] = employmentType ?? "";
    params['event_name'] = eventName ?? "";
    params['event_type'] = eventType ?? "";
    params['passengers'] = passengers ?? "";
    params['est_revenue'] = estRevenue ?? "";
    params['position'] = position ?? "";
    params['est_cost'] = estCost ?? "";
    params['att_url'] = attUrl ?? "";

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

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  /// 编辑用工请求提交
  Future<HttpResult> editLabourRequestSubmit({
    required bool isReviewEdit,
    required String? requestId,
    required String? jobTitleId,
    required String? startTime,
    required String? endTime,
    String? repeatStart,
    String? repeatEnd,
    required String? outletId,
    int? sexLimit,
    String? needNum,
    String? maleLimit,
    String? femaleLimit,
    String? description,
    String? employmentType,
    String? eventName,
    String? eventType,
    String? passengers,
    String? estRevenue,
    String? position,
    String? estCost,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['request_id'] = requestId ?? "";
    params['job_title_id'] = jobTitleId ?? "";
    params['start_time'] = startTime ?? "";
    params['end_time'] = endTime ?? "";
    params['outlet_id'] = outletId ?? "";
    params['repeat_start'] = repeatStart ?? "";
    params['repeat_end'] = repeatEnd ?? "";

    params['sex_limit'] = sexLimit?.toString() ?? "0";
    if (sexLimit == 1) {
      params['male_limit'] = maleLimit ?? "0";
      params['female_limit'] = femaleLimit ?? "0";
    } else {
      params['need_num'] = needNum ?? "0";
    }

    params['description'] = description ?? "";
    params['employment_type'] = employmentType ?? "";
    params['event_name'] = eventName ?? "";
    params['event_type'] = eventType ?? "";
    params['passengers'] = passengers ?? "";
    params['est_revenue'] = estRevenue ?? "";
    params['position'] = position ?? "";
    params['est_cost'] = estCost ?? "";

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

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  /// 泰国上传文件
  Future<HttpResult<THUploadFileEntity>> uploadFile(
    String? filePath, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};

    //文件
    Map<String, String> fileParams = {};
    if (Utils.isNotEmpty(filePath)) {
      fileParams['file'] = filePath!;
    }

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

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

  /// OA 附件列表
  Future<HttpResult<THOAAttachmentEntity>> fetchAttachmentTable({
    required String? requestId,
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['request_id'] = requestId ?? "";
    params['cur_page'] = curPage.toString();
    params['page_size'] = "10";

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

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

  /// OA附件添加
  Future<HttpResult> addAttachmentSubmit({
    required String? requestId,
    required String? attUrl,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['request_id'] = requestId ?? "";
    params['att_url'] = attUrl ?? "";

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

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  /// OA附件删除
  Future<HttpResult> deleteAttachmentSubmit({
    required String? attId,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['att_id'] = attId ?? "";

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

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  // ===================================  用工审核  ↓  ===================================

  /// 用工审核选项
  Future<HttpResult<LabourRequestIndexEntity>> fetchLabourReviewIndex({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiOALabourReviewOptionTH,
      cancelToken: cancelToken,
    );

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

  /// 用工审核列表
  Future<HttpResult<THOALabourReviewTableEntity>> fetchLabourReviewList(
    String? keyword,
    String? startDate,
    String? endDate,
    String? outletId, {
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = "10";

    if (!Utils.isEmpty(keyword)) {
      params["job_title"] = keyword!;
    }
    if (!Utils.isEmpty(startDate)) {
      params["job_start"] = startDate!;
    }
    if (!Utils.isEmpty(endDate)) {
      params["job_end"] = endDate!;
    }
    if (!Utils.isEmpty(outletId)) {
      params["outlet_id"] = outletId!;
    }

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

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

  /// 根据ID获取主列表的Item数据，用于刷新审核列表Item
  Future<HttpResult<THOALabourReviewTableEntity>> fetchItemByRecordId(
    String? requestId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = "1";
    params["page_size"] = "1";

    if (!Utils.isEmpty(requestId)) {
      params["request_id"] = requestId!;
    }

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiOALabourReviewTableTH,
      params: params,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

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

  ///OA 用工审核工作流
  Future<HttpResult<LabourReviewStatusEntity>> fetchLabourReviewStatusView(
    String? orderId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(orderId)) {
      params["order_id"] = orderId!;
    }

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

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

  /// OA用工审核的批量操作
  Future<HttpResult> batchActionLabourReviews({
    required String? recordIds,
    required String? type,
    String? auditMark,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['record_ids'] = recordIds ?? "";
    params['type'] = type ?? "";
    if (Utils.isNotEmpty(auditMark)){
      params['audit_mark'] = auditMark!;
    }

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

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }
}
