/*
 * @Author: WuWei
 * @Date: 2020-07-21 15:22:12
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-04-11 15:13:07
 * @Description:
 */
import 'package:app_package/app_package.dart';
import 'package:report_package/src/models/deal_person.dart';
import 'package:report_package/src/models/inner_sort_list.dart';
import 'package:report_package/src/models/report_classify.dart';
import 'package:report_package/src/models/score_detail.dart';
import 'package:report_package/src/models/sort_list.dart';
import 'package:report_package/src/models/type_list.dart';
// import '../../../src/models/repair_detail.dart';
import '../../service/report_service.dart';
import '../../models/report_person_list.dart';
import '../../models/report_list_model.dart';
import '../../models/report_detail.dart';
import '../../models/report_dialog.dart';
import '../../models/service_fee_detail.dart';
import '../../models/service_project.dart';
import '../../models/status.dart';

class ReportServiceImpl implements ReportService {
  static String _prefix = '/app/admin';

  @override
  Future<ResModel<PagingModel<ReportListModel>>> getReportList(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/report',
      queryParameters: data,
    );

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

  /// 受理人列表
  Future<ResModel<List<ReportPersonListModel>?>> getPersonList(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/report/getAcceptEmployees',
      queryParameters: data,
    );

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

  /// 报事详情
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/1886
  Future<ResModel<ReportDetailModel>> getReportDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/report/${data['id']}',
    );
    return ResModel<ReportDetailModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => ReportDetailModel.fromJson(v),
    );
  }

  /// 报事咨询
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/1970
  Future<ResModel<ReportDialogModel>> getReportDialog(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/report/dialogs/${data['id']}',
      queryParameters: data,
    );

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

  /// 报事更新(受理/驳回/解决/挂起)v3
  /// http://yapi.wisdomcity.com.cn/project/16/interface/api/8015
  Future<int?> putReport(data) async {
    var res = await HttpUtil.instance.put(
      '$_prefix/report/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/1977
  Future<ResModel<List<ReportClassifyModel>?>> getDealClassify(data) async {
    var res = await HttpUtil.instance.get(
      '$_prefix/report/sort',
      queryParameters: data,
    );

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

  /// 内部分类列表 http://yapi.wisdomcity.com.cn/project/16/interface/api/20846
  Future<ResModel<List<InnerSortListModel>>> getInnerSortList() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/report/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(),
    );
  }

  ///报事对话
  ///http://yapi.wisdomcity.com.cn/project/16/interface/api/1963
  Future<int?> postDialog(data) async {
    var res = await HttpUtil.instance.post(
      '$_prefix/report/dialogs/${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/18932
  Future<int?> postTransfer(data) async {
    var res = await HttpUtil.instance.post(
      '$_prefix/report/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(),
    );
  }

  ///报事分类
  Future<ResModel<List<StatusModel>>> getReportSortList() async {
    final res = await HttpUtil.instance.get('$_prefix/reportSort/name');
    return ResModel<List<StatusModel>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<StatusModel>(
            (f) => StatusModel(code: f['id'], name: f['sortName']),
          )
          .toList(),
    );
  }

  ///报事业务类型
  Future<ResModel<List<StatusModel>>> getBusinessStatus(type) async {
    final res = await HttpUtil.instance.get(
      '$_prefix/serviceWorkBench/enu',
      queryParameters: {
        'type': type,
      },
    );
    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/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/20672
  Future<ResModel<List<TypeListModel>>> getTypeList() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/reportBusinessType',
    );

    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/reportSort/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(),
    );
  }

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

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