File size: 50,021 Bytes
ca371b2
264ecd0
 
 
ca371b2
 
264ecd0
20fbfc4
264ecd0
ca371b2
 
 
 
 
264ecd0
 
 
 
ca371b2
62e311a
e7d96fe
ca371b2
62e311a
ca371b2
62e311a
ca371b2
 
 
62e311a
ca371b2
 
 
 
62e311a
ca371b2
e7d96fe
62e311a
ca371b2
e7d96fe
62e311a
ca371b2
 
1ab2920
 
 
 
 
 
 
ca371b2
 
 
62e311a
1ab2920
ca371b2
 
62e311a
e7d96fe
ca371b2
 
 
 
 
 
 
62e311a
ca371b2
 
62e311a
ca371b2
 
62e311a
ca371b2
 
 
 
 
 
 
 
62e311a
e7d96fe
ca371b2
 
 
 
4c36de1
62e311a
 
4c36de1
ca371b2
 
62e311a
ca371b2
 
 
 
 
 
62e311a
ca371b2
 
 
 
 
 
 
 
 
 
324a397
 
 
2bf1d33
 
 
ca371b2
 
 
 
 
ee992c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca371b2
 
 
 
ee992c9
 
 
 
ca371b2
 
 
324a397
 
 
 
 
 
 
 
 
 
 
 
 
 
2bf1d33
f75a344
2bf1d33
6aa976f
2bf1d33
f75a344
 
 
2bf1d33
 
6aa976f
 
 
 
 
f75a344
2bf1d33
 
 
f75a344
2bf1d33
 
6aa976f
 
 
 
 
 
 
2bf1d33
 
f75a344
 
6aa976f
 
f75a344
 
6aa976f
 
 
 
 
 
 
 
 
 
 
f75a344
 
 
2bf1d33
 
 
 
 
 
 
 
 
 
 
 
 
 
ca371b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264ecd0
 
ca371b2
 
 
b306155
 
ca371b2
6e2409e
264ecd0
b306155
6e2409e
 
1c55618
ca371b2
b306155
 
ca371b2
264ecd0
6e2409e
2bf1d33
 
6e2409e
1c55618
2bf1d33
 
 
 
6e2409e
1c55618
2bf1d33
 
 
ca371b2
6e2409e
1c55618
2bf1d33
 
 
 
 
 
 
 
 
 
 
 
 
264ecd0
7c9c127
a0758f7
 
b306155
 
6e2409e
 
 
7c9c127
6e2409e
a0758f7
7c9c127
 
6e2409e
7c9c127
6e2409e
 
 
7c9c127
6e2409e
 
 
7c9c127
6e2409e
 
 
 
 
 
 
a0758f7
 
 
 
ca371b2
 
 
 
264ecd0
a0758f7
264ecd0
 
 
ca371b2
264ecd0
ca371b2
264ecd0
ca371b2
264ecd0
 
ca371b2
264ecd0
ca371b2
264ecd0
 
ee992c9
 
 
 
 
 
 
 
ca371b2
264ecd0
 
 
 
ca371b2
264ecd0
 
 
 
ca371b2
264ecd0
 
ca371b2
 
 
e60c2f9
 
 
 
 
 
 
ca371b2
 
 
d06b99c
 
 
 
 
 
 
 
 
ca371b2
 
d06b99c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2150b01
ca371b2
 
d06b99c
 
 
 
ca371b2
264ecd0
ca371b2
264ecd0
ca371b2
 
 
264ecd0
ca371b2
 
264ecd0
 
ca371b2
 
 
 
264ecd0
 
ca371b2
 
264ecd0
 
ca371b2
ee992c9
 
 
 
ca371b2
 
 
ee992c9
 
 
 
ca371b2
 
 
e60c2f9
 
ca371b2
264ecd0
ca371b2
e60c2f9
 
 
 
 
 
 
 
ca371b2
e60c2f9
 
 
 
 
 
 
 
 
 
 
 
 
264ecd0
 
ca371b2
 
2150b01
ca371b2
 
ee992c9
 
 
 
 
 
6e2409e
ee992c9
1c55618
2150b01
ee992c9
 
ca371b2
 
 
 
 
 
 
264ecd0
ca371b2
 
 
 
 
 
 
 
 
 
4de3e9c
ca371b2
 
 
 
 
 
 
 
 
 
 
 
2150b01
 
 
 
 
ca371b2
 
2150b01
 
 
 
ca371b2
 
 
264ecd0
ca371b2
 
4de3e9c
 
 
 
 
 
2150b01
ca371b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9197106
ca371b2
 
 
62e311a
ca371b2
9de2c7c
e7d96fe
 
62e311a
e7d96fe
 
 
 
3047280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6bca2b
 
 
 
9de2c7c
62e311a
e7d96fe
 
 
 
62e311a
e7d96fe
a6bca2b
 
62e311a
a6bca2b
e7d96fe
 
 
62e311a
e7d96fe
 
 
 
62e311a
e7d96fe
 
 
 
62e311a
e7d96fe
 
62e311a
e7d96fe
 
 
 
 
 
 
 
 
62e311a
e7d96fe
 
 
 
 
 
 
 
 
62e311a
a6bca2b
 
9de2c7c
 
9197106
3047280
 
 
 
9197106
3047280
a26c316
a0758f7
ca371b2
62e311a
4c36de1
ca371b2
264ecd0
a26c316
 
 
 
 
 
62e311a
a26c316
 
 
 
 
62e311a
264ecd0
a26c316
 
 
 
 
 
2bf1d33
a26c316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62e311a
a26c316
 
 
 
 
 
 
62e311a
a26c316
 
 
 
 
 
 
 
 
f75a344
 
 
 
 
 
 
d06b99c
 
a26c316
44730c0
a26c316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf43a83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a26c316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4de3e9c
a0758f7
 
 
 
ca371b2
 
 
 
4de3e9c
ca371b2
2150b01
 
4de3e9c
 
 
ca371b2
 
 
 
 
 
 
439b97d
ca371b2
 
b306155
ca371b2
4c36de1
 
a662e75
 
b306155
a662e75
 
 
 
 
ca371b2
 
a662e75
 
 
 
 
 
 
79b15bc
 
a662e75
 
 
2150b01
a662e75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439b97d
a662e75
 
 
 
 
 
 
439b97d
a662e75
 
 
 
 
264ecd0
 
 
ca371b2
a662e75
 
 
 
 
 
 
ca371b2
264ecd0
ca371b2
264ecd0
 
ca371b2
a0758f7
 
 
 
 
 
ca371b2
 
 
 
264ecd0
 
ca371b2
 
 
fa7e64a
 
 
 
ca371b2
 
 
fa7e64a
264ecd0
fa7e64a
264ecd0
fa7e64a
264ecd0
ca371b2
fa7e64a
ca371b2
 
fa7e64a
 
264ecd0
 
ca371b2
 
 
 
 
 
 
 
 
 
 
 
 
 
2150b01
264ecd0
ca371b2
 
 
 
 
 
 
264ecd0
ca371b2
 
 
264ecd0
ca371b2
 
 
264ecd0
 
ca371b2
264ecd0
ca371b2
 
264ecd0
 
ca371b2
 
264ecd0
 
ca371b2
 
 
 
264ecd0
ca371b2
 
 
264ecd0
 
ca371b2
 
 
 
264ecd0
ca371b2
 
264ecd0
 
 
 
ca371b2
 
264ecd0
e7d96fe
ca371b2
4c36de1
 
 
264ecd0
4c36de1
e7d96fe
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
// TreeTrack Enhanced Map with Authentication and Tree Management
class TreeTrackMap {
    constructor() {
        this.map = null;
        this.tempMarker = null;
        this.selectedLocation = null;
        this.treeMarkers = [];
        this.markerClusterGroup = null;
        this.userLocation = null;
        this.isLocationSelected = false;
        
        // Authentication properties
        this.currentUser = null;
        this.authToken = null;
        
        this.init();
    }

    async init() {
        console.log('TreeTrackMap initialization started');
        
        // Check authentication first
        console.log('Checking authentication...');
        if (!await this.checkAuthentication()) {
            console.log('Authentication failed, redirecting to login');
            window.location.href = '/login';
            return;
        }
        console.log('Authentication successful');
        
        this.showLoading();
        
        try {
            console.log('Initializing map...');
            await this.initializeMap();
            
            console.log('Setting up event listeners...');
            this.setupEventListeners();
            
            console.log('Setting up user interface...');
            this.setupUserInterface();
            
            // Load trees asynchronously without blocking UI
            console.log('Starting tree loading (async)...');
            this.loadTrees().catch(error => {
                console.error('Tree loading failed:', error);
                this.showMessage('Failed to load trees', 'error');
            });
            
            setTimeout(() => {
                this.hideLoading();
                this.showGestureHint();
                console.log('Map initialization complete!');
            }, 500);
            
        } catch (error) {
            console.error('Map initialization failed:', error);
            console.error('Error stack:', error.stack);
            this.showMessage('Failed to initialize map. Please refresh the page.', 'error');
            this.hideLoading();
        }
    }
    
    // Authentication methods
    async checkAuthentication() {
        console.log('Checking for auth token...');
        const token = localStorage.getItem('auth_token');
        if (!token) {
            console.log('No auth token found in localStorage');
            return false;
        }
        console.log('Auth token found, validating...');
        
        try {
            const response = await fetch('/api/auth/validate', {
                headers: {
                    'Authorization': `Bearer ${token}`
                }
            });
            
            console.log('Auth validation response status:', response.status);
            
            if (response.ok) {
                const data = await response.json();
                this.currentUser = data.user;
                this.authToken = token;
                
                console.log('Authentication successful for user:', data.user.username);
                console.log('User permissions:', data.user.permissions);
                
                return true;
            } else {
                console.log('Token invalid, status:', response.status);
                // Token invalid, remove it
                localStorage.removeItem('auth_token');
                localStorage.removeItem('user_info');
                return false;
            }
        } catch (error) {
            console.error('Auth validation error:', error);
            return false;
        }
    }
    
    setupUserInterface() {
        // Add user info to header
        this.displayUserInfo();
        
        // Add logout functionality
        this.addLogoutButton();
        
        // Show welcome button only for demo users
        this.setupWelcomeButton();
        
        // Setup instructions panel
        this.setupInstructionsPanel();
    }
    
    displayUserInfo() {
        if (!this.currentUser) return;
        
        // Update existing user info elements in the HTML
        const userAvatar = document.getElementById('userAvatar');
        const userName = document.getElementById('userName');
        const userRole = document.getElementById('userRole');
        
        if (userAvatar && this.currentUser.full_name) {
            userAvatar.textContent = this.currentUser.full_name.charAt(0).toUpperCase();
        }
        
        if (userName) {
            userName.textContent = this.currentUser.full_name || this.currentUser.username || 'User';
        }
        
        if (userRole) {
            userRole.textContent = this.currentUser.role || 'User';
        }
    }
    
    addLogoutButton() {
        // Use existing logout button instead of creating a new one
        const existingLogoutBtn = document.getElementById('logoutBtn');
        if (existingLogoutBtn) {
            existingLogoutBtn.addEventListener('click', () => this.logout());
        }
    }
    
    isDemoUser() {
        if (!this.currentUser) return false;
        return this.currentUser.role === 'demo_user' || 
               this.currentUser.username === 'demo_user' ||
               (this.currentUser.permissions && this.currentUser.permissions.includes('demo_view'));
    }
    
    setupWelcomeButton() {
        const welcomeBtn = document.getElementById('welcomeBtn');
        if (welcomeBtn && this.isDemoUser()) {
            welcomeBtn.style.display = 'inline-flex';
        }
    }
    
    setupInstructionsPanel() {
        // Always show instructions panel (persistent like map controls)
        const instructionsPanel = document.getElementById('instructionsPanel');
        const toggleBtn = document.getElementById('instructionsToggle');
        
        if (instructionsPanel) {
            // Keep instructions visible (remove hidden class if present)
            instructionsPanel.classList.remove('hidden');
        }
        
        if (toggleBtn) {
            // Hide toggle button initially since panel is visible
            toggleBtn.classList.add('hidden');
        }
        
        // Setup close/toggle functionality for manual control
        const closeBtn = document.getElementById('closeInstructions');
        if (closeBtn) {
            closeBtn.addEventListener('click', () => {
                this.toggleInstructions();
            });
        }
        
        // Setup toggle button functionality
        if (toggleBtn) {
            toggleBtn.addEventListener('click', () => {
                this.toggleInstructions();
            });
        }
    }
    
    toggleInstructions() {
        const instructionsPanel = document.getElementById('instructionsPanel');
        const toggleBtn = document.getElementById('instructionsToggle');
        
        if (instructionsPanel) {
            instructionsPanel.classList.toggle('hidden');
            
            // Show/hide toggle button based on panel visibility
            if (toggleBtn) {
                if (instructionsPanel.classList.contains('hidden')) {
                    // Panel is hidden, show toggle button
                    toggleBtn.classList.remove('hidden');
                } else {
                    // Panel is visible, hide toggle button
                    toggleBtn.classList.add('hidden');
                }
            }
        }
    }
    
    hideInstructions() {
        const instructionsPanel = document.getElementById('instructionsPanel');
        if (instructionsPanel) {
            instructionsPanel.classList.add('hidden');
        }
    }
    
    showInstructions() {
        const instructionsPanel = document.getElementById('instructionsPanel');
        if (instructionsPanel) {
            instructionsPanel.classList.remove('hidden');
        }
    }
    
    async logout() {
        try {
            await fetch('/api/auth/logout', {
                method: 'POST',
                headers: {
                    'Authorization': `Bearer ${this.authToken}`
                }
            });
        } catch (error) {
            console.error('Logout error:', error);
        } finally {
            localStorage.removeItem('auth_token');
            localStorage.removeItem('user_info');
            window.location.href = '/login';
        }
    }
    
    // Enhanced API calls with authentication
    async authenticatedFetch(url, options = {}) {
        const headers = {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${this.authToken}`,
            ...options.headers
        };
        
        const response = await fetch(url, {
            ...options,
            headers
        });
        
        if (response.status === 401) {
            // Token expired or invalid
            localStorage.removeItem('auth_token');
            localStorage.removeItem('user_info');
            window.location.href = '/login';
            return null;
        }
        
        return response;
    }
    
    // Permission checking methods
    canEditTree(createdBy) {
        if (!this.currentUser) return false;
        
        // Admin and system can edit any tree
        if (this.currentUser.permissions.includes('admin') || this.currentUser.permissions.includes('system')) {
            return true;
        }
        
        // Users can edit trees they created
        if (this.currentUser.permissions.includes('edit_own') && createdBy === this.currentUser.username) {
            return true;
        }
        
        // Users with delete permission can edit any tree
        if (this.currentUser.permissions.includes('delete')) {
            return true;
        }
        
        return false;
    }
    
    canDeleteTree(createdBy) {
        if (!this.currentUser) return false;
        
        // Only admin and system can delete trees
        if (this.currentUser.permissions.includes('admin') || this.currentUser.permissions.includes('system')) {
            return true;
        }
        
        // Users with explicit delete permission
        if (this.currentUser.permissions.includes('delete')) {
            return true;
        }
        
        return false;
    }

    async initializeMap() {
        console.log('Initializing map...');
        
        // Center on Tezpur, Assam - the actual research area
        const tezpurLocation = [26.6340, 92.7840]; // Tezpur coordinates
        
        // Initialize map with clustering-optimized zoom level
        this.map = L.map('map', {
            center: tezpurLocation,
            zoom: 15, // Start with good clustering view (matches center view)
            maxZoom: 17, // Allow enough zoom for spiderfy to work properly
            minZoom: 10, // Set reasonable minimum zoom
            zoomControl: true,
            attributionControl: true,
            preferCanvas: true // Better performance for many markers
        });

        // Add multiple beautiful tile layers with spiderfy-compatible zoom limits
        const satelliteLayer = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
            attribution: 'Tiles © Esri — Source: Esri, Maxar, GeoEye, Earthstar Geographics, CNES/Airbus DS, USDA, AeroGRID, IGN, and the GIS User Community',
            maxZoom: 17, // Allow higher zoom for spiderfy functionality
            minZoom: 10
        });
        
        const terrainLayer = L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}{r}.png', {
            attribution: 'Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors',
            maxZoom: 17,
            minZoom: 10
        });
        
        const streetLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '© OpenStreetMap contributors',
            maxZoom: 17,
            minZoom: 10
        });

        // Use satellite as primary layer for Tezpur's natural beauty
        satelliteLayer.addTo(this.map);
        
        // Add layer control for users to switch
        const baseMaps = {
            "Satellite": satelliteLayer,
            "Terrain": terrainLayer,
            "Street Map": streetLayer
        };
        
        L.control.layers(baseMaps).addTo(this.map);

        // Initialize marker clustering with clean spiderfy behavior (no blue polygons)
        this.markerClusterGroup = L.markerClusterGroup({
            chunkedLoading: true,
            chunkInterval: 100, // Faster processing for single query
            chunkDelay: 25,     // Reduced delay
            maxClusterRadius: 60, // Balanced radius for good clustering and spiderfy
            spiderfyOnMaxZoom: true, // Enable spiral/flower view on max zoom
            spiderfyDistanceMultiplier: 1.5, // Larger spiral spread
            showCoverageOnHover: false, // DISABLE blue polygon coverage - it's hideous!
            zoomToBoundsOnClick: false, // Disable auto-zoom, prefer spiderfy
            removeOutsideVisibleBounds: true, // Performance optimization
            animate: false, // Disable animations to prevent ghost trees
            animateAddingMarkers: false, // Disable marker addition animations
            disableClusteringAtZoom: 18, // Allow clustering until very high zoom
            // Improved spiderfy settings for smoother spiral view without ghosts
            spiderfyShapePositions: function(count, centerPt) {
                const positions = [];
                const angleStep = (2 * Math.PI) / count;
                const baseRadius = 45; // Base distance from center
                
                for (let i = 0; i < count; i++) {
                    const angle = i * angleStep;
                    const spiralRadius = baseRadius + (i * 3); // Tighter spiral to reduce ghosting
                    positions.push({
                        x: centerPt.x + spiralRadius * Math.cos(angle),
                        y: centerPt.y + spiralRadius * Math.sin(angle)
                    });
                }
                return positions;
            }
        });
        
        this.map.addLayer(this.markerClusterGroup);

        // Map click handler for pin dropping
        this.map.on('click', (e) => {
            this.onMapClick(e);
        });

        console.log('Map initialized successfully with clustering');
    }

    setupEventListeners() {
        console.log('Setting up event listeners...');

        // My Location button
        document.getElementById('myLocationBtn').addEventListener('click', () => {
            this.getCurrentLocation();
        });

        // Clear Pins button
        document.getElementById('clearPinsBtn').addEventListener('click', () => {
            this.clearTempMarker();
        });

        // Center Map button
        const centerMapBtn = document.getElementById('centerMapBtn');
        if (centerMapBtn) {
            centerMapBtn.addEventListener('click', () => {
                this.centerMapToTrees();
            });
        }

        // Use Location button
        document.getElementById('useLocationBtn').addEventListener('click', () => {
            this.useSelectedLocation();
        });

        // Cancel button
        document.getElementById('cancelBtn').addEventListener('click', () => {
            this.cancelLocationSelection();
        });

        console.log('Event listeners setup complete');
    }

    onMapClick(e) {
        console.log('Map clicked at:', e.latlng);
        
        // Remove existing temp marker first (before setting new location)
        if (this.tempMarker) {
            this.map.removeLayer(this.tempMarker);
            this.tempMarker = null;
        }
        
        // Now set the new location
        this.selectedLocation = e.latlng;
        this.isLocationSelected = true;

        // Create beautiful tree-shaped temp marker with red coloring for selection
        const tempColors = {
            canopy1: '#ef4444',    // Red for temp marker
            canopy2: '#dc2626',    
            canopy3: '#b91c1c',    
            trunk: '#7f5a44',      // Keep trunk natural
            shadow: 'rgba(185, 28, 28, 0.4)'
        };
        
        const tempIcon = L.divIcon({
            html: `
                <div class="map-marker temp-marker" style="filter: drop-shadow(2px 3px 6px ${tempColors.shadow});">
                    <svg width="36" height="44" viewBox="0 0 36 44" fill="none" style="transition: transform 0.2s ease;">
                        <!-- Tree Shadow/Base -->
                        <ellipse cx="18" cy="42" rx="7" ry="1.5" fill="${tempColors.shadow}" opacity="0.4"/>
                        
                        <!-- Tree Trunk -->
                        <path d="M16 35 Q16 37 16.5 39 Q17 41 17.5 42 Q18 42.5 18 42.5 Q18 42.5 18.5 42 Q19 41 19.5 39 Q20 37 20 35 L20 29 Q19.8 28 19 27.5 Q18 27 18 27 Q18 27 17 27.5 Q16.2 28 16 29 Z" fill="${tempColors.trunk}" stroke="#6b4a39" stroke-width="0.4"/>
                        
                        <!-- Tree Trunk Texture -->
                        <path d="M17 29 Q17 32 17 35" stroke="#5a3e32" stroke-width="0.2" opacity="0.6"/>
                        <path d="M19 30 Q19 33 19 36" stroke="#5a3e32" stroke-width="0.2" opacity="0.6"/>
                        
                        <!-- Main Canopy (Back Layer) -->
                        <circle cx="18" cy="20" r="10" fill="${tempColors.canopy3}" opacity="0.8"/>
                        
                        <!-- Secondary Canopy Clusters -->
                        <circle cx="14" cy="18" r="7" fill="${tempColors.canopy2}" opacity="0.85"/>
                        <circle cx="22" cy="17" r="6" fill="${tempColors.canopy2}" opacity="0.85"/>
                        <circle cx="16" cy="14" r="5" fill="${tempColors.canopy1}" opacity="0.9"/>
                        <circle cx="21" cy="22" r="5.5" fill="${tempColors.canopy2}" opacity="0.85"/>
                        
                        <!-- Top Canopy (Brightest) -->
                        <circle cx="18" cy="16" r="6" fill="${tempColors.canopy1}"/>
                        
                        <!-- Highlight clusters for 3D effect -->
                        <circle cx="15" cy="14" r="2.5" fill="#fca5a5" opacity="0.7"/>
                        <circle cx="21" cy="18" r="2" fill="#fca5a5" opacity="0.6"/>
                        <circle cx="16" cy="21" r="1.5" fill="#fca5a5" opacity="0.5"/>
                        
                        <!-- Small light spots -->
                        <circle cx="13" cy="13" r="0.8" fill="#fee2e2" opacity="0.8"/>
                        <circle cx="20" cy="15" r="0.6" fill="#fee2e2" opacity="0.9"/>
                        <circle cx="23" cy="20" r="0.5" fill="#fee2e2" opacity="0.7"/>
                    </svg>
                </div>
            `,
            className: 'custom-marker-icon tree-pin-temp realistic-tree-temp',
            iconSize: [36, 44],
            iconAnchor: [18, 42],
            popupAnchor: [0, -44]
        });

        this.tempMarker = L.marker([e.latlng.lat, e.latlng.lng], { icon: tempIcon }).addTo(this.map);

        // Update coordinates display
        document.getElementById('latValue').textContent = e.latlng.lat.toFixed(6);
        document.getElementById('lngValue').textContent = e.latlng.lng.toFixed(6);

        // Show info panel
        this.showInfoPanel();
    }

    clearTempMarker() {
        if (this.tempMarker) {
            this.map.removeLayer(this.tempMarker);
            this.tempMarker = null;
        }
        this.selectedLocation = null;
        this.isLocationSelected = false;
        this.hideInfoPanel();
    }

    showInfoPanel() {
        const panel = document.getElementById('locationPanel');
        if (panel) {
            panel.classList.add('active');
        }
    }

    hideInfoPanel() {
        const panel = document.getElementById('locationPanel');
        if (panel) {
            panel.classList.remove('active');
        }
    }

    useSelectedLocation() {
        if (!this.selectedLocation || !this.isLocationSelected) {
            this.showMessage('No location selected. Please click on the map to drop a pin first.', 'error');
            return;
        }

        try {
            // Store location for the form page
            const locationData = {
                lat: this.selectedLocation.lat,
                lng: this.selectedLocation.lng
            };
            
            localStorage.setItem('selectedLocation', JSON.stringify(locationData));

            // Clear any previous messages and show success
            const messageElement = document.getElementById('message');
            if(messageElement) messageElement.classList.remove('show');
            this.showMessage('Location saved! Redirecting to form...', 'success');
            
            // Redirect after a short delay
            setTimeout(() => {
                window.location.href = '/';
            }, 1500);
        } catch (error) {
            console.error('Error saving location:', error);
            this.showMessage('Error saving location. Please try again.', 'error');
        }
    }

    cancelLocationSelection() {
        this.clearTempMarker();
        this.showMessage('Selection cancelled', 'info');
    }

    centerMapToTrees() {
        if (this.treeMarkers.length === 0) {
            this.showMessage('No trees to center on', 'info');
            return;
        }

        // Fit map to show all trees with clustering view (like screenshot)
        const group = new L.featureGroup(this.treeMarkers);
        this.map.fitBounds(group.getBounds().pad(0.1), { maxZoom: 15 });
        this.showMessage('Map centered on all trees', 'success');
    }

    getCurrentLocation() {
        console.log('Getting current location...');
        
        if (!navigator.geolocation) {
            this.showMessage('Geolocation not supported by this browser', 'error');
            return;
        }

        const myLocationBtn = document.getElementById('myLocationBtn');
        myLocationBtn.textContent = 'Getting...';
        myLocationBtn.disabled = true;

        navigator.geolocation.getCurrentPosition(
            (position) => {
                console.log('Location found:', position.coords);
                
                const lat = position.coords.latitude;
                const lng = position.coords.longitude;
                
                this.userLocation = { lat, lng };

                // Center map on user location
                this.map.setView([lat, lng], 16);

                // Add user location marker
                if (this.userLocationMarker) {
                    this.map.removeLayer(this.userLocationMarker);
                }

                const userIcon = L.divIcon({
                    html: `
                        <div class="map-marker user-marker">
                            <svg width="24" height="32" viewBox="0 0 24 32" fill="none">
                                <path d="M12 0C5.37 0 0 5.37 0 12C0 21 12 32 12 32S24 21 24 12C24 5.37 18.63 0 12 0Z" fill="#3b82f6"/>
                                <circle cx="12" cy="12" r="4" fill="white"/>
                            </svg>
                        </div>
                    `,
                    className: 'custom-marker-icon',
                    iconSize: [24, 32],
                    iconAnchor: [12, 32],
                    popupAnchor: [0, -32]
                });

                this.userLocationMarker = L.marker([lat, lng], { icon: userIcon }).addTo(this.map);

                // Add tooltip
                this.userLocationMarker.bindTooltip('Your Location', {
                    permanent: false,
                    direction: 'top',
                    offset: [0, -10],
                    className: 'tree-tooltip'
                });

                this.showMessage('Location found successfully', 'success');
                
                myLocationBtn.textContent = 'My Location';
                myLocationBtn.disabled = false;
            },
            (error) => {
                console.error('Geolocation error:', error);
                let errorMessage = 'Failed to get location';
                
                switch (error.code) {
                    case error.PERMISSION_DENIED:
                        errorMessage = 'Location access denied by user';
                        break;
                    case error.POSITION_UNAVAILABLE:
                        errorMessage = 'Location information unavailable';
                        break;
                    case error.TIMEOUT:
                        errorMessage = 'Location request timed out';
                        break;
                }
                
                this.showMessage(errorMessage, 'error');
                myLocationBtn.textContent = 'My Location';
                myLocationBtn.disabled = false;
            },
            {
                enableHighAccuracy: true,
                timeout: 10000,
                maximumAge: 60000
            }
        );
    }

    async loadTrees() {
        console.log('🗺️ Starting tree loading process (RLS-enabled backend)...');
        
        try {
            // Clear existing tree markers
            console.log('Clearing existing tree markers...');
            this.clearTreeMarkers();
            
            // Check if we have auth token
            if (!this.authToken) {
                console.error('No auth token available for API calls');
                this.showMessage('Authentication required to load trees', 'error');
                return;
            }
            
            // Check for cached tree data first
            const cacheKey = `treetrack_trees_${this.currentUser.username}`;
            const cachedData = localStorage.getItem(cacheKey);
            const cacheTimestamp = localStorage.getItem(`${cacheKey}_timestamp`);
            
            // Use cached data if it's less than 30 minutes old
            const cacheAge = Date.now() - parseInt(cacheTimestamp || '0');
            const maxCacheAge = 30 * 60 * 1000; // 30 minutes
            
            if (cachedData && cacheAge < maxCacheAge) {
                console.log('Using cached tree data (age: ' + Math.round(cacheAge / 1000) + 's)');
                const allTrees = JSON.parse(cachedData);
                this.renderTreesToMap(allTrees);
                this.showMessage(`Loaded ${allTrees.length} trees from cache`, 'success');
                return;
            }
            
            console.log('Cache miss or expired, loading fresh data...');
            
            // Load trees in batches
            let allTrees = [];
            let offset = 0;
            const batchSize = 1000; // API batch size (Supabase limit)
            let hasMoreTrees = true;
            
            console.log('Starting batch loading process...');
            const treeCountEl = document.getElementById('treeCount');
            if (treeCountEl) {
                treeCountEl.textContent = 'Loading trees...';
            } else {
                console.warn('treeCount element not found');
            }
            
            while (hasMoreTrees && allTrees.length < 3000) { // Safety limit
                console.log(`Loading batch: offset=${offset}, limit=${batchSize}`);
                
                try {
                    const response = await this.authenticatedFetch(`/api/trees?limit=${batchSize}&offset=${offset}`);
                    if (!response) {
                        console.error('Failed to fetch batch - no response');
                        break;
                    }
                    
                    if (!response.ok) {
                        console.error('API response not ok:', response.status, response.statusText);
                        break;
                    }
                    
                    const batchTrees = await response.json();
                    console.log(`Loaded batch: ${batchTrees.length} trees`);
                    
                    if (batchTrees.length === 0) {
                        console.log('No more trees to load');
                        hasMoreTrees = false;
                        break;
                    }
                    
                    allTrees = allTrees.concat(batchTrees);
                    offset += batchSize;
                    
                    // If we got less than the batch size, we've reached the end
                    if (batchTrees.length < batchSize) {
                        console.log(`Last batch loaded (${batchTrees.length} < ${batchSize})`);
                        hasMoreTrees = false;
                    }
                    
                    // Update progress
                    if (treeCountEl) {
                        treeCountEl.textContent = `${allTrees.length} trees loaded...`;
                    }
                    
                } catch (fetchError) {
                    console.error('Error in batch fetch:', fetchError);
                    break;
                }
            }
            
            console.log(`✅ Total trees loaded: ${allTrees.length} (via service_role backend)`);
            
            // Cache the fresh tree data
            localStorage.setItem(cacheKey, JSON.stringify(allTrees));
            localStorage.setItem(`${cacheKey}_timestamp`, Date.now().toString());
            console.log(`💾 Cached ${allTrees.length} trees to localStorage`);
            
            this.renderTreesToMap(allTrees);

        } catch (error) {
            console.error('Error loading trees:', error);
            this.showMessage('Failed to load trees. Please refresh the page.', 'error');
        }
    }
    
    
    renderTreesToMap(allTrees) {
        // Filter out specific trees (e.g., ID 18 as requested)
        const filteredTrees = allTrees.filter(tree => {
            if (tree.id === 18) {
                console.log('Excluding tree ID 18 from display');
                return false;
            }
            return true;
        });
        
        console.log(`Total trees to display: ${filteredTrees.length}`);

        // Add tree markers with optimized batch processing
        let loadedCount = 0;
        const uiBatchSize = 100; // UI batch size for smooth rendering
        
        for (let i = 0; i < filteredTrees.length; i += uiBatchSize) {
            const batch = filteredTrees.slice(i, i + uiBatchSize);
            
            // Process batch with small delay to prevent UI blocking
            setTimeout(() => {
                batch.forEach((tree) => {
                    try {
                        this.addTreeToMap(tree);
                        loadedCount++;
                    } catch (error) {
                        console.warn(`Failed to add marker for tree ${tree.id}:`, error);
                    }
                });
                
                // Update progress
                if (document.getElementById('treeCount')) {
                    document.getElementById('treeCount').textContent = `${loadedCount} trees`;
                }
                
                // If this is the last batch, finalize
                if (i + uiBatchSize >= filteredTrees.length) {
                    console.log(`Map loading complete: ${loadedCount} tree markers added`);
                    
                    // Auto-center on trees with clustering view
                    setTimeout(() => {
                        if (loadedCount > 0) {
                            const group = new L.featureGroup(this.treeMarkers);
                            const bounds = group.getBounds();
                            this.map.fitBounds(bounds.pad(0.1), { maxZoom: 15 });
                            console.log('Map auto-centered with clustering view');
                        }
                    }, 100);
                }
            }, (i / uiBatchSize) * 50); // Stagger UI batches by 50ms
        }
    }
    

    addTreeToMap(tree) {
        // Use consistent forest green colors for all trees
        const colors = {
            canopy1: '#22c55e',    // Forest green
            canopy2: '#16a34a',    // Darker green
            canopy3: '#15803d',    // Darkest green
            trunk: '#8b5a2b',      // Brown trunk
            shadow: 'rgba(34, 197, 94, 0.3)'
        };
        
        // Choose marker style - you can change this to test different options
        const markerStyle = 'realistic-tree'; // Options: 'realistic-tree', 'simple-circle', 'pin-style', 'geometric', 'minimalist', 'minimal-tree'
        
        let treeIcon;
        
        if (markerStyle === 'simple-circle') {
            treeIcon = L.divIcon({
                html: `<div style="width: 20px; height: 20px; background: ${colors.canopy1}; border: 3px solid white; border-radius: 50%; box-shadow: 0 2px 6px rgba(0,0,0,0.3);"></div>`,
                className: 'custom-marker-icon simple-circle',
                iconSize: [20, 20],
                iconAnchor: [10, 10],
                popupAnchor: [0, -10]
            });
        } else if (markerStyle === 'pin-style') {
            treeIcon = L.divIcon({
                html: `
                    <div class="map-marker pin-marker" style="color: ${colors.canopy1};">
                        <svg width="25" height="35" viewBox="0 0 25 35" fill="none">
                            <path d="M12.5 0C5.6 0 0 5.6 0 12.5C0 21.9 12.5 35 12.5 35C12.5 35 25 21.9 25 12.5C25 5.6 19.4 0 12.5 0Z" fill="${colors.canopy1}" stroke="white" stroke-width="2"/>
                            <circle cx="12.5" cy="12.5" r="5" fill="white"/>
                            <text x="12.5" y="17" text-anchor="middle" font-family="Arial" font-size="8" fill="${colors.canopy1}">T</text>
                        </svg>
                    </div>
                `,
                className: 'custom-marker-icon pin-style',
                iconSize: [25, 35],
                iconAnchor: [12, 35],
                popupAnchor: [0, -35]
            });
        } else if (markerStyle === 'geometric') {
            treeIcon = L.divIcon({
                html: `
                    <div class="map-marker geometric-marker">
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
                            <polygon points="12,2 22,20 2,20" fill="${colors.canopy1}" stroke="white" stroke-width="2"/>
                            <circle cx="12" cy="16" r="2" fill="white"/>
                        </svg>
                    </div>
                `,
                className: 'custom-marker-icon geometric',
                iconSize: [24, 24],
                iconAnchor: [12, 24],
                popupAnchor: [0, -24]
            });
        } else if (markerStyle === 'minimalist') {
            treeIcon = L.divIcon({
                html: `
                    <div style="width: 16px; height: 16px; background: ${colors.canopy1}; border: 2px solid white; border-radius: 3px; box-shadow: 0 2px 4px rgba(0,0,0,0.2); display: flex; align-items: center; justify-content: center; font-size: 8px; color: white; font-weight: bold;">T</div>
                `,
                className: 'custom-marker-icon minimalist',
                iconSize: [16, 16],
                iconAnchor: [8, 8],
                popupAnchor: [0, -8]
            });
        } else if (markerStyle === 'minimal-tree') {
            treeIcon = L.divIcon({
                html: `
                    <div class="map-marker minimal-tree-marker" style="filter: drop-shadow(0 2px 4px ${colors.shadow});">
                        <svg width="28" height="32" viewBox="0 0 28 32" fill="none">
                            <!-- Clean tree trunk -->
                            <rect x="12" y="24" width="4" height="8" fill="${colors.trunk}" rx="0.5"/>
                            
                            <!-- Main canopy - single clean shape -->
                            <path d="M14 2 C18.5 2, 22 6, 22 11 C22 16, 18.5 20, 14 20 C9.5 20, 6 16, 6 11 C6 6, 9.5 2, 14 2 Z" fill="${colors.canopy1}" stroke="white" stroke-width="1.5"/>
                            
                            <!-- Subtle canopy depth -->
                            <path d="M14 4 C17 4, 19.5 7, 19.5 10.5 C19.5 14, 17 17, 14 17 C11 17, 8.5 14, 8.5 10.5 C8.5 7, 11 4, 14 4 Z" fill="${colors.canopy2}" opacity="0.6"/>
                            
                            <!-- Sharp highlight -->
                            <circle cx="12" cy="9" r="2" fill="white" opacity="0.3"/>
                        </svg>
                    </div>
                `,
                className: 'custom-marker-icon minimal-tree',
                iconSize: [28, 32],
                iconAnchor: [14, 30],
                popupAnchor: [0, -32]
            });
        } else {
            // Default realistic tree style
            treeIcon = L.divIcon({
                html: `
                    <div class="map-marker tree-marker" style="filter: drop-shadow(2px 3px 6px ${colors.shadow});">
                        <svg width="40" height="48" viewBox="0 0 40 48" fill="none" style="transition: transform 0.2s ease;">
                            <!-- Tree Shadow/Base -->
                            <ellipse cx="20" cy="46" rx="8" ry="2" fill="${colors.shadow}" opacity="0.4"/>
                            
                            <!-- Tree Trunk -->
                            <path d="M17 38 Q17 40 17.5 42 Q18 44 19 45 Q19.5 45.5 20 45.5 Q20.5 45.5 21 45 Q22 44 22.5 42 Q23 40 23 38 L23 32 Q22.8 31 22 30.5 Q21 30 20 30 Q19 30 18 30.5 Q17.2 31 17 32 Z" fill="${colors.trunk}" stroke="#6b4a39" stroke-width="0.5"/>
                            
                            <!-- Tree Trunk Texture -->
                            <path d="M18.5 32 Q18.5 35 18.5 38" stroke="#5a3e32" stroke-width="0.3" opacity="0.6"/>
                            <path d="M21.5 33 Q21.5 36 21.5 39" stroke="#5a3e32" stroke-width="0.3" opacity="0.6"/>
                            
                            <!-- Main Canopy (Back Layer) -->
                            <circle cx="20" cy="22" r="12" fill="${colors.canopy3}" opacity="0.8"/>
                            
                            <!-- Secondary Canopy Clusters -->
                            <circle cx="15" cy="20" r="8" fill="${colors.canopy2}" opacity="0.85"/>
                            <circle cx="25" cy="19" r="7" fill="${colors.canopy2}" opacity="0.85"/>
                            <circle cx="18" cy="15" r="6" fill="${colors.canopy1}" opacity="0.9"/>
                            <circle cx="23" cy="25" r="6.5" fill="${colors.canopy2}" opacity="0.85"/>
                            
                            <!-- Top Canopy (Brightest) -->
                            <circle cx="20" cy="18" r="7" fill="${colors.canopy1}"/>
                            
                            <!-- Highlight clusters for 3D effect -->
                            <circle cx="16" cy="15" r="3" fill="#a8b9a0" opacity="0.7"/>
                            <circle cx="24" cy="20" r="2.5" fill="#a8b9a0" opacity="0.6"/>
                            <circle cx="18" cy="24" r="2" fill="#a8b9a0" opacity="0.5"/>
                            
                            <!-- Small light spots -->
                            <circle cx="14" cy="14" r="1" fill="#c0d4b2" opacity="0.8"/>
                            <circle cx="22" cy="16" r="0.8" fill="#c0d4b2" opacity="0.9"/>
                            <circle cx="26" cy="22" r="0.6" fill="#c0d4b2" opacity="0.7"/>
                            
                            <!-- Optional: Small leaves/details -->
                            <path d="M12 18 Q11 17 11.5 19 Q12.5 20 13 19" fill="${colors.canopy1}" opacity="0.6"/>
                            <path d="M28 25 Q29 24 28.5 26 Q27.5 27 27 26" fill="${colors.canopy1}" opacity="0.6"/>
                        </svg>
                    </div>
                `,
                className: 'custom-marker-icon tree-pin realistic-tree',
                iconSize: [40, 48],
                iconAnchor: [20, 46],
                popupAnchor: [0, -48]
            });
        }

        const marker = L.marker([tree.latitude, tree.longitude], { icon: treeIcon });
        
        // Add to cluster group instead of directly to map for better performance
        this.markerClusterGroup.addLayer(marker);

        // Enhanced tooltip
        const treeName = tree.scientific_name || tree.common_name || tree.local_name || 'Unknown Tree';
        const tooltipContent = `
            <div class="tree-tooltip-content">
                <div class="tree-name">${treeName}</div>
                <div class="tree-details">ID: ${tree.id}${tree.tree_code ? ' | ' + tree.tree_code : ''}</div>
                ${tree.created_by ? `<div class="tree-creator">by ${tree.created_by}</div>` : ''}
            </div>
        `;

        marker.bindTooltip(tooltipContent, {
            permanent: false,
            direction: 'top',
            offset: [0, -10],
            className: 'tree-tooltip'
        });

        // Enhanced popup with action buttons
        const canEdit = this.canEditTree(tree.created_by);
        const canDelete = this.canDeleteTree(tree.created_by);

        const popupContent = `
            <div style="width: 300px; max-width: 90vw; font-family: 'Segoe UI', sans-serif; position: relative;">
                <div style="padding: 16px;">
                    <div style="display: flex; justify-content: between; align-items: flex-start; margin-bottom: 16px;">
                        <div style="flex: 1;">
                            <h3 style="margin: 0 0 8px 0; color: #059669; font-size: 18px; font-weight: 600; line-height: 1.2; word-wrap: break-word;">
                                ${treeName}
                            </h3>
                            <div style="color: #6b7280; font-size: 13px;">
                                <strong>Tree ID:</strong> #${tree.id}${tree.tree_code ? ' (' + tree.tree_code + ')' : ''}
                            </div>
                        </div>
                    </div>
                    
                    <div style="margin-bottom: 16px;">
                        <div style="display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: 13px;">
                            ${tree.local_name ? `<strong style="color: #374151;">Local:</strong><span style="word-wrap: break-word;">${tree.local_name}</span>` : ''}
                            ${tree.scientific_name ? `<strong style="color: #374151;">Scientific:</strong><span style="word-wrap: break-word;"><em>${tree.scientific_name}</em></span>` : ''}
                            ${tree.common_name ? `<strong style="color: #374151;">Common:</strong><span style="word-wrap: break-word;">${tree.common_name}</span>` : ''}
                            <strong style="color: #374151;">Location:</strong><span style="font-family: monospace; font-size: 12px;">${tree.latitude.toFixed(4)}, ${tree.longitude.toFixed(4)}</span>
${tree.height ? `<strong style="color: #374151;">Height:</strong><span>${tree.height} ft</span>` : ''}
                            ${tree.width ? `<strong style="color: #374151;">Girth:</strong><span>${tree.width} ft</span>` : ''}
                            <strong style="color: #374151;">Added by:</strong><span>${tree.created_by || 'Unknown'}</span>
                            <strong style="color: #374151;">Date:</strong><span>${new Date(tree.created_at).toLocaleDateString()}</span>
                        </div>
                    </div>
                    
                    ${tree.notes ? `
                        <div style="margin-bottom: 16px; padding: 12px; background: #f8fafc; border-radius: 6px; border-left: 4px solid #e2e8f0;">
                            <strong style="color: #374151; font-size: 13px; display: block; margin-bottom: 6px;">Notes:</strong>
                            <div style="color: #6b7280; font-size: 12px; line-height: 1.4; word-wrap: break-word;">
                                ${tree.notes}
                            </div>
                        </div>
                    ` : ''}
                    
                    ${canEdit || canDelete ? `
                        <div style="display: flex; gap: 8px; margin-top: 16px; padding-top: 16px; border-top: 1px solid #e5e7eb;">
                            ${canEdit ? `
                                <button onclick="mapApp.editTree(${tree.id})" 
                                        style="flex: 1; background: #3b82f6; color: white; border: none; padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 12px; font-weight: 600; transition: all 0.2s; min-height: 36px;" 
                                        onmouseover="this.style.backgroundColor='#2563eb'; this.style.transform='translateY(-1px)';" 
                                        onmouseout="this.style.backgroundColor='#3b82f6'; this.style.transform='translateY(0)';">
                                    Edit
                                </button>
                            ` : ''}
                            ${canDelete ? `
                                <button onclick="mapApp.deleteTree(${tree.id})" 
                                        style="flex: 1; background: #ef4444; color: white; border: none; padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 12px; font-weight: 600; transition: all 0.2s; min-height: 36px;" 
                                        onmouseover="this.style.backgroundColor='#dc2626'; this.style.transform='translateY(-1px)';" 
                                        onmouseout="this.style.backgroundColor='#ef4444'; this.style.transform='translateY(0)';">
                                    Delete
                                </button>
                            ` : ''}
                        </div>
                    ` : ''}
                </div>
            </div>
        `;

        marker.bindPopup(popupContent, {
            maxWidth: 320,
            minWidth: 280,
            className: 'tree-popup',
            closeButton: true,
            autoClose: true,
            autoPan: true,
            closeOnEscapeKey: true
        });

        this.treeMarkers.push(marker);
    }

    clearTreeMarkers() {
        // Clear clustered markers
        if (this.markerClusterGroup) {
            this.markerClusterGroup.clearLayers();
        }
        
        // Clear individual markers array
        this.treeMarkers.forEach(marker => {
            this.map.removeLayer(marker);
        });
        this.treeMarkers = [];
    }

    // Tree management methods
    async editTree(treeId) {
        try {
            // Check if user has permission to edit (basic check)
            if (!this.currentUser) {
                this.showMessage('Authentication required', 'error');
                return;
            }
            
            // Store tree ID in localStorage for the form
            localStorage.setItem('editTreeId', treeId.toString());
            
            this.showMessage('Redirecting to form for editing...', 'success');
            
            // Redirect to form page immediately 
            setTimeout(() => {
                window.location.href = '/';
            }, 500);
            
        } catch (error) {
            console.error('Error setting up tree for edit:', error);
            this.showMessage('Error preparing tree for editing: ' + error.message, 'error');
        }
    }
    
    async deleteTree(treeId) {
        if (!confirm(`Are you sure you want to delete Tree #${treeId}? This action cannot be undone.`)) {
            return;
        }
        
        try {
            const response = await this.authenticatedFetch(`/api/trees/${treeId}`, {
                method: 'DELETE'
            });
            
            if (!response) return;
            
            if (response.ok) {
                this.showMessage(`Tree #${treeId} deleted successfully`, 'success');
                
                // Reload trees to update the map
                setTimeout(() => {
                    this.loadTrees();
                }, 1000);
                
                // Close any open popups
                this.map.closePopup();
                
            } else {
                const error = await response.json();
                this.showMessage('Error deleting tree: ' + (error.detail || 'Unknown error'), 'error');
            }
        } catch (error) {
            console.error('Error deleting tree:', error);
            this.showMessage('Network error: ' + error.message, 'error');
        }
    }
    

    showLoading() {
        document.getElementById('loading').style.display = 'block';
    }

    hideLoading() {
        document.getElementById('loading').style.display = 'none';
    }

    showMessage(message, type = 'success') {
        const messageElement = document.getElementById('message');
        messageElement.textContent = message;
        messageElement.className = `message ${type} show`;

        setTimeout(() => {
            messageElement.classList.remove('show');
        }, 3000);
    }

    showGestureHint() {
        const hint = document.querySelector('.gesture-hint');
        if (hint) {
            hint.style.display = 'block';
            setTimeout(() => {
                hint.style.display = 'none';
            }, 4000);
        }
    }
}

// Initialize map when DOM is loaded
let mapApp;
document.addEventListener('DOMContentLoaded', () => {
    console.log('🗺️ DOM loaded, initializing TreeTrack Map...');
    mapApp = new TreeTrackMap();
    
    // Expose globally for popup button callbacks
    window.mapApp = mapApp;
});

// Note: ES6 exports removed for traditional script loading