<?php
// admin/view-customer.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';

if (!isAdmin()) {
    header('Location: login.php');
    exit;
}

$id = intval($_GET['id'] ?? 0);
if (!$id) {
    header('Location: customers.php');
    exit;
}

// Get customer details
$stmt = $pdo->prepare("SELECT c.*, 
                       (SELECT COUNT(*) FROM orders WHERE customer_id = c.id) as order_count,
                       (SELECT COALESCE(SUM(total), 0) FROM orders WHERE customer_id = c.id) as total_spent,
                       (SELECT MAX(created_at) FROM orders WHERE customer_id = c.id) as last_order
                       FROM customers c 
                       WHERE c.id = ?");
$stmt->execute([$id]);
$customer = $stmt->fetch();

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

// Get customer orders
$stmt = $pdo->prepare("SELECT o.*, 
                       (SELECT COUNT(*) FROM order_items WHERE order_id = o.id) as item_count 
                       FROM orders o 
                       WHERE o.customer_id = ? 
                       ORDER BY o.created_at DESC");
$stmt->execute([$id]);
$orders = $stmt->fetchAll();

include 'header.php';
?>

<style>
.customer-profile {
    background: #fff;
    border-radius: 12px;
    padding: 25px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    margin-bottom: 20px;
}

.profile-header {
    display: flex;
    align-items: center;
    gap: 20px;
    padding-bottom: 20px;
    border-bottom: 2px solid #e2e8f0;
    flex-wrap: wrap;
}

.profile-avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: #047dc3;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    font-weight: 700;
    flex-shrink: 0;
}

.profile-info h2 {
    font-size: 24px;
    font-weight: 700;
    margin: 0;
}

.profile-info .customer-code {
    color: #94a3b8;
    font-size: 14px;
}

.profile-info .customer-status {
    margin-top: 5px;
}

.status-badge {
    padding: 4px 14px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
}

.status-badge.active {
    background: #86efac;
    color: #166534;
}

.status-badge.inactive {
    background: #fca5a5;
    color: #991b1b;
}

.profile-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 15px;
    margin-top: 20px;
}

.stat-item {
    background: #f8fafc;
    border-radius: 10px;
    padding: 15px;
    text-align: center;
}

.stat-item .stat-number {
    font-size: 24px;
    font-weight: 700;
}

.stat-item .stat-label {
    font-size: 13px;
    color: #64748b;
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 15px;
    margin-top: 20px;
}

.info-card {
    background: #f8fafc;
    border-radius: 10px;
    padding: 15px 20px;
}

.info-card h6 {
    font-weight: 600;
    color: #64748b;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
    padding-bottom: 6px;
    border-bottom: 1px solid #e2e8f0;
}

.info-card p {
    margin-bottom: 3px;
    font-size: 14px;
}

.info-card .label {
    color: #64748b;
    font-weight: 500;
}

.order-table td {
    vertical-align: middle;
}

.order-status {
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 500;
}

.order-status.pending { background: #fcd34d; color: #92400e; }
.order-status.processing { background: #93c5fd; color: #1e40af; }
.order-status.shipped { background: #86efac; color: #166534; }
.order-status.delivered { background: #22c55e; color: #fff; }
.order-status.cancelled { background: #fca5a5; color: #991b1b; }

.action-buttons {
    display: flex;
    gap: 10px;
    margin-top: 20px;
    flex-wrap: wrap;
}

.btn-print {
    background: #1e293b;
    color: #fff;
    border: none;
    padding: 8px 20px;
    border-radius: 8px;
    transition: all 0.3s ease;
}

.btn-print:hover {
    background: #0f172a;
    color: #fff;
}
</style>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fa-solid fa-user me-2"></i> Customer Profile</h2>
        <div>
            <a href="edit-customer.php?id=<?php echo $customer['id']; ?>" class="btn btn-warning">
                <i class="fa-solid fa-edit"></i> Edit
            </a>
            <a href="customers.php" class="btn btn-secondary">
                <i class="fa-solid fa-arrow-left"></i> Back
            </a>
        </div>
    </div>
    
    <!-- Profile -->
    <div class="customer-profile">
        <div class="profile-header">
            <div class="profile-avatar">
                <?php echo strtoupper(substr($customer['name'], 0, 1)); ?>
            </div>
            <div class="profile-info">
                <h2><?php echo htmlspecialchars($customer['name']); ?></h2>
                <div class="customer-code"><?php echo $customer['customer_code']; ?></div>
                <div class="customer-status">
                    <span class="status-badge <?php echo $customer['status'] ? 'active' : 'inactive'; ?>">
                        <?php echo $customer['status'] ? 'Active' : 'Inactive'; ?>
                    </span>
                </div>
            </div>
        </div>
        
        <!-- Stats -->
        <div class="profile-stats">
            <div class="stat-item">
                <div class="stat-number text-primary"><?php echo $customer['order_count']; ?></div>
                <div class="stat-label">Total Orders</div>
            </div>
            <div class="stat-item">
                <div class="stat-number text-success"><?php echo number_format($customer['total_spent']); ?> TK</div>
                <div class="stat-label">Total Spent</div>
            </div>
            <div class="stat-item">
                <div class="stat-number text-warning">
                    <?php 
                    if ($customer['last_order']) {
                        echo date('d M Y', strtotime($customer['last_order']));
                    } else {
                        echo '-';
                    }
                    ?>
                </div>
                <div class="stat-label">Last Order</div>
            </div>
        </div>
        
        <!-- Contact Info -->
        <div class="info-grid">
            <div class="info-card">
                <h6><i class="fa-solid fa-phone me-2"></i> Contact</h6>
                <p><span class="label">Phone:</span> <?php echo htmlspecialchars($customer['phone']); ?></p>
                <p><span class="label">Email:</span> <?php echo htmlspecialchars($customer['email'] ?? 'N/A'); ?></p>
            </div>
            <div class="info-card">
                <h6><i class="fa-solid fa-location-dot me-2"></i> Address</h6>
                <p><?php echo htmlspecialchars($customer['address'] ?? 'N/A'); ?></p>
                <?php if ($customer['city']): ?>
                <p><span class="label">City:</span> <?php echo htmlspecialchars($customer['city']); ?></p>
                <?php endif; ?>
            </div>
            <div class="info-card">
                <h6><i class="fa-solid fa-calendar me-2"></i> Account Info</h6>
                <p><span class="label">Joined:</span> <?php echo date('d M Y, h:i A', strtotime($customer['created_at'])); ?></p>
                <?php if ($customer['last_order']): ?>
                <p><span class="label">Last Active:</span> <?php echo date('d M Y', strtotime($customer['last_order'])); ?></p>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <!-- Orders -->
    <div class="admin-card">
        <h5 class="card-title">
            <i class="fa-solid fa-shopping-cart me-2"></i> Order History
            <span class="badge bg-primary ms-2"><?php echo count($orders); ?></span>
        </h5>
        
        <?php if (empty($orders)): ?>
        <p class="text-muted text-center py-3">No orders found for this customer</p>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table table-hover order-table">
                <thead>
                    <tr>
                        <th>Invoice</th>
                        <th>Date</th>
                        <th>Items</th>
                        <th>Total</th>
                        <th>Status</th>
                        <th>Payment</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($orders as $order): ?>
                    <tr>
                        <td><code><?php echo $order['invoice_no']; ?></code></td>
                        <td><?php echo date('d M Y', strtotime($order['created_at'])); ?></td>
                        <td><?php echo $order['item_count']; ?></td>
                        <td class="fw-bold text-success"><?php echo number_format($order['total']); ?> TK</td>
                        <td>
                            <span class="order-status <?php echo $order['order_status']; ?>">
                                <?php echo ucfirst($order['order_status']); ?>
                            </span>
                        </td>
                        <td>
                            <span class="badge bg-<?php echo $order['payment_status'] == 'paid' ? 'success' : 'warning'; ?>">
                                <?php echo ucfirst($order['payment_status']); ?>
                            </span>
                        </td>
                        <td>
                            <a href="order-details.php?id=<?php echo $order['id']; ?>" class="btn btn-sm btn-primary">
                                <i class="fa-solid fa-eye"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
    
    <!-- Action Buttons -->
    <div class="action-buttons">
        <a href="edit-customer.php?id=<?php echo $customer['id']; ?>" class="btn btn-warning">
            <i class="fa-solid fa-edit"></i> Edit Customer
        </a>
        <button onclick="window.print()" class="btn btn-print">
            <i class="fa-solid fa-print"></i> Print Profile
        </button>
        <a href="customers.php" class="btn btn-secondary">
            <i class="fa-solid fa-arrow-left"></i> Back to List
        </a>
    </div>
</div>

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