File size: 7,833 Bytes
aaa2e91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da475f5
aaa2e91
da475f5
aaa2e91
da475f5
aaa2e91
 
 
 
 
 
 
 
da475f5
aaa2e91
 
 
 
 
da475f5
aaa2e91
 
 
da475f5
aaa2e91
 
 
da475f5
aaa2e91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da475f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaa2e91
 
 
 
 
 
 
 
 
da475f5
 
 
 
 
aaa2e91
 
 
 
 
 
 
da475f5
 
 
aaa2e91
 
 
 
da475f5
aaa2e91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
let selectedType = 'Appartement';
const steppers = { pieces: 3, lots: 1 };

// ── Three.js background ──
function initThree() {
  const canvas = document.getElementById('bg-canvas');
  const renderer = new THREE.WebGLRenderer({ canvas, alpha: true, antialias: true });
  renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
  renderer.setSize(window.innerWidth, window.innerHeight);

  const scene = new THREE.Scene();
  const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100);
  camera.position.z = 30;

  const count = 1200;
  const positions = new Float32Array(count * 3);
  const colors = new Float32Array(count * 3);

  for (let i = 0; i < count; i++) {
    positions[i * 3]     = (Math.random() - 0.5) * 80;
    positions[i * 3 + 1] = (Math.random() - 0.5) * 80;
    positions[i * 3 + 2] = (Math.random() - 0.5) * 40;

    const r = Math.random();
    if (r < 0.5) {
      colors[i * 3] = 0.23; colors[i * 3 + 1] = 0.51; colors[i * 3 + 2] = 0.96;
    } else if (r < 0.8) {
      colors[i * 3] = 0.02; colors[i * 3 + 1] = 0.71; colors[i * 3 + 2] = 0.83;
    } else {
      colors[i * 3] = 0.55; colors[i * 3 + 1] = 0.58; colors[i * 3 + 2] = 0.70;
    }
  }

  const geo = new THREE.BufferGeometry();
  geo.setAttribute('position', new THREE.BufferAttribute(positions, 3));
  geo.setAttribute('color', new THREE.BufferAttribute(colors, 3));

  const mat = new THREE.PointsMaterial({
    size: 0.12, vertexColors: true, transparent: true, opacity: 0.7, sizeAttenuation: true
  });

  const points = new THREE.Points(geo, mat);
  scene.add(points);

  const lineMat = new THREE.LineBasicMaterial({ color: 0x1e3a5f, transparent: true, opacity: 0.15 });

  for (let i = -5; i <= 5; i++) {
    const hGeo = new THREE.BufferGeometry().setFromPoints([
      new THREE.Vector3(-40, i * 8, -10), new THREE.Vector3(40, i * 8, -10)
    ]);
    scene.add(new THREE.Line(hGeo, lineMat));
    const vGeo = new THREE.BufferGeometry().setFromPoints([
      new THREE.Vector3(i * 8, -40, -10), new THREE.Vector3(i * 8, 40, -10)
    ]);
    scene.add(new THREE.Line(vGeo, lineMat));
  }

  let mouse = { x: 0, y: 0 };
  document.addEventListener('mousemove', e => {
    mouse.x = (e.clientX / window.innerWidth - 0.5) * 0.3;
    mouse.y = (e.clientY / window.innerHeight - 0.5) * 0.3;
  });

  window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
  });

  let t = 0;
  function animate() {
    requestAnimationFrame(animate);
    t += 0.0005;
    points.rotation.y = t + mouse.x;
    points.rotation.x = mouse.y * 0.5;
    points.rotation.z = t * 0.3;
    renderer.render(scene, camera);
  }
  animate();
}

// ── Compteur animé ──
function animateCounters() {
  document.querySelectorAll('.stat-n').forEach(el => {
    const target = parseInt(el.dataset.target);
    const duration = 1500;
    const start = performance.now();
    function update(now) {
      const progress = Math.min((now - start) / duration, 1);
      const ease = 1 - Math.pow(1 - progress, 3);
      const val = Math.floor(ease * target);
      el.textContent = target >= 1000000
        ? (val / 1000000).toFixed(1) + 'M'
        : val.toLocaleString('fr-FR');
      if (progress < 1) requestAnimationFrame(update);
    }
    requestAnimationFrame(update);
  });
}

// ── Formulaire ──
function selectType(btn) {
  document.querySelectorAll('.type-btn').forEach(b => b.classList.remove('active'));
  btn.classList.add('active');
  selectedType = btn.dataset.value;
}

function step(id, delta) {
  const min = id === 'pieces' ? 1 : 0;
  const max = id === 'pieces' ? 15 : 50;
  steppers[id] = Math.min(max, Math.max(min, steppers[id] + delta));
  document.getElementById(id + '-val').textContent = steppers[id];
  document.getElementById(id).value = steppers[id];
}

function fmt(n) {
  return new Intl.NumberFormat('fr-FR').format(n) + ' €';
}

async function loadDepartements() {
  const res = await fetch('/api/departements');
  const depts = await res.json();
  const sel = document.getElementById('departement');
  sel.innerHTML = depts.map(d => `<option value="${d}">${d}</option>`).join('');
  const idx = depts.indexOf('75');
  if (idx >= 0) sel.selectedIndex = idx;
  loadCommunes();
}

async function loadCommunes() {
  const dept = document.getElementById('departement').value;
  if (!dept) return;
  const res = await fetch(`/api/communes/${dept}`);
  const communes = await res.json();
  const sel = document.getElementById('commune');
  sel.innerHTML = communes.map(c => `<option value="${c}">${c}</option>`).join('');
}

async function predict() {
  const btn = document.getElementById('cta-btn');
  const label = document.getElementById('cta-label');
  const icon = document.getElementById('cta-icon');
  const loader = document.getElementById('cta-loader');

  label.textContent = 'Analyse en cours';
  icon.style.display = 'none';
  loader.style.display = 'block';
  btn.disabled = true;

  try {
    const commune = document.getElementById('commune').value;
    const dept = document.getElementById('departement').value;

    let latitude = 0, longitude = 0, code_postal = '00000';

    try {
      const adresse = `${commune} ${dept} France`;
      const geoRes = await fetch(
        `https://api-adresse.data.gouv.fr/search/?q=${encodeURIComponent(adresse)}&limit=1`
      );
      const geoData = await geoRes.json();
      if (geoData.features && geoData.features.length > 0) {
        longitude = geoData.features[0].geometry.coordinates[0];
        latitude = geoData.features[0].geometry.coordinates[1];
        code_postal = geoData.features[0].properties.postcode || '00000';
      }
    } catch (e) {
      console.log('Géocodage échoué, fallback centroïde');
    }

    const res = await fetch('/api/predict', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        type_bien: selectedType,
        surface: document.getElementById('surface').value,
        nb_pieces: steppers.pieces,
        nb_lots: steppers.lots,
        surface_terrain: document.getElementById('terrain').value,
        commune: commune,
        departement: dept,
        latitude: latitude,
        longitude: longitude,
        code_postal: code_postal
      })
    });

    const data = await res.json();

    document.getElementById('result-price').textContent = fmt(data.prix);
    document.getElementById('result-sub').textContent =
      selectedType + ' · ' + commune + ' (' + dept + ')' +
      ' · ' + data.dist_centre_km + ' km du centre · ' +
      new Intl.NumberFormat('fr-FR').format(data.revenu_median_zone) + '€ revenu médian';
    document.getElementById('r-bas').textContent = fmt(data.fourchette_bas);
    document.getElementById('r-haut').textContent = fmt(data.fourchette_haut);
    document.getElementById('r-m2').textContent = new Intl.NumberFormat('fr-FR').format(data.prix_m2) + ' €/m²';
    document.getElementById('r-precision').textContent = data.precision + '%';
    document.getElementById('r-segment').textContent = data.segment;

    const result = document.getElementById('result');
    result.style.display = 'block';
    setTimeout(() => {
      document.getElementById('bar-fill').style.width = data.precision + '%';
    }, 100);

    result.scrollIntoView({ behavior: 'smooth', block: 'start' });

  } catch (e) {
    alert('Erreur de connexion. Vérifiez que Flask tourne bien sur le port 5000.');
  } finally {
    label.textContent = 'Estimer maintenant';
    icon.style.display = 'block';
    loader.style.display = 'none';
    btn.disabled = false;
  }
}

// ── Init ──
initThree();
animateCounters();
loadDepartements();