<?php
require_once __DIR__ . '/../functions.php';
requireAuth();
$page_title = 'My Profile';
$db = db();
$user = getCurrentUser();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $address = trim($_POST['address'] ?? '');
    $city = trim($_POST['city'] ?? '');
    $state = trim($_POST['state'] ?? '');
    $pincode = trim($_POST['pincode'] ?? '');
    
    $db->prepare("UPDATE users SET name=?, phone=?, address=?, city=?, state=?, pincode=? WHERE id=?")
        ->execute([$name, $phone, $address, $city, $state, $pincode, $user['id']]);
    
    $_SESSION['user_name'] = $name;
    flash('success', 'Profile updated successfully!');
    header('Location: profile.php');
    exit;
}

require_once __DIR__ . '/../includes/header.php';
?>
<div class="max-w-2xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
    <h1 class="text-2xl font-bold text-gray-800 mb-6">My Profile</h1>
    <form method="POST" class="bg-white rounded-xl border border-gray-200 p-6 space-y-4">
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div><label class="block text-sm font-medium text-gray-700 mb-1">Name</label><input type="text" name="name" value="<?= h($user['name']) ?>" required class="w-full border border-gray-300 rounded-lg px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary-500"></div>
            <div><label class="block text-sm font-medium text-gray-700 mb-1">Email</label><input type="email" value="<?= h($user['email']) ?>" disabled class="w-full border border-gray-200 rounded-lg px-3 py-2 bg-gray-50"></div>
            <div><label class="block text-sm font-medium text-gray-700 mb-1">Phone</label><input type="text" name="phone" value="<?= h($user['phone']) ?>" class="w-full border border-gray-300 rounded-lg px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary-500"></div>
            <div><label class="block text-sm font-medium text-gray-700 mb-1">Pincode</label><input type="text" name="pincode" id="pincode-input" value="<?= h($user['pincode']) ?>" maxlength="6" class="w-full border border-gray-300 rounded-lg px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary-500" oninput="autoFillLocation(this.value)"><span id="pincode-status" class="text-xs"></span></div>
        </div>
        <div><label class="block text-sm font-medium text-gray-700 mb-1">Address</label><textarea name="address" rows="2" class="w-full border border-gray-300 rounded-lg px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary-500"><?= h($user['address']) ?></textarea></div>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div><label class="block text-sm font-medium text-gray-700 mb-1">City</label><input type="text" name="city" id="city-input" value="<?= h($user['city']) ?>" class="w-full border border-gray-300 rounded-lg px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary-500"></div>
            <div><label class="block text-sm font-medium text-gray-700 mb-1">State</label><input type="text" name="state" id="state-input" value="<?= h($user['state']) ?>" class="w-full border border-gray-300 rounded-lg px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary-500"></div>
        </div>
        <button type="submit" class="bg-primary-600 text-white px-6 py-2.5 rounded-lg font-semibold hover:bg-primary-700 transition">Update Profile</button>
    </form>
</div>
<script>
let pincodeTimer;
function autoFillLocation(pincode) {
    const status = document.getElementById('pincode-status');
    if (status) status.textContent = '';
    if (pincode.length !== 6) return;
    clearTimeout(pincodeTimer);
    pincodeTimer = setTimeout(async () => {
        if (status) status.innerHTML = ' <i class="fas fa-spinner fa-spin text-gray-400"></i>';
        try {
            const r = await fetch('<?= BASE_PATH ?>/api/pincode-lookup.php?pincode=' + pincode);
            const d = await r.json();
            if (d.success) {
                document.getElementById('city-input').value = d.city;
                document.getElementById('state-input').value = d.state;
                if (status) status.innerHTML = ' <i class="fas fa-check text-green-500"></i>';
            } else if (status) {
                status.innerHTML = ' <span class="text-red-500">Not found</span>';
            }
        } catch(e) {
            if (status) status.innerHTML = ' <span class="text-red-500">Error</span>';
        }
    }, 300);
}
</script>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
