-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderChat.php
More file actions
216 lines (199 loc) · 7.44 KB
/
Copy pathorderChat.php
File metadata and controls
216 lines (199 loc) · 7.44 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
<?php
session_start();
require 'db.php';
if (!isset($_SESSION['logged_in']) || $_SESSION['Category'] != 0) {
$_SESSION['message'] = "Please login as a buyer to access the order chat!";
header("Location: Login/error.php");
exit();
}
// Get cart items and their farmers
$buyer_id = $_SESSION['id'];
$sql = "SELECT DISTINCT f.fid, f.fname, f.fmobile, f.femail
FROM mycart c
JOIN fproduct p ON c.pid = p.pid
JOIN farmer f ON p.fid = f.fid
WHERE c.bid = $buyer_id";
$result = mysqli_query($conn, $sql);
// Create order for each farmer
while ($farmer = mysqli_fetch_assoc($result)) {
$farmer_id = $farmer['fid'];
// Get products from this farmer in cart
$products_sql = "SELECT p.*, c.quantity
FROM mycart c
JOIN fproduct p ON c.pid = p.pid
WHERE c.bid = $buyer_id AND p.fid = $farmer_id";
$products_result = mysqli_query($conn, $products_sql);
// Create order
$order_sql = "INSERT INTO orders (buyer_id, farmer_id, status, created_at)
VALUES ($buyer_id, $farmer_id, 'pending', NOW())";
mysqli_query($conn, $order_sql);
$order_id = mysqli_insert_id($conn);
// Add products to order
while ($product = mysqli_fetch_assoc($products_result)) {
$order_item_sql = "INSERT INTO order_items (order_id, product_id, quantity, price)
VALUES ($order_id, {$product['pid']}, {$product['quantity']}, {$product['price']})";
mysqli_query($conn, $order_item_sql);
}
// Clear cart items for this farmer
$clear_cart_sql = "DELETE c FROM mycart c
JOIN fproduct p ON c.pid = p.pid
WHERE c.bid = $buyer_id AND p.fid = $farmer_id";
mysqli_query($conn, $clear_cart_sql);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Order Chat - Agrilink</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<script src="bootstrap\js\bootstrap.min.js"></script>
<!--[if lte IE 8]><script src="css/ie/html5shiv.js"></script><![endif]-->
<link rel="stylesheet" href="login.css"/>
<script src="js/jquery.min.js"></script>
<script src="js/skel.min.js"></script>
<script src="js/skel-layers.min.js"></script>
<script src="js/init.js"></script>
<noscript>
<link rel="stylesheet" href="css/skel.css" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/style-xlarge.css" />
</noscript>
<link rel="stylesheet" href="indexfooter.css" />
<style>
.chat-container {
max-width: 800px;
margin: 20px auto;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.chat-header {
background: #4CAF50;
color: white;
padding: 15px;
border-radius: 8px 8px 0 0;
}
.chat-messages {
height: 400px;
overflow-y: auto;
padding: 15px;
background: #f9f9f9;
}
.message {
margin: 10px 0;
padding: 10px;
border-radius: 10px;
max-width: 70%;
position: relative;
}
.buyer-message {
background-color: #e3f2fd;
margin-left: auto;
text-align: right;
}
.farmer-message {
background-color: #f5f5f5;
margin-right: auto;
}
.message-sender {
font-weight: bold;
margin-bottom: 5px;
font-size: 0.9em;
}
.message-content {
word-wrap: break-word;
}
.message-time {
font-size: 0.8em;
color: #666;
margin-top: 5px;
}
.chat-input {
padding: 15px;
background: white;
border-top: 1px solid #ddd;
}
.farmer-info {
background: #f8f9fa;
padding: 15px;
border-bottom: 1px solid #ddd;
}
</style>
</head>
<body>
<?php require 'menu.php'; ?>
<div class="container">
<div class="chat-container">
<div class="chat-header">
<h3>Order Chat</h3>
</div>
<?php
// Get the first farmer's details
mysqli_data_seek($result, 0);
$farmer = mysqli_fetch_assoc($result);
if ($farmer) {
?>
<div class="farmer-info">
<h4>Farmer: <?php echo htmlspecialchars($farmer['fname']); ?></h4>
<p>Contact: <?php echo htmlspecialchars($farmer['fmobile']); ?></p>
<p>Email: <?php echo htmlspecialchars($farmer['femail']); ?></p>
</div>
<div class="chat-messages" id="chat-messages">
<!-- Chat messages will be loaded here -->
</div>
<div class="chat-input">
<form id="message-form">
<input type="hidden" id="farmer_id" value="<?php echo $farmer['fid']; ?>">
<div class="input-group">
<input type="text" class="form-control" id="message-input" placeholder="Type your message...">
<div class="input-group-append">
<button type="submit" class="btn btn-primary">Send</button>
</div>
</div>
</form>
</div>
<?php } else { ?>
<div class="alert alert-info">
<h4>No Active Orders</h4>
<p>You don't have any active orders to chat about. Please add items to your cart and place an order first.</p>
<a href="myCart.php" class="btn btn-primary">View Cart</a>
</div>
<?php } ?>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
const farmerId = $('#farmer_id').val();
// Function to load messages
function loadMessages() {
$.get('get_messages.php?farmer_id=' + farmerId, function(data) {
$('#chat-messages').html(data);
$('#chat-messages').scrollTop($('#chat-messages')[0].scrollHeight);
});
}
// Load messages initially and every 3 seconds
loadMessages();
setInterval(loadMessages, 3000);
// Handle message submission
$('#message-form').submit(function(e) {
e.preventDefault();
const message = $('#message-input').val();
if (message.trim() !== '') {
$.post('send_message.php?farmer_id=' + farmerId, { message: message }, function(response) {
if (response.status === 'success') {
$('#message-input').val('');
loadMessages();
} else {
alert(response.message || 'Error sending message. Please try again.');
}
}, 'json');
}
});
});
</script>
</body>
</html>