import { Bank, PenempatanBank } from "@/types";

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

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

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

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

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

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

export async function getTransaksiPenempatan(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.rekening) params.append('rekening', filter.rekening);

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