File size: 16,603 Bytes
fab0b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Perfil de Consumo</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Inter', sans-serif;
        }
        .chart-container {
            position: relative;
            height: 300px;
            width: 100%;
        }
        .fade-in {
            animation: fadeIn 0.3s ease-in-out;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .month-input::-webkit-calendar-picker-indicator {
            filter: invert(0.5);
        }
        .month-input::-webkit-datetime-edit-text {
            color: #6b7280;
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen">
    <div class="container max-w-3xl mx-auto py-8 px-4">
        <div class="mb-6">
            <h1 class="text-2xl font-semibold text-gray-800 mb-2">Perfil de Consumo</h1>
            <p class="text-gray-500">
                Forneça informações sobre seu consumo mensal para análise personalizada.
            </p>
        </div>

        <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
            <div class="p-6">
                <form id="consumptionForm" class="space-y-6">
                    <!-- Average Consumption -->
                    <div class="space-y-2">
                        <label for="averageKwh" class="block text-sm font-medium text-gray-700">Consumo médio mensal (kWh)</label>
                        <input 
                            type="number" 
                            id="averageKwh" 
                            class="block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                            placeholder="Ex: 250" 
                            min="0"
                            required
                        >
                        <p class="text-xs text-gray-500">
                            Este valor pode ser encontrado em sua conta de energia
                        </p>
                    </div>

                    <!-- Consumption History -->
                    <div class="space-y-3">
                        <div class="flex justify-between items-center">
                            <label class="block text-sm font-medium text-gray-700">Histórico de consumo (opcional)</label>
                            <button 
                                type="button" 
                                id="addReadingBtn"
                                class="inline-flex items-center px-3 py-1.5 border border-gray-300 shadow-sm text-sm leading-4 font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
                            >
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                                </svg>
                                Adicionar
                            </button>
                        </div>

                        <div id="readingsContainer" class="space-y-3">
                            <!-- Readings will be added here -->
                        </div>

                        <div id="noReadingsMessage" class="text-sm text-gray-500">
                            Adicione leituras de consumo para uma análise mais precisa
                        </div>
                    </div>

                    <!-- Chart Visualization -->
                    <div id="chartContainer" class="pt-4 hidden">
                        <label class="block text-sm font-medium text-gray-700 mb-2">Visualização do consumo</label>
                        <div class="chart-container">
                            <canvas id="consumptionChart"></canvas>
                        </div>
                    </div>

                    <!-- Buttons -->
                    <div class="flex justify-between pt-6 border-t border-gray-200">
                        <button 
                            type="button" 
                            id="backBtn"
                            class="px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
                        >
                            Voltar
                        </button>
                        <button 
                            type="submit" 
                            id="submitBtn"
                            class="px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50 disabled:cursor-not-allowed"
                        >
                            Analisar Perfil de Consumo
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // State
            let readings = [];
            let chart = null;

            // Elements
            const form = document.getElementById('consumptionForm');
            const addReadingBtn = document.getElementById('addReadingBtn');
            const readingsContainer = document.getElementById('readingsContainer');
            const noReadingsMessage = document.getElementById('noReadingsMessage');
            const chartContainer = document.getElementById('chartContainer');
            const averageKwhInput = document.getElementById('averageKwh');
            const submitBtn = document.getElementById('submitBtn');
            const backBtn = document.getElementById('backBtn');

            // Initialize with sample data if needed
            // averageKwhInput.value = '250';

            // Add new reading entry
            addReadingBtn.addEventListener('click', function() {
                const now = new Date();
                const currentMonth = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`;
                
                const readingId = `reading-${Date.now()}`;
                readings.push({
                    id: readingId,
                    month: currentMonth,
                    value: ''
                });

                renderReadings();
                updateChart();
            });

            // Render all readings
            function renderReadings() {
                readingsContainer.innerHTML = '';
                
                if (readings.length === 0) {
                    noReadingsMessage.classList.remove('hidden');
                    return;
                }
                
                noReadingsMessage.classList.add('hidden');
                
                readings.forEach(reading => {
                    const readingElement = document.createElement('div');
                    readingElement.className = 'flex gap-3 items-center fade-in';
                    readingElement.innerHTML = `
                        <div class="flex-1">
                            <input
                                type="month"
                                class="month-input block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                value="${reading.month}"
                                onchange="updateReading('${reading.id}', 'month', this.value)"
                                required
                            >
                        </div>
                        <div class="flex-1">
                            <input
                                type="number"
                                class="block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                value="${reading.value}"
                                placeholder="kWh"
                                min="0"
                                onchange="updateReading('${reading.id}', 'value', this.value)"
                                required
                            >
                        </div>
                        <button
                            type="button"
                            class="p-2 rounded-md text-gray-400 hover:text-red-500 hover:bg-red-50 focus:outline-none focus:ring-2 focus:ring-red-500"
                            onclick="removeReading('${reading.id}')"
                        >
                            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                            </svg>
                        </button>
                    `;
                    readingsContainer.appendChild(readingElement);
                });
            }

            // Update reading value
            window.updateReading = function(id, field, value) {
                readings = readings.map(r => r.id === id ? { ...r, [field]: value } : r);
                updateChart();
            };

            // Remove reading
            window.removeReading = function(id) {
                readings = readings.filter(r => r.id !== id);
                renderReadings();
                updateChart();
            };

            // Update chart with current data
            function updateChart() {
                const validReadings = readings
                    .filter(r => r.month && r.value !== '' && !isNaN(Number(r.value)))
                    .map(r => ({
                        month: r.month,
                        value: Number(r.value)
                    }))
                    .sort((a, b) => a.month.localeCompare(b.month));
                
                if (validReadings.length >= 2) {
                    chartContainer.classList.remove('hidden');
                    
                    const ctx = document.getElementById('consumptionChart').getContext('2d');
                    const labels = validReadings.map(r => {
                        const [year, month] = r.month.split('-');
                        const monthNames = ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'];
                        return `${monthNames[parseInt(month) - 1]}/${year.slice(2)}`;
                    });
                    const data = validReadings.map(r => r.value);
                    
                    if (chart) {
                        chart.destroy();
                    }
                    
                    chart = new Chart(ctx, {
                        type: 'line',
                        data: {
                            labels: labels,
                            datasets: [{
                                label: 'Consumo (kWh)',
                                data: data,
                                backgroundColor: 'rgba(99, 102, 241, 0.1)',
                                borderColor: 'rgba(99, 102, 241, 1)',
                                borderWidth: 2,
                                tension: 0.3,
                                pointBackgroundColor: 'rgba(99, 102, 241, 1)',
                                pointRadius: 4,
                                pointHoverRadius: 6
                            }]
                        },
                        options: {
                            responsive: true,
                            maintainAspectRatio: false,
                            scales: {
                                y: {
                                    beginAtZero: true,
                                    title: {
                                        display: true,
                                        text: 'kWh'
                                    }
                                }
                            },
                            plugins: {
                                tooltip: {
                                    callbacks: {
                                        label: function(context) {
                                            return `${context.parsed.y} kWh`;
                                        }
                                    }
                                }
                            }
                        }
                    });
                } else {
                    chartContainer.classList.add('hidden');
                    if (chart) {
                        chart.destroy();
                        chart = null;
                    }
                }
            }

            // Form submission
            form.addEventListener('submit', function(e) {
                e.preventDefault();
                
                // Validate average consumption
                if (!averageKwhInput.value || isNaN(Number(averageKwhInput.value)) || Number(averageKwhInput.value) < 0) {
                    showToast('Erro de validação', 'Consumo médio deve ser um número positivo', 'error');
                    return;
                }
                
                // Simulate loading
                submitBtn.disabled = true;
                submitBtn.innerHTML = `
                    <svg class="animate-spin -ml-1 mr-2 h-4 w-4 text-white inline" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    Processando...
                `;
                
                // Simulate API call
                setTimeout(() => {
                    showToast('Perfil de consumo analisado', 'Sua análise de consumo foi concluída com sucesso', 'success');
                    
                    // Reset button
                    submitBtn.disabled = false;
                    submitBtn.textContent = 'Analisar Perfil de Consumo';
                    
                    // In a real app, you would redirect here
                    // window.location.href = '/onboard/resumo';
                }, 2000);
            });

            // Back button
            backBtn.addEventListener('click', function() {
                // In a real app, you would go back
                // window.history.back();
                console.log('Back button clicked');
            });

            // Show toast notification
            function showToast(title, message, type) {
                // In a real app, you would use a proper toast library
                console.log(`${type.toUpperCase()}: ${title} - ${message}`);
                alert(`${title}\n${message}`);
            }
        });
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=fernando-bold/src-app-auth-onboard-consumo-page-tsx" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>