import 'dart:convert';

import '../models/detail_history_model.dart';
import '../models/history_model.dart';
import '../models/history_type_model.dart';
import '../models/work_status_model.dart';
import '../models/work_supervisor_model.dart';

import '../app/constant.dart';
import '../models/work_type_model.dart';
import 'base_repository.dart';

class HistoryRepository extends BaseRepository {
  HistoryRepository({required super.dio});

  Future<List<HistoryModel>> getData(
      {required String id, required String date, required String type}) async {
    // http: //10.9.9.3/one-api/v1/courier/mobile/history/list_all/?id=40&startdate=11-09-2023&tipeid=3
    final url = Constant.baseUrl +
        "/v1/courier/mobile/history/list_all/?id=$id&startdate=$date&tipeid=$type";
    final resp = await get(service: url);
    List<HistoryModel> data = [];
    print(resp['data']);
    if (resp['status'] == 'OK') {
      data = [];
      resp['data'].forEach((e) {
        final model = HistoryModel.fromJson(e);
        data.add(model);
      });
    } else {
      data = [];
    }

    return data;
  }

  Future<DetailHistoryModel> getDetail(
      {required String id,
      required String tipeId,
      required String deliveryId}) async {
    final url =
        "${Constant.baseUrl}/v1/courier/mobile/history/list_by_id/?id=$id&tipeid=$tipeId&deliveryid=$deliveryId";
    final resp = await get(service: url);
    // DetailHistoryModel data;
    // print(resp['data']);

    DetailHistoryModel data = DetailHistoryModel.fromJson(resp['data'][0]);

    // resp['data'].forEach((e) {
    //   final model = HistoryModel.fromJson(e);
    //   data.add(model);
    // });
    // }
    // print(data.alamatPengambilan);

    return data;
  }

  Future<List<HistoryTypeModel>> getHistoryType() async {
    // http: //10.9.9.3/one-api/v1/courier/mobile/homescreen/list_tipe_history
    final url =
        "${Constant.baseUrl}/v1/courier/mobile/homescreen/list_tipe_history";
    final resp = await get(service: url);
    // DetailHistoryModel data;
    // print(resp['data']);

    List<HistoryTypeModel> data = List.empty(growable: true);

    resp['data'].forEach((e) {
      final model = HistoryTypeModel.fromJson(e);
      data.add(model);
    });

    // print(data.alamatPengambilan);

    return data;
  }
}
