const selectedLoation = JSON.parse(localStorage.getItem('selected-location'));
// let locationDb = null;
const searchBtn = document.querySelector('#create-search');
const locationContainer = document.querySelector('#location-container');
locationContainer.textContent = selectedLoation.name;
// getDblocationObject(selectedLoation);
// async function getDblocationObject(location) {
//     let loadingAnimation = startLoadingAnimation();
//     let res = await api.GET('/getLocationByLocationId.php?location_id='+location.id,true);
//     endLoadingAnimation(loadingAnimation);
//     if(!res.success)
//     {
//         showPopup('Fehler',res.msg);
//         return false;
//     }
//     locationDb = res.location;
//     locationContainer.textContent = locationDb.name;
// }

async function createSearch(e) {
    e.preventDefault();
    let fieldIds = ['type','radius','minRooms','maxRooms','minPrice','maxPrice'];

    let searchObj = {
        location_id: selectedLoation.id,
    }
    for(let id of fieldIds) {
        searchObj[id] = document.querySelector('#'+id).value;
    }

    if(!searchObj.radius) {
        showMsg('Bitte gib einen Radius ein',3000,'error');
        return false;
    }

    let loadingAnimation = startLoadingAnimation();
    let res = await api.POST('/createNewSearch.php',searchObj,true);
    endLoadingAnimation(loadingAnimation);

    if(!res.success) {
        showPopup('Fehler',res.msg);
        return false;
    }

    showMsg('Suche erfolgreich gespeichert',3000,'correct');
    return true;
}

searchBtn.addEventListener('click',createSearch)
