<?php
// forgot_password.php - Request Password Reset
// File location: /bidwheels.net/user/forgot_password.php

require_once '../includes/auth.php';
require_once '../includes/mailer.php';
require_once '../includes/header.php';

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = sanitize($_POST['email']);
    
    if (empty($email)) {
        $error = 'Please enter your email address.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } else {
        $user = fetchOne("SELECT id, name FROM users WHERE email = '$email' AND is_dummy = 0");
        
        if ($user) {
            // Delete any existing unused tokens for this user
            delete('password_resets', "user_id = {$user['id']} AND used = 0");
            
            // Generate reset token
            $token = bin2hex(random_bytes(32));
            $expires = date('Y-m-d H:i:s', strtotime('+1 hour'));
            
            // Save token to database
            insert('password_resets', [
                'user_id' => $user['id'],
                'token' => $token,
                'expires_at' => $expires,
                'used' => 0
            ]);
            
            // Send reset email
            $reset_link = SITE_URL . "/user/reset_password.php?token=" . $token;
            
            $email_content = "
                <div style='font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;'>
                    <div style='background: #1a1a1a; padding: 30px; text-align: center; border-radius: 20px 20px 0 0;'>
                        <img src='" . SITE_URL . SITE_LOGO . "' alt='" . SITE_NAME . "' style='height: 50px;'>
                        <h2 style='color: white; margin-top: 15px;'>Password Reset Request</h2>
                    </div>
                    <div style='background: white; padding: 30px; border-radius: 0 0 20px 20px; box-shadow: 0 5px 20px rgba(0,0,0,0.1);'>
                        <p>Dear <strong>{$user['name']}</strong>,</p>
                        <p>We received a request to reset your password. Click the button below to create a new password:</p>
                        <div style='text-align: center; margin: 30px 0;'>
                            <a href='{$reset_link}' style='background: #FF8C3E; color: white; padding: 12px 30px; text-decoration: none; border-radius: 50px; font-weight: 600; display: inline-block;'>Reset Password</a>
                        </div>
                        <p>This link will expire in <strong>1 hour</strong>.</p>
                        <p>If you didn't request this, please ignore this email.</p>
                        <hr style='margin: 20px 0; border-color: #eee;'>
                        <p style='color: #999; font-size: 12px;'>If the button doesn't work, copy and paste this link into your browser:<br>
                        <a href='{$reset_link}' style='color: #FF8C3E;'>{$reset_link}</a></p>
                    </div>
                </div>
            ";
            
            sendMail($email, 'Password Reset - ' . SITE_NAME, $email_content);
            $success = "Password reset link has been sent to your email address. Please check your inbox.";
        } else {
            // Don't reveal if email exists or not for security
            $success = "If an account exists with this email, you will receive a password reset link.";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Forgot Password - <?php echo SITE_NAME; ?></title>
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        .forgot-page {
            min-height: calc(100vh - 200px);
            background: linear-gradient(135deg, #EFEBE0 0%, #e5e0d4 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 60px 24px;
        }
        
        .forgot-container {
            max-width: 500px;
            width: 100%;
            background: white;
            border-radius: 32px;
            overflow: hidden;
            box-shadow: 0 25px 50px rgba(0,0,0,0.1);
        }
        
        .forgot-header {
            background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
            padding: 40px;
            text-align: center;
        }
        
        .forgot-header img {
            height: 50px;
            width: auto;
            margin-bottom: 20px;
        }
        
        .forgot-header h1 {
            font-size: 28px;
            font-weight: 700;
            color: white;
            margin-bottom: 10px;
        }
        
        .forgot-header p {
            color: #aaa;
            font-size: 14px;
        }
        
        .forgot-body {
            padding: 40px;
        }
        
        .form-group {
            margin-bottom: 24px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            font-size: 13px;
            color: #333;
        }
        
        .form-group label i {
            color: #FF8C3E;
            margin-right: 6px;
        }
        
        .input-wrapper {
            position: relative;
        }
        
        .input-wrapper i {
            position: absolute;
            left: 16px;
            top: 50%;
            transform: translateY(-50%);
            color: #aaa;
            font-size: 16px;
        }
        
        .input-wrapper input {
            width: 100%;
            padding: 14px 16px 14px 48px;
            border: 2px solid #f0f0f0;
            border-radius: 16px;
            font-size: 14px;
            transition: all 0.3s;
            background: #f8f8f8;
        }
        
        .input-wrapper input:focus {
            outline: none;
            border-color: #FF8C3E;
            background: white;
        }
        
        .btn-submit {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #FF8C3E, #e67a22);
            border: none;
            border-radius: 50px;
            color: white;
            font-weight: 700;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }
        
        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(255,140,62,0.3);
        }
        
        .back-link {
            text-align: center;
            margin-top: 24px;
        }
        
        .back-link a {
            color: #FF8C3E;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
        }
        
        .back-link a:hover {
            text-decoration: underline;
        }
        
        .alert {
            padding: 14px 18px;
            border-radius: 16px;
            margin-bottom: 24px;
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 13px;
        }
        
        .alert-success {
            background: #d4edda;
            color: #155724;
            border-left: 4px solid #28a745;
        }
        
        .alert-danger {
            background: #f8d7da;
            color: #721c24;
            border-left: 4px solid #dc3545;
        }
        
        .alert i {
            font-size: 18px;
        }
        
        @media (max-width: 576px) {
            .forgot-page {
                padding: 40px 16px;
            }
            
            .forgot-header {
                padding: 30px 20px;
            }
            
            .forgot-header h1 {
                font-size: 24px;
            }
            
            .forgot-body {
                padding: 30px 20px;
            }
        }
    </style>
</head>
<body>

<div class="forgot-page">
    <div class="forgot-container">
        <div class="forgot-header">
            <img src="../bidwheel_trans.png" alt="<?php echo SITE_NAME; ?>">
            <h1>Forgot Password?</h1>
            <p>Enter your email address and we'll send you a link to reset your password.</p>
        </div>
        
        <div class="forgot-body">
            <?php if ($error): ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-circle"></i>
                    <span><?php echo $error; ?></span>
                </div>
            <?php endif; ?>
            
            <?php if ($success): ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i>
                    <span><?php echo $success; ?></span>
                </div>
            <?php endif; ?>
            
            <form method="POST" action="">
                <div class="form-group">
                    <label><i class="fas fa-envelope"></i> Email Address</label>
                    <div class="input-wrapper">
                        <i class="fas fa-envelope"></i>
                        <input type="email" name="email" placeholder="your@email.com" required>
                    </div>
                </div>
                
                <button type="submit" class="btn-submit">
                    <i class="fas fa-paper-plane"></i> Send Reset Link
                </button>
                
                <div class="back-link">
                    <a href="login.php"><i class="fas fa-arrow-left"></i> Back to Login</a>
                </div>
            </form>
        </div>
    </div>
</div>

<?php require_once '../includes/footer.php'; ?>