Coots commited on
Commit
1cbee68
·
verified ·
1 Parent(s): 0b2ad96

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +120 -64
index.html CHANGED
@@ -1,69 +1,125 @@
1
- <script>
2
- const state={step:'tileType',tileType:null,area:null,length:null,width:null};
3
- function selectTileType(t){
4
- state.tileType=t;state.step='area';
5
- addMessage('user',t+' tiles'); ask('area');
6
- }
7
- function ask(field){
8
- showTyping();
9
- setTimeout(()=>{
10
- hideTyping();
11
- addMessage('bot',
12
- field==='area' ? "Total area (sq.ft)?" :
13
- field==='length' ? "Tile length (ft)?" : "Tile width (ft)?"
14
- );
15
- state.step=field;
16
- },1000);
17
- }
18
- function sendMessage(){
19
- const m=userInput.value.trim(); if(!m)return;
20
- addMessage('user',m);userInput.value='';
21
- handleUser(m);
22
- }
23
- function handleUser(msg){
24
- if(state.step==='area'){
25
- const v=parseFloat(msg);
26
- return isNaN(v)? addBot("Enter valid area.") : (state.area=v,ask('length'));
 
 
 
 
 
 
 
27
  }
28
- if(state.step==='length'){
29
- const v=parseFloat(msg);
30
- return isNaN(v)? addBot("Enter valid length.") : (state.length=v,ask('width'));
 
 
 
31
  }
32
- if(state.step==='width'){
33
- const v=parseFloat(msg);
34
- return isNaN(v)? addBot("Enter valid width.") : (
35
- state.width=v,
36
- showResults()
37
- );
38
  }
39
- }
40
- function showResults(){
41
- const c = state.length * state.width;
42
- const tiles = Math.ceil((state.area/c)*1.1);
43
- addBot(`📦 You need ${tiles} tiles (~10% buffer).`);
44
- fetch('/recommend', {
45
- method:'POST', headers:{'Content-Type':'application/json'},
46
- body:JSON.stringify({
47
- tile_type: state.tileType,
48
- area: state.area,
49
- length: state.length,
50
- width: state.width,
51
- price_range: [3,10]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  })
53
- })
54
- .then(r=>r.json())
55
- .then(r=>{
56
- if(r.recommended_products && r.recommended_products.length){
57
- recommendations.innerHTML = '';
58
- r.recommended_products.forEach(p=>{
59
- recommendations.innerHTML += `
60
- <div class="card">
61
- <h4>${p.name}</h4>
62
- <p>${p.size} — ₹${p.price}/box</p>
63
- </div>`;
64
- });
65
- } else addBot("No recommendations.");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  });
67
- }
68
- // Helper functions: addBot, addMessage, showTyping, hideTyping (as before)...
69
- </script>
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6
+ <title>Tile Calculator AI</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <style>
9
+ /* Your existing styles… */
10
+ </style>
11
+ </head>
12
+ <body class="bg-gray-100">
13
+ <!-- (Header + Chat Area + Input Area omitted for brevity) -->
14
+ <div id="chat-area" class="chat-container p-4 overflow-y-auto">
15
+ <div class="bot-message bg-gray-100 rounded-lg p-4 max-w-xs mb-3">
16
+ <p>Hello! 👋 Floor or wall tiles?</p>
17
+ <button onclick="selectTileType('floor')" class="quick-reply">Floor</button>
18
+ <button onclick="selectTileType('wall')" class="quick-reply">Wall</button>
19
+ </div>
20
+ </div>
21
+ <!-- Recommendations Section -->
22
+ <div id="recommendations" class="grid grid-cols-2 md:grid-cols-4 gap-4 p-4 border-t bg-gray-50"></div>
23
+
24
+ <script>
25
+ const state = { step: 'tileType', tileType: null, length: null, width: null };
26
+ const chatArea = document.getElementById('chat-area'),
27
+ userInput = null, /* we won't use user text input here for simplicity */,
28
+ recommendations = document.getElementById('recommendations');
29
+
30
+ function selectTileType(type){
31
+ state.tileType = type;
32
+ state.step = 'length';
33
+ appendBot(`Great. What's the length of the area in ft?`);
34
  }
35
+
36
+ function appendBot(text) {
37
+ chatArea.insertAdjacentHTML('beforeend', `
38
+ <div class="bot-message bg-gray-100 rounded-lg p-4 mb-3">${text}</div>
39
+ `);
40
+ chatArea.scrollTop = chatArea.scrollHeight;
41
  }
42
+
43
+ function appendUser(text) {
44
+ chatArea.insertAdjacentHTML('beforeend', `
45
+ <div class="user-message ml-auto bg-indigo-600 text-white rounded-lg p-4 mb-3">${text}</div>
46
+ `);
47
+ chatArea.scrollTop = chatArea.scrollHeight;
48
  }
49
+
50
+ function askValue(val){
51
+ const answer = prompt(val);
52
+ if (!answer) return appendBot('Input canceled.');
53
+ appendUser(answer);
54
+ processValue(parseFloat(answer));
55
+ }
56
+
57
+ function processValue(num){
58
+ if (state.step === 'length'){
59
+ state.length = num;
60
+ state.step = 'width';
61
+ appendBot("And the width (ft)?");
62
+ askValue("Width in ft:");
63
+ } else if (state.step === 'width'){
64
+ state.width = num;
65
+ calculate();
66
+ }
67
+ }
68
+
69
+ function calculate(){
70
+ const area = state.length * state.width;
71
+ const coverage = 1; // tile covers 1 sq ft default
72
+ const numTiles = Math.ceil(area * 1.1 / coverage);
73
+ appendBot(`📐 Area: ${area.toFixed(2)} ft²<br>🧮 Tiles needed (incl. 10%): ${numTiles}`);
74
+ fetchRecommendations(coverage, area);
75
+ }
76
+
77
+ function fetchRecommendations(coverage, area){
78
+ fetch('/recommend', {
79
+ method: 'POST',
80
+ headers:{'Content-Type':'application/json'},
81
+ body: JSON.stringify({
82
+ tile_type: state.tileType,
83
+ coverage,
84
+ area,
85
+ price_range: [3,10]
86
+ })
87
  })
88
+ .then(r=>r.json())
89
+ .then(d=> {
90
+ if (d.recommended_products?.length){
91
+ showProducts(d.recommended_products);
92
+ } else {
93
+ appendBot("No recommendations found for your specs.");
94
+ }
95
+ });
96
+ }
97
+
98
+ function showProducts(products){
99
+ recommendations.innerHTML = '';
100
+ products.forEach(p => {
101
+ recommendations.insertAdjacentHTML('beforeend', `
102
+ <div class="bg-white shadow rounded-lg p-4">
103
+ <h4 class="font-medium">${p.name}</h4>
104
+ <p class="text-sm">₹${p.price}/box</p>
105
+ <a href="${p.link}" target="_blank" class="text-indigo-600 hover:underline">View</a>
106
+ </div>
107
+ `);
108
+ });
109
+ }
110
+
111
+ // start logic: after selecting tile type, ask length
112
+ document.addEventListener('DOMContentLoaded', ()=> {
113
+ if(state.step!=='tileType') return;
114
+ document.querySelectorAll('.quick-reply').forEach(btn=>{
115
+ btn.onclick = ()=> {
116
+ const type = btn.getAttribute('onclick').match(/'(\w+)'/)[1];
117
+ appendUser(type);
118
+ selectTileType(type);
119
+ askValue("Length in ft:");
120
+ }
121
+ });
122
  });
123
+ </script>
124
+ </body>
125
+ </html>