Spaces:
Running
Running
File size: 6,697 Bytes
8722bff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | class DataManager {
constructor() {
this.interventions = JSON.parse(localStorage.getItem('interventions')) || [];
this.cultures = JSON.parse(localStorage.getItem('cultures')) || [];
this.parametres = JSON.parse(localStorage.getItem('parametres')) || this.getDefaultParams();
}
getDefaultParams() {
return {
lbc: {
n2o_direct: 0.01,
n2o_indirect: 0.01,
co2_engrais: 0.6,
co2_pesticides: 3.0
},
giec: {
n2o_direct: 0.01,
n2o_indirect: 0.0075,
co2_engrais: 1.3,
co2_pesticides: 5.0
}
};
}
addIntervention(intervention) {
this.interventions.push({
...intervention,
id: Date.now().toString(),
createdAt: new Date().toISOString()
});
this.saveData();
}
addCulture(culture) {
this.cultures.push({
...culture,
id: Date.now().toString(),
createdAt: new Date().toISOString()
});
this.saveData();
}
removeIntervention(id) {
this.interventions = this.interventions.filter(i => i.id !== id);
this.saveData();
}
removeCulture(id) {
this.cultures = this.cultures.filter(c => c.id !== id);
this.saveData();
}
calculateEmissions() {
const results = {
lbc: this.calculateWithMethod('lbc'),
giec: this.calculateWithMethod('giec'),
total: {
lbc: 0,
giec: 0
}
};
results.total.lbc = Object.values(results.lbc).reduce((sum, val) => sum + val, 0);
results.total.giec = Object.values(results.giec).reduce((sum, val) => sum + val, 0);
return results;
}
calculateWithMethod(method) {
const params = this.parametres[method];
let emissions = {
n2o_direct: 0,
n2o_indirect: 0,
co2_engrais: 0,
co2_pesticides: 0,
co2_carburant: 0
};
this.interventions.forEach(intervention => {
if (intervention.type === 'engrais') {
emissions.n2o_direct += intervention.quantite * params.n2o_direct;
emissions.n2o_indirect += intervention.quantite * params.n2o_indirect;
emissions.co2_engrais += intervention.quantite * params.co2_engrais;
} else if (intervention.type === 'pesticide') {
emissions.co2_pesticides += intervention.quantite * params.co2_pesticides;
} else if (intervention.type === 'travail_sol') {
emissions.co2_carburant += intervention.quantite * 2.68; // kg CO2/L de gasoil
}
});
return emissions;
}
exportToExcel() {
const data = {
interventions: this.interventions,
cultures: this.cultures,
emissions: this.calculateEmissions(),
calcul: new Date().toISOString()
};
const blob = new Blob([JSON.stringify(data, null, 2)], {
type: 'application/json'
});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `emissions_agricoles_${new Date().toISOString().split('T')[0]}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
saveData() {
localStorage.setItem('interventions', JSON.stringify(this.interventions));
localStorage.setItem('cultures', JSON.stringify(this.cultures));
localStorage.setItem('parametres', JSON.stringify(this.parametres));
}
clearAllData() {
this.interventions = [];
this.cultures = [];
this.saveData();
}
}
// Initialisation globale
window.dataManager = new DataManager();
// Fonctions utilitaires
function formatNumber(number, decimals = 2) {
return new Intl.NumberFormat('fr-FR', {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals
}).format(number);
}
function showAlert(message, type = 'info') {
const alert = document.createElement('div');
alert.className = `alert alert-${type} fixed top-4 right-4 z-50`;
alert.innerHTML = `
<div class="flex items-center">
<i data-feather="${type === 'success' ? 'check-circle' : type === 'error' ? 'alert-circle' : 'info'}"
class="w-5 h-5 mr-2"></i>
<span>${message}</span>
</div>
`;
document.body.appendChild(alert);
feather.replace();
setTimeout(() => {
alert.remove();
}, 5000);
}
// Gestion des événements
document.addEventListener('DOMContentLoaded', function() {
// Initialisation des tooltips
const tooltips = document.querySelectorAll('[data-tooltip]');
tooltips.forEach(tooltip => {
tooltip.addEventListener('mouseenter', function(e) {
const tooltipText = this.getAttribute('data-tooltip');
const tooltipEl = document.createElement('div');
tooltipEl.className = 'absolute z-50 px-3 py-2 text-sm text-white bg-gray-900 rounded-md shadow-lg';
tooltipEl.textContent = tooltipText;
tooltipEl.style.top = (e.clientY - 40) + 'px';
tooltipEl.style.left = (e.clientX + 10) + 'px';
document.body.appendChild(tooltipEl);
this.addEventListener('mouseleave', function() {
tooltipEl.remove();
});
});
});
// Gestion des formulaires
const forms = document.querySelectorAll('form[data-ajax]');
forms.forEach(form => {
form.addEventListener('submit', async function(e) {
e.preventDefault();
const formData = new FormData(this);
const submitBtn = this.querySelector('button[type="submit"]');
const originalText = submitBtn.textContent;
submitBtn.disabled = true;
submitBtn.innerHTML = '<div class="loading-spinner"></div>';
try {
// Simulation de traitement
await new Promise(resolve => setTimeout(resolve, 1000));
const data = Object.fromEntries(formData.entries());
if (this.id === 'intervention-form') {
window.dataManager.addIntervention(data);
showAlert('Intervention ajoutée avec succès', 'success');
} else if (this.id === 'culture-form |