<?php
/**
 * ADMIN CUSTOMER DETAILS PAGE
 * File: admin/customer-details.php
 * View detailed information about a specific customer
 */

require_once '../app/Core/init.php';

// Check admin authentication
Auth::requireAdmin();

$page_title = "Customer Details";
$active_menu = "customers";

// Get customer ID
$customer_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$customer_id) {
    header('Location: customers.php');
    exit;
}

// Initialize models
$db = Database::getInstance();

// Get customer details with user information
$customer_sql = "SELECT c.*, u.email, u.status as user_status, u.email_verified, u.created_at as user_created,
                 w.balance
                 FROM customers c 
                 LEFT JOIN users u ON c.user_id = u.id 
                 LEFT JOIN wallets w ON c.user_id = w.user_id
                 WHERE c.id = ?";
$customer = $db->fetch($customer_sql, [$customer_id]);

if (!$customer) {
    header('Location: customers.php');
    exit;
}

// Get customer statistics
$stats_sql = "SELECT 
              (SELECT COUNT(*) FROM orders WHERE customer_id = ?) as total_orders,
              (SELECT COUNT(*) FROM orders WHERE customer_id = ? AND status = 'completed') as completed_orders,
              (SELECT COUNT(*) FROM orders WHERE customer_id = ? AND status = 'cancelled') as cancelled_orders,
              (SELECT COALESCE(SUM(total_amount), 0) FROM orders WHERE customer_id = ? AND payment_status = 'paid') as total_spent,
              (SELECT COALESCE(AVG(rating), 0) FROM reviews WHERE customer_id = ?) as avg_rating,
              (SELECT COUNT(*) FROM reviews WHERE customer_id = ?) as total_reviews";
$stats = $db->fetch($stats_sql, array_fill(0, 6, $customer_id));

// Get recent orders
$recent_orders_sql = "SELECT o.*, i.title as item_title, v.business_name as vendor_name,
                      i.main_image as item_image
                      FROM orders o
                      LEFT JOIN items i ON o.item_id = i.id
                      LEFT JOIN vendors v ON o.vendor_id = v.id
                      WHERE o.customer_id = ?
                      ORDER BY o.created_at DESC
                      LIMIT 20";
$recent_orders = $db->fetchAll($recent_orders_sql, [$customer_id]);

// Get customer reviews
$reviews_sql = "SELECT r.*, i.title as item_title, v.business_name as vendor_name
                FROM reviews r
                LEFT JOIN items i ON r.item_id = i.id
                LEFT JOIN vendors v ON r.vendor_id = v.id
                WHERE r.customer_id = ?
                ORDER BY r.created_at DESC
                LIMIT 15";
$reviews = $db->fetchAll($reviews_sql, [$customer_id]);

// Calculate customer lifetime value and order frequency
$lifetime_sql = "SELECT 
                 DATEDIFF(NOW(), MIN(o.created_at)) as days_since_first_order,
                 COUNT(o.id) as order_count,
                 SUM(o.total_amount) as total_spent,
                 AVG(o.total_amount) as avg_order_value
                 FROM orders o
                 WHERE o.customer_id = ? AND o.payment_status = 'paid'";
$lifetime = $db->fetch($lifetime_sql, [$customer_id]);

// Handle flash messages
$flash_message = flash('message');

include '../templates/layouts/admin/head.php';
?>

<div class="main-content">
    <?php include '../templates/layouts/admin/header.php'; ?>
    
    <!-- Page Header -->
    <div style="padding: 30px; background: white; border-bottom: 1px solid #eee;">
        <h1 style="font-size: 28px; font-weight: 700; color: var(--dark); margin-bottom: 5px;">
            <?= Security::escapeOutput($customer['first_name'] . ' ' . $customer['last_name']) ?>
        </h1>
        <div style="display: flex; gap: 8px; font-size: 14px; color: #666;">
            <a href="index.php" style="color: var(--primary); text-decoration: none;">Home</a>
            <span>/</span>
            <a href="customers.php" style="color: var(--primary); text-decoration: none;">Customers</a>
            <span>/</span>
            <span><?= Security::escapeOutput($customer['first_name'] . ' ' . $customer['last_name']) ?></span>
        </div>
    </div>
    
    <!-- Content -->
    <div style="padding: 30px;">
        
        <!-- Flash Messages -->
        <?php if ($flash_message): ?>
            <div class="alert alert-<?= $flash_message['type'] ?>">
                <?= Security::escapeOutput($flash_message['message']) ?>
            </div>
        <?php endif; ?>
        
        <!-- Customer Info Cards -->
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin-bottom: 30px;">
            <!-- Personal Info -->
            <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05);">
                <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                    <i class="fas fa-user"></i> Personal Information
                </h3>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Full Name</label>
                    <div style="font-size: 16px; font-weight: 600;">
                        <?= Security::escapeOutput($customer['first_name'] . ' ' . $customer['last_name']) ?>
                    </div>
                </div>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Email Address</label>
                    <div style="font-size: 14px;"><?= Security::escapeOutput($customer['email']) ?></div>
                    <?php if ($customer['email_verified']): ?>
                        <span style="background: var(--success); color: white; padding: 2px 6px; border-radius: 10px; font-size: 10px; font-weight: 600;">Verified</span>
                    <?php else: ?>
                        <span style="background: var(--warning); color: white; padding: 2px 6px; border-radius: 10px; font-size: 10px; font-weight: 600;">Unverified</span>
                    <?php endif; ?>
                </div>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Phone Number</label>
                    <div style="font-size: 14px;"><?= format_phone($customer['phone']) ?></div>
                </div>
                
                <?php if (!empty($customer['date_of_birth'])): ?>
                    <div style="margin-bottom: 15px;">
                        <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Date of Birth</label>
                        <div style="font-size: 14px;"><?= format_date($customer['date_of_birth']) ?></div>
                    </div>
                <?php endif; ?>
                
                <?php if (!empty($customer['gender'])): ?>
                    <div style="margin-bottom: 15px;">
                        <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Gender</label>
                        <div style="font-size: 14px;"><?= ucfirst($customer['gender']) ?></div>
                    </div>
                <?php endif; ?>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Account Status</label>
                    <div>
                        <?php
                        $status_class = $customer['user_status'] === 'active' ? 'status-approved' : 'status-rejected';
                        ?>
                        <span class="status-badge <?= $status_class ?>">
                            <?= ucfirst($customer['user_status']) ?>
                        </span>
                    </div>
                </div>
            </div>
            
            <!-- Address Info -->
            <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05);">
                <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                    <i class="fas fa-map-marker-alt"></i> Address Information
                </h3>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Street Address</label>
                    <div style="font-size: 14px;"><?= Security::escapeOutput($customer['address'] ?? 'Not provided') ?></div>
                </div>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">City</label>
                    <div style="font-size: 14px;"><?= Security::escapeOutput($customer['city'] ?? 'Not provided') ?></div>
                </div>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">State</label>
                    <div style="font-size: 14px;"><?= Security::escapeOutput($customer['state'] ?? 'Not provided') ?></div>
                </div>
                
                <?php if (!empty($customer['profile_picture'])): ?>
                    <div style="margin-top: 20px;">
                        <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Profile Picture</label>
                        <img src="<?= upload($customer['profile_picture']) ?>" 
                             alt="Profile Picture"
                             style="width: 80px; height: 80px; border-radius: 50%; object-fit: cover; margin-top: 10px;">
                    </div>
                <?php endif; ?>
            </div>
            
            <!-- Account Stats -->
            <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05);">
                <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                    <i class="fas fa-chart-line"></i> Account Statistics
                </h3>
                
                <div style="margin-bottom: 20px; padding: 15px; background: #f8f9fa; border-radius: 8px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Total Spent</label>
                    <div style="font-size: 24px; font-weight: 700; color: var(--success);">
                        <?= format_money($stats['total_spent']) ?>
                    </div>
                </div>
                
                <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 20px;">
                    <div style="text-align: center; padding: 10px; background: #f8f9fa; border-radius: 8px;">
                        <div style="font-size: 20px; font-weight: 700; color: var(--primary);"><?= number_format($stats['total_orders']) ?></div>
                        <div style="font-size: 11px; color: #666;">Total Orders</div>
                    </div>
                    
                    <div style="text-align: center; padding: 10px; background: #f8f9fa; border-radius: 8px;">
                        <div style="font-size: 20px; font-weight: 700; color: var(--success);"><?= number_format($stats['completed_orders']) ?></div>
                        <div style="font-size: 11px; color: #666;">Completed</div>
                    </div>
                </div>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Average Order Value</label>
                    <div style="font-size: 16px; font-weight: 600;"><?= format_money($lifetime['avg_order_value'] ?? 0) ?></div>
                </div>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Customer Rating</label>
                    <div style="font-size: 16px; font-weight: 600;">
                        <?= number_format($stats['avg_rating'], 1) ?>/5 
                        (<?= number_format($stats['total_reviews']) ?> reviews)
                    </div>
                </div>
                
                <div style="margin-bottom: 15px;">
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Wallet Balance</label>
                    <div style="font-size: 16px; font-weight: 600;"><?= format_money($customer['balance'] ?? 0) ?></div>
                </div>
                
                <div>
                    <label style="font-weight: 600; color: #666; font-size: 12px; text-transform: uppercase;">Member Since</label>
                    <div style="font-size: 14px;"><?= format_date($customer['user_created']) ?></div>
                </div>
            </div>
        </div>
        
        <!-- Tabs for different sections -->
        <div style="background: white; border-radius: 12px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); overflow: hidden;">
            <div style="border-bottom: 1px solid #eee;">
                <div style="display: flex; padding: 0;">
                    <button class="tab-button active" onclick="showTab('orders')">
                        <i class="fas fa-shopping-bag"></i> Order History (<?= count($recent_orders) ?>)
                    </button>
                    <button class="tab-button" onclick="showTab('reviews')">
                        <i class="fas fa-star"></i> Reviews (<?= count($reviews) ?>)
                    </button>
                    <button class="tab-button" onclick="showTab('activity')">
                        <i class="fas fa-chart-bar"></i> Customer Activity
                    </button>
                </div>
            </div>
            
            <!-- Orders Tab -->
            <div id="orders-tab" class="tab-content">
                <?php if (empty($recent_orders)): ?>
                    <div style="padding: 40px; text-align: center; color: #999;">
                        <i class="fas fa-shopping-bag fa-3x mb-3"></i>
                        <p>No orders found for this customer</p>
                    </div>
                <?php else: ?>
                    <div style="padding: 20px;">
                        <div style="overflow-x: auto;">
                            <table style="width: 100%; border-collapse: collapse;">
                                <thead>
                                    <tr style="background: #f8f9fa;">
                                        <th style="padding: 12px 15px; text-align: left; font-size: 13px; font-weight: 600; color: #666;">Order #</th>
                                        <th style="padding: 12px 15px; text-align: left; font-size: 13px; font-weight: 600; color: #666;">Item</th>
                                        <th style="padding: 12px 15px; text-align: left; font-size: 13px; font-weight: 600; color: #666;">Vendor</th>
                                        <th style="padding: 12px 15px; text-align: left; font-size: 13px; font-weight: 600; color: #666;">Rental Period</th>
                                        <th style="padding: 12px 15px; text-align: left; font-size: 13px; font-weight: 600; color: #666;">Amount</th>
                                        <th style="padding: 12px 15px; text-align: left; font-size: 13px; font-weight: 600; color: #666;">Status</th>
                                        <th style="padding: 12px 15px; text-align: left; font-size: 13px; font-weight: 600; color: #666;">Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($recent_orders as $order): ?>
                                        <tr style="border-bottom: 1px solid #f0f0f0;">
                                            <td style="padding: 15px;">
                                                <strong><?= Security::escapeOutput($order['order_number']) ?></strong>
                                            </td>
                                            <td style="padding: 15px;">
                                                <div style="display: flex; align-items: center; gap: 10px;">
                                                    <img src="<?= upload('items/' . $order['item_image']) ?>" 
                                                         alt="<?= Security::escapeOutput($order['item_title']) ?>"
                                                         style="width: 40px; height: 40px; object-fit: cover; border-radius: 5px;">
                                                    <div>
                                                        <strong><?= Security::escapeOutput($order['item_title']) ?></strong><br>
                                                        <small style="color: #666;"><?= $order['rental_days'] ?> days</small>
                                                    </div>
                                                </div>
                                            </td>
                                            <td style="padding: 15px;">
                                                <?= Security::escapeOutput($order['vendor_name']) ?>
                                            </td>
                                            <td style="padding: 15px;">
                                                <small>
                                                    <?= format_date($order['start_date'], 'M d') ?> - 
                                                    <?= format_date($order['end_date'], 'M d') ?>
                                                </small>
                                            </td>
                                            <td style="padding: 15px;">
                                                <strong><?= format_money($order['total_amount']) ?></strong>
                                            </td>
                                            <td style="padding: 15px;">
                                                <?php
                                                $status_class = '';
                                                switch($order['status']) {
                                                    case 'pending':
                                                        $status_class = 'status-pending';
                                                        break;
                                                    case 'confirmed':
                                                        $status_class = 'status-approved';
                                                        break;
                                                    case 'ongoing':
                                                        $status_class = 'status-info';
                                                        break;
                                                    case 'completed':
                                                        $status_class = 'status-success';
                                                        break;
                                                    case 'cancelled':
                                                        $status_class = 'status-rejected';
                                                        break;
                                                    case 'returned':
                                                        $status_class = 'status-info';
                                                        break;
                                                }
                                                ?>
                                                <span class="status-badge <?= $status_class ?>">
                                                    <?= ucfirst($order['status']) ?>
                                                </span>
                                            </td>
                                            <td style="padding: 15px;">
                                                <small><?= format_date($order['created_at'], 'M d, Y') ?></small>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
            
            <!-- Reviews Tab -->
            <div id="reviews-tab" class="tab-content" style="display: none;">
                <?php if (empty($reviews)): ?>
                    <div style="padding: 40px; text-align: center; color: #999;">
                        <i class="fas fa-star fa-3x mb-3"></i>
                        <p>No reviews found for this customer</p>
                    </div>
                <?php else: ?>
                    <div style="padding: 20px;">
                        <?php foreach ($reviews as $review): ?>
                            <div style="border-bottom: 1px solid #f0f0f0; padding: 20px 0;">
                                <div style="display: flex; justify-content: between; align-items: flex-start; margin-bottom: 10px;">
                                    <div style="flex: 1;">
                                        <div style="display: flex; align-items: center; gap: 10px; margin-bottom: 5px;">
                                            <strong><?= Security::escapeOutput($review['item_title']) ?></strong>
                                            <div style="color: #ffc107;">
                                                <?php for ($i = 1; $i <= 5; $i++): ?>
                                                    <i class="fas fa-star <?= $i <= $review['rating'] ? '' : 'text-light' ?>"></i>
                                                <?php endfor; ?>
                                            </div>
                                            <small style="color: #666;"><?= format_date($review['created_at'], 'M d, Y') ?></small>
                                        </div>
                                        <div style="font-size: 14px; margin-bottom: 8px;">
                                            <strong>Vendor:</strong> <?= Security::escapeOutput($review['vendor_name']) ?>
                                        </div>
                                    </div>
                                </div>
                                <p style="margin: 0; line-height: 1.6;">
                                    <?= Security::escapeOutput($review['review']) ?>
                                </p>
                                <?php if (!empty($review['vendor_reply'])): ?>
                                    <div style="margin-top: 15px; padding: 15px; background: #f8f9fa; border-radius: 8px; border-left: 3px solid var(--primary);">
                                        <strong>Vendor Reply:</strong>
                                        <p style="margin: 5px 0 0 0; line-height: 1.5;">
                                            <?= Security::escapeOutput($review['vendor_reply']) ?>
                                        </p>
                                        <small style="color: #666;">
                                            <?= format_date($review['vendor_replied_at'], 'M d, Y h:i A') ?>
                                        </small>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
            
            <!-- Activity Tab -->
            <div id="activity-tab" class="tab-content" style="display: none;">
                <div style="padding: 20px;">
                    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin-bottom: 30px;">
                        <div style="background: #f8f9fa; padding: 20px; border-radius: 8px; text-align: center;">
                            <div style="font-size: 24px; font-weight: 700; color: var(--primary);"><?= $lifetime['days_since_first_order'] ?? 0 ?></div>
                            <div style="font-size: 12px; color: #666;">Days as Customer</div>
                        </div>
                        
                        <div style="background: #f8f9fa; padding: 20px; border-radius: 8px; text-align: center;">
                            <div style="font-size: 24px; font-weight: 700; color: var(--success);"><?= $lifetime['order_count'] ?? 0 ?></div>
                            <div style="font-size: 12px; color: #666;">Completed Orders</div>
                        </div>
                        
                        <div style="background: #f8f9fa; padding: 20px; border-radius: 8px; text-align: center;">
                            <div style="font-size: 24px; font-weight: 700; color: var(--warning);"><?= $stats['cancelled_orders'] ?></div>
                            <div style="font-size: 12px; color: #666;">Cancelled Orders</div>
                        </div>
                        
                        <div style="background: #f8f9fa; padding: 20px; border-radius: 8px; text-align: center;">
                            <div style="font-size: 24px; font-weight: 700; color: var(--info);">
                                <?= $lifetime['days_since_first_order'] > 0 ? round($lifetime['order_count'] / ($lifetime['days_since_first_order'] / 30), 1) : 0 ?>
                            </div>
                            <div style="font-size: 12px; color: #666;">Orders/Month</div>
                        </div>
                    </div>
                    
                    <!-- Customer Insights -->
                    <div style="background: #f8f9fa; padding: 20px; border-radius: 8px; margin-bottom: 20px;">
                        <h4 style="margin-bottom: 15px; color: var(--dark);">Customer Insights</h4>
                        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 15px;">
                            <div>
                                <strong>Customer Value:</strong>
                                <?php
                                $total_spent = $stats['total_spent'];
                                if ($total_spent >= 100000) {
                                    $value = 'High Value';
                                    $color = 'var(--success)';
                                } elseif ($total_spent >= 50000) {
                                    $value = 'Medium Value';
                                    $color = 'var(--warning)';
                                } else {
                                    $value = 'Low Value';
                                    $color = 'var(--info)';
                                }
                                ?>
                                <span style="color: <?= $color ?>; font-weight: 600;"><?= $value ?></span>
                            </div>
                            
                            <div>
                                <strong>Order Frequency:</strong>
                                <?php
                                if ($lifetime['order_count'] >= 10) {
                                    $frequency = 'Frequent';
                                    $color = 'var(--success)';
                                } elseif ($lifetime['order_count'] >= 5) {
                                    $frequency = 'Regular';
                                    $color = 'var(--warning)';
                                } else {
                                    $frequency = 'Occasional';
                                    $color = 'var(--info)';
                                }
                                ?>
                                <span style="color: <?= $color ?>; font-weight: 600;"><?= $frequency ?></span>
                            </div>
                            
                            <div>
                                <strong>Customer Satisfaction:</strong>
                                <?php
                                $avg_rating = $stats['avg_rating'];
                                if ($avg_rating >= 4.5) {
                                    $satisfaction = 'Excellent';
                                    $color = 'var(--success)';
                                } elseif ($avg_rating >= 4.0) {
                                    $satisfaction = 'Good';
                                    $color = 'var(--warning)';
                                } else {
                                    $satisfaction = 'Needs Improvement';
                                    $color = 'var(--danger)';
                                }
                                ?>
                                <span style="color: <?= $color ?>; font-weight: 600;"><?= $satisfaction ?></span>
                            </div>
                            
                            <div>
                                <strong>Payment Behavior:</strong>
                                <?php
                                $completed = $stats['completed_orders'];
                                $cancelled = $stats['cancelled_orders'];
                                $total_attempts = $completed + $cancelled;
                                $success_rate = $total_attempts > 0 ? ($completed / $total_attempts) * 100 : 0;
                                
                                if ($success_rate >= 90) {
                                    $behavior = 'Excellent';
                                    $color = 'var(--success)';
                                } elseif ($success_rate >= 75) {
                                    $behavior = 'Good';
                                    $color = 'var(--warning)';
                                } else {
                                    $behavior = 'Risky';
                                    $color = 'var(--danger)';
                                }
                                ?>
                                <span style="color: <?= $color ?>; font-weight: 600;"><?= $behavior ?></span>
                                <small style="display: block; color: #666;">(<?= round($success_rate, 1) ?>% success rate)</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Action Buttons -->
        <div style="margin-top: 30px; text-align: center;">
            <div style="display: flex; justify-content: center; gap: 15px;">
                <?php if ($customer['user_status'] === 'active'): ?>
                    <button onclick="suspendCustomer(<?= $customer['id'] ?>)" 
                            style="background: var(--warning); color: white; border: none; padding: 12px 24px; border-radius: 6px; font-weight: 600; cursor: pointer;">
                        <i class="fas fa-ban"></i> Suspend Customer
                    </button>
                <?php else: ?>
                    <button onclick="activateCustomer(<?= $customer['id'] ?>)" 
                            style="background: var(--success); color: white; border: none; padding: 12px 24px; border-radius: 6px; font-weight: 600; cursor: pointer;">
                        <i class="fas fa-check"></i> Activate Customer
                    </button>
                <?php endif; ?>
                
                <button onclick="sendMessage(<?= $customer['id'] ?>)" 
                        style="background: var(--primary); color: white; border: none; padding: 12px 24px; border-radius: 6px; font-weight: 600; cursor: pointer;">
                    <i class="fas fa-envelope"></i> Send Message
                </button>
                
                <button onclick="exportCustomerData(<?= $customer['id'] ?>)" 
                        style="background: var(--info); color: white; border: none; padding: 12px 24px; border-radius: 6px; font-weight: 600; cursor: pointer;">
                    <i class="fas fa-download"></i> Export Data
                </button>
            </div>
        </div>
    </div>
</div>

<style>
.tab-button {
    background: none;
    border: none;
    padding: 15px 20px;
    font-weight: 600;
    cursor: pointer;
    border-bottom: 3px solid transparent;
    color: #666;
    transition: all 0.3s;
    font-size: 14px;
}

.tab-button:hover {
    color: var(--primary);
    background: #f8f9fa;
}

.tab-button.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    background: #f8f9fa;
}

.status-pending {
    background: var(--warning);
    color: white;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
}

.status-approved {
    background: var(--success);
    color: white;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
}

.status-rejected {
    background: var(--danger);
    color: white;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
}

.status-info {
    background: var(--info);
    color: white;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
}

.status-success {
    background: var(--success);
    color: white;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
}

.text-light {
    color: #ccc !important;
}
</style>

<script>
function showTab(tabName) {
    // Hide all tabs
    const tabs = ['orders', 'reviews', 'activity'];
    tabs.forEach(tab => {
        const tabElement = document.getElementById(tab + '-tab');
        const buttonElement = document.querySelector(`[onclick="showTab('${tab}')"]`);
        if (tabElement) tabElement.style.display = 'none';
        if (buttonElement) buttonElement.classList.remove('active');
    });
    
    // Show selected tab
    const selectedTab = document.getElementById(tabName + '-tab');
    const selectedButton = document.querySelector(`[onclick="showTab('${tabName}')"]`);
    if (selectedTab) selectedTab.style.display = 'block';
    if (selectedButton) selectedButton.classList.add('active');
}

function suspendCustomer(customerId) {
    if (confirm('Are you sure you want to suspend this customer account?')) {
        window.location.href = 'customer-suspend.php?id=' + customerId;
    }
}

function activateCustomer(customerId) {
    if (confirm('Are you sure you want to activate this customer account?')) {
        window.location.href = 'customer-activate.php?id=' + customerId;
    }
}

function sendMessage(customerId) {
    // Implementation for sending message to customer
    alert('Message feature coming soon!');
}

function exportCustomerData(customerId) {
    // Implementation for exporting customer data
    window.location.href = 'customer-export.php?id=' + customerId;
}
</script>

<?php include '../templates/layouts/admin/footer.php'; ?>