/home/awneajlw/.trash/completed-orders.php
<?php
/**
* Completed Orders Page - Order Management
* This page displays all completed orders in a table format
* Features: Order listing, view/delete actions, responsive design
*/
// Start session if not already started
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Include required files
require_once 'config/database.php'; // Database connection configuration
require_once 'includes/auth.php'; // Authentication functions
/**
* Authentication Check
* Redirect to welcome page if user is not logged in
*/
if (!isLoggedIn()) {
header('Location: welcome.php');
exit();
}
// Initialize variables for order management
$orders = []; // Array to store completed orders
$error_message = ''; // Error message for failed operations
$success_message = ''; // Success message for completed operations
/**
* Delete Order Handler
* Process POST request to delete completed orders
*/
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete_order') {
$order_id = isset($_POST['order_id']) ? (int)$_POST['order_id'] : 0;
if ($order_id > 0) {
try {
$database = new Database();
$db = $database->getConnection();
$user_id = $_SESSION['user_id'];
// Delete the order
$delete_query = "DELETE FROM orders WHERE id = ? AND user_id = ? AND status = 'Completed'";
$stmt = $db->prepare($delete_query);
$stmt->execute([$order_id, $user_id]);
if ($stmt->rowCount() > 0) {
$success_message = 'Order deleted successfully!';
} else {
$error_message = 'Failed to delete order or order not found.';
}
} catch (Exception $e) {
$error_message = 'Database error: ' . $e->getMessage();
}
}
}
try {
$database = new Database();
$db = $database->getConnection();
// Create orders table if it doesn't exist
$create_table_sql = "CREATE TABLE IF NOT EXISTS orders (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
tracking_id VARCHAR(50) DEFAULT NULL,
patient_name VARCHAR(255) NOT NULL,
whatsapp_number VARCHAR(20) NOT NULL,
frame_detail VARCHAR(255) DEFAULT NULL,
lens_type VARCHAR(255) DEFAULT NULL,
total_amount DECIMAL(10,2) NOT NULL,
advance DECIMAL(10,2) DEFAULT 0,
balance DECIMAL(10,2) DEFAULT 0,
delivery_date DATE DEFAULT NULL,
right_sph VARCHAR(10) DEFAULT NULL,
right_cyl VARCHAR(10) DEFAULT NULL,
right_axis VARCHAR(10) DEFAULT NULL,
left_sph VARCHAR(10) DEFAULT NULL,
left_cyl VARCHAR(10) DEFAULT NULL,
left_axis VARCHAR(10) DEFAULT NULL,
add_power VARCHAR(10) DEFAULT NULL,
important_note TEXT DEFAULT NULL,
description TEXT DEFAULT NULL,
status VARCHAR(20) DEFAULT 'Pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_user_id (user_id),
INDEX idx_tracking_id (tracking_id),
INDEX idx_patient_name (patient_name),
INDEX idx_created_at (created_at)
)";
$db->exec($create_table_sql);
// Add status column to existing orders table if it doesn't exist
try {
$db->exec("ALTER TABLE orders ADD COLUMN status VARCHAR(20) DEFAULT 'Pending'");
} catch (PDOException $e) {
// Column already exists, ignore error
}
$user_id = $_SESSION['user_id'];
// Get completed orders for current user
$query = "SELECT * FROM orders WHERE user_id = ? AND status = 'Completed' ORDER BY created_at DESC";
$stmt = $db->prepare($query);
$stmt->execute([$user_id]);
$orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
$error_message = 'Database error: ' . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Completed Orders - OPTI SLIP</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Inter', sans-serif;
background: linear-gradient(135deg, #1e3a8a 0%, #3730a3 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
display: flex;
align-items: center;
margin-bottom: 30px;
gap: 15px;
}
.back-btn {
background: #10b981;
color: white;
border: none;
border-radius: 50%;
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.3s ease;
text-decoration: none;
}
.back-btn:hover {
background: #059669;
transform: scale(1.05);
color: white;
}
.page-title {
color: white;
font-size: 24px;
font-weight: 600;
}
.orders-card {
background: white;
border-radius: 20px;
padding: 30px;
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
}
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 30px;
flex-wrap: wrap;
gap: 20px;
}
.logo-section {
display: flex;
align-items: center;
gap: 10px;
}
.logo-icon {
width: 40px;
height: 40px;
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 18px;
}
.logo-text {
font-size: 24px;
font-weight: 700;
color: #1e3a8a;
}
.date-range {
display: flex;
align-items: center;
gap: 10px;
background: #f8fafc;
padding: 10px 15px;
border-radius: 10px;
border: 1px solid #e2e8f0;
}
.date-text {
color: #475569;
font-weight: 500;
font-size: 14px;
}
.calendar-icon {
color: #64748b;
font-size: 16px;
}
.orders-table-container {
overflow-x: auto;
border-radius: 12px;
border: 1px solid #e5e7eb;
}
.orders-table {
width: 100%;
border-collapse: collapse;
background: white;
}
.orders-table thead {
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
}
.orders-table th {
color: white;
font-weight: 600;
padding: 15px 12px;
text-align: left;
font-size: 14px;
white-space: nowrap;
}
.orders-table td {
padding: 15px 12px;
border-bottom: 1px solid #f3f4f6;
color: #374151;
font-size: 14px;
vertical-align: middle;
}
.orders-table tbody tr:hover {
background: #f9fafb;
}
.orders-table tbody tr:last-child td {
border-bottom: none;
}
.action-buttons {
display: flex;
gap: 8px;
align-items: center;
}
.action-btn {
width: 32px;
height: 32px;
border-radius: 50%;
border: none;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.3s ease;
font-size: 12px;
color: white;
}
.view-btn {
background: #3b82f6;
}
.view-btn:hover {
background: #2563eb;
transform: scale(1.1);
}
.bill-btn {
background: #10b981;
font-weight: bold;
}
.bill-btn:hover {
background: #059669;
transform: scale(1.1);
}
.delete-btn {
background: #ef4444;
}
.delete-btn:hover {
background: #dc2626;
transform: scale(1.1);
}
.status-badge {
padding: 6px 12px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
text-transform: uppercase;
}
.status-completed {
background: #d1fae5;
color: #065f46;
}
.no-orders {
text-align: center;
padding: 60px 20px;
color: #6b7280;
}
.no-orders i {
font-size: 64px;
margin-bottom: 20px;
color: #d1d5db;
}
.no-orders h3 {
font-size: 20px;
margin-bottom: 10px;
color: #374151;
}
.no-orders p {
font-size: 16px;
margin-bottom: 30px;
}
.create-order-btn {
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
color: white;
padding: 12px 24px;
border-radius: 8px;
text-decoration: none;
font-weight: 600;
display: inline-flex;
align-items: center;
gap: 8px;
transition: all 0.3s ease;
}
.create-order-btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(16, 185, 129, 0.3);
color: white;
}
@media (max-width: 768px) {
.orders-card {
padding: 20px;
margin: 0 10px;
}
.card-header {
flex-direction: column;
align-items: flex-start;
gap: 15px;
}
.logo-section {
flex-direction: column;
gap: 15px;
}
.orders-table th,
.orders-table td {
padding: 10px 8px;
font-size: 12px;
}
.action-buttons {
flex-direction: row;
gap: 8px;
justify-content: center;
}
.action-btn {
width: 28px;
height: 28px;
font-size: 10px;
}
}
@media (max-width: 480px) {
.orders-table {
font-size: 11px;
}
.orders-table th,
.orders-table td {
padding: 8px 6px;
}
.status-badge {
padding: 4px 8px;
font-size: 10px;
}
}
</style>
</head>
<body>
<div class="container">
<!-- Header -->
<div class="header">
<a href="home.php" class="back-btn">
<i class="fas fa-arrow-left"></i>
</a>
<h1 class="page-title">Complete</h1>
</div>
<!-- Orders Card -->
<div class="orders-card">
<?php if (isset($error_message) && !empty($error_message)): ?>
<div class="alert alert-danger" style="background: #fef2f2; border: 1px solid #fecaca; color: #dc2626; padding: 12px 16px; border-radius: 8px; margin-bottom: 20px; text-align: center;">
<i class="fas fa-exclamation-triangle me-2"></i>
<?php echo htmlspecialchars($error_message); ?>
</div>
<?php endif; ?>
<?php if (isset($success_message) && !empty($success_message)): ?>
<div class="alert alert-success" style="background: #f0fdf4; border: 1px solid #bbf7d0; color: #166534; padding: 12px 16px; border-radius: 8px; margin-bottom: 20px; text-align: center;">
<i class="fas fa-check-circle me-2"></i>
<?php echo htmlspecialchars($success_message); ?>
</div>
<?php endif; ?>
<!-- Card Header -->
<div class="card-header">
<div class="logo-section">
<div class="logo-icon">
<i class="fas fa-glasses"></i>
</div>
<div class="logo-text">OPTI SLIP</div>
</div>
<div class="date-range">
<i class="fas fa-calendar-alt calendar-icon"></i>
<span class="date-text">Jan 1 To Jan 30</span>
<i class="fas fa-calendar-alt calendar-icon"></i>
</div>
</div>
<!-- Orders Table -->
<?php if (!empty($orders)): ?>
<div class="orders-table-container">
<table class="orders-table">
<thead>
<tr>
<th>UID</th>
<th>Patient Name</th>
<th>Tracking Id</th>
<th>Amount</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $order): ?>
<tr>
<td>U<?php echo str_pad($order['id'], 4, '0', STR_PAD_LEFT); ?></td>
<td><?php echo htmlspecialchars($order['patient_name']); ?></td>
<td><?php echo htmlspecialchars($order['tracking_id']); ?></td>
<td>$<?php echo number_format($order['total_amount'], 0); ?></td>
<td>
<span class="status-badge status-completed">
Complete
</span>
</td>
<td>
<div class="action-buttons">
<button class="action-btn view-btn" onclick="viewOrder(<?php echo $order['id']; ?>)" title="View Details">
<i class="fas fa-eye"></i>
</button>
<button class="action-btn delete-btn" onclick="deleteOrder(<?php echo $order['id']; ?>)" title="Delete Order">
<i class="fas fa-trash"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="no-orders">
<i class="fas fa-check-circle"></i>
<h3>No Completed Orders</h3>
<p>You don't have any completed orders yet. Complete some orders to see them here.</p>
<a href="pending-orders.php" class="create-order-btn">
<i class="fas fa-eye"></i>
View Pending Orders
</a>
</div>
<?php endif; ?>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function viewOrder(orderId) {
// Redirect to order slip page
window.location.href = 'order-slip.php?id=' + orderId;
}
function deleteOrder(orderId) {
if (confirm('Are you sure you want to delete this completed order? This action cannot be undone.')) {
// Create and submit form to delete order
const form = document.createElement('form');
form.method = 'POST';
form.action = ''; // Submit to the same page
const actionInput = document.createElement('input');
actionInput.type = 'hidden';
actionInput.name = 'action';
actionInput.value = 'delete_order';
const orderIdInput = document.createElement('input');
orderIdInput.type = 'hidden';
orderIdInput.name = 'order_id';
orderIdInput.value = orderId;
form.appendChild(actionInput);
form.appendChild(orderIdInput);
document.body.appendChild(form);
form.submit();
}
}
// Auto refresh every 30 seconds
setTimeout(function() {
location.reload();
}, 30000);
</script>
</body>
</html>