@extends('layouts.index')
@section('contenido')
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAvrv5r9q9VqybgrO0Vg6Qd_6AqVgOm2HU&callback=initMap" defer></script>
    <script>
        // JSON simulado con las ubicaciones
        const locations = [{
                "name": "Wilson Mardoqueo Sales Jerez",
                "lat": 14.634915,
                "lng": -90.506882
            },
            {
                "name": "Iving Villeda",
                "lat": 14.609053,
                "lng": -90.499411
            },
            {
                "name": "Carlos Marrroquin",
                "lat": 14.589546,
                "lng": -90.551444
            }
        ];

        let map;

        function initMap() {
            const center = {
                lat: 14.634915,
                lng: -90.506882
            };
            map = new google.maps.Map(document.getElementById("map"), {
                zoom: 10,
                center: center,
            });

            // Iterar sobre cada ubicación en el JSON y crear un marcador y una InfoWindow
            locations.forEach(function(location) {
                const marker = new google.maps.Marker({
                    position: {
                        lat: location.lat,
                        lng: location.lng
                    },
                    map: map,
                    title: location.name
                });

                const infoWindow = new google.maps.InfoWindow({
                    content: `<h5>${location.name}</h5>`
                });

                // Mostrar la InfoWindow inmediatamente
                infoWindow.open(map, marker);

                // Añadir listener para abrir la InfoWindow al hacer clic en el marcador
                marker.addListener('click', function() {
                    infoWindow.open(map, marker);
                });
            });
        }
    </script>
    <style>
        #map {
            min-height: 600px;
        }
    </style>
    <div class="content">
        <div class="row">
            <div class="col-md-6">
                <!-- Simple list -->
                <div class="card">
                    <div class="card-header header-elements-inline">
                        <h5 class="card-title">STATUS VISITADORES</h5>
                        <div class="header-elements">
                            <div class="list-icons">
                                <a class="list-icons-item" data-action="collapse"></a>
                            </div>
                        </div>
                    </div>

                    <div class="card-body">
                        <ul class="media-list listaAPM">
                            <!-- <li class="media font-weight-semibold py-1">ALFA</li> -->
                            @foreach ($apms as $apm)
                                <li class="media">
                                    <div class="media-body">
                                        <div class="media-title font-weight-semibold">
                                            {{ $apm['primer_nombre'] }} {{ $apm['segundo_nombre'] }}
                                            {{ $apm['primer_apellido'] }} {{ $apm['segundo_apellido'] }}
                                        </div>
                                        <span class="text-muted">{{ $apm->linea['descripcion_linea'] }}</span>
                                    </div>

                                    <div class="align-self-center ml-3">
                                        <div class="list-icons list-icons-extended">
                                            <!-- border-danger -->
                                            @if ($apm->status)
                                                <span class="badge badge-mark border-success"></span>
                                            @else
                                                <span class="badge badge-mark border-danger"></span>
                                            @endif

                                            <span class="text-success">
                                                @if (@$socios->id_apm == $apm['id_user'])
                                                    <i data-logitud="{{ $socios->coordenada_log }}"
                                                        data-latiud="{{ $socios->coordenada_lat }}"
                                                        data-nombre="{{ $socios->primer_nombre_socio }} {{ $socios->primer_apellido_socio }}"
                                                        class="mapaUbicacion icon-pin-alt font-size-base mr-1"></i>
                                                @endif
                                            </span>
                                        </div>
                                    </div>
                                </li>
                            @endforeach
                        </ul>
                    </div>
                </div>
                <!-- /simple list -->

            </div>
            <div class="col-md-6">
                <div id="map"></div>
            </div>
        </div>
    </div>
@endsection

@section('javascript')
    {{-- <script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBsmICYP3n5S2U6HCoAT_Phxe2AUqAcivI&callback=initMap&libraries=&v=weekly"
async
></script>
<script>
    function initMap() {
        const myLatlng = { lat: 14.58276, lng: -90.5773056 };
        const map = new google.maps.Map(document.getElementById("map"), {
            zoom: 10,
            center: myLatlng,
        });
        // Create the initial InfoWindow.
        // Configure the click listener.

    }

    jQuery( document ).ready(function() {
        $('.mapaUbicacion').on('click',function(){
            var ln=parseFloat($(this).attr('data-logitud'));
            var lt=parseFloat($(this).attr('data-latiud'));
            var n=$(this).attr('data-nombre');
            const myLatlng = { lat: lt, lng: ln };
            const map = new google.maps.Map(document.getElementById("map"), {
                zoom: 10,
                center: myLatlng,
            });
            // Create the initial InfoWindow.
            let infoWindow = new google.maps.InfoWindow({
                content: 'Socio: '+n,
                position: myLatlng,
            });
            infoWindow.open(map);
            // Configure the click listener.
            map.addListener("click", (mapsMouseEvent) => {
                // Close the current InfoWindow.
                infoWindow.close();
                // Create a new InfoWindow.
                infoWindow = new google.maps.InfoWindow({
                position: mapsMouseEvent.latLng,
                });
                infoWindow.setContent(
                JSON.stringify(mapsMouseEvent.latLng.toJSON(), null, 2)
                );
                infoWindow.open(map);
            });
        })
    });
</script> --}}
@endsection
