import 'dart:typed_data';

import 'package:domain/entity/response/job_list_add_staff_entity.dart';
import 'package:domain/entity/response/job_list_applied_edit_entity.dart';
import 'package:domain/entity/response/job_list_applied_info_entity.dart';
import 'package:domain/entity/response/job_list_applied_staff_list_entity.dart';
import 'package:domain/entity/response/job_list_applied_staff_search_entity.dart';
import 'package:domain/entity/response/job_list_detail_entity.dart';
import 'package:domain/entity/response/job_list_entity.dart';
import 'package:domain/entity/response/job_list_index_entity.dart';
import 'package:domain/entity/response/job_list_remark_view_entity.dart';
import 'package:domain/entity/response/staff_detail_entity.dart';
import 'package:domain/entity/response/staff_labour_history_entity.dart';
import 'package:domain/entity/response/staff_remark_history_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/response/attendance_entity.dart';
import '../entity/response/attendance_review_entity.dart';
import '../entity/response/check_success_entity.dart';
import '../entity/response/job_list_applied_work_flow_entity.dart';

/// 工作相关
class JobRepository extends GetxService {
  HttpProvider httpProvider;

  JobRepository({required this.httpProvider});

  /// 获取当前酒店的考勤成员列表
  Future<HttpResult<AttendanceEntity>> fetchAttendanceList(
    String? keyword,
    String? startDate,
    String? endDate, {
    int curPage = 1,
    int pageSize = 9999,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = pageSize.toString();

    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.apiSignApplied,
      params: params,
      cancelToken: cancelToken,
    );

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

  /// 用户上传考勤
  Future<HttpResult<CheckSuccessEntity>> submitCheckInOut(
    String? appliedId,
    ByteData? byteData, {
    bool isCheckIn = false,
    CancelToken? cancelToken,
  }) async {
    //Post请求
    Map<String, String> params = {};
    params['applied_id'] = appliedId ?? "";
    params['check_type'] = isCheckIn ? "1" : "2";

    Map<String, Uint8List> streams = {};
    if (byteData != null) {
      streams["capture"] = byteData.buffer.asUint8List();
    }

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

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

  /// 工作列表的筛选选项
  Future<HttpResult<JobListIndexEntity>> fetchJobListIndex({
    CancelToken? cancelToken,
  }) async {
    final result = await httpProvider.requestNetResult(
      ApiConstants.apiJobListIndex,
      cancelToken: cancelToken,
    );

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

  /// 获取工作列表的主列表
  Future<HttpResult<JobListEntity>> fetchJobListTable(
    String? keyword,
    String? startDate,
    String? endDate,
    String? statusId,
    String? departmentId, {
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = "20";

    if (!Utils.isEmpty(keyword)) {
      params["job_title"] = keyword!;
    }
    if (!Utils.isEmpty(startDate)) {
      params["start_time"] = startDate!;
    }
    if (!Utils.isEmpty(endDate)) {
      params["end_time"] = endDate!;
    }
    if (!Utils.isEmpty(statusId)) {
      params["status"] = statusId!;
    }
    if (!Utils.isEmpty(departmentId)) {
      params["co_department_id"] = departmentId!;
    }

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

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

  /// 工作列表的详情
  Future<HttpResult<JobListDetailEntity>> fetchJobListDetail(
    String? jobId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['job_id'] = jobId ?? "";

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

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

  /// 工作中已申请的工作信息
  Future<HttpResult<JobListAppliedInfoEntity>> fetchJobAppliedInfo(
    String? jobId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params['job_id'] = jobId ?? "";

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

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

  /// 工作中已申请的成员列表
  Future<HttpResult<JobListAppliedStaffListEntity>> fetchJobAppliedStaffs(
    String? jobId,
    String? staffName, {
    String? appliedId,
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = "10";

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

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

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

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

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

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

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

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

  /// 添加员工的全部员工数据列表
  Future<HttpResult<JobListAppliedStaffSearchEntity>> searchStaffList(
    String? jobId,
    String? keyword, {
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = "10";

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

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

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

  /// 添加员工到工作
  Future<HttpResult<JobListAddStaffEntity>> addStaff2Job(
    String? jobId,
    String? staffIds, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(jobId)) {
      params["job_id"] = jobId!;
    }
    if (!Utils.isEmpty(staffIds)) {
      params["staff_ids"] = staffIds!;
    }

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

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

  /// 批量修改员工的签到信息与状态
  Future<HttpResult> batchEditStaffCheckTime(
    String? jobId,
    String? appliedIds,
    String? startTime,
    String? endTime, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(startTime)) {
      params["start_time"] = startTime!;
    }
    if (!Utils.isEmpty(endTime)) {
      params["end_time"] = endTime!;
    }
    if (!Utils.isEmpty(appliedIds)) {
      params["applied_ids"] = appliedIds!;
    }
    if (!Utils.isEmpty(jobId)) {
      params["job_id"] = jobId!;
    }

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

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

  /// 获取单个员工的信息，准备修改
  Future<HttpResult<JobListAppliedEditEntity>> fetchAppliedEditView(
    String? appliedId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(appliedId)) {
      params["applied_id"] = appliedId!;
    }

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

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

  /// 单独修改单个员工的信息
  Future<HttpResult> editAppliedSingleStaffInfo(
    String? appliedId,
    JobListAppliedEditEntity entity, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(appliedId)) {
      params["applied_id"] = appliedId!;
    }
    if (!Utils.isEmpty(entity.startTime)) {
      params["start_time"] = entity.startTime!;
    }
    if (!Utils.isEmpty(entity.endTime)) {
      params["end_time"] = entity.endTime!;
    }
    if (!Utils.isEmpty(entity.securityIn)) {
      params["security_in"] = entity.securityIn!;
    }
    if (!Utils.isEmpty(entity.securityOut)) {
      params["security_out"] = entity.securityOut!;
    }
    if (!Utils.isEmpty(entity.workIn)) {
      params["work_in"] = entity.workIn!;
    }
    if (!Utils.isEmpty(entity.workOut)) {
      params["work_out"] = entity.workOut!;
    }

    if (entity.jobUnit == "hour") {
      if (!Utils.isEmpty(entity.adjustHours)) {
        params["adjust_hours"] = entity.adjustHours!;
      }
    } else {
      if (entity.totalRooms != 0) {
        params["total_rooms"] = entity.totalRooms.toString();
      }
    }

    if (entity.reasonType != 0) {
      params["co_reason_type"] = entity.reasonType.toString();
    }
    if (!Utils.isEmpty(entity.coReason)) {
      params["co_reason"] = entity.coReason!;
    }

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

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

  /// 给员工评分的详情与选项
  Future<HttpResult<JobListRemarkViewEntity>> fetchAppliedStaffReviewView(
    String? appliedId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(appliedId)) {
      params["applied_id"] = appliedId!;
    }

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

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

  /// 单独给单个员工评分的提交
  Future<HttpResult> remarkAppliedSingleStaffSubmit(
    String? appliedId,
    String? attitude_rate,
    String? grooming_rate,
    String? performance_rate,
    String? experience_rate,
    String? content, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(appliedId)) {
      params["applied_id"] = appliedId!;
    }
    if (!Utils.isEmpty(attitude_rate)) {
      params["attitude_rate"] = attitude_rate!;
    }
    if (!Utils.isEmpty(grooming_rate)) {
      params["grooming_rate"] = grooming_rate!;
    }
    if (!Utils.isEmpty(performance_rate)) {
      params["performance_rate"] = performance_rate!;
    }
    if (!Utils.isEmpty(experience_rate)) {
      params["experience_rate"] = experience_rate!;
    }
    if (!Utils.isEmpty(content)) {
      params["content"] = content!;
    }

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

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

  /// 批量提交多个员工的考勤
  Future<HttpResult> submitBatchStaffApprove(
    String? appliedIds, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(appliedIds)) {
      params["applied_ids"] = appliedIds!;
    }

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

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

  // 员工状态的工作流
  Future<HttpResult<JobListAppliedWorkFlowEntity>> fetchAppliedWorkFlow(
    String? appliedId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(appliedId)) {
      params["applied_id"] = appliedId!;
    }

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

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

  // 员工的信息
  Future<HttpResult<StaffDetailEntity>> fetchStaffDetail(
    String? memberId, {
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    if (!Utils.isEmpty(memberId)) {
      params["member_id"] = memberId!;
    }

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

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

  // 员工的历史申请记录
  Future<HttpResult<StaffLabourHistoryEntity>> fetchStaffLabourHistory(
    String? memberId,
    String? keyword, {
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = "10";

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

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

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

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

  // 员工的历史评价记录
  Future<HttpResult<StaffRemarkHistoryEntity>> fetchStaffRemarkHistory(
    String? memberId, {
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = "10";

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

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

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

  /// 考勤审核列表
  Future<HttpResult<AttendanceReviewEntity>> fetchAttendanceReviewList(
    String? keyword, {
    required int curPage,
    CancelToken? cancelToken,
  }) async {
    //参数
    Map<String, String> params = {};
    params["cur_page"] = curPage.toString();
    params["page_size"] = "20";

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

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

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

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

    final result = await httpProvider.requestNetResult(
      ApiConstants.apiAttendanceReviewApprove,
      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.apiAttendanceReviewReject,
      method: HttpMethod.POST,
      params: params,
      networkDebounce: true,
      isShowLoadingDialog: true,
      cancelToken: cancelToken,
    );

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