import 'dart:typed_data';

import 'package:domain/entity/response/job_list_edit_index_s_g_entity.dart';
import 'package:domain/entity/response/job_list_index_s_g_entity.dart';
import 'package:domain/entity/response/job_template_edit_index_entity.dart';
import 'package:domain/entity/response/job_template_s_g_entity.dart';
import 'package:domain/entity/response/job_title_edit_index_entity.dart';
import 'package:domain/entity/response/job_title_s_g_entity.dart';
import 'package:domain/entity/response/s_g_labour_request_worl_flow_entity.dart';
import 'package:domain/entity/response/s_g_labour_review_option_entity.dart';
import 'package:domain/entity/response/s_g_labour_review_table_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/util.dart';

import '../constants/api_constants.dart';
import '../entity/agency_need_number.dart';
import '../entity/response/job_list_s_g_entity.dart';
import '../entity/response/s_g_labour_request_add_option_entity.dart';
import '../entity/response/s_g_labour_request_detail_entity.dart';
import '../entity/response/s_g_labour_request_option_entity.dart';
import '../entity/response/s_g_labour_request_table_entity.dart';

/// 用工相关(新加坡)
class LabourSGRepository extends GetxService {
  HttpProvider httpProvider;

  LabourSGRepository({required this.httpProvider});

  /// 用工模版中的工作标题列表
  Future<HttpResult<JobTitleSGEntity>> fetchJobTitleList(
    String? keyword, {
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = "10";

    if (Utils.isNotEmpty(keyword)) {
      params["keyword"] = keyword!;
    }

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

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

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

    if (!Utils.isEmpty(ids)) {
      params["job_title_id"] = ids!;
    }

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

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

  /// 添加工作标题的详情或选项数据
  Future<HttpResult<JobTitleEditIndexEntity>> fetchJobTitleAddIndex({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiJobTitleAddIndexSG,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

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

  /// 编辑工作标题的详情或选项数据
  Future<HttpResult<JobTitleEditIndexEntity>> fetchJobTitleEditIndex(
    String? titleId, {
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params["title_id"] = titleId ?? "";

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

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

  /// 添加工作标题的提交
  Future<HttpResult> addJobTitleSubmit(
    String? jobTitle,
    String? sort,
    String? templateId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['job_title'] = jobTitle ?? "";

    if (!Utils.isEmpty(templateId)) {
      params["template_id"] = templateId!;
    }

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

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiJobTitleAddSubmitSG,
      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> editJobTitleSubmit(
    String? titleId,
    String? jobTitle,
    String? sort,
    String? templateId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['job_title'] = jobTitle ?? "";
    params['title_id'] = titleId ?? "";

    if (!Utils.isEmpty(templateId)) {
      params["template_id"] = templateId!;
    }

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

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiJobTitleEditSubmitSG,
      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> deleteJobTitleSubmit(
    String? titleId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};

    params['title_id'] = titleId ?? "";

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

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

  // ===================================  Template  ↓  ===================================

  /// 用工模版中的工作标题列表
  Future<HttpResult<JobTemplateSGEntity>> fetchJobTemplateList(
    String? keyword, {
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = "10";

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

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

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

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

    if (!Utils.isEmpty(ids)) {
      params["job_temp_id"] = ids!;
    }

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

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

  /// 添加工作模板的详情或选项数据
  Future<HttpResult<JobTemplateEditIndexEntity>> fetchJobTemplateAddIndex({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiJobTemplateAddIndexSG,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

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

  /// 编辑工作模板的详情或选项数据
  Future<HttpResult<JobTemplateEditIndexEntity>> fetchJobTemplateEditIndex(
    String? templateId, {
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params["template_id"] = templateId ?? "";

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

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

  /// 添加工作模板的提交
  Future<HttpResult> addJobTemplateSubmit(
    String? name,
    String? positionId,
    String? contact,
    String? contactNo,
    String? description,
    String? note,
    String? age,
    String? sex,
    String? withFoodCert,
    String? language, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['name'] = name ?? "";
    params['position_id'] = positionId ?? "";

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

    if (!Utils.isEmpty(contactNo)) {
      params["contact_no"] = contactNo!;
    }

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

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

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

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

    if (!Utils.isEmpty(withFoodCert)) {
      params["with_food_cert"] = withFoodCert!;
    }

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

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiJobTemplateAddSubmitSG,
      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> editJobTemplateSubmit(
    String? templateId,
    String? name,
    String? positionId,
    String? contact,
    String? contactNo,
    String? description,
    String? note,
    String? age,
    String? sex,
    String? withFoodCert,
    String? language, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['template_id'] = templateId ?? "";
    params['name'] = name ?? "";
    params['position_id'] = positionId ?? "";

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

    if (!Utils.isEmpty(contactNo)) {
      params["contact_no"] = contactNo!;
    }

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

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

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

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

    if (!Utils.isEmpty(withFoodCert)) {
      params["with_food_cert"] = withFoodCert!;
    }

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

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiJobTemplateEditSubmitSG,
      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> deleteJobTemplateSubmit(
    String? templateId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};

    params['template_id'] = templateId ?? "";

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

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

  // ===================================  LabourRequest  ↓  ===================================

  /// 用工请求列表的选项数据
  Future<HttpResult<SGLabourRequestOptionEntity>> fetchLabourRequestOption({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiLabourRequestOptionSG,
      cancelToken: cancelToken,
    );

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

  /// 用工请求列表 Table
  Future<HttpResult<SGLabourRequestTableEntity>> fetchLabourRequestTable({
    String? requestId,
    String? agencyId,
    String? startDate,
    String? endDate,
    String? outletId,
    String? status,
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};

    if (Utils.isNotEmpty(requestId)) {
      params['request_id'] = requestId ?? "";
    }
    if (Utils.isNotEmpty(agencyId)) {
      params['agency_id'] = agencyId ?? "";
    }
    if (Utils.isNotEmpty(startDate)) {
      params['start_date'] = startDate ?? "";
    }
    if (Utils.isNotEmpty(endDate)) {
      params['end_date'] = endDate ?? "";
    }
    if (Utils.isNotEmpty(status)) {
      params['status'] = status ?? "";
    }
    if (Utils.isNotEmpty(outletId)) {
      params['outlet_id'] = outletId ?? "";
    }

    params['cur_page'] = curPage.toString();
    params['page_size'] = "10";

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

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

  /// 用工请求 Add submit
  Future<HttpResult> addLabourRequestSubmit({
    required String? jobTitleId,
    required String? startTime,
    required String? endTime,
    required String? outletId,
    String? repeatStart,
    String? repeatEnd,
    String? requestType,
    String? positionId,
    List<AgencyNeedNumberEntity>? list,
    String? remark,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};

    params['job_title_id'] = jobTitleId ?? "";
    params['start_time'] = startTime ?? "";
    params['end_time'] = endTime ?? "";
    params['outlet_id'] = outletId ?? "";

    if (Utils.isNotEmpty(repeatStart)) {
      params['repeat_start'] = repeatStart ?? "";
    }
    if (Utils.isNotEmpty(repeatEnd)) {
      params['repeat_end'] = repeatEnd ?? "";
    }
    if (Utils.isNotEmpty(requestType)) {
      params['request_type'] = requestType ?? "";
    }
    if (Utils.isNotEmpty(positionId) && requestType == "1") {
      params['position_id'] = positionId ?? "";
    }
    if (Utils.isNotEmpty(remark)) {
      params['remark'] = remark ?? "";
    }

    if (list != null && list.isNotEmpty) {
      for (var item in list) {
        if (item.isInHouse) {
          params['sex_limit_0'] = item.sexLimit.toString();
          if (item.sexLimit == 1) {
            params['male_limit_0'] = item.maleLimit.toString();
            params['female_limit_0'] = item.femaleLimit.toString();
          } else {
            params['need_num_0'] = item.needNum.toString();
          }
        } else {
          params['sex_limit_${item.agencyId}'] = item.sexLimit.toString();
          if (item.sexLimit == 1) {
            params['male_limit_${item.agencyId}'] = item.maleLimit.toString();
            params['female_limit_${item.agencyId}'] = item.femaleLimit.toString();
          } else {
            params['need_num_${item.agencyId}'] = item.needNum.toString();
          }
        }
      }
    }

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

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

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

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

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

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

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

  /// 新加坡的用工请求 Recall操作
  Future<HttpResult> recallLabourRequestSG(
    String? requestId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['request_id'] = requestId ?? "";

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiLabourRequestRecallSG,
      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> deleteLabourRequestSG(
    String? requestId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['request_id'] = requestId ?? "";

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

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

  /// 用工请求 Edit submit
  Future<HttpResult> editLabourRequestSubmit({
    required String? requestId,
    required String? startTime,
    required String? endTime,
    required String? outletId,
    int sexLimit = 0,
    String? maleLimit,
    String? femaleLimit,
    String? needNum,
    String? requestType,
    String? positionId,
    String? remark,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};

    params['request_id'] = requestId ?? "";
    params['start_time'] = startTime ?? "";
    params['end_time'] = endTime ?? "";
    params['outlet_id'] = outletId ?? "";

    if (Utils.isNotEmpty(requestType)) {
      params['request_type'] = requestType ?? "";
    }
    if (Utils.isNotEmpty(positionId) && requestType == "1") {
      params['position_id'] = positionId ?? "";
    }
    if (Utils.isNotEmpty(remark)) {
      params['remark'] = remark ?? "";
    }

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

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiLabourRequestEditSubmitSG,
      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<SGLabourRequestWorlFlowEntity>> fetchLabourRequestWorkFlow(
    String? requestId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['request_id'] = requestId ?? "";

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

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

  // ===================================  Job List  ↓  ===================================

  //用工请求的查询选项（Job List）
  Future<HttpResult<JobListIndexSGEntity>> fetchJobListIndex({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiJobListIndexSG,
      cancelToken: cancelToken,
    );

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

  //用工请求的查询列表（Job List）
  Future<HttpResult<JobListSGEntity>> fetchJobListTable(
    String? startDate,
    String? endDate,
    String? status,
    String? outletId, {
    required int curPage,
    String? agencyId,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};

    if (Utils.isNotEmpty(startDate)) {
      params['start_date'] = startDate ?? "";
    }
    if (Utils.isNotEmpty(endDate)) {
      params['end_date'] = endDate ?? "";
    }
    if (Utils.isNotEmpty(status)) {
      params['status'] = status ?? "";
    }
    if (Utils.isNotEmpty(outletId)) {
      params['outlet_id'] = outletId ?? "";
    }
    if (Utils.isNotEmpty(agencyId)) {
      params['agency_id'] = agencyId ?? "";
    }

    params['cur_page'] = curPage.toString();
    params['page_size'] = "10";

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

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

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

    if (!Utils.isEmpty(jobId)) {
      params["job_id"] = jobId!;
    }

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

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

  //用工请求的编辑详情（Job List）
  Future<HttpResult<JobListEditIndexSGEntity>> fetchJobListEditIndex(
    String? jobId, {
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['job_id'] = jobId ?? "";

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

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

  //用工请求的编辑提交（Job List）
  Future<HttpResult> editJobListSubmit(
    String? jobId,
    String? startTime,
    String? endTime,
    String? outletId,
    int sexLimit,
    String? maleLimit,
    String? femaleLimit,
    String? needNum,
    String? requestType,
    String? positionId,
    String? remark, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};

    params['job_id'] = jobId ?? "";
    params['start_time'] = startTime ?? "";
    params['end_time'] = endTime ?? "";
    params['outlet_id'] = outletId ?? "";

    params['sex_limit'] = sexLimit.toString();

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

    if (Utils.isNotEmpty(requestType)) {
      params['request_type'] = requestType ?? "";
    }

    if (Utils.isNotEmpty(positionId) && requestType == "1") {
      params['position_id'] = positionId ?? "";
    }

    if (Utils.isNotEmpty(remark)) {
      params['remark'] = remark ?? "";
    }

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

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

  //用工请求的取消（Job List）
  Future<HttpResult> cancelJobList(
    String? jobId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['job_id'] = jobId ?? "";

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

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

  //用工请求的删除（Job List）
  Future<HttpResult> deleteJobList(
    String? jobId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['job_id'] = jobId ?? "";

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

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

  //用工请求的考勤确认（Job List）发送到考勤
  Future<HttpResult> approveAppliedJob(
    String? appliedIds, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['applied_ids'] = appliedIds ?? "";

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

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

  // ===================================  SG 的 Labour Request Review 相关接口  ↓  ===================================

  /// 用工请求审核列表的选项数据
  Future<HttpResult<SGLabourReviewOptionEntity>> fetchLabourReviewOption({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiLabourRequestReviewOptionSG,
      cancelToken: cancelToken,
    );

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

  /// 用工请求审核列表 Table
  Future<HttpResult<SGLabourReviewTableEntity>> fetchLabourReviewTable({
    String? recordId,
    String? agencyId,
    String? startDate,
    String? endDate,
    String? outletId,
    String? keyword,
    String? status,
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['cur_page'] = curPage.toString();
    params['page_size'] = "10";

    if (Utils.isNotEmpty(recordId)) {
      params['record_id'] = recordId ?? "";
    }
    if (Utils.isNotEmpty(agencyId)) {
      params['agency_id'] = agencyId ?? "";
    }
    if (Utils.isNotEmpty(startDate)) {
      params['start_date'] = startDate ?? "";
    }
    if (Utils.isNotEmpty(endDate)) {
      params['end_date'] = endDate ?? "";
    }
    if (Utils.isNotEmpty(status)) {
      params['status'] = status ?? "";
    }
    if (Utils.isNotEmpty(outletId)) {
      params['outlet_id'] = outletId ?? "";
    }
    if (Utils.isNotEmpty(keyword)) {
      params['job_title'] = keyword ?? "";
    }

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

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

  /// 用工审核的批量同意
  Future<HttpResult> approveLabourReviews(
    String? recordIds, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['record_ids'] = recordIds ?? "";

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiLabourRequestReviewApproveSG,
      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> rejectLabourReviews(
    String? recordIds,
    String? reason, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['record_ids'] = recordIds ?? "";

    if (Utils.isNotEmpty(reason)) {
      params['audit_mark'] = reason ?? "";
    }

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

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

  /// 用工请求 Review 的 Edit submit
  Future<HttpResult> editLabourRequestReviewSubmit({
    required String? requestId,
    required String? startTime,
    required String? endTime,
    required String? outletId,
    int sexLimit = 0,
    String? maleLimit,
    String? femaleLimit,
    String? needNum,
    String? requestType,
    String? positionId,
    String? remark,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};

    params['request_id'] = requestId ?? "";
    params['start_time'] = startTime ?? "";
    params['end_time'] = endTime ?? "";
    params['outlet_id'] = outletId ?? "";

    if (Utils.isNotEmpty(requestType)) {
      params['request_type'] = requestType ?? "";
    }
    if (Utils.isNotEmpty(positionId) && requestType == "1") {
      params['position_id'] = positionId ?? "";
    }
    if (Utils.isNotEmpty(remark)) {
      params['remark'] = remark ?? "";
    }

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

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

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