import 'package:domain/entity/response/u_k_template_detail_entity.dart';
import 'package:domain/entity/response/u_k_template_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 UKJobRepository extends GetxService {
  HttpProvider httpProvider;

  UKJobRepository({required this.httpProvider});

  /// 用工模版中的工作标题列表
  Future<HttpResult<UKTemplateEntity>> 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["job_title"] = keyword!;
    }

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

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

  /// 用工模版中的添加选项
  Future<HttpResult<UKTemplateDetailEntity>> fetchJobTemplateIndex({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiJobTemplateAddIndexSG,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

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

  /// 工作模板的添加提交
  Future<HttpResult> addJobTemplateSubmit({
    required String jobTitle,
    List<String>? certificate,
    List<String>? vehicle,
    String? note,
    String? desc,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, dynamic> params = {};
    params['job_title'] = jobTitle;

    if (certificate != null && certificate.isNotEmpty) {
      params['certificate[]'] = certificate;
    }
    if (vehicle != null && vehicle.isNotEmpty) {
      params['vehicle[]'] = vehicle;
    }
    if (Utils.isNotEmpty(note)) {
      params['note'] = note ?? "";
    }
    if (Utils.isNotEmpty(desc)) {
      params['description'] = desc ?? "";
    }

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

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

  /// 用工模版中的详情
  Future<HttpResult<UKTemplateDetailEntity>> fetchJobTemplateDetail({
    required 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,
    );

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

  /// 工作模板的编辑提交
  Future<HttpResult> editJobTemplateSubmit({
    required String templateId,
    required String jobTitle,
    List<String>? certificate,
    List<String>? vehicle,
    String? note,
    String? desc,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, dynamic> params = {};
    params['job_title'] = jobTitle;
    params['template_id'] = templateId;

    if (certificate != null && certificate.isNotEmpty) {
      params['certificate[]'] = certificate;
    }
    if (vehicle != null && vehicle.isNotEmpty) {
      params['vehicle[]'] = vehicle;
    }
    if (Utils.isNotEmpty(note)) {
      params['note'] = note ?? "";
    }
    if (Utils.isNotEmpty(desc)) {
      params['description'] = desc ?? "";
    }

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

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

  ///工作模板的删除
  Future<HttpResult> deleteJobTemplate({
    required 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,
    );

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