-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlelang_saya.php
More file actions
353 lines (308 loc) · 16.1 KB
/
lelang_saya.php
File metadata and controls
353 lines (308 loc) · 16.1 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
session_start();
include 'connect.php';
include 'buat_notifikasi.php';
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
// Process bid submission via AJAX
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit_bid'])) {
$response = array();
$auction_id = mysqli_real_escape_string($conn, $_POST['auction_id']);
$bid_amount = mysqli_real_escape_string($conn, $_POST['bid_amount']);
$bidder_id = $_SESSION['user_id'];
// Get current highest bid
$check_bid = "SELECT current_highest_bid FROM auctions WHERE auction_id = ?";
$stmt = mysqli_prepare($conn, $check_bid);
mysqli_stmt_bind_param($stmt, "i", $auction_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$current_bid = mysqli_fetch_assoc($result)['current_highest_bid'];
if ($bid_amount <= $current_bid) {
$response['status'] = 'error';
$response['message'] = "Penawaran harus lebih tinggi dari penawaran tertinggi saat ini";
} else {
mysqli_begin_transaction($conn);
try {
$insert_bid = "INSERT INTO bids (auction_id, bidder_id, bid_amount) VALUES (?, ?, ?)";
$stmt = mysqli_prepare($conn, $insert_bid);
mysqli_stmt_bind_param($stmt, "iid", $auction_id, $bidder_id, $bid_amount);
mysqli_stmt_execute($stmt);
$update_auction = "UPDATE auctions SET current_highest_bid = ?, winner_id = ? WHERE auction_id = ?";
$stmt = mysqli_prepare($conn, $update_auction);
mysqli_stmt_bind_param($stmt, "dii", $bid_amount, $bidder_id, $auction_id);
mysqli_stmt_execute($stmt);
createBidNotifications($conn, $auction_id, $bidder_id, $bid_amount);
mysqli_commit($conn);
$response['status'] = 'success';
$response['message'] = "Penawaran berhasil diajukan!";
$response['new_bid'] = number_format($bid_amount, 0, ',', '.');
$response['is_winner'] = true;
} catch (Exception $e) {
mysqli_rollback($conn);
$response['status'] = 'error';
$response['message'] = "Terjadi kesalahan saat mengajukan penawaran";
}
}
header('Content-Type: application/json');
echo json_encode($response);
exit;
}
$sql = "SELECT
ROW_NUMBER() OVER (ORDER BY a.end_time DESC) as row_num,
c.model,
c.foto1,
c.year,
a.end_time,
a.auction_id,
a.status,
a.current_highest_bid,
a.winner_id,
MAX(b.bid_amount) as user_highest_bid,
u.name as winner_name
FROM bids b
JOIN auctions a ON b.auction_id = a.auction_id
JOIN cars c ON a.car_id = c.car_id
LEFT JOIN users u ON a.winner_id = u.user_id
WHERE b.bidder_id = ?
GROUP BY a.auction_id
ORDER BY a.end_time DESC";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "i", $_SESSION['user_id']);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lelang Saya - Bidly</title>
<link rel="icon" type="image/png" sizes="32x32" href="assets/logo2.png">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@200;300;400;500;600;700;800&display=swap" rel="stylesheet">
<script>
tailwind.config = {
theme: {
extend: {
fontFamily: {
body: ['Plus Jakarta Sans']
},
colors: {
primary: '#1b9af5',
secondary: '#bcedff',
tertiary: '#1767b6',
accent: '#143557',
},
},
}
}
</script>
</head>
<body class="bg-gray-50 font-body">
<?php include 'navbar.php'; ?>
<!-- Bid Modal -->
<div id="bidModal" class="hidden fixed inset-0 bg-black bg-opacity-50 z-50">
<div class="fixed top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 bg-white rounded-lg p-6 max-w-md w-full">
<button onclick="closeBidModal()" class="absolute right-4 top-4 text-gray-500 hover:text-gray-700">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
<h2 class="text-2xl font-bold text-accent mb-4">Ajukan Penawaran</h2>
<div id="bid-message" class="hidden mb-4"></div>
<form id="bidForm" class="space-y-4">
<input type="hidden" id="auction_id" name="auction_id">
<div>
<label class="block text-gray-700 mb-2">Penawaran Saat Ini</label>
<p class="text-xl font-bold text-primary" id="modal-current-bid"></p>
</div>
<div>
<label for="bid_amount" class="block text-gray-700 mb-2">Jumlah Penawaran Anda</label>
<input type="number"
name="bid_amount"
id="bid_amount"
class="w-full border rounded-md px-4 py-2 focus:outline-none focus:ring-2 focus:ring-primary"
required>
<p class="text-sm text-gray-500 mt-1"></p>
</div>
<button type="submit"
name="submit_bid"
class="w-full bg-primary text-white px-6 py-3 rounded-md hover:bg-tertiary transition-colors">
Ajukan Penawaran
</button>
</form>
</div>
</div>
<main class="container mx-auto px-4 py-8">
<h1 class="text-2xl font-bold text-accent mb-6">Lelang Saya</h1>
<?php if (mysqli_num_rows($result) > 0): ?>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<?php while ($row = mysqli_fetch_assoc($result)): ?>
<div class="bg-white rounded-lg shadow-lg overflow-hidden">
<!-- Car Image -->
<div class="relative h-48">
<img src="<?php echo htmlspecialchars($row['foto1']); ?>"
alt="<?php echo htmlspecialchars($row['model']); ?>"
class="w-full h-full object-cover">
<div class="absolute top-4 right-4">
<span class="px-3 py-1 rounded-full text-sm font-medium
<?php echo $row['status'] === 'completed' ? 'bg-green-100 text-green-800' : 'bg-blue-100 text-blue-800'; ?>">
<?php echo ucfirst($row['status']); ?>
</span>
</div>
</div>
<!-- Car Details -->
<div class="p-6">
<div class="flex justify-between items-start mb-4">
<div>
<h2 class="text-xl font-semibold text-gray-900"><?php echo htmlspecialchars($row['model']); ?></h2>
<p class="text-sm text-gray-600">Tahun <?php echo $row['year']; ?></p>
</div>
<span class="text-sm text-gray-500">#<?php echo $row['row_num']; ?></span>
</div>
<!-- Bid Information -->
<div class="space-y-3">
<div class="flex justify-between items-center text-sm">
<span class="text-gray-600">Penawaran Anda:</span>
<span class="font-medium">Rp <?php echo number_format($row['user_highest_bid'], 0, ',', '.'); ?></span>
</div>
<div class="flex justify-between items-center text-sm">
<span class="text-gray-600">Harga Tertinggi:</span>
<span class="font-medium current-highest-bid" data-auction-id="<?php echo $row['auction_id']; ?>">
Rp <?php echo number_format($row['current_highest_bid'], 0, ',', '.'); ?>
</span>
</div>
<div class="flex justify-between items-center text-sm">
<span class="text-gray-600">Penutupan:</span>
<span class="font-medium"><?php echo date('d/m/Y H:i', strtotime($row['end_time'])); ?></span>
</div>
</div>
<!-- Status and Action -->
<!-- Status and Action -->
<div class="mt-6 space-y-4">
<div class="text-center">
<?php if ($row['user_highest_bid'] == $row['current_highest_bid']): ?>
<span class="text-green-600 font-medium">Anda merupakan Penawar tertinggi</span>
<?php if ($row['status'] !== 'completed'): ?>
<p class="text-gray-600 mt-2">Menunggu selesai</p>
<?php endif; ?>
<?php else: ?>
<span class="text-red-600 font-medium">Penawaran Anda Kalah</span>
<?php if ($row['status'] !== 'completed'): ?>
<button onclick="openBidModal(<?php echo $row['auction_id']; ?>, <?php echo $row['current_highest_bid']; ?>, '<?php echo htmlspecialchars($row['model']); ?>')"
class="mt-2 w-full bg-primary text-white px-4 py-2 rounded-lg hover:bg-tertiary transition-colors">
Ajukan Penawaran
</button>
<?php endif; ?>
<?php endif; ?>
</div>
<?php if ($row['status'] === 'completed'): ?>
<div class="text-center">
<?php if ($row['winner_id'] == $_SESSION['user_id']): ?>
<a href="payment.php?auction_id=<?php echo $row['auction_id']; ?>"
class="inline-block w-full bg-primary text-white px-4 py-2 rounded-lg hover:bg-tertiary transition-colors text-center">
Bayar Sekarang
</a>
<?php else: ?>
<p class="text-gray-600">Pemenang: <?php echo htmlspecialchars($row['winner_name']); ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="bg-white p-8 rounded-lg shadow-md text-center">
<p class="text-gray-600">Anda belum melakukan penawaran</p>
</div>
<?php endif; ?>
</main>
<?php include 'footer.php'; ?>
<script>
function openBidModal(auctionId, currentBid, model) {
document.getElementById('bidModal').classList.remove('hidden');
document.getElementById('auction_id').value = auctionId;
document.getElementById('modal-current-bid').textContent = 'Rp ' + currentBid.toLocaleString('id-ID');
// Set minimum bid amount
const minBid = currentBid + 1000;
document.getElementById('bid_amount').min = minBid;
document.getElementById('bid_amount').placeholder = 'Minimal Rp ' + minBid.toLocaleString('id-ID');
document.querySelector('.text-sm.text-gray-500').textContent =
'Minimal penawaran: Rp ' + minBid.toLocaleString('id-ID');
}
function closeBidModal() {
document.getElementById('bidModal').classList.add('hidden');
document.getElementById('bid-message').classList.add('hidden');
document.getElementById('bidForm').reset();
}
// Close modal when clicking outside
document.getElementById('bidModal').addEventListener('click', function(e) {
if (e.target === this) {
closeBidModal();
}
});
function showMessage(type, message) {
const messageDiv = document.getElementById('bid-message');
messageDiv.className = `p-4 rounded ${type === 'success' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'}`;
messageDiv.textContent = message;
messageDiv.classList.remove('hidden');
}
// Handle bid form submission
$(document).ready(function() {
$('#bidForm').on('submit', function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: window.location.href,
data: $(this).serialize() + '&submit_bid=1',
dataType: 'json',
success: function(response) {
if (response.status === 'success') {
showMessage('success', response.message);
// Update current highest bid display
const auctionId = $('#auction_id').val();
$(`.current-highest-bid[data-auction-id="${auctionId}"]`).text('Rp ' + response.new_bid);
// Update status display
const statusDiv = $(`.current-highest-bid[data-auction-id="${auctionId}"]`).closest('.bg-white')
.find('.text-red-600')
.closest('.text-center')
.html('<span class="text-green-600 font-medium">Anda merupakan Penawar tertinggi</span>');
// Clear form
$('#bidForm')[0].reset();
// Close modal after 2 seconds
setTimeout(closeBidModal, 2000);
// Reload page after 2.5 seconds to get updated data
setTimeout(function() {
location.reload();
}, 2500);
} else {
showMessage('error', response.message);
}
},
error: function() {
showMessage('error', 'Terjadi kesalahan. Silakan coba lagi.');
}
});
});
});
// Format bid amount input with thousand separator
const bidInput = document.getElementById('bid_amount');
bidInput.addEventListener('input', function(e) {
// Remove non-numeric characters
let value = this.value.replace(/\D/g, '');
// Format with thousand separator
if (value.length > 3) {
let formattedValue = new Intl.NumberFormat('id-ID').format(value);
document.querySelector('.text-sm.text-gray-500').textContent =
'Jumlah penawaran: Rp. ' + formattedValue;
}
});
</script>
</body>
</html>
<?php mysqli_close($conn); ?>