File size: 4,695 Bytes
1ac84c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
let map;
let drawingManager;
let lastDrawnShape = null;
let displayedShapes = [];

const difficultyColors = {
    easy: '#34A853',   // Green
    medium: '#F9AB00', // Yellow
    hard: '#EA4335'    // Red
};

function initAdminMap() {
    map = new google.maps.Map(document.getElementById('map'), {
        center: { lat: 20, lng: 0 },
        zoom: 2,
    });

    drawingManager = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.RECTANGLE,
        drawingControl: true,
        drawingControlOptions: {
            position: google.maps.ControlPosition.TOP_CENTER,
            drawingModes: [google.maps.drawing.OverlayType.RECTANGLE],
        },
        rectangleOptions: {
            fillColor: '#F97316',
            fillOpacity: 0.3,
            strokeWeight: 1,
            clickable: true,
            editable: true,
            zIndex: 1,
        },
    });

    drawingManager.setMap(map);

    google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
        if (lastDrawnShape) {
            lastDrawnShape.setMap(null);
        }
        lastDrawnShape = event.overlay;
        drawingManager.setDrawingMode(null); // Exit drawing mode
        document.getElementById('save-zone').disabled = false;
    });

    document.getElementById('save-zone').addEventListener('click', saveLastZone);
    document.getElementById('new-zone-btn').addEventListener('click', () => {
        drawingManager.setDrawingMode(google.maps.drawing.OverlayType.RECTANGLE);
        document.getElementById('save-zone').disabled = true;
    });

    loadExistingZones();
}

function saveLastZone() {
    if (!lastDrawnShape) {
        alert('Please draw a zone first.');
        return;
    }

    const difficulty = document.getElementById('difficulty-select').value;
    const bounds = lastDrawnShape.getBounds().toJSON();

    const zoneData = {
        type: 'rectangle',
        bounds: bounds,
    };

    fetch('/api/zones', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ difficulty: difficulty, zone: zoneData }),
    })
    .then(response => response.json())
    .then(data => {
        const statusMsg = document.getElementById('status-message');
        statusMsg.textContent = data.message || `Error: ${data.error}`;
        setTimeout(() => statusMsg.textContent = '', 3000);
        
        // Clean up the drawn shape and reload all zones to get the new one with its listener
        if (lastDrawnShape) {
            lastDrawnShape.setMap(null);
            lastDrawnShape = null;
        }
        document.getElementById('save-zone').disabled = true;
        loadExistingZones();
    });
}

function loadExistingZones() {
    // Clear existing shapes from the map
    displayedShapes.forEach(shape => shape.setMap(null));
    displayedShapes = [];

    fetch('/api/zones')
        .then(response => response.json())
        .then(zones => {
            for (const difficulty in zones) {
                zones[difficulty].forEach(zone => {
                    if (zone.type === 'rectangle') {
                        const rectangle = new google.maps.Rectangle({
                            bounds: zone.bounds,
                            map: map,
                            fillColor: difficultyColors[difficulty],
                            fillOpacity: 0.35,
                            strokeColor: difficultyColors[difficulty],
                            strokeWeight: 2,
                            editable: false,
                            clickable: true,
                        });

                        rectangle.zoneId = zone.id;
                        
                        google.maps.event.addListener(rectangle, 'click', function() {
                            if (confirm('Are you sure you want to delete this zone?')) {
                                deleteZone(this.zoneId, this);
                            }
                        });

                        displayedShapes.push(rectangle);
                    }
                });
            }
        });
}

function deleteZone(zoneId, shape) {
    fetch('/api/zones', {
        method: 'DELETE',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ zone_id: zoneId })
    })
    .then(response => response.json())
    .then(data => {
        const statusMsg = document.getElementById('status-message');
        statusMsg.textContent = data.message || `Error: ${data.error}`;
        setTimeout(() => statusMsg.textContent = '', 3000);

        if (data.message) {
            shape.setMap(null);
        }
    });
}