<!-- Modal HTML Markup -->
<html>
    @push('headerScripts')
        <script type='text/javascript' src='https://api.mapy.cz/loader.js'></script>
        <script type='text/javascript' >Loader.load()</script>
    @endpush
    <div id="showAdresy" class="modal fade">
        <div class="modal-dialog">
            <div style="position: absolute; float: left; left: 50%; transform: translate(-50%, 10%);">
                <div class="modal-content" style="width: 600px; height: 400px;">
                    <input type="hidden" id="id_stanice_modal" value="0">
                    <div class="modal-header">
                        <h1 class="modal-title">Nalezené adresy</h1>
                    </div>
                    <div class="modal-body">
                        <div class="card-body">
                            <label for="hleday_vyraz">Hledáno podle výrazu: </label><span class="pl-1" id="hleday_vyraz"></span>
                            <div id="adresy" style="height: 200px;">

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<script type="application/javascript">

    var adresaPole = [];
    function vratSouradnice(val)
    {
        $('#souradnice_x').css('background-color','');
        $('#souradnice_y').css('background-color','');
        $('#hleday_vyraz').empty();
        $('#hleday_vyraz').append(val);
        new SMap.Geocoder(val, odpoved,
            {
            bbox: [SMap.Coords.fromWGS84(12.09, 51.06), SMap.Coords.fromWGS84(18.87, 48.55)]
            });
    }

    function odpoved(geocoder)
    {
        if (!geocoder.getResults()[0].results.length)
        {
            $('#adresy').empty();
            $('#adresy').append('Bohužel místo nebylo nalezeno :( '+geocoder);
            return;
        }

        var vysledky = geocoder.getResults()[0].results;
        var data = [];
        var i = 0;
        while (vysledky.length)
        {
            var item = vysledky.shift()
            var souradnice_odectene=item.coords.toJTSK(2).reverse().join(' ');
            array= souradnice_odectene.split(' ');
            adresy = item.label.split(', ');
            var adresaText = "";
            if(adresy[0] !== undefined)
            {
                adresaText = adresy[0];
            }
            if(adresy[1] !== undefined)
            {
                adresaText = adresaText +", "+adresy[1];
            }
            if(adresy[3] !== undefined)
            {
                adresaText = adresaText +", "+adresy[3];
            }
            adresaPole.push(adresaText);
            data.push(adresaText + "<button type='button' class='pl-2 ml-2 btn btn-link' onclick='priradSouradnice("+array[1]+","+array[0]+","+ i +");'>Vybrat</button><br>");
            i++;
        }
        {
            $('#adresy').empty();
            $('#adresy').append("<div class='w-100 h-100 overflow-auto'>"+data.join("\n")+"</div>");
        }
    }

    function priradSouradnice(x,y,i)
    {
        $('#souradnice_x').val(Math.round(x));
        $('#souradnice_y').val(Math.round(y));
        $('#souradnice_x').css('background-color','limegreen');
        $('#souradnice_y').css('background-color','limegreen');
        $('#adresa').val(adresaPole[i]);
        $("#showAdresy").modal('hide');
    }
</script>

</html>
