<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
include "../config/db.php";

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

// Get user addresses
$user_query = mysqli_query($conn, "SELECT * FROM users WHERE id='$user_id'");
$user = mysqli_fetch_assoc($user_query);

// Add new address
if (isset($_POST['add_address'])) {
    $address_type = mysqli_real_escape_string($conn, $_POST['address_type']);
    $full_name = mysqli_real_escape_string($conn, $_POST['full_name']);
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $landmark = mysqli_real_escape_string($conn, $_POST['landmark']);
    $city = mysqli_real_escape_string($conn, $_POST['city']);
    $state = mysqli_real_escape_string($conn, $_POST['state']);
    $pincode = mysqli_real_escape_string($conn, $_POST['pincode']);
    
    // In a real application, you would have a separate addresses table
    // For now, we'll update the main user address
    if ($address_type == 'primary') {
        $update_query = "UPDATE users SET 
                        name='$full_name', 
                        phone='$phone', 
                        address='$address, $landmark, $city, $state - $pincode' 
                        WHERE id='$user_id'";
        
        if (mysqli_query($conn, $update_query)) {
            $_SESSION['user_name'] = $full_name;
            $success = "Primary address updated successfully!";
            $user_query = mysqli_query($conn, "SELECT * FROM users WHERE id='$user_id'");
            $user = mysqli_fetch_assoc($user_query);
        }
    }
}

// Set default address
if (isset($_GET['set_default'])) {
    // In a real application, you would update a default flag in addresses table
    $success = "Default address updated!";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Addresses - ShopNest</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/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 href="../assets/css/style.css" rel="stylesheet">
    <style>
        .address-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 20px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            border: 2px solid transparent;
            transition: all 0.3s;
            word-wrap: break-word;
            overflow-wrap: break-word;
            word-break: break-all;
            overflow: hidden;
            position: relative;
            z-index: 1;
        }
        .address-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
            z-index: 2;
        }
        .address-card.default {
            border-color: #ff6b35;
            background: linear-gradient(45deg, rgba(255,107,53,0.05), rgba(52,152,219,0.05));
        }
        .address-type {
            display: inline-block;
            padding: 5px 15px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: 600;
            margin-bottom: 10px;
        }
        .type-home { background: #d4edda; color: #155724; }
        .type-work { background: #cce5ff; color: #004085; }
        .type-other { background: #e2e3e5; color: #383d41; }
        .empty-state {
            text-align: center;
            padding: 60px 20px;
        }
        .empty-state i {
            font-size: 4rem;
            color: #ddd;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <?php include "../includes/header.php"; ?>

    <div class="container mt-4">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="../index.php">Home</a></li>
                <li class="breadcrumb-item"><a href="profile.php">My Account</a></li>
                <li class="breadcrumb-item active">My Addresses</li>
            </ol>
        </nav>

        <div class="row">
            <!-- Sidebar -->
            <!-- Sidebar -->
            <?php include "includes/sidebar.php"; ?>

            <!-- Addresses Content -->
            <div class="col-md-9">
                <div class="profile-content" style="background: white; border-radius: 15px; padding: 30px; box-shadow: 0 5px 15px rgba(0,0,0,0.05);">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h3 style="color: #2c3e50;">
                            <i class="fas fa-map-marker-alt me-2"></i>My Addresses
                        </h3>
                        <button type="button" class="btn btn-orange" data-bs-toggle="modal" data-bs-target="#addAddressModal">
                            <i class="fas fa-plus me-2"></i>Add New Address
                        </button>
                    </div>

                    <?php if(isset($success)): ?>
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="fas fa-check-circle me-2"></i>
                            <?php echo $success; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>

                    <!-- Current Addresses -->
                    <div class="row">
                        <!-- Primary Address -->
                        <div class="col-md-6 mb-4 d-flex">
                            <div class="address-card default flex-fill">
                                <div class="d-flex justify-content-between align-items-start mb-3">
                                    <span class="address-type type-home">
                                        <i class="fas fa-home me-1"></i>Home
                                    </span>
                                    <span class="badge bg-success">Default</span>
                                </div>
                                
                                <h6 class="mb-2"><?php echo htmlspecialchars($user['name']); ?></h6>
                                <p class="text-muted mb-2">
                                    <?php if(!empty($user['phone'])): ?>
                                        <i class="fas fa-phone me-1"></i><?php echo htmlspecialchars($user['phone']); ?><br>
                                    <?php endif; ?>
                                    <?php if(!empty($user['address'])): ?>
                                        <i class="fas fa-map-marker-alt me-1"></i><?php echo htmlspecialchars($user['address']); ?>
                                    <?php else: ?>
                                        <i class="fas fa-map-marker-alt me-1"></i>No address saved yet
                                    <?php endif; ?>
                                </p>
                                
                                <div class="mt-3">
                                    <button class="btn btn-sm btn-outline-primary me-2" data-bs-toggle="modal" data-bs-target="#editAddressModal">
                                        <i class="fas fa-edit me-1"></i>Edit
                                    </button>
                                    <button class="btn btn-sm btn-outline-secondary" disabled>
                                        <i class="fas fa-star me-1"></i>Default
                                    </button>
                                </div>
                            </div>
                        </div>

                        <!-- Add New Address Card -->
                        <div class="col-md-6 mb-4 d-flex">
                            <div class="address-card text-center d-flex flex-column justify-content-center align-items-center flex-fill"
                                 style="border: 2px dashed #ddd; cursor: pointer; min-height: 250px;"
                                 data-bs-toggle="modal" data-bs-target="#addAddressModal">
                                <i class="fas fa-plus fa-3x mb-3" style="color: #ddd;"></i>
                                <h5 style="color: #6c757d;">Add New Address</h5>
                                <p class="text-muted">Click to add a new delivery address</p>
                            </div>
                        </div>
                    </div>

                    <!-- Address Instructions -->
                    <div class="card mt-4">
                        <div class="card-body">
                            <h6><i class="fas fa-info-circle me-2"></i>Address Guidelines</h6>
                            <ul class="mb-0">
                                <li>Ensure your address is accurate for smooth delivery</li>
                                <li>Include landmark details for easy location</li>
                                <li>Provide a working phone number for delivery updates</li>
                                <li>You can have multiple addresses for different purposes</li>
                                <li>Set one address as default for quicker checkout</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Add Address Modal -->
    <div class="modal fade" id="addAddressModal" tabindex="-1" aria-labelledby="addAddressModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addAddressModalLabel">
                        <i class="fas fa-plus me-2"></i>Add New Address
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <form method="POST">
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Address Type</label>
                                <select name="address_type" class="form-select" required>
                                    <option value="home">Home</option>
                                    <option value="work">Work</option>
                                    <option value="other">Other</option>
                                </select>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Full Name</label>
                                <input type="text" name="full_name" class="form-control" required
                                       value="<?php echo htmlspecialchars($user['name']); ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Phone Number</label>
                                <input type="tel" name="phone" class="form-control" required
                                       value="<?php echo htmlspecialchars($user['phone'] ?? ''); ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Pincode</label>
                                <input type="text" name="pincode" class="form-control" required>
                            </div>
                            <div class="col-md-12 mb-3">
                                <label class="form-label">Address (House No, Building, Street)</label>
                                <textarea name="address" class="form-control" rows="2" required><?php echo htmlspecialchars($user['address'] ?? ''); ?></textarea>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Landmark</label>
                                <input type="text" name="landmark" class="form-control">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">City</label>
                                <input type="text" name="city" class="form-control" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">State</label>
                                <input type="text" name="state" class="form-control" required>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" name="add_address" class="btn btn-orange">Save Address</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Edit Address Modal -->
    <div class="modal fade" id="editAddressModal" tabindex="-1" aria-labelledby="editAddressModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editAddressModalLabel">
                        <i class="fas fa-edit me-2"></i>Edit Address
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <form method="POST">
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Address Type</label>
                                <select name="address_type" class="form-select" required>
                                    <option value="primary" selected>Primary</option>
                                </select>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Full Name</label>
                                <input type="text" name="full_name" class="form-control" required
                                       value="<?php echo htmlspecialchars($user['name']); ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Phone Number</label>
                                <input type="tel" name="phone" class="form-control" required
                                       value="<?php echo htmlspecialchars($user['phone'] ?? ''); ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Pincode</label>
                                <input type="text" name="pincode" class="form-control" required>
                            </div>
                            <div class="col-md-12 mb-3">
                                <label class="form-label">Address</label>
                                <textarea name="address" class="form-control" rows="2" required><?php echo htmlspecialchars($user['address'] ?? ''); ?></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" name="add_address" class="btn btn-orange">Update Address</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <?php include "../includes/footer.php"; ?>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>