quantum-ai-v2/data_analyzer/templates/data_analyzer/detail.html
Claude 375b2b9d65 Implement wallet deduction after success & fix display issues
🚀 Major improvements to agent system:

## Wallet Management 
- Move wallet deduction to AFTER successful processing (not before)
- Add real-time wallet balance updates in frontend
- Prevent users from losing money on failed requests
- Update both data_analyzer and weather_reporter processors

## Data Analyzer Agent 📊
- Fix N8N integration to handle array response format
- Add binary PDF file upload (multipart/form-data)
- Implement real-time AJAX results display below form
- Add wallet balance updates after successful processing
- Support continuous workflow with "Analyze Another File"

## Weather Reporter Agent 🌤️
- Update price from 2.5 AED to 2.0 AED
- Fix results display (was using page reload, now AJAX)
- Add real-time wallet balance updates
- Implement dynamic results rendering below form
- Add "Get Another Report" functionality

## Template System 🎨
- Update agent generator templates with correct wallet flow
- Add data-wallet-balance attributes for easy targeting
- Fix JavaScript querySelector errors
- Implement proper error handling and logging

## Documentation 📚
- Update CLAUDE.md with wallet best practices
- Add examples of current production agents
- Document required JavaScript functions
- Update pricing information and modern agent features
- Add gitignore entries for nextjs/ and netcop-ai-hub/

## Frontend JavaScript 💻
- Add updateWalletBalance() function for real-time updates
- Implement displayResults() for dynamic content rendering
- Add proper error handling with user-friendly messages
- Support continuous workflow without page refresh

🎉 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-10 16:34:03 +05:30

1138 lines
48 KiB
HTML

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data Analyzer Agent - NetCop AI Hub</title>
<style>
* {
box-sizing: border-box;
}
html, body {
margin: 0;
padding: 0;
font-family: system-ui, -apple-system, sans-serif;
overflow-x: hidden;
}
/* Responsive utilities */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 clamp(16px, 4vw, 24px);
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(350px, 100%), 1fr));
gap: clamp(16px, 4vw, 24px);
align-items: start;
}
.card {
background: rgba(255, 255, 255, 0.9);
border-radius: clamp(12px, 3vw, 16px);
padding: clamp(16px, 4vw, 24px);
border: 1px solid rgba(255, 255, 255, 0.3);
backdrop-filter: blur(20px);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
margin-bottom: clamp(16px, 4vw, 24px);
}
.form-group {
margin-bottom: clamp(12px, 3vw, 16px);
}
.form-group label {
display: block;
font-weight: 600;
color: #1f2937;
margin-bottom: clamp(6px, 2vw, 8px);
font-size: clamp(14px, 3.5vw, 16px);
}
.form-control {
width: 100%;
padding: clamp(12px, 3vw, 16px) clamp(16px, 4vw, 20px);
border: 2px solid #e5e7eb;
border-radius: clamp(8px, 2vw, 12px);
font-size: clamp(14px, 3.5vw, 16px);
transition: border-color 0.2s ease;
min-height: 48px;
}
.form-control:focus {
outline: none;
border-color: #10b981;
}
.btn {
padding: clamp(12px, 3vw, 16px) clamp(20px, 5vw, 32px);
border: none;
border-radius: clamp(8px, 2vw, 12px);
font-weight: 600;
cursor: pointer;
transition: all 0.2s ease;
font-size: clamp(14px, 3.5vw, 16px);
min-height: 48px;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
}
.btn-primary {
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
color: white;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(16, 185, 129, 0.4);
}
.btn-primary:disabled {
background: #9ca3af;
cursor: not-allowed;
transform: none;
}
.radio-group {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(180px, 100%), 1fr));
gap: clamp(8px, 2vw, 12px);
}
.radio-option {
display: flex;
align-items: center;
gap: clamp(8px, 2vw, 12px);
padding: clamp(12px, 3vw, 16px);
border: 2px solid #e5e7eb;
border-radius: clamp(8px, 2vw, 12px);
cursor: pointer;
background: white;
transition: all 0.2s ease;
min-height: 44px;
}
.radio-option:hover {
border-color: #10b981;
}
.radio-option.selected {
border-color: #10b981;
background: #f0fdf4;
}
.radio-option input[type="radio"] {
margin: 0;
}
.file-upload-zone {
border: 2px dashed #d1d5db;
border-radius: clamp(12px, 3vw, 16px);
padding: clamp(24px, 6vw, 40px);
text-align: center;
transition: all 0.3s ease;
background: #f9fafb;
cursor: pointer;
}
.file-upload-zone.dragover {
border-color: #10b981;
background: #f0fdf4;
}
.file-upload-zone:hover {
border-color: #10b981;
background: #f0fdf4;
}
.file-preview {
background: #f0fdf4;
border: 1px solid #86efac;
border-radius: 8px;
padding: 12px;
margin-top: 12px;
display: flex;
align-items: center;
gap: 12px;
}
.processing-status {
padding: clamp(16px, 4vw, 20px);
background: #f0fdf4;
border: 1px solid #10b981;
border-radius: clamp(8px, 2vw, 12px);
color: #047857;
font-weight: 600;
text-align: center;
margin-bottom: clamp(16px, 4vw, 24px);
}
.analysis-results {
background: rgba(255, 255, 255, 0.9);
border-radius: 16px;
padding: 24px;
border: 1px solid rgba(255, 255, 255, 0.3);
backdrop-filter: blur(20px);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
margin-top: 24px;
}
.help-text {
font-size: clamp(12px, 3vw, 14px);
color: #6b7280;
margin-top: 8px;
}
.section-title {
font-size: clamp(16px, 4vw, 18px);
font-weight: 600;
color: #1f2937;
margin-bottom: clamp(12px, 3vw, 16px);
}
.error-message {
background: #fef2f2;
border: 1px solid #fca5a5;
color: #dc2626;
padding: 12px;
border-radius: 8px;
margin-bottom: 16px;
}
.success-message {
background: #f0fdf4;
border: 1px solid #86efac;
color: #166534;
padding: 12px;
border-radius: 8px;
margin-bottom: 16px;
}
/* Mobile optimizations */
@media (max-width: 768px) {
.grid {
grid-template-columns: 1fr;
}
.radio-group {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div style="
min-height: 100vh;
background: linear-gradient(135deg, #f6f8ff 0%, #e8f0fe 50%, #f0f7ff 100%);
padding: clamp(20px, 5vw, 40px) 0;
">
<!-- Header -->
<nav style="
background: rgba(255, 255, 255, 0.9);
backdrop-filter: blur(20px);
padding: 16px 0;
margin-bottom: 24px;
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
">
<div class="container">
<div style="display: flex; align-items: center; justify-content: space-between;">
<div style="display: flex; align-items: center; gap: 16px;">
<a href="{% url 'core:homepage' %}" style="
font-size: 24px;
font-weight: 700;
color: #10b981;
text-decoration: none;
">
🚀 NetCop AI Hub
</a>
</div>
<div style="display: flex; align-items: center; gap: 16px;">
<a href="{% url 'core:marketplace' %}" style="color: #374151; text-decoration: none; font-weight: 500;">Marketplace</a>
{% if user.is_authenticated %}
<a href="{% url 'authentication:profile' %}" style="color: #374151; text-decoration: none; font-weight: 500;">Profile</a>
<span style="color: #6b7280;" data-wallet-balance>{{ user.wallet_balance|floatformat:2 }} AED</span>
{% else %}
<a href="{% url 'authentication:login' %}" style="color: #10b981; text-decoration: none; font-weight: 600;">Login</a>
{% endif %}
</div>
</div>
</div>
</nav>
<div class="container">
<!-- Page Title -->
<div style="text-align: center; margin-bottom: clamp(24px, 6vw, 40px);">
<h1 style="
font-size: clamp(28px, 7vw, 36px);
font-weight: 700;
color: #1f2937;
margin: 0 0 clamp(12px, 3vw, 16px) 0;
">
📊 Data Analyzer Agent
</h1>
<p style="
font-size: clamp(16px, 4vw, 18px);
color: #6b7280;
margin: 0;
max-width: 600px;
margin: 0 auto;
">
Analyze your PDF documents and generate comprehensive insights with statistical analysis and trend detection.
</p>
<div style="
background: rgba(16, 185, 129, 0.1);
color: #047857;
padding: 8px 16px;
border-radius: 20px;
display: inline-block;
margin-top: 12px;
font-size: clamp(14px, 3.5vw, 16px);
font-weight: 600;
">
💰 Cost: 5.00 AED
</div>
</div>
<!-- Messages -->
{% if messages %}
{% for message in messages %}
<div class="{% if message.tags == 'error' %}error-message{% else %}success-message{% endif %}">
{{ message }}
</div>
{% endfor %}
{% endif %}
<!-- Main Content -->
<div class="grid">
<!-- Data Analysis Form -->
<div>
<form method="POST" enctype="multipart/form-data" id="dataForm" action="{% url 'data_analyzer:process' %}">
{% csrf_token %}
<!-- File Upload -->
<div class="card">
<h3 class="section-title">📁 Upload Your Data File</h3>
<div class="form-group">
<div class="file-upload-zone" id="fileUploadZone" onclick="document.getElementById('fileInput').click()">
<div style="font-size: clamp(32px, 8vw, 48px); margin-bottom: 12px;">📊</div>
<div style="font-size: clamp(16px, 4vw, 18px); font-weight: 600; color: #374151; margin-bottom: 8px;">
Choose or drag your data file here
</div>
<div style="font-size: clamp(14px, 3.5vw, 16px); color: #6b7280;">
Supports PDF files only (up to 10MB)
</div>
</div>
<input
type="file"
name="file"
id="fileInput"
accept=".pdf"
style="display: none;"
required
/>
<div id="filePreview" class="file-preview" style="display: none;">
<div style="font-size: 24px;">📄</div>
<div style="flex: 1;">
<div style="font-weight: 600; color: #1f2937;" id="fileName"></div>
<div style="font-size: 14px; color: #6b7280;" id="fileSize"></div>
</div>
<button type="button" onclick="removeFile()" style="
background: #ef4444;
color: white;
border: none;
border-radius: 4px;
padding: 4px 8px;
cursor: pointer;
font-size: 12px;
">
Remove
</button>
</div>
</div>
</div>
<!-- Analysis Type Selection -->
<div class="card">
<h3 class="section-title">🔍 Analysis Type</h3>
<div class="radio-group">
<label class="radio-option {% if form.analysis_type.value == 'summary' or not form.analysis_type.value %}selected{% endif %}" onclick="selectAnalysisType('summary')">
<input type="radio" name="analysis_type" value="summary" {% if form.analysis_type.value == 'summary' or not form.analysis_type.value %}checked{% endif %} />
<div style="font-size: clamp(16px, 4vw, 20px);">📋</div>
<div>
<div style="font-weight: 600; margin-bottom: clamp(2px, 1vw, 4px); font-size: clamp(14px, 3.5vw, 16px);">
Summary Analysis
</div>
<div style="font-size: clamp(12px, 3vw, 14px); color: #6b7280;">
Quick overview and key insights
</div>
</div>
</label>
<label class="radio-option {% if form.analysis_type.value == 'detailed' %}selected{% endif %}" onclick="selectAnalysisType('detailed')">
<input type="radio" name="analysis_type" value="detailed" {% if form.analysis_type.value == 'detailed' %}checked{% endif %} />
<div style="font-size: clamp(16px, 4vw, 20px);">📈</div>
<div>
<div style="font-weight: 600; margin-bottom: clamp(2px, 1vw, 4px); font-size: clamp(14px, 3.5vw, 16px);">
Detailed Analysis
</div>
<div style="font-size: clamp(12px, 3vw, 14px); color: #6b7280;">
Comprehensive statistical analysis
</div>
</div>
</label>
<label class="radio-option {% if form.analysis_type.value == 'statistical' %}selected{% endif %}" onclick="selectAnalysisType('statistical')">
<input type="radio" name="analysis_type" value="statistical" {% if form.analysis_type.value == 'statistical' %}checked{% endif %} />
<div style="font-size: clamp(16px, 4vw, 20px);">📊</div>
<div>
<div style="font-weight: 600; margin-bottom: clamp(2px, 1vw, 4px); font-size: clamp(14px, 3.5vw, 16px);">
Statistical Analysis
</div>
<div style="font-size: clamp(12px, 3vw, 14px); color: #6b7280;">
Advanced statistics and trends
</div>
</div>
</label>
</div>
</div>
<!-- Processing Status -->
{% if processing %}
<div class="processing-status">
<div style="font-size: clamp(16px, 4vw, 18px); margin-bottom: 8px;">
⏳ Processing...
</div>
<div style="font-size: clamp(14px, 3.5vw, 16px);">
{{ processing_status|default:"Analyzing your data file..." }}
</div>
</div>
{% endif %}
</form>
</div>
<!-- Sidebar -->
<div>
<!-- Wallet Balance Card -->
<div class="card">
<h3 class="section-title">💳 Your Wallet</h3>
<div style="margin-bottom: clamp(16px, 4vw, 20px);">
<div style="font-size: clamp(24px, 6vw, 28px); font-weight: 700; color: #1f2937;" data-wallet-balance>
{% if user.is_authenticated %}
{{ user.wallet_balance|floatformat:2 }} AED
{% else %}
0.00 AED
{% endif %}
</div>
<div style="font-size: clamp(14px, 3.5vw, 16px); color: #6b7280;">
Available Balance
</div>
</div>
{% if user.is_authenticated %}
{% if user.wallet_balance >= 5.00 %}
<button
type="submit"
form="dataForm"
class="btn btn-primary"
style="width: 100%; margin-bottom: 12px;"
{% if processing %}disabled{% endif %}
>
{% if processing %}
⏳ Processing...
{% else %}
📊 Analyze Data (5.00 AED)
{% endif %}
</button>
{% else %}
<div style="
background: #fef2f2;
border: 1px solid #fca5a5;
color: #dc2626;
padding: 12px;
border-radius: 8px;
text-align: center;
font-size: clamp(14px, 3.5vw, 16px);
margin-bottom: 12px;
">
Insufficient balance! You need 5.00 AED.
</div>
<a href="{% url 'core:wallet_topup' %}" class="btn btn-primary" style="width: 100%; text-decoration: none;">
💰 Top Up Wallet
</a>
{% endif %}
{% else %}
<a href="{% url 'authentication:login' %}" class="btn btn-primary" style="width: 100%; text-decoration: none;">
🔑 Login to Continue
</a>
{% endif %}
</div>
<!-- Usage Info -->
<div style="
padding: 16px;
background: rgba(16, 185, 129, 0.1);
border-radius: 12px;
border: 1px solid rgba(16, 185, 129, 0.2);
">
<h4 style="margin: 0 0 8px 0; font-size: 14px; font-weight: 600; color: #047857;">
💡 How it works
</h4>
<ul style="margin: 0; font-size: 12px; color: #374151; line-height: 1.4; list-style: none; padding-left: 0;">
<li style="margin: 4px 0; padding-left: 16px; position: relative;">
<span style="position: absolute; left: 0; color: #10b981;"></span>
Upload PDF files for analysis
</li>
<li style="margin: 4px 0; padding-left: 16px; position: relative;">
<span style="position: absolute; left: 0; color: #10b981;"></span>
Choose your analysis depth
</li>
<li style="margin: 4px 0; padding-left: 16px; position: relative;">
<span style="position: absolute; left: 0; color: #10b981;"></span>
Get AI-powered insights
</li>
<li style="margin: 4px 0; padding-left: 16px; position: relative;">
<span style="position: absolute; left: 0; color: #10b981;"></span>
Download comprehensive reports
</li>
</ul>
</div>
</div>
</div>
<!-- Analysis Results -->
{% if analysis_results %}
<div class="analysis-results">
<!-- Status Header -->
<div style="display: flex; align-items: center; gap: 12px; margin-bottom: 20px;">
<div style="font-size: 24px;"></div>
<h3 style="font-size: 20px; font-weight: 600; color: #1f2937; margin: 0;">
Data Analysis Results
</h3>
<div style="
background: #10b981;
color: white;
padding: 6px 12px;
border-radius: 6px;
font-size: 14px;
font-weight: 600;
margin-left: auto;
">
✅ Complete
</div>
</div>
<!-- Analysis Content -->
<div style="
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 24px;
margin-bottom: 20px;
">
<h4 style="
font-size: 18px;
font-weight: 600;
color: #1f2937;
margin-bottom: 16px;
">
📊 Analysis Report
</h4>
<div style="
white-space: pre-line;
line-height: 1.7;
color: #374151;
font-size: 15px;
">
{{ analysis_results.analysis|default:""|cut:"(Demo mode - N8N integration will be activated in production)" }}
</div>
{% if analysis_results.file_info %}
<div style="margin-top: 20px; padding-top: 20px; border-top: 1px solid #e5e7eb;">
<h5 style="font-size: 16px; font-weight: 600; color: #1f2937; margin-bottom: 12px;">
📄 File Information
</h5>
<div style="font-size: 14px; color: #6b7280;">
<strong>File:</strong> {{ analysis_results.file_info.name }}<br>
<strong>Size:</strong> {{ analysis_results.file_info.size }}<br>
<strong>Type:</strong> {{ analysis_results.file_info.type }}<br>
<strong>Processed:</strong> {{ analysis_results.processed_at|date:"F j, Y g:i A" }}
</div>
</div>
{% endif %}
</div>
<!-- Download/Copy Actions -->
<div style="
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid #e5e7eb;
display: flex;
gap: 12px;
flex-wrap: wrap;
">
<button
onclick="copyAnalysisReport()"
class="btn btn-primary"
style="flex: 1; min-width: 120px;"
>
📋 Copy Report
</button>
<button
onclick="downloadAnalysisReport()"
class="btn"
style="
flex: 1;
min-width: 120px;
background: white;
color: #374151;
border: 2px solid #e5e7eb;
"
>
💾 Download Report
</button>
</div>
</div>
{% endif %}
</div>
<!-- Footer -->
<footer style="
background: rgba(255, 255, 255, 0.9);
backdrop-filter: blur(20px);
padding: 24px 0;
margin-top: 48px;
border-top: 1px solid rgba(255, 255, 255, 0.2);
text-align: center;
color: #6b7280;
">
<div class="container">
<p style="margin: 0; font-size: 14px;">
© 2024 NetCop AI Hub. Powered by AI agents.
</p>
</div>
</footer>
</div>
<script>
// File upload handling
const fileInput = document.getElementById('fileInput');
const fileUploadZone = document.getElementById('fileUploadZone');
const filePreview = document.getElementById('filePreview');
// Handle file input change
fileInput.addEventListener('change', function(e) {
if (e.target.files.length > 0) {
showFilePreview(e.target.files[0]);
}
});
// Drag and drop functionality
fileUploadZone.addEventListener('dragover', function(e) {
e.preventDefault();
fileUploadZone.classList.add('dragover');
});
fileUploadZone.addEventListener('dragleave', function(e) {
e.preventDefault();
fileUploadZone.classList.remove('dragover');
});
fileUploadZone.addEventListener('drop', function(e) {
e.preventDefault();
fileUploadZone.classList.remove('dragover');
const files = e.dataTransfer.files;
if (files.length > 0) {
const file = files[0];
if (isValidFile(file)) {
fileInput.files = files;
showFilePreview(file);
} else {
showToast('Please select a valid PDF file', 'error');
}
}
});
// Show file preview
function showFilePreview(file) {
document.getElementById('fileName').textContent = file.name;
document.getElementById('fileSize').textContent = formatFileSize(file.size);
filePreview.style.display = 'flex';
fileUploadZone.style.display = 'none';
}
// Remove file
function removeFile() {
fileInput.value = '';
filePreview.style.display = 'none';
fileUploadZone.style.display = 'block';
}
// Validate file type
function isValidFile(file) {
const validTypes = [
'application/pdf'
];
return validTypes.includes(file.type) || file.name.match(/\.pdf$/i);
}
// Format file size
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
// Analysis type selection
function selectAnalysisType(type) {
document.querySelectorAll('.radio-option').forEach(option => {
option.classList.remove('selected');
});
event.currentTarget.classList.add('selected');
document.querySelector(`input[value="${type}"]`).checked = true;
}
// Copy analysis report to clipboard
function copyAnalysisReport() {
const reportText = generateReportText();
navigator.clipboard.writeText(reportText).then(() => {
showToast('📋 Report copied to clipboard!', 'success');
}).catch(() => {
showToast('Failed to copy report', 'error');
});
}
// Download analysis report as text file
function downloadAnalysisReport() {
const reportText = generateReportText();
const blob = new Blob([reportText], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `data-analysis-report-${Date.now()}.txt`;
a.click();
URL.revokeObjectURL(url);
showToast('💾 Report downloaded!', 'success');
}
// Generate report text for copy/download
function generateReportText() {
const analysisContent = document.getElementById('analysisContent');
if (analysisContent) {
let report = `Data Analysis Report\n`;
report += `Generated: ${new Date().toLocaleString()}\n\n`;
report += `Analysis Results:\n`;
report += `${analysisContent.textContent}\n\n`;
report += `Generated by NetCop AI Data Analyzer Agent`;
return report;
} else {
// Fallback to template data if available
{% if analysis_results %}
let report = `Data Analysis Report\n`;
report += `Generated: {{ analysis_results.processed_at|date:'F j, Y g:i A' }}\n\n`;
{% if analysis_results.file_info %}
report += `File Information:\n`;
report += `Name: {{ analysis_results.file_info.name }}\n`;
report += `Size: {{ analysis_results.file_info.size }}\n`;
report += `Type: {{ analysis_results.file_info.type }}\n\n`;
{% endif %}
report += `Analysis Results:\n`;
report += `{{ analysis_results.analysis|cut:"(Demo mode - N8N integration will be activated in production)" }}\n\n`;
report += `Generated by NetCop AI Data Analyzer Agent`;
return report;
{% else %}
return 'No analysis data available';
{% endif %}
}
}
// Simple toast notification
function showToast(message, type = 'info') {
const toast = document.createElement('div');
toast.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
padding: 12px 20px;
border-radius: 8px;
color: white;
font-weight: 600;
z-index: 1000;
${type === 'success' ? 'background: #10b981;' : 'background: #ef4444;'}
`;
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 3000);
}
// Handle form submission
document.getElementById('dataForm').addEventListener('submit', function(e) {
e.preventDefault(); // Always prevent default to handle via JavaScript
if (!fileInput.files.length) {
showToast('Please select a data file', 'error');
return;
}
// Check user authentication
{% if not user.is_authenticated %}
window.location.href = "{% url 'authentication:login' %}";
return;
{% endif %}
// Check wallet balance (use current balance if updated, otherwise template value)
const balance = window.currentWalletBalance !== undefined ? window.currentWalletBalance : {{ user.wallet_balance|default:0 }};
if (balance < 5.00) {
showToast('Insufficient balance! You need 5.00 AED.', 'error');
setTimeout(() => {
window.location.href = "{% url 'core:wallet_topup' %}";
}, 2000);
return;
}
// Submit form via AJAX
submitForm();
});
// Submit form and handle response
async function submitForm() {
const form = document.getElementById('dataForm');
const formData = new FormData(form);
const submitButton = document.querySelector('button[type="submit"]');
try {
// Disable submit button and show processing
submitButton.disabled = true;
submitButton.innerHTML = '⏳ Processing...';
showProcessingStatus('Uploading file and starting analysis...');
// Submit form
const response = await fetch(form.action, {
method: 'POST',
body: formData,
headers: {
'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value
}
});
const result = await response.json();
if (result.success) {
showToast('✅ Analysis started successfully!', 'success');
showProcessingStatus('Analysis in progress. Please wait...');
// Update wallet balance if provided
if (result.wallet_balance !== undefined) {
updateWalletBalance(result.wallet_balance);
}
// Poll for results
pollForResults(result.request_id);
} else {
showToast(`❌ Error: ${result.error}`, 'error');
resetForm();
}
} catch (error) {
showToast(`❌ Network error: ${error.message}`, 'error');
resetForm();
}
}
// Poll for analysis results
async function pollForResults(requestId, maxAttempts = 30) {
let attempts = 0;
const pollInterval = setInterval(async () => {
attempts++;
try {
const response = await fetch(`{% url 'data_analyzer:result' '00000000-0000-0000-0000-000000000000' %}`.replace('00000000-0000-0000-0000-000000000000', requestId));
const result = await response.json();
if (result.success && result.status === 'completed') {
clearInterval(pollInterval);
// Update wallet balance after successful completion
if (result.wallet_balance !== undefined) {
updateWalletBalance(result.wallet_balance);
}
displayResults(result);
resetForm(); // Form stays ready with current file
showToast('✅ Analysis completed and payment processed!', 'success');
} else if (result.status === 'failed') {
clearInterval(pollInterval);
showToast('❌ Analysis failed - no charge applied', 'error');
resetForm();
} else if (attempts >= maxAttempts) {
clearInterval(pollInterval);
showToast('⚠️ Analysis is taking longer than expected', 'error');
resetForm();
}
// Continue polling if still processing
} catch (error) {
clearInterval(pollInterval);
showToast(`❌ Error checking results: ${error.message}`, 'error');
resetForm();
}
}, 2000); // Poll every 2 seconds
}
// Display analysis results
function displayResults(result) {
hideProcessingStatus();
// Create results HTML
const resultsHtml = `
<div class="analysis-results">
<!-- Status Header -->
<div style="display: flex; align-items: center; gap: 12px; margin-bottom: 20px;">
<div style="font-size: 24px;">✅</div>
<h3 style="font-size: 20px; font-weight: 600; color: #1f2937; margin: 0;">
Data Analysis Results
</h3>
<div style="
background: #10b981;
color: white;
padding: 6px 12px;
border-radius: 6px;
font-size: 14px;
font-weight: 600;
margin-left: auto;
">
✅ Complete
</div>
</div>
<!-- Analysis Content -->
<div style="
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 24px;
margin-bottom: 20px;
">
<h4 style="
font-size: 18px;
font-weight: 600;
color: #1f2937;
margin-bottom: 16px;
">
📊 Analysis Report
</h4>
<div style="
white-space: pre-line;
line-height: 1.7;
color: #374151;
font-size: 15px;
" id="analysisContent">
${result.report_text || result.analysis_results || 'No analysis content available'}
</div>
<div style="margin-top: 20px; padding-top: 20px; border-top: 1px solid #e5e7eb;">
<h5 style="font-size: 16px; font-weight: 600; color: #1f2937; margin-bottom: 12px;">
📄 Processing Information
</h5>
<div style="font-size: 14px; color: #6b7280;">
<strong>Processing Time:</strong> ${result.processing_time ? result.processing_time.toFixed(2) + 's' : 'N/A'}<br>
<strong>Status:</strong> ${result.status}<br>
<strong>Completed:</strong> ${new Date().toLocaleString()}
</div>
</div>
</div>
<!-- Download/Copy Actions -->
<div style="
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid #e5e7eb;
display: flex;
gap: 12px;
flex-wrap: wrap;
">
<button
onclick="copyAnalysisReport()"
class="btn btn-primary"
style="flex: 1; min-width: 120px;"
>
📋 Copy Report
</button>
<button
onclick="downloadAnalysisReport()"
class="btn"
style="
flex: 1;
min-width: 120px;
background: white;
color: #374151;
border: 2px solid #e5e7eb;
"
>
💾 Download Report
</button>
<button
onclick="analyzeAnotherFile()"
class="btn"
style="
flex: 1;
min-width: 120px;
background: #10b981;
color: white;
border: 2px solid #10b981;
"
>
📄 Analyze Another File
</button>
</div>
</div>
`;
// Find the main grid container and insert results after it
const gridContainer = document.querySelector('.grid');
const existingResults = document.querySelector('.analysis-results');
if (existingResults) {
existingResults.remove();
}
// Insert results right after the main grid (below the form)
gridContainer.insertAdjacentHTML('afterend', resultsHtml);
// Scroll to results smoothly
setTimeout(() => {
document.querySelector('.analysis-results').scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}, 100);
}
// Show processing status
function showProcessingStatus(message) {
let statusDiv = document.querySelector('.processing-status');
if (!statusDiv) {
const form = document.getElementById('dataForm');
statusDiv = document.createElement('div');
statusDiv.className = 'processing-status';
form.appendChild(statusDiv);
}
statusDiv.innerHTML = `
<div style="font-size: clamp(16px, 4vw, 18px); margin-bottom: 8px;">
⏳ Processing...
</div>
<div style="font-size: clamp(14px, 3.5vw, 16px);">
${message}
</div>
`;
statusDiv.style.display = 'block';
}
// Hide processing status
function hideProcessingStatus() {
const statusDiv = document.querySelector('.processing-status');
if (statusDiv) {
statusDiv.style.display = 'none';
}
}
// Reset form to initial state (but keep it ready for next file)
function resetForm() {
const submitButton = document.querySelector('button[type="submit"]');
submitButton.disabled = false;
submitButton.innerHTML = '📊 Analyze Data (5.00 AED)';
hideProcessingStatus();
// Don't clear the file - keep form ready for another analysis
// User can manually clear or choose new file if needed
}
// Function to prepare form for another file analysis
function analyzeAnotherFile() {
// Clear the current file
removeFile();
// Remove existing results
const existingResults = document.querySelector('.analysis-results');
if (existingResults) {
existingResults.remove();
}
// Scroll back to form
document.querySelector('.grid').scrollIntoView({
behavior: 'smooth',
block: 'start'
});
// Show success message
showToast('📄 Ready for another file!', 'success');
}
// Update wallet balance display in real-time
function updateWalletBalance(newBalance) {
try {
// Update the balance check in JavaScript first
window.currentWalletBalance = newBalance;
// Update all elements with data-wallet-balance attribute (most reliable method)
const balanceElements = document.querySelectorAll('[data-wallet-balance]');
console.log(`Found ${balanceElements.length} wallet balance elements to update`);
balanceElements.forEach((element, index) => {
element.textContent = `${newBalance.toFixed(2)} AED`;
console.log(`Updated element ${index + 1}: ${element.textContent}`);
});
// Fallback: Update any element containing AED with a number pattern
if (balanceElements.length === 0) {
console.log('No data-wallet-balance elements found, using fallback method');
const allElements = document.querySelectorAll('span, div');
let updated = 0;
allElements.forEach(element => {
const text = element.textContent;
if (text && text.includes('AED') && text.match(/\d+\.\d+/)) {
element.textContent = `${newBalance.toFixed(2)} AED`;
updated++;
}
});
console.log(`Fallback method updated ${updated} elements`);
}
console.log(`✅ Wallet balance successfully updated to: ${newBalance.toFixed(2)} AED`);
} catch (error) {
console.error('❌ Error updating wallet balance:', error);
}
}
</script>
</body>
</html>