import { Kas, TransaksiKas, Verbal } from "@/types";

export const getKas = async (token : string) => {
    return await fetch('/api/kas', {
        method : 'GET',
        headers : {
            'Accept' : 'application/json',
            'Authorization': `Bearer ${token}`
        }
    });
}

export const getKasById = async (id : string, token : string) => {
    return await fetch(`/api/kas/${id}`, {
        method : 'GET',
        headers : {
            'Accept' : 'application/json',
            'Authorization': `Bearer ${token}`
        }
    });
}

export const saveKas = async (kas: Kas, token : string) => {
    if(kas?.id){
        return await fetch(`/api/kas/${kas.id}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(kas)
        });
    } else {
        return await fetch('/api/kas', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(kas)
        });
    }
}

export async function saveTransaksiKas(trans : TransaksiKas, token : string) {
    if(!trans.sumber_kas){
        throw Error("Sumber kas tidak ditemukan");
    }
    if(trans.sumber_kas == '1'){
        return await fetch('/api/transaksi/kas', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(trans)
        });
    } else
    if(trans.sumber_kas == '2'){
        return await fetch('/api/transaksi/penempatan', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(trans)
        });
    }
}

export async function deleteTransaksiKas(transId : string, token : string) {
    return await fetch(`/api/transaksi/kas/${transId}`, {
        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
    });
}

export async function validasiTransaksiKas(transId : string, token : string) {
    return await fetch(`/api/transaksi/kas/${transId}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
    });
}

export async function getTransaksiKas(filter, token : string) {
    const params = new URLSearchParams();
    if (filter.tgl_awal) params.append('tgl_awal', filter.tgl_awal);
    if (filter.tgl_akhir) params.append('tgl_akhir', filter.tgl_akhir);
    if (filter.id) params.append('id', filter.id);

    return await fetch(`/api/transaksi/kas?${params.toString()}`,{
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
    });
}

export async function getVerbalKas(tanggal : string, token : string) {
    return await fetch(`/api/laporan/verbal?tanggal=${tanggal}`,{
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
    });
}

export async function simpanVerbalKas(verbal : Verbal, token : string) {
    const data = {
        ...verbal.uangLogam,
        ...verbal.uangKertas,
        tanggal : verbal.tanggal,
        total_kas_fisik : verbal.total_kas_fisik
    }
    return await fetch(`/api/laporan/verbal`,{
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${token}`
        },
        body: JSON.stringify(data)
    });
}


