'use client' import { useState } from 'react' import API_URL from '@/lib/api' interface JourneyFormProps { onDataCalculated: (data: any) => void setLoading: (loading: boolean) => void onFormDataChange: (formData: any) => void } export default function JourneyForm({ onDataCalculated, setLoading, onFormDataChange }: JourneyFormProps) { const [formData, setFormData] = useState({ username: '', password: '', startDate: new Date().toISOString().slice(0, 10), endDate: new Date().toISOString().slice(0, 10), startKm: '', endKm: '', vehicleRegistration: '4SH1148', variance: '0.1' }) const [error, setError] = useState('') const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError('') setLoading(true) try { const response = await fetch(`${API_URL}/api/calculate`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ username: formData.username, password: formData.password, start_date: formData.startDate, end_date: formData.endDate, start_km: parseInt(formData.startKm), end_km: parseInt(formData.endKm), vehicle_registration: formData.vehicleRegistration, variance: parseFloat(formData.variance) }), }) if (!response.ok) { const errorData = await response.json() throw new Error(errorData.detail || 'Chyba při zpracování dat') } const data = await response.json() onDataCalculated(data) onFormDataChange(formData) } catch (err: any) { setError(err.message) onDataCalculated(null) onFormDataChange(null) } finally { setLoading(false) } } const handleExport = async () => { setLoading(true) try { const response = await fetch(`${API_URL}/api/export/excel`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ username: formData.username, password: formData.password, start_date: formData.startDate, end_date: formData.endDate, start_km: parseInt(formData.startKm), end_km: parseInt(formData.endKm), vehicle_registration: formData.vehicleRegistration, variance: parseFloat(formData.variance) }), }) if (!response.ok) throw new Error('Export failed') const blob = await response.blob() const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `journeybook_${formData.startDate}_${formData.endDate}.xlsx` a.click() } catch (err: any) { setError(err.message) } finally { setLoading(false) } } return (