195 lines
10 KiB
PHP
195 lines
10 KiB
PHP
<?php
|
|
require_once 'partials/header.php';
|
|
require_once 'db/config.php';
|
|
|
|
// If the user is not logged in, redirect to the login page.
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT credits FROM users WHERE id = ?");
|
|
$stmt->execute([$_SESSION['user_id']]);
|
|
$user = $stmt->fetch();
|
|
$user_credits = $user ? $user['credits'] : 0;
|
|
|
|
$upload_dir = 'uploads/';
|
|
$uploaded_file_path = null;
|
|
$error_message = null;
|
|
|
|
if ($user_credits > 0 && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['vehicleImage'])) {
|
|
if ($_FILES['vehicleImage']['error'] === UPLOAD_ERR_OK) {
|
|
$tmp_name = $_FILES['vehicleImage']['tmp_name'];
|
|
$name = basename($_FILES['vehicleImage']['name']);
|
|
$file_ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
|
$allowed_ext = ['jpg', 'jpeg', 'png', 'gif'];
|
|
|
|
if (in_array($file_ext, $allowed_ext)) {
|
|
// Create a unique filename to avoid conflicts
|
|
$new_filename = uniqid('', true) . '.' . $file_ext;
|
|
$destination = $upload_dir . $new_filename;
|
|
|
|
if (move_uploaded_file($tmp_name, $destination)) {
|
|
$uploaded_file_path = $destination;
|
|
try {
|
|
$stmt = db()->prepare("INSERT INTO uploads (user_id, file_path) VALUES (?, ?)");
|
|
$stmt->execute([$_SESSION['user_id'], $uploaded_file_path]);
|
|
} catch (PDOException $e) {
|
|
$error_message = "Database error: " . $e->getMessage();
|
|
// Optionally, delete the uploaded file if DB insertion fails
|
|
unlink($destination);
|
|
}
|
|
} else {
|
|
$error_message = 'Failed to move uploaded file.';
|
|
}
|
|
} else {
|
|
$error_message = 'Invalid file type. Please upload a JPG, PNG, or GIF image.';
|
|
}
|
|
} else {
|
|
$error_message = 'File upload failed with error code: ' . $_FILES['vehicleImage']['error'];
|
|
}
|
|
}
|
|
?>
|
|
|
|
|
|
<main class="container">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1 class="h2">My Dashboard</h1>
|
|
<div class="text-end">
|
|
<span class="badge bg-primary fs-6">Credits: <?= $user_credits ?></span>
|
|
<a href="pricing.php" class="btn btn-sm btn-outline-primary ms-2">Buy More</a>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($user_credits > 0): ?>
|
|
<div class="card p-4 p-md-5 border-0 shadow-sm mb-5">
|
|
<div class="row align-items-center">
|
|
<div class="col-lg-6 mb-4 mb-lg-0">
|
|
<h2 class="h1 mb-3">Upload Vehicle Photo</h2>
|
|
<p class="lead mb-4">Get an instant AI-powered damage analysis. Drag and drop an image or click to select a file.</p>
|
|
<form id="uploadForm" action="index.php" method="post" enctype="multipart/form-data">
|
|
<div id="uploadZone" class="upload-zone">
|
|
<i data-feather="upload-cloud" class="icon text-primary"></i>
|
|
<p class="m-0"><strong>Drag & drop</strong> or <strong>click to browse</strong></p>
|
|
<p class="text-muted small">Supports: JPG, PNG, GIF</p>
|
|
</div>
|
|
<input type="file" name="vehicleImage" id="vehicleImage" class="d-none">
|
|
<button type="submit" class="btn btn-primary btn-lg mt-3">Upload Image</button>
|
|
</form>
|
|
</div>
|
|
<div class="col-lg-6">
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger">
|
|
<?php echo htmlspecialchars($error_message); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($uploaded_file_path): ?>
|
|
<div class="text-center">
|
|
<h3 class="h5 mb-3">Analysis Result</h3>
|
|
<div class="card result-card">
|
|
<img src="<?php echo htmlspecialchars($uploaded_file_path); ?>" class="card-img-top" alt="Uploaded Vehicle Image">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Image Received</h5>
|
|
<p class="card-text d-flex align-items-center justify-content-center">
|
|
Status: <span class="badge bg-info text-dark ms-2">Pending Analysis</span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="card p-4 p-md-5 border-0 shadow-sm mb-5 text-center">
|
|
<h2 class="h1 mb-3">You're Out of Credits!</h2>
|
|
<p class="lead mb-4">You need to buy more credits to upload and analyze images.</p>
|
|
<a href="pricing.php" class="btn btn-primary btn-lg">Buy Credits</a>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="dashboard">
|
|
<h2 class="h3 mb-4">My Uploads</h2>
|
|
<div class="row">
|
|
<?php
|
|
$stmt = db()->prepare("SELECT * FROM uploads WHERE user_id = ? ORDER BY created_at DESC");
|
|
$stmt->execute([$_SESSION['user_id']]);
|
|
$uploads = $stmt->fetchAll();
|
|
|
|
if (count($uploads) > 0):
|
|
foreach ($uploads as $upload):
|
|
$status = htmlspecialchars($upload['status']);
|
|
$status_badge_class = 'bg-secondary';
|
|
if ($status === 'pending') {
|
|
$status_badge_class = 'bg-warning text-dark';
|
|
} elseif ($status === 'analyzing') {
|
|
$status_badge_class = 'bg-info text-dark';
|
|
} elseif ($status === 'completed') {
|
|
$status_badge_class = 'bg-success';
|
|
} elseif ($status === 'failed') {
|
|
$status_badge_class = 'bg-danger';
|
|
}
|
|
?>
|
|
<div class="col-md-4 mb-4">
|
|
<div class="card upload-card h-100">
|
|
<img src="<?php echo htmlspecialchars($upload['file_path']); ?>" class="card-img-top" alt="Uploaded Image">
|
|
<div class="card-body d-flex flex-column">
|
|
<p class="card-text small text-muted mb-2">
|
|
Uploaded: <?php echo date("M d, Y", strtotime($upload['created_at'])); ?>
|
|
</p>
|
|
<p class="card-text mb-3">
|
|
Status: <span class="badge <?php echo $status_badge_class; ?>"><?php echo ucfirst($status); ?></span>
|
|
</p>
|
|
|
|
<?php
|
|
$result = $upload['analysis_result'] ? json_decode($upload['analysis_result'], true) : null;
|
|
|
|
if ($status === 'completed' && $result && !isset($result['error'])):
|
|
?>
|
|
<div class="analysis-result small mb-3">
|
|
<strong>Analysis Result:</strong><br>
|
|
Damage Detected: <?php echo isset($result['damage_detected']) && $result['damage_detected'] ? 'Yes' : 'No'; ?><br>
|
|
Confidence: <?php echo $result['confidence'] ?? 'N/A'; ?>%<br>
|
|
</div>
|
|
<?php elseif ($status === 'failed' && $result && isset($result['error'])):
|
|
?>
|
|
<div class="analysis-result small text-danger mb-3">
|
|
<strong>Analysis Failed:</strong><br>
|
|
<?php echo htmlspecialchars($result['error']); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="mt-auto">
|
|
<?php if ($user_credits > 0): ?>
|
|
<?php if ($status === 'pending' || $status === 'failed'): ?>
|
|
<form action="analyze.php" method="post" class="d-grid">
|
|
<input type="hidden" name="upload_id" value="<?php echo $upload['id']; ?>">
|
|
<button type="submit" class="btn btn-primary btn-sm"><?php echo ($status === 'failed') ? 'Retry Analysis' : 'Analyze'; ?></button>
|
|
</form>
|
|
<?php elseif ($status === 'analyzing'): ?>
|
|
<button type="button" class="btn btn-secondary btn-sm w-100" disabled>Analyzing...</button>
|
|
<?php elseif ($status === 'completed'): ?>
|
|
<a href="report.php?id=<?php echo $upload['id']; ?>" class="btn btn-outline-secondary btn-sm">View Report</a>
|
|
<?php endif; ?>
|
|
<?php else: ?>
|
|
<a href="pricing.php" class="btn btn-primary btn-sm disabled" title="Buy more credits">Analyze</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
endforeach;
|
|
else:
|
|
?>
|
|
<div class="col">
|
|
<p>You haven't uploaded any images yet.</p>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?php require_once 'partials/footer.php'; ?>
|