<?php
// profile.php
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/functions.php';

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

$user_id = $_SESSION['user_id'];

// Get user data
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();

// Get orders
$stmt = $pdo->prepare("SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC");
$stmt->execute([$user_id]);
$orders = $stmt->fetchAll();

// Update profile
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_profile'])) {
    $name = sanitize($_POST['name'] ?? '');
    $phone = sanitize($_POST['phone'] ?? '');
    $address = sanitize($_POST['address'] ?? '');
    $city = sanitize($_POST['city'] ?? '');
    
    $stmt = $pdo->prepare("UPDATE users SET name = ?, phone = ?, address = ?, city = ? WHERE id = ?");
    $stmt->execute([$name, $phone, $address, $city, $user_id]);
    $_SESSION['user_name'] = $name;
    $user = get_user($pdo, $user_id);
    $success = "Profile updated successfully";
}

include __DIR__ . '/includes/header.php';
?>

<div class="container py-4">
    <div class="row">
        <div class="col-lg-3">
            <div class="profile-sidebar">
                <div class="profile-avatar">
                    <img src="assets/images/default-avatar.png" alt="Profile">
                    <h5><?php echo $user['name']; ?></h5>
                    <p><?php echo $user['email']; ?></p>
                </div>
                <ul class="profile-menu">
                    <li class="active"><a href="profile.php"><i class="fa-solid fa-dashboard"></i> Dashboard</a></li>
                    <li><a href="profile.php?tab=orders"><i class="fa-solid fa-shopping-bag"></i> Orders</a></li>
                    <li><a href="profile.php?tab=profile"><i class="fa-regular fa-user"></i> Profile</a></li>
                    <li><a href="logout.php"><i class="fa-solid fa-sign-out-alt"></i> Logout</a></li>
                </ul>
            </div>
        </div>
        <div class="col-lg-9">
            <?php if (isset($success)): ?>
            <div class="alert alert-success"><?php echo $success; ?></div>
            <?php endif; ?>
            
            <?php $tab = $_GET['tab'] ?? 'orders'; ?>
            
            <?php if ($tab === 'orders'): ?>
            <div class="profile-content">
                <h4>My Orders</h4>
                <hr>
                <?php if (empty($orders)): ?>
                <p class="text-muted">No orders found.</p>
                <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Invoice</th>
                                <th>Date</th>
                                <th>Total</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($orders as $order): ?>
                            <tr>
                                <td><?php echo $order['invoice_no']; ?></td>
                                <td><?php echo date('d M Y', strtotime($order['created_at'])); ?></td>
                                <td><?php echo number_format($order['total']); ?> TK</td>
                                <td>
                                    <span class="badge bg-<?php echo $order['order_status'] == 'delivered' ? 'success' : ($order['order_status'] == 'cancelled' ? 'danger' : 'warning'); ?>">
                                        <?php echo ucfirst($order['order_status']); ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="order-tracking.php?invoice=<?php echo $order['invoice_no']; ?>" class="btn btn-sm btn-primary">View</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>
            
            <?php if ($tab === 'profile'): ?>
            <div class="profile-content">
                <h4>Profile Information</h4>
                <hr>
                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label">Full Name</label>
                        <input type="text" name="name" class="form-control" value="<?php echo $user['name']; ?>" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Email</label>
                        <input type="email" class="form-control" value="<?php echo $user['email']; ?>" disabled>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Phone</label>
                        <input type="text" name="phone" class="form-control" value="<?php echo $user['phone']; ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Address</label>
                        <textarea name="address" class="form-control" rows="3"><?php echo $user['address']; ?></textarea>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">City</label>
                        <input type="text" name="city" class="form-control" value="<?php echo $user['city']; ?>">
                    </div>
                    <button type="submit" name="update_profile" class="btn btn-primary">Update Profile</button>
                </form>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<style>
.profile-sidebar {
    background: #fff;
    border-radius: 12px;
    padding: 25px;
    text-align: center;
    border: 1px solid #e2e8f0;
}

.profile-avatar img {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    object-fit: cover;
    margin-bottom: 15px;
}

.profile-avatar h5 {
    margin: 0;
}

.profile-avatar p {
    color: #94a3b8;
    font-size: 14px;
}

.profile-menu {
    list-style: none;
    padding: 0;
    margin: 20px 0 0;
    text-align: left;
}

.profile-menu li {
    border-bottom: 1px solid #f1f5f9;
}

.profile-menu li:last-child {
    border-bottom: none;
}

.profile-menu a {
    display: block;
    padding: 10px 0;
    color: #64748b;
    transition: all 0.3s ease;
}

.profile-menu a:hover,
.profile-menu li.active a {
    color: #047dc3;
}

.profile-menu a i {
    width: 25px;
}

.profile-content {
    background: #fff;
    border-radius: 12px;
    padding: 25px;
    border: 1px solid #e2e8f0;
}
</style>

<?php include __DIR__ . '/includes/footer.php'; ?>