File size: 27,691 Bytes
87dbbc6
 
74460ff
bc3a44a
08b99b2
 
 
 
 
 
 
87dbbc6
 
 
5086de6
08b99b2
e1e9687
 
 
87dbbc6
08b99b2
 
 
87dbbc6
5086de6
 
 
 
 
bc3a44a
 
 
 
5086de6
 
 
08b99b2
bc3a44a
 
 
 
 
 
5086de6
08b99b2
 
 
5086de6
 
87dbbc6
08b99b2
 
 
 
 
 
 
 
 
 
 
 
 
d936242
08b99b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87dbbc6
74460ff
 
 
87dbbc6
 
c485991
 
74460ff
 
c485991
 
 
 
 
 
 
 
 
 
 
 
 
 
74460ff
c485991
 
 
 
 
 
 
 
 
 
08b99b2
74460ff
 
 
87dbbc6
08b99b2
 
87dbbc6
08b99b2
74460ff
08b99b2
74460ff
 
5086de6
 
74460ff
5086de6
87dbbc6
 
 
 
 
74460ff
08b99b2
74460ff
08b99b2
 
74460ff
87dbbc6
08b99b2
 
74460ff
 
 
 
 
 
08b99b2
87dbbc6
74460ff
87dbbc6
 
 
 
08b99b2
74460ff
 
bc3a44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08b99b2
bc3a44a
 
5086de6
08b99b2
 
87dbbc6
74460ff
08b99b2
87dbbc6
74460ff
87dbbc6
 
 
08b99b2
74460ff
 
08b99b2
 
74460ff
08b99b2
 
74460ff
08b99b2
 
 
74460ff
08b99b2
74460ff
08b99b2
 
74460ff
08b99b2
 
 
 
 
5086de6
e1e9687
74460ff
 
08b99b2
e1e9687
 
 
 
 
 
08b99b2
 
 
 
74460ff
5086de6
74460ff
 
 
 
08b99b2
 
 
 
74460ff
08b99b2
74460ff
08b99b2
5086de6
 
08b99b2
 
 
 
74460ff
08b99b2
5086de6
e1e9687
5086de6
08b99b2
 
74460ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08b99b2
 
 
 
e1e9687
08b99b2
 
 
 
 
e1e9687
08b99b2
 
74460ff
e1e9687
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08b99b2
 
 
5086de6
 
 
08b99b2
74460ff
 
 
 
 
 
 
 
 
bc3a44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08b99b2
bc3a44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08b99b2
74460ff
08b99b2
 
 
74460ff
 
 
 
 
5086de6
74460ff
5086de6
74460ff
5086de6
 
08b99b2
 
87dbbc6
74460ff
 
 
 
87dbbc6
74460ff
 
 
87dbbc6
74460ff
87dbbc6
 
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
import React, { useState } from 'react';

export default function AdminPanel({
  gameState,
  airports = [],
  activeAirport,
  activeAirportConfig,
  onSelectAirport,
  sendWSMessage,
  draftingMode,
  setDraftingMode,
  airportName,
  setAirportName,
  isRunwayBidirectional,
  setIsRunwayBidirectional,
  starDraft,
  setStarDraft,
  sidDraft,
  setSidDraft
}) {
  const [editingWaypoint, setEditingWaypoint] = useState(null); // {gate, runway, index, wp}
  const [editingRunway, setEditingRunway] = useState(null); // {rw}

  const [runwayLabel, setRunwayLabel] = useState("");
  const [spawnFields, setSpawnFields] = useState({
    callsign: `UA${Math.floor(Math.random() * 900) + 100}`,
    type: "B738",
    altitude: 10000,
    speed: 250,
    gate: "N",
    isDeparture: false,
    runwayId: "",
    terminalGateId: ""
  });

  const handleSpawn = () => {
    sendWSMessage('spawn', {
      ...spawnFields,
      is_departure: spawnFields.isDeparture,
      runway_id: spawnFields.runwayId,
      terminal_gate_id: spawnFields.terminalGateId,
      altitude: parseInt(spawnFields.altitude || 0),
      speed: parseInt(spawnFields.speed || 0)
    });
    setSpawnFields(prev => ({
      ...prev,
      callsign: `UA${Math.floor(Math.random() * 900) + 100}`
    }));
  };

  const renderEditModal = () => {
    if (!editingWaypoint && !editingRunway) return null;

    const isWaypoint = !!editingWaypoint;
    const title = isWaypoint ? `Edit Waypoint: ${editingWaypoint.wp.name}` : `Edit Runway: ${editingRunway.rw.id}`;

    const handleSave = (e) => {
      e.preventDefault();
      const formData = new FormData(e.target);

      if (isWaypoint) {
        sendWSMessage('update_waypoint', {
          airport_code: activeAirport.airport_code,
          waypoint_id: editingWaypoint.wp.id,
          name: formData.get('name'),
          target_alt: parseInt(formData.get('alt')),
          target_speed: parseInt(formData.get('speed'))
        });
        setEditingWaypoint(null);
      } else {
        sendWSMessage('update_runway', {
          airport_code: activeAirport.airport_code,
          runway_id: editingRunway.rw.id,
          new_id: formData.get('id'),
          heading: parseFloat(formData.get('heading'))
        });
        setEditingRunway(null);
      }
    };

    return (
      <div style={{
        position: 'fixed', top: 0, left: 0, right: 0, bottom: 0,
        backgroundColor: 'rgba(0,0,0,0.5)', zIndex: 3000,
        display: 'flex', justifyContent: 'center', alignItems: 'center'
      }}>
        <div style={{
          background: 'white', padding: '20px', borderRadius: '8px',
          width: '300px', boxShadow: '0 4px 12px rgba(0,0,0,0.2)'
        }}>
          <h3 style={{ marginTop: 0, fontSize: '1rem', color: '#1a1a1a' }}>{title}</h3>
          <form onSubmit={handleSave}>
            {isWaypoint ? (
              <>
                <div style={{ marginBottom: '10px' }}>
                  <label style={{ display: 'block', fontSize: '0.75rem', color: '#666' }}>Name</label>
                  <input name="name" defaultValue={editingWaypoint.wp.name} style={{ width: '100%', padding: '4px' }} />
                </div>
                <div style={{ marginBottom: '10px' }}>
                  <label style={{ display: 'block', fontSize: '0.75rem', color: '#666' }}>Target Altitude (ft)</label>
                  <input name="alt" type="number" defaultValue={editingWaypoint.wp.target_alt} style={{ width: '100%', padding: '4px' }} />
                </div>
                <div style={{ marginBottom: '15px' }}>
                  <label style={{ display: 'block', fontSize: '0.75rem', color: '#666' }}>Target Speed (kts)</label>
                  <input name="speed" type="number" defaultValue={editingWaypoint.wp.target_speed} style={{ width: '100%', padding: '4px' }} />
                </div>
              </>
            ) : (
              <>
                <div style={{ marginBottom: '10px' }}>
                  <label style={{ display: 'block', fontSize: '0.75rem', color: '#666' }}>Runway ID</label>
                  <input name="id" defaultValue={editingRunway.rw.id} style={{ width: '100%', padding: '4px' }} />
                </div>
                <div style={{ marginBottom: '15px' }}>
                  <label style={{ display: 'block', fontSize: '0.75rem', color: '#666' }}>Heading (°)</label>
                  <input name="heading" type="number" step="0.1" defaultValue={editingRunway.rw.heading} style={{ width: '100%', padding: '4px' }} />
                </div>
              </>
            )}
            <div style={{ display: 'flex', justifyContent: 'flex-end', gap: '8px' }}>
              <button type="button" onClick={() => { setEditingWaypoint(null); setEditingRunway(null); }} className="btn btn-outline" style={{ padding: '4px 12px' }}>Cancel</button>
              <button type="submit" className="btn btn-primary" style={{ padding: '4px 12px', background: '#007bff', color: 'white', border: 'none', borderRadius: '4px' }}>Save</button>
            </div>
          </form>
        </div>
      </div>
    );
  };

  return (
    <nav className="admin-panel thin-scroll">
      <div style={{ marginBottom: '16px' }}>
        <h2 style={{ color: '#555', fontSize: '0.75rem', textTransform: 'uppercase', letterSpacing: '0.1em', fontWeight: 'bold' }}>Admin Control</h2>
      </div>

      {/* Simulation Speed Section */}
      <div className="section" style={{ marginBottom: '16px' }}>
        <h4 style={{ margin: '0 0 8px 0', fontSize: '0.7rem', color: '#888', textTransform: 'uppercase', fontWeight: 'bold' }}>Sim Scale</h4>
        <div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
          <input 
            type="range" 
            min="1" 
            max="8" 
            step="1"
            value={activeAirportConfig?.time_scale || 1}
            onChange={(e) => sendWSMessage('set_time_scale', { scale: e.target.value })}
            style={{ 
              flex: 1,
              cursor: 'pointer',
              accentColor: '#007bff'
            }}
          />
          <span style={{ 
            minWidth: '35px', 
            textAlign: 'right', 
            fontWeight: 'bold', 
            color: '#007bff',
            fontSize: '0.8rem'
          }}>
            {activeAirportConfig?.time_scale || 1}x
          </span>
        </div>
      </div>

      {/* Airports Section */}
      <div className="section" style={{ borderTop: '1px solid #f0f0f0', paddingTop: '12px', marginTop: '12px' }}>
        <h4 style={{ margin: '0 0 8px 0', fontSize: '0.7rem', color: '#888', textTransform: 'uppercase', fontWeight: 'bold' }}>Airports</h4>
        <div style={{ maxHeight: '80px', overflowY: 'auto', border: '1px solid #eee', padding: '4px', borderRadius: '4px', background: '#fafafa', marginBottom: '8px' }}>
          {airports.map(ap => (
            <div
              key={ap.name}
              onClick={() => onSelectAirport(ap)}
              style={{
                padding: '4px 8px',
                cursor: 'pointer',
                fontSize: '0.7rem',
                borderRadius: '3px',
                marginBottom: '2px',
                background: activeAirport?.name === ap.name ? '#007bff' : 'none',
                color: activeAirport?.name === ap.name ? '#fff' : '#444'
              }}
            >
              {ap.name}
            </div>
          ))}
        </div>
        <div style={{ display: 'flex', gap: '4px' }}>
          <input
            placeholder="New..."
            value={airportName}
            onChange={e => setAirportName(e.target.value)}
            style={{ flex: 1, padding: '4px', fontSize: '0.7rem', border: '1px solid #eee', borderRadius: '4px' }}
          />
          <button
            className="btn"
            style={{ 
              padding: '4px 8px', 
              fontSize: '0.65rem',
              background: draftingMode === 'airport' ? '#ffcccc' : '#e7f3ff', 
              color: '#0056b3'
            }}
            onClick={() => setDraftingMode(draftingMode === 'airport' ? null : 'airport')}
          >
            {draftingMode === 'airport' ? '📍' : 'Add'}
          </button>
        </div>
      </div>

      {/* Runway Management */}
      <div className="section" style={{ borderTop: '1px solid #f0f0f0', paddingTop: '12px', marginTop: '12px' }}>
        <h4 style={{ margin: '0 0 8px 0', fontSize: '0.7rem', color: '#888', textTransform: 'uppercase', fontWeight: 'bold' }}>Runways ({activeAirport?.name})</h4>
        <div style={{ maxHeight: '120px', overflowY: 'auto', background: '#fafafa', padding: '4px', borderRadius: '4px', border: '1px solid #eee', marginBottom: '8px' }}>
          {activeAirport?.runways?.map(rw => {
            const statusObj = gameState?.runway_status?.[rw.id];
            const status = statusObj?.status || "CLEAR";
            
            let statusColor = "#6c757d"; // Clear
            let statusText = "CLEAR";
            
            if (status === "OCCUPIED") {
              statusColor = "#dc3545"; // Red
              statusText = statusObj.occupied_by;
            } else if (status === "RESERVED") {
              statusColor = "#007bff"; // Blue
              statusText = `Reserved: ${statusObj.reserved_by}`;
            } else if (status === "COOLDOWN") {
              statusColor = "#ffc107"; // Amber
              statusText = `Cooling: ${statusObj.cooldown_remaining}s`;
            }

            return (
              <div key={rw.id} style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '6px 4px', borderBottom: '1px solid #f0f0f0', color: '#444' }}>
                <div style={{ display: 'flex', flexDirection: 'column' }}>
                  <span style={{ fontSize: '0.7rem', fontWeight: 'bold' }}>{rw.heading.toFixed(0).padStart(2, '0')} [{rw.id}]</span>
                  <span style={{ 
                    fontSize: '0.55rem', 
                    color: status === "CLEAR" ? "#28a745" : "white", 
                    background: status === "CLEAR" ? "none" : statusColor,
                    padding: status === "CLEAR" ? "0" : "1px 4px",
                    borderRadius: '2px',
                    fontWeight: 'bold',
                    width: 'fit-content'
                  }}>
                    {statusText}
                  </span>
                </div>
                <div style={{ display: 'flex', gap: '4px' }}>
                  <button onClick={() => setEditingRunway({ rw })} style={{ background: 'none', border: 'none', cursor: 'pointer', fontSize: '0.7rem' }}>✏️</button>
                  <button
                    onClick={() => {
                      if (window.confirm(`Delete runway ${rw.id}?`)) {
                        sendWSMessage('delete_runway', { airport_code: activeAirport.airport_code, runway_id: rw.id });
                      }
                    }}
                    style={{ background: 'none', border: 'none', color: '#dc3545', cursor: 'pointer', fontWeight: 'bold', fontSize: '1rem' }}
                  >&times;</button>
                </div>
              </div>
            );
          })}
        </div>
        <button
          className="btn"
          disabled={!activeAirport}
          style={{ width: '100%', background: draftingMode === 'runway' ? '#ffcccc' : '#f8f9fa', fontSize: '0.65rem' }}
          onClick={() => setDraftingMode(draftingMode === 'runway' ? null : 'runway')}
        >
          {draftingMode === 'runway' ? 'Click Map...' : 'Add Runway'}
        </button>
      </div>

      {/* Pooled Waypoint Dropper */}
      <div className="section" style={{ borderTop: '1px solid #f0f0f0', paddingTop: '12px', marginTop: '12px' }}>
        <h4 style={{ margin: '0 0 8px 0', fontSize: '0.7rem', color: '#888', textTransform: 'uppercase', fontWeight: 'bold' }}>Waypoints</h4>
        <button
          className="btn"
          style={{ width: '100%', background: draftingMode === 'waypoint' ? '#ffcccc' : '#e7f3ff', fontSize: '0.65rem', marginBottom: '8px' }}
          onClick={() => setDraftingMode(draftingMode === 'waypoint' ? null : 'waypoint')}
        >
          {draftingMode === 'waypoint' ? 'Drop Active...' : 'Activate Dropper'}
        </button>

        {activeAirportConfig?.waypoints && Object.keys(activeAirportConfig.waypoints).length > 0 && (
          <div style={{ maxHeight: '80px', overflowY: 'auto', background: '#fafafa', borderRadius: '4px', border: '1px solid #eee' }}>
            {Object.values(activeAirportConfig.waypoints).map((wp) => (
              <div key={wp.id} style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', fontSize: '0.6rem', padding: '2px 6px', borderBottom: '1px solid #f9f9f9' }}>
                <span>{wp.name}</span>
                <div style={{ display: 'flex', gap: '4px' }}>
                  <button onClick={() => setEditingWaypoint({ wp })} style={{ background: 'none', border: 'none', cursor: 'pointer', fontSize: '0.65rem' }}>✏️</button>
                </div>
              </div>
            ))}
          </div>
        )}
      </div>
      {/* Arrival Route Builder (STAR) */}
      <div className="section" style={{ borderTop: '1px solid #f0f0f0', paddingTop: '12px', marginTop: '12px' }}>
        <h4 style={{ margin: '0 0 8px 0', fontSize: '0.7rem', color: '#888', textTransform: 'uppercase', fontWeight: 'bold' }}>Arrival Routes</h4>
        <div style={{ display: 'flex', flexDirection: 'column', gap: '4px' }}>
          <input
            placeholder="Procedure Name (e.g. SUTRA 1A)"
            value={starDraft.name || ''}
            onChange={e => setStarDraft({ ...starDraft, name: e.target.value })}
            style={{ padding: '4px', fontSize: '0.65rem', border: '1px solid #eee', borderRadius: '4px' }}
          />
          <div style={{ display: 'flex', gap: '4px' }}>
            <select
              value={starDraft.gate}
              onChange={e => setStarDraft({ ...starDraft, gate: e.target.value })}
              style={{ flex: 1, padding: '2px', fontSize: '0.65rem', border: '1px solid #eee' }}
            >
              {activeAirportConfig?.gates ? 
                Object.keys(activeAirportConfig.gates).map(g => <option key={g} value={g}>{g}</option>) :
                <><option value="N">N</option><option value="S">S</option><option value="E">E</option><option value="W">W</option></>
              }
            </select>
            <select
              value={starDraft.runway_id || ''}
              onChange={e => setStarDraft({ ...starDraft, runway_id: e.target.value })}
              style={{ flex: 1, padding: '2px', fontSize: '0.65rem', border: '1px solid #eee' }}
            >
              <option value="">RWY...</option>
              {activeAirport?.runways?.map(rw => <option key={rw.id} value={rw.id}>{rw.id}</option>)}
            </select>
          </div>

          <button
            className="btn"
            disabled={!starDraft.runway_id}
            style={{ width: '100%', background: draftingMode === 'route' ? '#ffcccc' : '#f8f9fa', fontSize: '0.65rem' }}
            onClick={() => setDraftingMode(draftingMode === 'route' ? null : 'route')}
          >
            {draftingMode === 'route' ? 'Building STAR...' : 'Build STAR'}
          </button>

          {draftingMode === 'route' && (
            <div style={{ padding: '6px', background: '#fff9e6', border: '1px solid #ffeeba', borderRadius: '4px', marginTop: '4px' }}>
              <select 
                value="" 
                onChange={(e) => setStarDraft(prev => ({ ...prev, sequence: [...prev.sequence, e.target.value] }))} 
                style={{ width: '100%', padding: '2px', fontSize: '0.65rem', marginBottom: '6px' }}
              >
                <option value="">Add Waypoint...</option>
                {Object.values(activeAirportConfig?.waypoints || {})
                  .sort((a,b) => a.name.localeCompare(b.name))
                  .map(wp => <option key={wp.id} value={wp.id}>{wp.name}</option>)}
              </select>
              
              <div style={{ fontSize: '0.6rem', marginBottom: '6px', color: '#666', maxHeight: '40px', overflowY: 'auto' }}>
                {starDraft.sequence.map(id => activeAirportConfig.waypoints[id]?.name || id).join(' → ')}
              </div>

              <div style={{ display: 'flex', gap: '4px' }}>
                <button onClick={() => setStarDraft({ ...starDraft, sequence: [] })} className="btn" style={{ flex: 1, fontSize: '0.6rem', padding: '2px' }}>Clear</button>
                <button 
                  disabled={starDraft.sequence.length === 0}
                  onClick={() => {
                    sendWSMessage('save_star_route', {
                      airport_code: activeAirport.airport_code,
                      name: starDraft.name || `Route_${starDraft.gate}_${starDraft.runway_id}`,
                      gate: starDraft.gate,
                      runway_id: starDraft.runway_id,
                      sequence: starDraft.sequence
                    });
                    setDraftingMode(null);
                    setStarDraft(prev => ({ ...prev, sequence: [], name: '' }));
                  }}
                  className="btn btn-primary"
                  style={{ flex: 1, background: '#007bff', fontSize: '0.6rem', padding: '2px' }}
                >Save STAR</button>
              </div>
            </div>
          )}
        </div>
      </div>

      {/* SID Route Builder */}
      <div className="section" style={{ borderTop: '1px solid #f0f0f0', paddingTop: '12px', marginTop: '12px' }}>
        <h4 style={{ margin: '0 0 8px 0', fontSize: '0.7rem', color: '#888', textTransform: 'uppercase', fontWeight: 'bold' }}>Departure Routes</h4>
        <div style={{ display: 'flex', flexDirection: 'column', gap: '4px' }}>
          <input
            placeholder="Procedure Name (e.g. RWY1 EXIT)"
            value={sidDraft.name || ''}
            onChange={e => setSidDraft({ ...sidDraft, name: e.target.value })}
            style={{ padding: '4px', fontSize: '0.65rem', border: '1px solid #eee', borderRadius: '4px' }}
          />
          <div style={{ display: 'flex', gap: '4px' }}>
            <select
              value={sidDraft.runway_id || ''}
              onChange={e => setSidDraft({ ...sidDraft, runway_id: e.target.value })}
              style={{ flex: 1, padding: '2px', fontSize: '0.65rem', border: '1px solid #eee' }}
            >
              <option value="">RWY...</option>
              {activeAirport?.runways?.map(rw => <option key={rw.id} value={rw.id}>{rw.id}</option>)}
            </select>
            <select
              value={sidDraft.gate}
              onChange={e => setSidDraft({ ...sidDraft, gate: e.target.value })}
              style={{ flex: 1, padding: '2px', fontSize: '0.65rem', border: '1px solid #eee' }}
            >
              {activeAirportConfig?.gates ? 
                Object.keys(activeAirportConfig.gates).map(g => <option key={g} value={g}>{g}</option>) :
                <><option value="N">N</option><option value="S">S</option><option value="E">E</option><option value="W">W</option></>
              }
            </select>
          </div>

          <button
            className="btn"
            disabled={!sidDraft.runway_id}
            style={{ width: '100%', background: draftingMode === 'sid_route' ? '#ffcccc' : '#f8f9fa', fontSize: '0.65rem' }}
            onClick={() => setDraftingMode(draftingMode === 'sid_route' ? null : 'sid_route')}
          >
            {draftingMode === 'sid_route' ? 'Building SID...' : 'Build SID'}
          </button>

          {draftingMode === 'sid_route' && (
            <div style={{ padding: '6px', background: '#e6ffed', border: '1px solid #baf0cc', borderRadius: '4px', marginTop: '4px' }}>
              <select 
                value="" 
                onChange={(e) => setSidDraft(prev => ({ ...prev, sequence: [...prev.sequence, e.target.value] }))} 
                style={{ width: '100%', padding: '2px', fontSize: '0.65rem', marginBottom: '6px' }}
              >
                <option value="">Add Waypoint...</option>
                {Object.values(activeAirportConfig?.waypoints || {})
                  .sort((a,b) => a.name.localeCompare(b.name))
                  .map(wp => <option key={wp.id} value={wp.id}>{wp.name}</option>)}
              </select>
              
              <div style={{ fontSize: '0.6rem', marginBottom: '6px', color: '#666', maxHeight: '40px', overflowY: 'auto' }}>
                {sidDraft.sequence.map(id => activeAirportConfig.waypoints[id]?.name || id).join(' → ')}
              </div>

              <div style={{ display: 'flex', gap: '4px' }}>
                <button onClick={() => setSidDraft({ ...sidDraft, sequence: [] })} className="btn" style={{ flex: 1, fontSize: '0.6rem', padding: '2px' }}>Clear</button>
                <button 
                  disabled={sidDraft.sequence.length === 0}
                  onClick={() => {
                    sendWSMessage('save_sid_route', {
                      airport_code: activeAirport.airport_code,
                      name: sidDraft.name || `Route_${sidDraft.runway_id}_${sidDraft.gate}`,
                      runway_id: sidDraft.runway_id,
                      gate: sidDraft.gate,
                      sequence: sidDraft.sequence
                    });
                    setDraftingMode(null);
                    setSidDraft(prev => ({ ...prev, sequence: [], name: '' }));
                  }}
                  className="btn btn-primary"
                  style={{ flex: 1, background: '#28a745', fontSize: '0.6rem', padding: '2px' }}
                >Save SID</button>
              </div>
            </div>
          )}
        </div>
      </div>

      {/* Spawn Section */}
      <div className="section" style={{ borderTop: '1px solid #f0f0f0', paddingTop: '12px', marginTop: '12px' }}>
        <h4 style={{ margin: '0 0 8px 0', fontSize: '0.7rem', color: '#888', textTransform: 'uppercase', fontWeight: 'bold' }}>Spawn Aircraft</h4>
        <div style={{ display: 'flex', flexDirection: 'column', gap: '6px' }}>
          <input 
            placeholder="Callsign" 
            value={spawnFields.callsign} 
            onChange={e => setSpawnFields({ ...spawnFields, callsign: e.target.value })} 
            style={{ padding: '6px', fontSize: '0.7rem', border: '1px solid #eee', borderRadius: '4px' }} 
          />
          <div style={{ display: 'flex', gap: '4px', alignItems: 'center' }}>
            <label style={{ fontSize: '0.65rem', display: 'flex', alignItems: 'center', gap: '4px', cursor: 'pointer' }}>
              <input 
                type="checkbox" 
                checked={spawnFields.isDeparture} 
                onChange={e => setSpawnFields({ ...spawnFields, isDeparture: e.target.checked })} 
              />
              Departure
            </label>
            {spawnFields.isDeparture && (
              <select 
                value={spawnFields.runwayId} 
                onChange={e => setSpawnFields({ ...spawnFields, runwayId: e.target.value })} 
                style={{ flex: 1, fontSize: '0.65rem', padding: '4px', border: '1px solid #eee' }}
              >
                <option value="">Select Runway...</option>
                {activeAirport?.runways?.map(rw => <option key={rw.id} value={rw.id}>{rw.id}</option>)}
              </select>
            )}
          </div>

          <div style={{ display: 'flex', gap: '4px' }}>
            {/* Case 1: Arrival (Always show Entry Gate) */}
            {!spawnFields.isDeparture && (
              <select 
                value={spawnFields.gate} 
                onChange={e => setSpawnFields({ ...spawnFields, gate: e.target.value })} 
                style={{ flex: 1, fontSize: '0.65rem', padding: '4px', border: '1px solid #eee' }}
              >
                <option value="" disabled>Entry Gate</option>
                {activeAirportConfig?.gates ? 
                  Object.keys(activeAirportConfig.gates).map(g => <option key={g} value={g}>{g}</option>) :
                  <><option value="N">N</option><option value="S">S</option><option value="E">E</option><option value="W">W</option></>
                }
              </select>
            )}

            {/* Case 2: Departure (Show BOTH Exit Gate and Terminal Stand) */}
            {spawnFields.isDeparture && (
              <>
                <select 
                  value={spawnFields.gate} 
                  onChange={e => setSpawnFields({ ...spawnFields, gate: e.target.value })} 
                  style={{ flex: 1, fontSize: '0.65rem', padding: '4px', border: '1px solid #eee' }}
                >
                  <option value="" disabled>Exit Gate</option>
                  {activeAirportConfig?.gates ? 
                    Object.keys(activeAirportConfig.gates).map(g => <option key={g} value={g}>{g}</option>) :
                    <><option value="N">N</option><option value="S">S</option><option value="E">E</option><option value="W">W</option></>
                  }
                </select>
                <select 
                  value={spawnFields.terminalGateId} 
                  onChange={e => setSpawnFields({ ...spawnFields, terminalGateId: e.target.value })} 
                  style={{ flex: 1, fontSize: '0.65rem', padding: '4px', border: '1px solid #eee', background: '#f0f4ff' }}
                >
                  <option value="">Threshold (Direct)</option>
                  {activeAirportConfig?.terminal_gates && 
                    Object.keys(activeAirportConfig.terminal_gates).map(g => (
                      <option key={g} value={g}>Stand: {g}</option>
                    ))
                  }
                </select>
              </>
            )}

            {!spawnFields.isDeparture && (
              <>
                <input type="number" placeholder="Alt" value={spawnFields.altitude} onChange={e => setSpawnFields({ ...spawnFields, altitude: e.target.value })} style={{ flex: 1, padding: '4px', fontSize: '0.65rem', border: '1px solid #eee' }} />
                <input type="number" placeholder="Spd" value={spawnFields.speed} onChange={e => setSpawnFields({ ...spawnFields, speed: e.target.value })} style={{ flex: 1, padding: '4px', fontSize: '0.65rem', border: '1px solid #eee' }} />
              </>
            )}
          </div>
          <button onClick={handleSpawn} className="btn btn-primary" style={{ background: '#28a745', color: 'white', border: 'none', padding: '8px', fontSize: '0.7rem' }}>Spawn Flight</button>
        </div>
      </div>

      <div style={{ marginTop: 'auto', paddingTop: '16px', display: 'flex', gap: '4px' }}>
        <button className="btn" style={{ flex: 1, fontSize: '0.65rem', color: '#dc3545' }} onClick={() => sendWSMessage('reset', {})}>Reset</button>
        <button 
          className="btn" 
          style={{ flex: 1, fontSize: '0.65rem', background: '#dc3545', color: '#fff', border: 'none' }} 
          onClick={() => {
            if (window.confirm("Shut down server?")) sendWSMessage('shutdown', {});
          }}
        >Stop</button>
      </div>

      {renderEditModal()}

      <style>{`
        .admin-panel {
          height: 100%;
          display: flex;
          flex-direction: column;
        }
        .thin-scroll::-webkit-scrollbar { width: 4px; }
        .thin-scroll::-webkit-scrollbar-track { background: #f1f1f1; }
        .thin-scroll::-webkit-scrollbar-thumb { background: #ccc; border-radius: 10px; }
      `}</style>
    </nav>
  );
}