import 'package:mitra_corporate/model/create_surat_jalan_model.dart';
import 'package:mitra_corporate/model/delivery_type_model.dart';
import 'package:mitra_corporate/model/destination_model.dart';
import 'package:mitra_corporate/model/order_model.dart';
import 'package:mitra_corporate/model/registration_filter_model.dart';
import 'package:mitra_corporate/model/surat_jalan_detail_model.dart';

import '../app/constant.dart';
import '../model/surat_jalan_model.dart';
import 'base_repository.dart';

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

  Future<List<DeliveryTypeModel>> getDeliveryType({
    required String token,
  }) async {
    var param = {"token": token};
    final url = "${Constant.baseUrl}deliveryorder/getdeliverytype";
    final resp = await post(param: param, service: url);
    List<DeliveryTypeModel> data;
    // print(resp['data']);
    if (resp['status'] == 'OK') {
      data = [];
      resp['data'].forEach((e) {
        final model = DeliveryTypeModel.fromJson(e);
        data.add(model);
      });
    } else {
      data = [];
    }

    return data;
  }

  Future<List<OrderModel>> getOrder({
    required String token,
    required String company_id,
    required String regional_id,
  }) async {
    var param = {
      "token": token,
      "company_id": company_id,
      "regional_id": regional_id
    };
    final url = "${Constant.baseUrl}deliveryorder/getorder";
    final resp = await post(param: param, service: url);
    List<OrderModel> data;
    // print(resp['data']);
    if (resp['status'] == 'OK') {
      data = [];
      resp['data'].forEach((e) {
        final model = OrderModel.fromJson(e);
        data.add(model);
      });
    } else {
      data = [];
    }

    return data;
  }

  Future<String> createSuratJalan({required CreateSuratJalanModel prm}) async {
    final url = "${Constant.baseUrl}deliveryorder/addDelivery";
    final resp = await post(param: prm.toJson(), service: url);
    String number;
    // print(resp['data']);
    if (resp['status'] == 'OK') {
      number = resp['data']['orderNumber'];
    } else {
      number = "";
    }

    return number;
  }

  Future<SearchSuratJalanModel> getSuratJalan({
    required String token,
    required String company_id,
    required String rpp,
    required String keyword,
    required String page,
    required String startDate,
    required String endDate,
    required String dateType,
  }) async {
    var param = {
      "token": token,
      "page": page,
      "keyword": keyword,
      "rpp": rpp,
      "company_id": company_id,
      "start_date": startDate,
      "end_date": endDate,
      "date_type": dateType
    };
    final url = "${Constant.baseUrl}deliveryorder/getdelivery";
    final resp = await post(param: param, service: url);
    SearchSuratJalanModel data;
    // print(resp);
    if (resp['status'] == 'OK') {
      data = SearchSuratJalanModel.fromJson(resp['data']);
    } else {
      data = SearchSuratJalanModel();
    }

    return data;
  }

  Future<List<SuratJalanDetailModel>> getSuratJalanSetail({
    required String token,
    required String deliveryID,
  }) async {
    var param = {"token": token, "id": deliveryID};
    final url = "${Constant.baseUrl}deliveryorder/detaildelivery";
    final resp = await post(param: param, service: url);
    List<SuratJalanDetailModel> data;
    // print(resp['data']);
    if (resp['status'] == 'OK') {
      data = [];
      resp['data'].forEach((e) {
        final model = SuratJalanDetailModel.fromJson(e);
        data.add(model);
      });
    } else {
      data = [];
    }

    return data;
  }

  Future<String> cancelSuratJalan({
    required String token,
    required String deliveryID,
  }) async {
    var param = {"token": token, "id": deliveryID};
    final url = "${Constant.baseUrl}deliveryorder/cancel";
    final resp = await post(param: param, service: url);
    String data;
    // print(resp['data']);
    if (resp['status'] == 'OK') {
      data = resp['data'];
    } else {
      data = "";
    }

    return data;
  }

  Future<String> sendSuratJalan({
    required String token,
    required String deliveryID,
  }) async {
    var param = {"token": token, "id": deliveryID};
    final url = "${Constant.baseUrl}deliveryorder/send";
    final resp = await post(param: param, service: url);
    String data;
    // print(resp['data']);
    if (resp['status'] == 'OK') {
      data = resp['data'];
    } else {
      data = "";
    }

    return data;
  }

  Future<List<DestinationModel>> getDestination({
    required String token,
  }) async {
    var param = {"token": token};
    final url = "${Constant.baseUrl}deliveryorder/getdestination";
    final resp = await post(param: param, service: url);
    List<DestinationModel> data = List.empty(growable: true);
    if (resp['status'] == 'OK') {
      data = [];
      resp['data'].forEach((e) {
        final model = DestinationModel.fromJson(e);
        data.add(model);
      });
    } else {
      data = [];
    }

    return data;
  }

  Future<List<RegionalModel>> getRegional({
    required String token,
  }) async {
    var param = {"token": token};
    final url = "${Constant.baseUrl}deliveryorder/getregional";
    final resp = await post(param: param, service: url);
    List<RegionalModel> data = List.empty(growable: true);
    if (resp['status'] == 'OK') {
      data = [];
      resp['data'].forEach((e) {
        final model = RegionalModel.fromJson(e);
        data.add(model);
      });
    } else {
      data = [];
    }

    return data;
  }

  Future<String> sendQrCode(
      {required String token, required List<String> arrOrderID}) async {
    var param = {"token": token, 'arr_order_id': arrOrderID};
    final url = "${Constant.baseUrl}deliveryorder/sendqrcode";
    final resp = await post(param: param, service: url);
    String data = "";
    if (resp['status'] == 'OK') {
      data = "OK";
    } else {
      data = "ERROR";
    }

    return data;
  }
}
