/home/awneajlw/.trash/services.php.1
<?php
require_once '../config/database.php';
require_once '../includes/auth.php';
requireAdmin();

$database = new Database();
$db = $database->getConnection();

$action = isset($_GET['action']) ? $_GET['action'] : 'list';
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

$error = '';
$success = '';

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title = sanitizeInput($_POST['title']);
    $description = sanitizeInput($_POST['description']);
    $price = (float)$_POST['price'];
    $status = sanitizeInput($_POST['status']);
    
    if (empty($title) || empty($description) || $price <= 0) {
        $error = 'Please fill in all required fields with valid data.';
    } else {
        if ($action == 'add') {
            $query = "INSERT INTO services (title, description, price, status) VALUES (?, ?, ?, ?)";
            $stmt = $db->prepare($query);
            if ($stmt->execute([$title, $description, $price, $status])) {
                $success = 'Service added successfully!';
                $action = 'list';
            } else {
                $error = 'Failed to add service. Please try again.';
            }
        } elseif ($action == 'edit' && $id > 0) {
            $query = "UPDATE services SET title = ?, description = ?, price = ?, status = ? WHERE id = ?";
            $stmt = $db->prepare($query);
            if ($stmt->execute([$title, $description, $price, $status, $id])) {
                $success = 'Service updated successfully!';
                $action = 'list';
            } else {
                $error = 'Failed to update service. Please try again.';
            }
        }
    }
}

// Handle delete
if (isset($_GET['delete']) && $id > 0) {
    $query = "DELETE FROM services WHERE id = ?";
    $stmt = $db->prepare($query);
    if ($stmt->execute([$id])) {
        $success = 'Service deleted successfully!';
    } else {
        $error = 'Failed to delete service.';
    }
    $action = 'list';
}

// Get services for listing
if ($action == 'list') {
    $query = "SELECT * FROM services ORDER BY created_at DESC";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $services = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

// Get service for editing
if ($action == 'edit' && $id > 0) {
    $query = "SELECT * FROM services WHERE id = ?";
    $stmt = $db->prepare($query);
    $stmt->execute([$id]);
    $service = $stmt->fetch(PDO::FETCH_ASSOC);
    if (!$service) {
        $error = 'Service not found.';
        $action = 'list';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Services - Opti-Vision Eye Clinic</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">
    <link href="../assets/css/style.css" rel="stylesheet">
</head>
<body class="bg-light">
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary shadow-sm">
        <div class="container-fluid">
            <a class="navbar-brand fw-bold" href="../index.php">
                <i class="fas fa-eye me-2"></i>Opti-Vision Admin
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="dashboard.php">Dashboard</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="appointments.php">Appointments</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="users.php">Users</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="services.php">Services</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="doctors.php">Doctors</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="messages.php">Messages</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="../logout.php">Logout</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container-fluid py-4">
        <div class="row">
            <div class="col-12">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="fw-bold">Manage Services</h2>
                    <div class="d-flex gap-2">
                        <a href="?action=add" class="btn btn-primary">
                            <i class="fas fa-plus me-2"></i>Add Service
                        </a>
                        <a href="dashboard.php" class="btn btn-outline-secondary">
                            <i class="fas fa-arrow-left me-2"></i>Back to Dashboard
                        </a>
                    </div>
                </div>
                
                <?php if ($error): ?>
                    <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php endif; ?>
                
                <?php if ($success): ?>
                    <div class="alert alert-success"><?php echo $success; ?></div>
                <?php endif; ?>
                
                <?php if ($action == 'list'): ?>
                <!-- Services List -->
                <div class="card dashboard-card">
                    <div class="card-body">
                        <?php if (empty($services)): ?>
                            <div class="text-center py-5">
                                <i class="fas fa-list fa-3x text-muted mb-3"></i>
                                <h5 class="text-muted">No services found</h5>
                                <p class="text-muted">Add your first service to get started.</p>
                                <a href="?action=add" class="btn btn-primary">Add Service</a>
                            </div>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Title</th>
                                            <th>Description</th>
                                            <th>Price</th>
                                            <th>Status</th>
                                            <th>Created</th>
                                            <th>Actions</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach($services as $service): ?>
                                        <tr>
                                            <td>#<?php echo $service['id']; ?></td>
                                            <td>
                                                <strong><?php echo htmlspecialchars($service['title']); ?></strong>
                                            </td>
                                            <td>
                                                <small class="text-muted">
                                                    <?php echo htmlspecialchars(substr($service['description'], 0, 50)); ?>
                                                    <?php echo strlen($service['description']) > 50 ? '...' : ''; ?>
                                                </small>
                                            </td>
                                            <td>
                                                <strong class="text-primary">PKR <?php echo number_format($service['price']); ?></strong>
                                            </td>
                                            <td>
                                                <span class="badge bg-<?php echo $service['status'] === 'active' ? 'success' : 'secondary'; ?>">
                                                    <?php echo ucfirst($service['status']); ?>
                                                </span>
                                            </td>
                                            <td><?php echo date('M d, Y', strtotime($service['created_at'])); ?></td>
                                            <td>
                                                <div class="btn-group btn-group-sm">
                                                    <a href="?action=edit&id=<?php echo $service['id']; ?>" 
                                                       class="btn btn-outline-primary" title="Edit">
                                                        <i class="fas fa-edit"></i>
                                                    </a>
                                                    <a href="?delete=1&id=<?php echo $service['id']; ?>" 
                                                       class="btn btn-outline-danger" 
                                                       onclick="return confirm('Are you sure you want to delete this service?')" 
                                                       title="Delete">
                                                        <i class="fas fa-trash"></i>
                                                    </a>
                                                </div>
                                            </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                
                <?php elseif ($action == 'add' || $action == 'edit'): ?>
                <!-- Add/Edit Service Form -->
                <div class="card dashboard-card">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-<?php echo $action == 'add' ? 'plus' : 'edit'; ?> me-2"></i>
                            <?php echo $action == 'add' ? 'Add New Service' : 'Edit Service'; ?>
                        </h5>
                    </div>
                    <div class="card-body">
                        <form method="POST">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="title" class="form-label">Service Title <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="title" name="title" 
                                           value="<?php echo isset($service) ? htmlspecialchars($service['title']) : ''; ?>" required>
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="price" class="form-label">Price (PKR) <span class="text-danger">*</span></label>
                                    <input type="number" class="form-control" id="price" name="price" 
                                           step="0.01" min="0"
                                           value="<?php echo isset($service) ? $service['price'] : ''; ?>" required>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="description" class="form-label">Description <span class="text-danger">*</span></label>
                                <textarea class="form-control" id="description" name="description" rows="4" required><?php echo isset($service) ? htmlspecialchars($service['description']) : ''; ?></textarea>
                            </div>
                            
                            <div class="mb-4">
                                <label for="status" class="form-label">Status</label>
                                <select class="form-select" id="status" name="status">
                                    <option value="active" <?php echo (isset($service) && $service['status'] === 'active') ? 'selected' : ''; ?>>Active</option>
                                    <option value="inactive" <?php echo (isset($service) && $service['status'] === 'inactive') ? 'selected' : ''; ?>>Inactive</option>
                                </select>
                            </div>
                            
                            <div class="d-flex gap-3">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-save me-2"></i>
                                    <?php echo $action == 'add' ? 'Add Service' : 'Update Service'; ?>
                                </button>
                                <a href="services.php" class="btn btn-outline-secondary">
                                    <i class="fas fa-times me-2"></i>Cancel
                                </a>
                            </div>
                        </form>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>