import { Anggota } from "@/types";

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

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

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

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

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

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