35249-vm/my_deals.php
Flatlogic Bot 3db54a0124 3.0
2025-10-26 12:42:58 +00:00

140 lines
5.7 KiB
PHP

<?php
// my_deals.php
session_start();
require_once 'db/config.php';
// Hardcoded user ID for demonstration.
// In a real app, you'd get this from $_SESSION['user_id']
$current_user_id = 2; // Let's assume this is the user who is browsing their deals
$orders = [];
$error = '';
try {
$pdo = db();
// Fetch orders where the current user is either the buyer or the seller
$stmt = $pdo->prepare(
"SELECT
o.id, o.ad_id, o.amount_crypto, o.amount_fiat, o.status, o.created_at,
b.nickname AS buyer_nickname,
s.nickname AS seller_nickname,
a.currency, a.payment_currency
FROM orders o
JOIN users b ON o.buyer_id = b.id
JOIN users s ON o.seller_id = s.id
JOIN ads a ON o.ad_id = a.id
WHERE o.buyer_id = :user_id OR o.seller_id = :user_id
ORDER BY o.created_at DESC"
);
$stmt->bindParam(':user_id', $current_user_id, PDO::PARAM_INT);
$stmt->execute();
$orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$error = "Database error: " . $e->getMessage();
}
// Helper to display status with a nice badge
function getStatusBadge($status) {
$map = [
'AWAITING_PAYMENT' => 'bg-warning text-dark',
'AWAITING_SELLER_CONFIRMATION' => 'bg-info text-dark',
'COMPLETED' => 'bg-success',
'CANCELED' => 'bg-secondary',
'DISPUTED' => 'bg-danger',
'PENDING_CONFIRMATION' => 'bg-light text-dark',
];
$class = $map[$status] ?? 'bg-light text-dark';
$status_text = str_replace('_', ' ', $status);
return "<span class=\"badge {$class}\">" . htmlspecialchars($status_text) . "</span>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Deals</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="index.php">P2P Platform</a>
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="index.php">Dashboard</a></li>
<li class="nav-item"><a class="nav-link" href="user_ads.php">Browse Ads</a></li>
</ul>
</div>
</nav>
<div class="container mt-5">
<h1 class="mb-4">My Deals</h1>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if (empty($orders)): ?>
<div class="alert alert-info">You have no deals yet. <a href="user_ads.php">Find an ad</a> to start one.</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead class="table-light">
<tr>
<th>Role</th>
<th>Deal With</th>
<th>Amount</th>
<th>Status</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $order):
// Determine user's role in the deal
$is_buyer = ($order['buyer_id'] == $current_user_id);
$partner_nickname = $is_buyer ? $order['seller_nickname'] : $order['buyer_nickname'];
?>
<tr>
<td>
<?php if ($is_buyer):
// User is buying
?>
<span class="badge bg-primary">BUYING</span>
<?php else:
// User is selling
?>
<span class="badge bg-warning text-dark">SELLING</span>
<?php endif; ?>
</td>
<td>
<?php echo htmlspecialchars($partner_nickname);
?>
</td>
<td>
<strong><?php echo rtrim(rtrim(number_format($order['amount_crypto'], 8), '0'), '.'); ?> <?php echo htmlspecialchars($order['currency']); ?></strong>
<br>
<small class="text-muted"><?php echo number_format($order['amount_fiat'], 2); ?> <?php echo htmlspecialchars($order['payment_currency']); ?></small>
</td>
<td><?php echo getStatusBadge($order['status']); ?></td>
<td><?php echo date("Y-m-d H:i", strtotime($order['created_at'])); ?></td>
<td>
<a href="#" class="btn btn-sm btn-outline-primary">View</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
<footer class="text-center mt-5 mb-3">
<p class="text-muted">&copy; <?php echo date("Y"); ?> P2P Platform</p>
</footer>
</body>
</html>