import { Akun } from "@/types";

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

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

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

export const deleteAkun = async (akun: Akun, token : string ) => {
    console.log(akun?.kode_akun);
    return await fetch(`/api/akun/${akun?.kode_akun}`, {
        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
    });
}

