<!DOCTYPE html>
<html lang="hu">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Service Finder</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <style>
        body { background-color: #f0f2f5; font-family: 'Segoe UI', sans-serif; }
        .main-container { max-width: 1000px; margin: 30px auto; }
        .nav-pills .nav-link { border-radius: 50px; padding: 10px 25px; font-weight: 600; color: #6c757d; margin-right: 10px; }
        .nav-pills .nav-link.active { background-color: #0d6efd; color: white; }
        
        .garage-card { border: none; border-radius: 12px; background: white; box-shadow: 0 2px 8px rgba(0,0,0,0.04); transition: transform 0.2s; cursor: pointer; height: 100%; position: relative; overflow: hidden; }
        .garage-card:hover { transform: translateY(-3px); box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
        .card-top-strip { height: 6px; background: #0d6efd; width: 100%; position: absolute; top: 0; left: 0; }
        .card-top-strip.danger { background: #dc3545; }
        .plate-badge { background: #ffcc00; color: black; border: 1px solid #e0b000; font-family: 'Courier New', monospace; font-weight: bold; padding: 2px 6px; font-size: 0.9rem; border-radius: 4px; }
        
        .detail-header { background: white; border-radius: 15px; padding: 25px; margin-bottom: 20px; box-shadow: 0 2px 10px rgba(0,0,0,0.03); }
        .stat-box { background: #f8f9fa; border-radius: 10px; padding: 15px; text-align: center; height: 100%; transition: 0.3s; display: flex; flex-direction: column; justify-content: center; }
        .stat-value { font-size: 1.5rem; font-weight: bold; color: #212529; }
        .stat-label { font-size: 0.85rem; color: #6c757d; text-transform: uppercase; margin-top: 5px; }
        .tab-content-area { background: white; border-radius: 15px; padding: 30px; box-shadow: 0 5px 15px rgba(0,0,0,0.03); }
        
        .history-item { border-left: 3px solid #e9ecef; padding-left: 20px; padding-bottom: 20px; position: relative; }
        .history-item:last-child { border-left: none; }
        
        /* ÚJ IKON SZÍNEK */
        .history-icon { position: absolute; left: -11px; top: 0; width: 20px; height: 20px; background: white; border-radius: 50%; border: 2px solid #6c757d; }
        .history-icon.FUEL { border-color: #198754; background: #198754; }       /* Zöld */
        .history-icon.PURCHASE { border-color: #0d6efd; background: #0d6efd; }   /* Kék */
        .history-icon.SERVICE { border-color: #fd7e14; background: #fd7e14; }    /* Narancs */
        .history-icon.INSURANCE { border-color: #6610f2; background: #6610f2; }  /* Lila */
        .history-icon.TAX { border-color: #dc3545; background: #dc3545; }        /* Piros */
        .history-icon.EQUIPMENT { border-color: #20c997; background: #20c997; }  /* Türkiz (Felszerelés) */
        .history-icon.OPERATIONAL { border-color: #6f42c1; background: #6f42c1; }/* Sötétlila (Üzemeltetés) */
        .history-icon.FINE { border-color: #212529; background: #212529; }       /* Fekete (Bírság) */
        
        .history-icon.ISSUE_REPORT { border-color: #dc3545; background: #white; border-width: 4px; }
        
        .modal-backdrop-custom { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 1040; display: flex; align-items: center; justify-content: center; }
        .modal-content-custom { background: white; padding: 30px; border-radius: 15px; width: 100%; max-width: 500px; box-shadow: 0 10px 30px rgba(0,0,0,0.2); }
    </style>
</head>
<body>

<div id="app">
    <nav class="navbar navbar-dark bg-primary mb-4 shadow-sm py-2">
        <div class="container"><span class="navbar-brand mb-0 h1 fs-5"><i class="bi bi-speedometer2 me-2"></i>Service Finder</span></div>
    </nav>

    <div class="container main-container">
        <div v-if="view === 'dashboard'">
            <ul class="nav nav-pills mb-4"><li class="nav-item"><a class="nav-link active" href="#">Garázs</a></li></ul>
            <div class="row g-3">
                <div class="col-md-6 col-lg-4" v-for="car in myCars" :key="car.vehicle_id">
                    <div class="card garage-card p-3" @click="openVehicleDetail(car.vehicle_id)">
                        <div class="card-top-strip" :class="{ 'danger': car.status !== 'OK' && car.status !== null }"></div>
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <div><h5 class="fw-bold mb-0">{{ car.brand }}</h5><div class="text-muted small">{{ car.model }}</div></div>
                            <i v-if="car.status !== 'OK' && car.status !== null" class="bi bi-exclamation-triangle-fill text-danger fs-2"></i>
                            <i v-else class="bi bi-car-front fs-2 text-secondary"></i>
                        </div>
                        <div class="mt-2 d-flex justify-content-between align-items-end"><div class="plate-badge">{{ car.plate }}</div><span class="badge" :class="(car.status !== 'OK' && car.status !== null) ? 'bg-danger' : 'bg-light text-dark border'">{{ (car.status !== 'OK' && car.status !== null) ? 'HIBA' : car.role }}</span></div>
                    </div>
                </div>
            </div>
        </div>

        <div v-if="view === 'detail' && selectedCar">
            <button class="btn btn-outline-secondary mb-3 rounded-pill" @click="view = 'dashboard'; fetchData()"><i class="bi bi-arrow-left me-2"></i>Garázs</button>
            <div class="detail-header d-flex justify-content-between align-items-center flex-wrap gap-3">
                <div class="d-flex align-items-center">
                    <div class="text-white rounded-circle d-flex align-items-center justify-content-center me-3" :class="(selectedCar.status !== 'OK' && selectedCar.status !== null) ? 'bg-danger' : 'bg-primary'" style="width: 60px; height: 60px;">
                        <i class="bi" :class="(selectedCar.status !== 'OK' && selectedCar.status !== null) ? 'bi-exclamation-triangle' : 'bi-car-front'" style="font-size: 1.8rem;"></i>
                    </div>
                    <div><h2 class="fw-bold mb-0">{{ selectedCar.brand }} {{ selectedCar.model }}</h2><div class="d-flex align-items-center mt-1 gap-2"><span class="plate-badge fs-6">{{ selectedCar.plate }}</span><span v-if="selectedCar.status !== 'OK' && selectedCar.status !== null" class="badge bg-danger">ÁLLAPOT: {{ selectedCar.status }}</span></div></div>
                </div>
                <div class="text-end"><button class="btn btn-outline-danger me-2" @click="openErrorModal"><i class="bi bi-exclamation-circle-fill me-1"></i> Jelentés</button><button class="btn btn-success" @click="openCostModal"><i class="bi bi-plus-lg me-1"></i> Költség</button></div>
            </div>
            <ul class="nav nav-tabs detail-tabs mb-3"><li class="nav-item"><a class="nav-link" :class="{active: detailTab==='overview'}" href="#" @click="detailTab='overview'">Áttekintés</a></li><li class="nav-item"><a class="nav-link" :class="{active: detailTab==='history'}" href="#" @click="loadHistory()">Szervizkönyv</a></li></ul>
            <div class="tab-content-area">
                <div v-if="detailTab === 'overview'">
                    <div class="row g-3">
                        <div class="col-md-3"><div class="stat-box"><div class="stat-value">{{ selectedCar.mileage.toLocaleString() }}</div><div class="stat-label">Km</div></div></div>
                        <div class="col-md-3"><div class="stat-box" :style="(selectedCar.status !== 'OK' && selectedCar.status !== null) ? 'background: #f8d7da; color: #721c24;' : 'background: #d1e7dd; color: #0f5132;'"><div class="stat-value"><i :class="(selectedCar.status !== 'OK' && selectedCar.status !== null) ? 'bi bi-x-circle' : 'bi bi-check-circle'"></i> {{ (selectedCar.status !== 'OK' && selectedCar.status !== null) ? 'HIBA' : 'OK' }}</div><div class="stat-label">Állapot</div></div></div>
                        <div class="col-md-3"><div class="stat-box"><div class="stat-value">{{ formatMoney(selectedCar.year_cost, selectedCar.currency) }}</div><div class="stat-label">Idei költés</div></div></div>
                        <div class="col-md-3">
                            <div v-if="selectedCar.status !== 'OK' && selectedCar.status !== null" class="alert alert-danger h-100 shadow-sm border-danger p-2 m-0 d-flex flex-column justify-content-center"><h6 class="fw-bold text-danger mb-1" style="font-size: 0.9rem">Hiba:</h6><p class="mb-2 text-dark small" style="line-height: 1.2">{{ selectedCar.current_issue }}</p><button class="btn btn-sm btn-danger w-100" @click="resolveIssue">Megjavítva</button></div><div v-else class="stat-box text-muted border bg-light"><div class="fs-4"><i class="bi bi-shield-check"></i></div><div class="stat-label">Nincs hiba</div></div>
                        </div>
                    </div>
                </div>
                <div v-if="detailTab === 'history'">
                    <div v-if="history.length === 0" class="text-center py-5 text-muted">Még üres a szervizkönyv.</div>
                    <div v-else class="ps-3 pt-2">
                        <div v-for="item in history" :key="item.date + item.type" class="history-item">
                            <div class="history-icon" :class="item.type.split('_')[0]"></div>
                            <div class="d-flex justify-content-between align-items-start">
                                <div>
                                    <h6 class="fw-bold mb-1">{{ translateType(item.type) }}<span v-if="item.amount" class="text-success ms-2">+{{ formatMoney(item.amount, item.currency) }}</span></h6>
                                    <p class="text-muted small mb-0">{{ item.description }}</p>
                                    <div class="d-flex align-items-center gap-2 text-muted small mt-1"><span v-if="item.mileage > 0"><i class="bi bi-speedometer2 me-1"></i>{{ item.mileage.toLocaleString() }} km</span><a v-if="item.document_url" :href="item.document_url" target="_blank" class="badge bg-primary text-decoration-none"><i class="bi bi-paperclip me-1"></i>Csatolmány</a></div>
                                </div>
                                <div class="text-end"><span class="badge bg-light text-dark border">{{ item.date }}</span></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div v-if="showCostModal" class="modal-backdrop-custom">
            <div class="modal-content-custom">
                <h4 class="fw-bold mb-4">Költség / Esemény</h4>
                <div class="row g-3">
                    <div class="col-6"><label class="form-label fw-bold small">Kategória</label><select class="form-select" v-model="costForm.mainCategory" @change="updateSubCategory"><option v-for="(val, key) in costDefinitions" :value="key">{{ val.label }}</option></select></div>
                    <div class="col-6"><label class="form-label fw-bold small">Típus</label><select class="form-select" v-model="costForm.subCategory" :disabled="!currentSubOptions"><option v-for="(label, key) in currentSubOptions" :value="key">{{ label }}</option></select></div>
                    <div class="col-7"><label class="form-label fw-bold small">Összeg</label><input type="number" class="form-control" v-model="costForm.amount"></div>
                    <div class="col-5"><label class="form-label fw-bold small">Pénznem</label><select class="form-select" v-model="costForm.currency"><option value="HUF">HUF</option><option value="EUR">EUR</option></select></div>
                    <div class="col-6"><label class="form-label fw-bold small">Km állás</label><input type="number" class="form-control" v-model="costForm.mileage"></div>
                    <div class="col-6"><label class="form-label fw-bold small">Dátum</label><input type="date" class="form-control" v-model="costForm.date"></div>
                    <div class="col-12"><label class="form-label small">Megjegyzés</label><textarea class="form-control" rows="2" v-model="costForm.description"></textarea></div>
                    <div class="col-12"><label class="form-label fw-bold small"><i class="bi bi-paperclip me-1"></i>Bizonylat</label><input type="file" class="form-control" ref="fileInput"></div>
                </div>
                <div class="d-flex justify-content-end gap-2 mt-4"><button class="btn btn-light" @click="showCostModal = false">Mégse</button><button class="btn btn-success" @click="submitCost">Mentés</button></div>
            </div>
        </div>

        <div v-if="showErrorModal" class="modal-backdrop-custom"><div class="modal-content-custom bg-danger bg-opacity-10 border border-danger"><h4 class="fw-bold text-danger">Hiba</h4><textarea class="form-control my-3" rows="3" v-model="errorForm.description"></textarea><div class="form-check mb-3"><input class="form-check-input" type="checkbox" v-model="errorForm.is_critical"><label class="form-check-label">Kritikus</label></div><div class="d-flex justify-content-end gap-2"><button class="btn btn-light" @click="showErrorModal = false">Mégse</button><button class="btn btn-danger" @click="submitError">Mentés</button></div></div></div>
    </div>
</div>

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
    const { createApp } = Vue
    createApp({
        data() { return { 
            view: 'dashboard', detailTab: 'overview', showErrorModal: false, showCostModal: false, myCars: [], selectedCar: null, history: [], errorForm: {description: '', is_critical: false}, 
            costForm: { mainCategory: '', subCategory: '', amount: 0, currency: 'HUF', mileage: 0, date: '', description: '' },
            costDefinitions: {} 
        }},
        computed: {
            currentSubOptions() { return (this.costDefinitions[this.costForm.mainCategory]) ? this.costDefinitions[this.costForm.mainCategory].subs : null; }
        },
        methods: {
            async fetchRefData() {
                const res = await fetch('/api/ref/cost_types');
                this.costDefinitions = await res.json();
                this.costForm.mainCategory = Object.keys(this.costDefinitions)[0];
            },
            async fetchData() { const res = await fetch('/api/my_vehicles'); this.myCars = await res.json(); },
            async openVehicleDetail(id) { const res = await fetch('/api/vehicle/' + id); this.selectedCar = await res.json(); this.view = 'detail'; this.detailTab = 'overview'; },
            
            openCostModal() { 
                this.costForm = { mainCategory: Object.keys(this.costDefinitions)[0], subCategory: '', amount: '', currency: this.selectedCar.currency || 'HUF', mileage: this.selectedCar.mileage, date: new Date().toISOString().split('T')[0], description: '' }; 
                this.updateSubCategory();
                this.showCostModal = true; 
            },
            updateSubCategory() {
                if(!this.costDefinitions[this.costForm.mainCategory]) return;
                const subs = this.costDefinitions[this.costForm.mainCategory].subs;
                this.costForm.subCategory = subs ? Object.keys(subs)[0] : '';
            },
            async submitCost() {
                const finalType = this.costForm.subCategory || this.costForm.mainCategory;
                const formData = new FormData();
                formData.append('vehicle_id', this.selectedCar.id); formData.append('cost_type', finalType); formData.append('amount', this.costForm.amount); formData.append('currency', this.costForm.currency); formData.append('mileage', this.costForm.mileage); formData.append('date_str', this.costForm.date); formData.append('description', this.costForm.description);
                const fileInput = this.$refs.fileInput; if(fileInput.files.length > 0) formData.append('file', fileInput.files[0]);
                try {
                    const res = await fetch('/api/add_cost', { method: 'POST', body: formData });
                    if (res.ok) { alert("Költség rögzítve!"); this.showCostModal = false; this.openVehicleDetail(this.selectedCar.id); if(this.detailTab === 'history') this.loadHistory(); } else { alert("Hiba: " + JSON.stringify(await res.json())); }
                } catch(e) { alert("Szerver hiba!"); }
            },
            async loadHistory() { this.detailTab = 'history'; const res = await fetch('/api/vehicle/' + this.selectedCar.id + '/history'); this.history = await res.json(); },
            formatMoney(amount, currency) { try { return new Intl.NumberFormat('hu-HU', { style: 'currency', currency: currency }).format(amount); } catch(e) { return amount + " " + currency; } },
            translateType(type) { 
                if(!this.costDefinitions) return type;
                if (this.costDefinitions[type]) return this.costDefinitions[type].label;
                for (const main in this.costDefinitions) {
                    if (this.costDefinitions[main].subs && this.costDefinitions[main].subs[type]) return this.costDefinitions[main].subs[type];
                }
                const map = { 'ISSUE_REPORT': 'Hiba Bejelentés', 'ISSUE_RESOLVED': 'Hiba Javítva', 'MILEAGE_UPDATE': 'Km Korrekció' }; 
                return map[type] || type; 
            },
            openErrorModal() { this.errorForm = {description: '', is_critical: false}; this.showErrorModal = true; },
            async submitError() { await fetch('/api/report_issue', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ vehicle_id: this.selectedCar.id, description: this.errorForm.description, is_critical: this.errorForm.is_critical }) }); this.showErrorModal = false; this.openVehicleDetail(this.selectedCar.id); },
            async resolveIssue() { if(!confirm("Megjavítva?")) return; await fetch('/api/resolve_issue', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ vehicle_id: this.selectedCar.id }) }); this.openVehicleDetail(this.selectedCar.id); }
        },
        mounted() { this.fetchData(); this.fetchRefData(); }
    }).mount('#app')
</script>
</body>
</html>
