<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notes - Masina-Dock</title>
    <link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
    <header>
        <div class="logo">
            <img src="/static/images/logo.svg" alt="Masina-Dock">
            <h1>Masina-Dock</h1>
        </div>
        <nav>
            <a href="/dashboard" data-translate="dashboard">Dashboard</a>
            <a href="#" onclick="navigateToVehicle(); return false;" data-translate="overview">Overview</a>
            <a href="/service-records" data-translate="service_records">Service Records</a>
            <a href="/repairs" data-translate="repairs">Repairs</a>
            <a href="/fuel" data-translate="fuel">Fuel</a>
            <a href="/taxes" data-translate="taxes">Taxes</a>
            <a href="/notes" class="active" data-translate="notes">Notes</a>
            <a href="/reminders" data-translate="reminders">Reminders</a>
            <button class="theme-toggle" onclick="toggleTheme()" data-translate="toggle_theme">Toggle Theme</button>
            <button class="btn btn-danger" onclick="logout()" data-translate="logout">Logout</button>
        </nav>
    </header>
    
    <div class="container">
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
            <h2 id="vehicle-title" data-translate="notes">Notes</h2>
            <button class="btn" onclick="navigateToVehicle()" data-translate="back_to_vehicle">Back to Vehicle</button>
        </div>
        
        <button class="btn btn-success" onclick="showModal('add-note-modal')" data-translate="add_note">+ Add Note</button>
        
        <div id="notes-container" style="display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; margin-top: 20px;"></div>
    </div>
    
    <div id="add-note-modal" class="modal">
        <div class="modal-content">
            <h2 data-translate="add_note">Add Note</h2>
            <form id="add-note-form">
                <div class="form-group">
                    <label for="note-title" data-translate="title">Title</label>
                    <input type="text" id="note-title" name="title" required>
                </div>
                <div class="form-group">
                    <label for="note-content" data-translate="content">Content</label>
                    <textarea id="note-content" name="content" rows="8" required></textarea>
                </div>
                <div style="display: flex; gap: 10px; margin-top: 20px;">
                    <button type="submit" class="btn btn-success" data-translate="add_record">Add Note</button>
                    <button type="button" class="btn" onclick="closeModal('add-note-modal')" data-translate="cancel">Cancel</button>
                </div>
            </form>
        </div>
    </div>
    
    <script src="/static/js/translations.js"></script>
    <script src="/static/js/app.js"></script>
    <script>
        let currentVehicleId = null;
        
        document.addEventListener('DOMContentLoaded', async () => {
            currentVehicleId = getSelectedVehicle();
            
            if (!currentVehicleId) {
                alert('Please select a vehicle from the dashboard.');
                window.location.href = '/dashboard';
                return;
            }
            
            await loadVehicleInfo();
            await loadNotes();
            
            setTimeout(() => {
                translatePage();
            }, 200);
        });
        
        async function loadVehicleInfo() {
            try {
                const vehicle = await apiRequest(`/api/vehicles/${currentVehicleId}`);
                const settings = JSON.parse(localStorage.getItem('userSettings') || '{"language":"en"}');
                const lang = settings.language || 'en';
                
                if (lang === 'ro') {
                    document.getElementById('vehicle-title').textContent = 
                        `${vehicle.year} ${vehicle.make} ${vehicle.model} - Notite`;
                } else {
                    document.getElementById('vehicle-title').textContent = 
                        `${vehicle.year} ${vehicle.make} ${vehicle.model} - Notes`;
                }
            } catch (error) {
                console.error('Failed to load vehicle info:', error);
            }
        }
        
        function navigateToVehicle() {
            if (currentVehicleId) {
                window.location.href = `/vehicle-detail?id=${currentVehicleId}`;
            } else {
                window.location.href = '/dashboard';
            }
        }
        
        async function loadNotes() {
            try {
                const records = await apiRequest(`/api/vehicles/${currentVehicleId}/service-records`);
                const notes = records.filter(r => r.category === 'Note');
                displayNotes(notes);
            } catch (error) {
                console.error('Failed to load notes:', error);
            }
        }
        
        function displayNotes(notes) {
            const container = document.getElementById('notes-container');
            if (!container) return;
            
            if (notes.length === 0) {
                container.innerHTML = '<p style="color: var(--text-secondary); text-align: center; grid-column: 1 / -1;">No notes yet</p>';
                return;
            }
            
            container.innerHTML = notes.map(n => `
                <div class="card" style="padding: 20px;">
                    <h3 style="margin-top: 0;">${n.description}</h3>
                    <p style="color: var(--text-secondary); font-size: 14px; margin-bottom: 15px;">${formatDate(n.date)}</p>
                    <p style="white-space: pre-wrap;">${n.notes || ''}</p>
                </div>
            `).join('');
        }
        
        document.getElementById('add-note-form').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = {
                date: new Date().toISOString().split('T')[0],
                odometer: 0,
                description: document.getElementById('note-title').value,
                category: 'Note',
                cost: 0,
                notes: document.getElementById('note-content').value,
                document_path: null
            };
            
            try {
                await apiRequest(`/api/vehicles/${currentVehicleId}/service-records`, {
                    method: 'POST',
                    body: JSON.stringify(formData)
                });
                closeModal('add-note-modal');
                await loadNotes();
                document.getElementById('add-note-form').reset();
            } catch (error) {
                alert('Failed to add note: ' + error.message);
            }
        });
    </script>
</body>
</html>
