����JFIF��`�`�����Viewing File: /home/u820193700/domains/throtllr.in/public_html/admin/manage_customers.php
<?php
require_once '../includes/db.php';
require_once '../includes/functions.php';
if(!is_logged_in()) { redirect("login.php"); }

// Handle delete action
if (isset($_GET['action']) && isset($_GET['phone'])) {
    if ($_GET['action'] == 'delete') {
        $phone = $_GET['phone'];
        try {
            $pdo->beginTransaction();
            // Delete from customers table (matching last 10 digits)
            $stmt = $pdo->prepare("DELETE FROM customers WHERE RIGHT(phone, 10) = RIGHT(?, 10)");
            $stmt->execute([$phone]);
            // Delete from inspection_bookings (matching last 10 digits)
            $stmt = $pdo->prepare("DELETE FROM inspection_bookings WHERE RIGHT(phone, 10) = RIGHT(?, 10)");
            $stmt->execute([$phone]);
            // Delete from sell_requests (matching last 10 digits)
            $stmt = $pdo->prepare("DELETE FROM sell_requests WHERE RIGHT(phone, 10) = RIGHT(?, 10)");
            $stmt->execute([$phone]);
            $pdo->commit();
        } catch(PDOException $e) {
            $pdo->rollBack();
        }
    }
    redirect("manage_customers.php");
}

// Fetch customers dynamically from all interactions and filter out empty names/phones
// We group by the last 10 digits of the phone number to unify duplicates with leading zeroes or +91
$stmt = $pdo->query("
    SELECT MAX(name) as name, RIGHT(phone, 10) as phone_key, MAX(phone) as phone, MAX(email) as email, MAX(city) as city, MIN(created_at) as joined_date
    FROM (
        SELECT name, phone, email, city, created_at FROM customers
        UNION ALL
        SELECT name, phone, email, city, created_at FROM sell_requests
        UNION ALL
        SELECT name, phone, NULL as email, NULL as city, created_at FROM inspection_bookings
    ) AS combined
    WHERE phone IS NOT NULL AND TRIM(phone) != '' AND name IS NOT NULL AND TRIM(name) != ''
    GROUP BY phone_key
    ORDER BY joined_date DESC
");
$customers = $stmt->fetchAll();

// Pre-fetch all user details, bookings, and sell requests to avoid nesting modal HTML inside <td>
$customer_data = [];
foreach($customers as $c) {
    $phone = $c['phone'];
    
    // Check if registered (matching last 10 digits)
    $reg_stmt = $pdo->prepare("SELECT * FROM customers WHERE RIGHT(phone, 10) = RIGHT(?, 10)");
    $reg_stmt->execute([$phone]);
    $reg_user = $reg_stmt->fetch();
    $is_registered = !empty($reg_user);

    // Fetch bookings (matching last 10 digits)
    $bookings_stmt = $pdo->prepare("
        SELECT ib.*, b.model_name, b.reference_id 
        FROM inspection_bookings ib 
        LEFT JOIN bikes b ON ib.bike_id = b.id 
        WHERE RIGHT(ib.phone, 10) = RIGHT(?, 10) 
        ORDER BY ib.created_at DESC
    ");
    $bookings_stmt->execute([$phone]);
    $bookings = $bookings_stmt->fetchAll();

    // Fetch sell requests (matching last 10 digits)
    $sell_stmt = $pdo->prepare("SELECT * FROM sell_requests WHERE RIGHT(phone, 10) = RIGHT(?, 10) ORDER BY created_at DESC");
    $sell_stmt->execute([$phone]);
    $sell_requests = $sell_stmt->fetchAll();

    $customer_data[] = [
        'info' => $c,
        'is_registered' => $is_registered,
        'bookings' => $bookings,
        'sell_requests' => $sell_requests
    ];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Customers - ReOwn Admin</title>
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="admin.css?v=<?php echo time(); ?>">
</head>
<body>

<?php include 'admin_sidebar.php'; ?>

<div id="main-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Manage Customers</h2>
    </div>

    <div class="glass-card p-4">
        <div class="table-responsive">
            <table class="table table-dark table-hover align-middle">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th>City</th>
                        <th>Joined Date</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($customer_data) > 0): ?>
                        <?php $i=1; foreach($customer_data as $cd): 
                            $c = $cd['info'];
                            $phone = $c['phone'];
                            $modal_id = 'customerModal_' . md5($phone);
                        ?>
                        <tr>
                            <td><?php echo $i++; ?></td>
                            <td><strong><?php echo htmlspecialchars($c['name']); ?></strong></td>
                            <td><?php echo htmlspecialchars($c['phone']); ?></td>
                            <td><?php echo htmlspecialchars($c['email'] ?? '-'); ?></td>
                            <td><?php echo htmlspecialchars($c['city'] ?? '-'); ?></td>
                            <td><?php echo date('Y-m-d', strtotime($c['joined_date'])); ?></td>
                            <td>
                                <!-- View Details Modal Trigger -->
                                <button type="button" class="btn btn-sm btn-outline-info me-1" data-bs-toggle="modal" data-bs-target="#<?php echo $modal_id; ?>" title="View Details">
                                    <i class="fas fa-eye"></i> View
                                </button>
                                <!-- Delete Button -->
                                <a href="?action=delete&phone=<?php echo urlencode($phone); ?>" class="btn btn-sm btn-outline-danger" title="Delete Customer" onclick="return confirm('Are you sure you want to delete this customer and all their transactions? This action cannot be undone.')">
                                    <i class="fas fa-trash"></i> Delete
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="7" class="text-center text-muted">No customers found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Customer Details Modals (Rendered outside table to prevent backdrop/positioning bugs) -->
<?php foreach($customer_data as $cd): 
    $c = $cd['info'];
    $phone = $c['phone'];
    $is_registered = $cd['is_registered'];
    $bookings = $cd['bookings'];
    $sell_requests = $cd['sell_requests'];
    $modal_id = 'customerModal_' . md5($phone);
?>
<div class="modal fade" id="<?php echo $modal_id; ?>" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" style="color: #212529 !important;"><i class="fas fa-user-circle me-2"></i>Customer Profile</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body text-start">
                <div class="row">
                    <!-- Left Column: Details -->
                    <div class="col-md-5 mb-4 mb-md-0 border-end border-secondary border-opacity-25">
                        <div class="text-center mb-4">
                            <div class="d-inline-flex align-items-center justify-content-center bg-danger bg-opacity-10 text-danger rounded-circle mb-3" style="width: 70px; height: 70px;">
                                <i class="fas fa-user fa-2x"></i>
                            </div>
                            <h4 style="color: #212529 !important;"><?php echo htmlspecialchars($c['name']); ?></h4>
                            <span class="badge <?php echo $is_registered ? 'bg-success bg-opacity-20 text-success border border-success border-opacity-50' : 'bg-secondary bg-opacity-20 text-secondary border border-secondary border-opacity-50'; ?>" style="color: <?php echo $is_registered ? '#198754' : '#6c757d'; ?> !important; font-weight: bold;">
                                <?php echo $is_registered ? 'Registered Member' : 'Guest Lead'; ?>
                            </span>
                        </div>

                        <div class="info-list">
                            <div class="info-list-item">
                                <span class="info-label" style="color: #6c757d !important;">Phone</span>
                                <span class="info-value" style="color: #212529 !important; font-weight: 500;"><?php echo htmlspecialchars($c['phone']); ?></span>
                            </div>
                            <div class="info-list-item">
                                <span class="info-label" style="color: #6c757d !important;">Email</span>
                                <span class="info-value" style="color: #212529 !important; font-weight: 500;"><?php echo htmlspecialchars($c['email'] ?? 'Not Provided'); ?></span>
                            </div>
                            <div class="info-list-item">
                                <span class="info-label" style="color: #6c757d !important;">City</span>
                                <span class="info-value" style="color: #212529 !important; font-weight: 500;"><?php echo htmlspecialchars($c['city'] ?? 'Not Provided'); ?></span>
                            </div>
                            <div class="info-list-item">
                                <span class="info-label" style="color: #6c757d !important;">First Seen</span>
                                <span class="info-value" style="color: #212529 !important; font-weight: 500;"><?php echo date('M j, Y', strtotime($c['joined_date'])); ?></span>
                            </div>
                        </div>
                    </div>

                    <!-- Right Column: Tabs for Booking / Sells -->
                    <div class="col-md-7">
                        <!-- Nav Tabs -->
                        <ul class="nav nav-tabs" id="myTab_<?php echo md5($phone); ?>" role="tablist">
                            <li class="nav-item" role="presentation">
                                <button class="nav-link active" id="bookings-tab-<?php echo md5($phone); ?>" data-bs-toggle="tab" data-bs-target="#bookings-panel-<?php echo md5($phone); ?>" type="button" role="tab" aria-controls="bookings" aria-selected="true">
                                    <i class="fas fa-clipboard-check me-1"></i> Bookings (<?php echo count($bookings); ?>)
                                </button>
                            </li>
                            <li class="nav-item" role="presentation">
                                <button class="nav-link" id="sell-tab-<?php echo md5($phone); ?>" data-bs-toggle="tab" data-bs-target="#sell-panel-<?php echo md5($phone); ?>" type="button" role="tab" aria-controls="sell" aria-selected="false">
                                    <i class="fas fa-tags me-1"></i> Sell Requests (<?php echo count($sell_requests); ?>)
                                </button>
                            </li>
                        </ul>

                        <!-- Tab Content -->
                        <div class="tab-content" id="myTabContent_<?php echo md5($phone); ?>" style="max-height: 300px; overflow-y: auto;">
                            <!-- Bookings Panel -->
                            <div class="tab-pane fade show active" id="bookings-panel-<?php echo md5($phone); ?>" role="tabpanel" aria-labelledby="bookings-tab-<?php echo md5($phone); ?>">
                                <?php if (count($bookings) > 0): ?>
                                    <div class="table-responsive">
                                        <table class="table table-sm table-hover align-middle" style="font-size: 0.85rem; color: #212529;">
                                            <thead>
                                                <tr>
                                                    <th>Bike</th>
                                                    <th>Date/Time</th>
                                                    <th>Status</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($bookings as $b): ?>
                                                    <tr>
                                                        <td>
                                                            <?php if ($b['bike_id']): ?>
                                                                <a href="manage_bikes.php?search=<?php echo urlencode($b['reference_id']); ?>" class="text-primary text-decoration-none fw-semibold">
                                                                    <?php echo htmlspecialchars($b['model_name'] ?? 'Bike #'.$b['bike_id']); ?>
                                                                </a>
                                                            <?php else: ?>
                                                                <span class="text-muted">General Inquiry</span>
                                                            <?php endif; ?>
                                                        </td>
                                                        <td>
                                                            <?php echo date('M d, Y', strtotime($b['preferred_date'])); ?>
                                                            <?php if ($b['preferred_time']) echo '<br><small class="text-muted">'.date('h:i A', strtotime($b['preferred_time'])).'</small>'; ?>
                                                        </td>
                                                        <td>
                                                            <span class="badge bg-<?php echo htmlspecialchars($b['status']); ?>">
                                                                <?php echo ucfirst($b['status']); ?>
                                                            </span>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                <?php else: ?>
                                    <div class="text-center py-4 text-muted">
                                        <i class="fas fa-calendar-times fa-2x mb-2 d-block opacity-50"></i>
                                        No inspections booked.
                                    </div>
                                <?php endif; ?>
                            </div>

                            <!-- Sell Requests Panel -->
                            <div class="tab-pane fade" id="sell-panel-<?php echo md5($phone); ?>" role="tabpanel" aria-labelledby="sell-tab-<?php echo md5($phone); ?>">
                                <?php if (count($sell_requests) > 0): ?>
                                    <div class="table-responsive">
                                        <table class="table table-sm table-hover align-middle" style="font-size: 0.85rem; color: #212529;">
                                            <thead>
                                                <tr>
                                                    <th>Bike Model</th>
                                                    <th>Expected Price</th>
                                                    <th>Status</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($sell_requests as $sr): ?>
                                                    <tr>
                                                        <td>
                                                            <strong><?php echo htmlspecialchars($sr['model_name']); ?></strong>
                                                            <br><small class="text-muted"><?php echo htmlspecialchars($sr['registration_number'] ?? ''); ?> (<?php echo htmlspecialchars($sr['year']); ?>)</small>
                                                        </td>
                                                        <td>₹ <?php echo number_format($sr['expected_price'], 2); ?></td>
                                                        <td>
                                                            <span class="badge bg-<?php echo htmlspecialchars($sr['status']); ?>">
                                                                <?php echo ucfirst($sr['status']); ?>
                                                            </span>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                <?php else: ?>
                                    <div class="text-center py-4 text-muted">
                                        <i class="fas fa-tag fa-2x mb-2 d-block opacity-50"></i>
                                        No sell requests submitted.
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<?php endforeach; ?>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Back to Directory �������}�!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������� ������w�!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������ ��?��_��+��?��(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(�����