import 'package:app_petty_cash/model/list_type_model.dart';

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

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

  // list type
  Future<List<ListType>> getListType() async {
    final service = "${Constant.baseUrlDevone}transaction/list_type/";
    final resp = await post(
      param: {
        "": "",
      },
      service: service,
    );

    print("url list type : $service");

    final result = List<ListType>.empty(growable: true);
    resp['data'].forEach((e) {
      final model = ListType.fromJson(e);
      result.add(model);
    });
    return result;
  }

  // list category
  Future<List<ListCategory>> getListCategory() async {
    final service = "${Constant.baseUrlDevone}transaction/list_category/";
    final resp = await post(
      param: {
        "": "",
      },
      service: service,
    );

    print("url list category : $service");

    final result = List<ListCategory>.empty(growable: true);
    resp['data'].forEach((e) {
      final model = ListCategory.fromJson(e);
      result.add(model);
    });
    return result;
  }

  // insert transaksi
  Future<String> insertTransaksi(
      String tanggal,
      String tipe,
      String kategoriid,
      String jumlah,
      String catatan,
      String userid,
      String sender,
      String url) async {
    String paramPostInUrl = "";
    if (tipe == "KREDIT") {
      sender = "";
      paramPostInUrl =
          "?tanggal=$tanggal&tipe=$tipe&kategoriid=$kategoriid&jumlah=$jumlah&catatan=$catatan&url=$url&userid=$userid&sender=$sender";
    } else {
      if (tipe == "DEBIT") {
        kategoriid = "0";
        paramPostInUrl =
            "?tanggal=$tanggal&tipe=$tipe&kategoriid=$kategoriid&jumlah=$jumlah&catatan=$catatan&url=$url&userid=$userid&sender=$sender";
      }
    }
    // /?tanggal=2023-12-29&tipe=KREDIT&kategoriid=3&jumlah=5000&catatan=Lakban%20Besar&url=&userid=1&sender=

    final service =
        "${Constant.baseUrlDevone}transaction/addtransaction/$paramPostInUrl";
    final resp = await get(
      // param: {
      //   "": "",
      // },
      service: service,
    );

    print("url insert transaksi : $service");

    // final result = List<ListCategory>.empty(growable: true);
    // resp['data'].forEach((e) {
    //   final model = ListCategory.fromJson(e);
    //   result.add(model);
    // });

    return resp['status'];
  }
}
