import React, { useEffect } from "react"; import { apiFetch } from "../../api/client"; const Step4Environment = ({ data, updateData }) => { const toggleTerrain = (terrain) => { const current = data.terrain || []; const updated = current.includes(terrain) ? current.filter((t) => t !== terrain) : [...current, terrain]; updateData({ terrain: updated }); }; // Автоматически определяем сезон из даты потери useEffect(() => { if (data.lost_time && !data.season) { const date = new Date(data.lost_time); const month = date.getMonth() + 1; // 1-12 let season = 'лето'; if (month >= 3 && month <= 5) season = 'весна'; else if (month >= 6 && month <= 8) season = 'лето'; else if (month >= 9 && month <= 11) season = 'осень'; else season = 'зима'; // 12, 1, 2 updateData({ season }); } }, [data.lost_time]); const handleGeocode = async () => { const query = (data.tnp_address || "").trim(); if (query.length < 3) { updateData({ geocode_error: "Введите адрес (минимум 3 символа)" }); return; } updateData({ geocode_loading: true, geocode_error: null, geocode_found: null }); try { const resp = await apiFetch(`/api/v1/geocode?q=${encodeURIComponent(query)}&limit=1`); if (!resp.ok) { const err = await resp.json().catch(() => ({})); throw new Error(err.detail || `Ошибка ${resp.status}`); } const result = await resp.json(); const hit = result.results && result.results[0]; if (!hit) { updateData({ geocode_loading: false, geocode_error: "Адрес не найден. Уточните: «пункт, адрес» без района/области.", }); return; } updateData({ tnp_lat: hit.lat.toFixed(6), tnp_lon: hit.lng.toFixed(6), geocode_loading: false, geocode_error: null, geocode_found: hit.display_name, }); } catch (e) { updateData({ geocode_loading: false, geocode_error: "Не удалось найти адрес: " + e.message }); } }; const handleGetLocation = () => { if (!navigator.geolocation) { updateData({ gps_error: 'Геолокация не поддерживается вашим браузером' }); return; } updateData({ gps_loading: true, gps_error: null }); navigator.geolocation.getCurrentPosition( (position) => { updateData({ tnp_lat: position.coords.latitude.toFixed(6), tnp_lon: position.coords.longitude.toFixed(6), gps_loading: false, gps_error: null }); }, (error) => { updateData({ gps_error: 'Ошибка получения координат: ' + error.message, gps_loading: false }); }, { enableHighAccuracy: true, timeout: 10000, maximumAge: 0 } ); }; return (
{data.geocode_error}
)} {data.geocode_found && (Найдено: {data.geocode_found}
)} {!data.geocode_error && !data.geocode_found && (Формат «пункт, адрес» (район и область не нужны). Координаты заполнятся в поля выше.
)}