<script>

    let map;

    (function($){

        $('a[href="#cities"]').on("shown.bs.tab",function(){
            var opt = {
                center: new google.maps.LatLng($("#map-center-lat").val(), $("#map-center-lon").val()),
                zoom: 12,
                streetViewControl: false,
                mapTypeControl: false
            };
            map = new google.maps.Map(document.getElementById("map"),opt);
            var markerOpts = {
                title: $(document.getElementById("area.title")).val(),
                position: opt.center,
                draggable: true,
                map: map
            };

            displayPins();
        });

        function displayPins() {
            var arrays = [];
            $('#coordinates').eq(0).find('tr').each((r,row) => arrays.push($(row).find('td,th').map((c,cell) => $(cell).text()).toArray()))

            arrays.forEach(function(city) {
               var marker = new google.maps.Marker({
                   position: new google.maps.LatLng(city[0], city[1]),
                   map: map,
                   title: city[2]
               });
            });
        }


        // On sélectionne une commune à supprimer
        $('a.cities-remove').click(function (ev) {
            $('#attr-cities-id-remove').val($(this).data('id'));
        });

        // On ajoute une commune au secteur
        $('a.city-add').click(function (ev) {
            $("#attr-city-zipcode input, #attr-city-title input").prop('required', true);
            $("#attr-city-latitude input, #attr-city-longitude input").prop('required', false);
        });

    })(jQuery);

</script>