import 'package:domain/entity/response/hotel_info_entity.dart';
import 'package:domain/entity/response/sign_in_sign_out_entity.dart';
import 'package:domain/entity/response/switch_project_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';

/// MS/NL等签到签出的功能单独的数据仓库
class MSRepository extends GetxService {
  HttpProvider httpProvider;

  MSRepository({required this.httpProvider});

  /// 获取当前酒店需要签到签出的考勤成员列表
  Future<HttpResult<SignInSignOutEntity>> fetchSignInSignOutList(
    String? keyword,
    String? startDate,
    String? endDate, {
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    if (!Utils.isEmpty(keyword)) {
      params["keyword"] = keyword!;
    }
    if (!Utils.isEmpty(startDate)) {
      params["start_date"] = startDate!;
    }
    if (!Utils.isEmpty(endDate)) {
      params["end_date"] = endDate!;
    }

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

    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = SignInSignOutEntity.fromJson(json!);
      return result.convert<SignInSignOutEntity>(data: data);
    }
    return result.convert();
  }

  /// 提交考勤的拍照图片
  Future<HttpResult<SignInSignOutRows>> signImageSubmit(
    String? appliedId,
    String? photo,
    String? type, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['applied_id'] = appliedId ?? "";
    params['type'] = type ?? "";

    Map<String, String> paths = {};
    if (!Utils.isEmpty(photo)) {
      paths["photo"] = photo ?? "";
    }

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

    if (result.isSuccess) {
      //重新赋值data或list
      final json = result.getDataJson();
      var data = SignInSignOutRows.fromJson(json!);
      return result.convert<SignInSignOutRows>(data: data);
    }
    return result.convert();
  }

  /// 可以切换的Project列表
  Future<HttpResult<SwitchProjectEntity>> fetchSwitchProjectList({
    required int country,
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      country == 5
          ? ApiConstants.apiAccountListTH //泰国
          : ApiConstants.apiAccountListMS,  //默认马来
      isShowLoadingDialog: true,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

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

  /// 切换项目
  Future<HttpResult<HotelInfoEntity>> switchProjectSubmit(
    String? adminId, {
      required int country,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['admin_id'] = adminId ?? "";

    final result = await httpProvider.requestNetResult(
      country == 5
          ? ApiConstants.apiSwitchAccountTH //泰国
          : ApiConstants.apiSwitchAccountMS,  //默认马来
      method: HttpMethod.POST,
      params: params,
      networkDebounce: true,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

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