-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.php
More file actions
105 lines (94 loc) · 4.05 KB
/
Copy pathchat.php
File metadata and controls
105 lines (94 loc) · 4.05 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
<?php
session_start();
require 'db.php';
if (!isset($_SESSION['id'])) {
header("Location: Login/login.php");
exit();
}
$order_id = isset($_GET['order_id']) ? (int)$_GET['order_id'] : 0;
// Get order details
$sql = "SELECT o.*, f.fname as farmer_name, b.bname as buyer_name, p.product, p.price
FROM orders o
JOIN farmer f ON o.farmer_id = f.fid
JOIN buyer b ON o.buyer_id = b.bid
JOIN fproduct p ON o.product_id = p.pid
WHERE o.order_id = $order_id";
$result = mysqli_query($conn, $sql);
$order = mysqli_fetch_assoc($result);
// Check if user has access to this chat
$user_id = $_SESSION['id'];
$user_type = $_SESSION['Category'] == 1 ? 'farmer' : 'buyer';
$opposite_type = $user_type == 'farmer' ? 'buyer' : 'farmer';
if (($user_type == 'farmer' && $order['farmer_id'] != $user_id) ||
($user_type == 'buyer' && $order['buyer_id'] != $user_id)) {
header("Location: error.php");
exit();
}
// Mark all unread messages from the opposite user type as read
$sql = "UPDATE chat_messages
SET is_read = 1
WHERE order_id = $order_id
AND sender_type = '$opposite_type'
AND is_read = 0";
mysqli_query($conn, $sql);
// Handle order completion
if (isset($_POST['complete_order']) && $order['status'] == 'pending') {
$sql = "UPDATE orders SET status = 'completed' WHERE order_id = $order_id";
if (mysqli_query($conn, $sql)) {
$order['status'] = 'completed';
// Add a system message about order completion
$system_message = "Order has been marked as completed.";
$sql = "INSERT INTO chat_messages (order_id, sender_id, sender_type, message)
VALUES ($order_id, $user_id, '$user_type', '$system_message')";
mysqli_query($conn, $sql);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chat - Order #<?php echo $order_id; ?></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="assets/css/chat.css">
</head>
<body></body>
<div class="container">
<div class="chat-container">
<div class="chat-header">
<div class="header-content">
<h3>Order #<?php echo $order_id; ?></h3>
<p>Product: <?php echo $order['product']; ?></p>
<p>Status: <span class="status-<?php echo $order['status']; ?>"><?php echo ucfirst($order['status']); ?></span></p>
<?php if ($order['status'] == 'pending'): ?>
<form method="post" class="complete-order-form">
<button type="submit" name="complete_order" class="btn btn-success">Mark as Complete</button>
</form>
<?php endif; ?>
</div>
<div class="header-buttons">
<?php if ($user_type == 'buyer'): ?>
<a href="market.php" class="btn btn-primary">Continue Shopping</a>
<?php endif; ?>
<a href="index.php" class="btn btn-secondary">Back to Dashboard</a>
</div>
</div>
<div class="chat-messages" id="chat-messages">
<!-- Messages will be loaded here via AJAX -->
</div>
<div class="chat-input">
<form id="message-form">
<input type="hidden" name="order_id" value="<?php echo $order_id; ?>">
<input type="hidden" name="sender_id" value="<?php echo $user_id; ?>">
<input type="hidden" name="sender_type" value="<?php echo $user_type; ?>">
<textarea name="message" placeholder="Type your message..." required></textarea>
<button type="submit">Send</button>
</form>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="assets/js/chat.js"></script>
</body>
</html>