/*
 * @Author: WuWei
 * @Date: 2020-07-02 09:58:34
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-04-11 15:15:35
 * @Description: 报修service
 */

import 'package:app_package/app_package.dart';
import 'package:repair_package/src/models/deal_person.dart';
import 'package:repair_package/src/models/status.dart';
import 'package:repair_package/src/models/inner_sort_list.dart';
import 'package:repair_package/src/models/score_detail.dart';
import 'package:repair_package/src/models/sort_list.dart';
import 'package:repair_package/src/models/type_list.dart';
import '../../../src/models/repair_detail.dart';
import '../../../src/models/repair_list.dart';
import '../../../src/models/repair_dialog.dart';
import '../repair_service.dart';
import '../../models/repair_person_list.dart';
import '../../models/service_fee_detail.dart';
import '../../models/service_project.dart';

class RepairServiceImpl implements RepairService {
  static String _prefix = '/app/admin';

  @override
  Future<ResModel<PagingModel<RepairListModel>>> getRepairList(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/repair',
      queryParameters: data,
    );
    return ResModel<PagingModel<RepairListModel>>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => PagingModel(
        json: v,
        to: (v) => v.toJson(),
        from: (v) => RepairListModel.fromJson(v),
      ),
    );
  }

  Future<ResModel<RepairDetailModel>> getRepairDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/repair/${data['id']}',
    );
    return ResModel<RepairDetailModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => RepairDetailModel.fromJson(v),
    );
  }

  Future<ResModel<RepairDialogModel>> getRepairDialog(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/repair/dialogs/${data['id']}',
      queryParameters: data,
    );

    return ResModel<RepairDialogModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => RepairDialogModel.fromJson(v),
    );
  }

  /// 报修更新(受理/驳回/解决/挂起)v4
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/20676
  Future<int?> putRepair(data) async {
    var res = await HttpUtil.instance.put(
      '$_prefix/repair/v4/${data['id']}',
      data: data,
    );

    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  ///报修对话
  ///http://yapi.wisdomcity.com.cn/project/16/interface/api/1865
  Future<int?> postDialog(Map data) async {
    var res = await HttpUtil.instance.post(
      '$_prefix/repair/dialogs/${data['id']}',
      data: data,
    );

    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  ///受理人列表
  Future<ResModel<List<RepairPersonListModel>>> getPersonList(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/report/getAcceptEmployees',
      queryParameters: data,
    );
    return ResModel<List<RepairPersonListModel>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<RepairPersonListModel>((f) => RepairPersonListModel.fromJson(f))
          .toList(),
    );
  }

  ///转派 http://yapi.wisdomcity.com.cn/project/16/interface/api/18932
  Future<int?> postTransfer(data) async {
    var res = await HttpUtil.instance.post(
      '$_prefix/repair/transfer',
      data: data,
    );

    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);

  /*
  * 费用
  */
  // 服务费用详情 http://yapi.wisdomcity.com.cn/project/40/interface/api/18851
  Future<ResModel<ServiceFeeDetailModel>> getServiceFeeDetaill(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/serviceFee/${data['id']}',
    );
    return ResModel<ServiceFeeDetailModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => ServiceFeeDetailModel.fromJson(v),
    );
  }

  /// 新增服务费用 http://yapi.wisdomcity.com.cn/project/40/interface/api/18833
  Future<String?> postServiceFee(data) async {
    var res = await HttpUtil.instance.post(
      '$_prefix/serviceFee',
      data: data,
    );

    if (res.data['data'] != null && res.data['data'] is String) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  /// 更新服务费用 http://yapi.wisdomcity.com.cn/project/40/interface/api/18842
  Future<int?> putServiceFee(id, data) async {
    var res = await HttpUtil.instance.put(
      '$_prefix/serviceFee/$id',
      data: data,
    );

    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  /// 服务项目列表 http://yapi.wisdomcity.com.cn/project/40/interface/api/18869
  Future<ResModel<List<ServiceProject>>> getServiceProjectList(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/payServiceProject/droplist',
      queryParameters: data,
    );

    return ResModel<List<ServiceProject>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<ServiceProject>((f) => ServiceProject.fromJson(f))
          .toList(),
    );
  }

  /// 报事报修状态枚举 http://yapi.wisdomcity.com.cn/project/40/interface/api/19049
  Future<ResModel<List<StatusModel>>> getStatusEnum() async {
    var res = await HttpUtil.instance.get(
      '$_prefix/commonEnum/app/ReportOrRepairStatusEnum',
    );

    return ResModel<List<StatusModel>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) =>
          (v ?? []).map<StatusModel>((f) => StatusModel.fromJson(f)).toList(),
    );
  }

  /// 服务评分详情
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/20162
  Future<ResModel<ScoreDetailModel>> getScoreDetail(id) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/serviceScore/$id',
    );

    return ResModel<ScoreDetailModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => ScoreDetailModel.fromJson(v),
    );
  }

  /// 处理人列表
  Future<ResModel<List<DealPeasonModel>>> getDealPerson(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/employee/report/acceptPersons',
      queryParameters: data,
    );

    return ResModel<List<DealPeasonModel>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<DealPeasonModel>((f) => DealPeasonModel.fromJson(f))
          .toList(),
    );
  }

  /// 类别列表 http://yapi.wisdomcity.com.cn/project/16/interface/api/20670
  Future<ResModel<List<TypeListModel>>> getTypeList() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/repairBusinessType',
    );

    return ResModel<List<TypeListModel>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<TypeListModel>((f) => TypeListModel.fromJson(f))
          .toList(),
    );
  }

  /// 分类列表 http://yapi.wisdomcity.com.cn/project/16/interface/api/20670
  Future<ResModel<List<SortListModel>>> getSortList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/repairSort/name',
      queryParameters: data,
    );
    return ResModel<List<SortListModel>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<SortListModel>((f) => SortListModel.fromJson(f))
          .toList(),
    );
  }

  /// 内部分类列表 http://yapi.wisdomcity.com.cn/project/16/interface/api/20844
  Future<ResModel<List<InnerSortListModel>>> getInnerSortList() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/repair/internalSort',
    );
    return ResModel<List<InnerSortListModel>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<InnerSortListModel>((f) => InnerSortListModel.fromJson(f))
          .toList(),
    );
  }

  /// 更改预约时间
  Future<ResModel<int>> setRepairTime(id, data) async {
    var res = await HttpUtil.instance.patch(
      '/app/admin/repair/$id/appointedTime',
      data: data,
    );
    return ResModel<int>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  /// 统计
  Future<ResModel<StatusStatistic>> getRepairStatistic(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/repair/info/statistics',
      queryParameters: data,
    );
    print(res);
    return ResModel<StatusStatistic>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => StatusStatistic.fromJson(v),
    );
  }
}
