/**
* Capture Form Component
* Data entry form for capture details
*/
const CaptureFormComponent = {
captureData: null,
location: null,
placeName: null,
render() {
const selectedSpecies = app.state.selectedSpecies || [];
return `
`;
},
async init() {
console.log('Capturando ubicación en segundo plano...');
try {
this.location = await geolocationService.getCurrentPosition();
console.log('Ubicación obtenida con éxito (silenciosa)');
} catch (error) {
console.warn('Error capturando ubicación inicial:', error);
}
},
getSpeciesName(speciesId) {
const species = SpeciesSelectorComponent.allSpecies.find(s => s.id === speciesId);
return species ? species.commonName : speciesId;
},
async submit(event) {
event.preventDefault();
// Refresco silencioso de GPS al enviar
try {
this.location = await geolocationService.getCurrentPosition();
} catch (e) {
console.warn('Refresh GPS falló, usando último valor conocido:', e);
}
if (!this.location) {
alert('Asegúrate de permitir el acceso al GPS para continuar. Es necesario para el registro científico.');
return;
}
const selectedSpecies = app.state.selectedSpecies || [];
const items = [];
// Collect data for each species
selectedSpecies.forEach((speciesId, index) => {
const speciesInfo = SpeciesSelectorComponent.allSpecies.find(s => s.id === speciesId) || {};
const quantity = parseFloat(document.getElementById(`quantity-${index}`).value);
const unit = document.getElementById(`unit-${index}`).value;
const customNameInput = document.getElementById(`custom-name-${index}`);
const customName = customNameInput ? customNameInput.value : null;
items.push({
speciesId: speciesId,
commonName: speciesId === 'otro' ? customName : speciesInfo.commonName,
category: speciesInfo.category || 'Otros',
quantity: quantity,
unit: unit,
customName: customName
});
});
// Create capture object
const capture = {
id: this.generateUUID(),
timestamp: new Date().toISOString(),
latitude: this.location.latitude,
longitude: this.location.longitude,
placeName: this.placeName,
species: selectedSpecies,
items: items,
fishingMethod: document.getElementById('fishing-method').value,
depth: parseFloat(document.getElementById('depth').value),
// Simplified fields
port: document.getElementById('port').value,
phone: document.getElementById('phone').value,
totalWeight: items.reduce((sum, item) => sum + (item.unit === 'lbs' ? item.quantity : 0), 0),
captureSource: 'device-gps',
synced: 0
};
// Save to IndexedDB
try {
await dbService.addCapture(capture);
// Store in app state for confirmation
app.state.lastCapture = capture;
// Navigate to confirmation
app.navigate('confirmation');
// Trigger sync if online
if (navigator.onLine) {
syncService.syncNow();
}
} catch (error) {
console.error('Error saving capture:', error);
alert('Error al guardar la captura. Por favor, intenta de nuevo.');
}
},
generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
};