<?php 
$page_title = "Forgot Password - Reset Your Shubank Account | Shubank";
$page_description = "Reset your Shubank account password securely. Enter your email to receive password reset instructions.";
$canonical_url = "https://shubank.co/forgot.php";

// Handle password reset request
$message = '';
$message_type = '';

if ($_POST) {
    include 'config.php';
    
    $email = mysqli_real_escape_string($conn, trim($_POST['email']));
    
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $message = "Please enter a valid email address.";
        $message_type = "error";
    } else {
        // Check if email exists in database
        $check_email = "SELECT id, name FROM users WHERE email = '$email'";
        $result = mysqli_query($conn, $check_email);
        
        if (mysqli_num_rows($result) > 0) {
            $user = mysqli_fetch_assoc($result);
            
            // Generate reset token
            $reset_token = bin2hex(random_bytes(32));
            $expires_at = date('Y-m-d H:i:s', strtotime('+1 hour'));
            
            // Store reset token in database
            $store_token = "INSERT INTO password_resets (email, token, expires_at) VALUES ('$email', '$reset_token', '$expires_at')
                           ON DUPLICATE KEY UPDATE token = '$reset_token', expires_at = '$expires_at'";
            
            if (mysqli_query($conn, $store_token)) {
                // Send reset email (simplified for demo)
                $reset_link = "https://shubank.co/reset-password.php?token=$reset_token";
                $subject = "Password Reset Request - Shubank";
                $email_message = "Hi " . $user['name'] . ",\n\n";
                $email_message .= "We received a request to reset your password. Click the link below to reset your password:\n\n";
                $email_message .= $reset_link . "\n\n";
                $email_message .= "This link will expire in 1 hour. If you didn't request this, please ignore this email.\n\n";
                $email_message .= "Best regards,\nShubank Team";
                
                // In production, use proper email service
                if (mail($email, $subject, $email_message)) {
                    $message = "Password reset instructions have been sent to your email address.";
                    $message_type = "success";
                } else {
                    $message = "Failed to send email. Please try again later.";
                    $message_type = "error";
                }
            } else {
                $message = "Database error. Please try again later.";
                $message_type = "error";
            }
        } else {
            // Don't reveal if email exists or not for security
            $message = "If your email is registered, you will receive password reset instructions.";
            $message_type = "success";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?></title>
    <meta name="description" content="<?php echo $page_description; ?>">
    <link rel="canonical" href="<?php echo $canonical_url; ?>">
    <meta name="robots" content="noindex, follow">
    <link rel="stylesheet" href="style.css">
</head>
<body>

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

<!-- Forgot Password Section -->
<section class="forgot-password">
    <div class="container">
        <div class="forgot-form-container">
            <div class="forgot-header">
                <div class="icon">
                    <svg width="60" height="60" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M12 2C13.1 2 14 2.9 14 4C14 5.1 13.1 6 12 6C10.9 6 10 5.1 10 4C10 2.9 10.9 2 12 2ZM21 9V7L15 1H5C3.89 1 3 1.89 3 3V19C3 20.1 3.9 21 5 21H11V19H5V3H14V8H21ZM15 21V19H21V17H15V15L12 18L15 21Z" fill="#005c97"/>
                    </svg>
                </div>
                <h1>Forgot Password?</h1>
                <p>No worries! Enter your email address and we'll send you password reset instructions.</p>
            </div>
            
            <?php if ($message): ?>
                <div class="alert alert-<?php echo $message_type; ?>">
                    <?php echo htmlspecialchars($message); ?>
                </div>
            <?php endif; ?>
            
            <form method="POST" action="" class="forgot-form">
                <div class="form-group">
                    <label for="email">Email Address</label>
                    <input type="email" id="email" name="email" required 
                           placeholder="Enter your registered email address"
                           value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>">
                    <div class="input-icon">
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M20 4H4C2.9 4 2.01 4.9 2.01 6L2 18C2 19.1 2.9 20 4 20H20C21.1 20 22 19.1 22 18V6C22 4.9 21.1 4 20 4ZM20 8L12 13L4 8V6L12 11L20 6V8Z" fill="#666"/>
                        </svg>
                    </div>
                </div>
                
                <button type="submit" class="btn-primary">
                    Send Reset Instructions
                </button>
            </form>
            
            <div class="form-footer">
                <p>Remember your password? <a href="login.php">Sign In</a></p>
                <p>Don't have an account? <a href="users.php">Sign Up</a></p>
            </div>
            
            <div class="security-info">
                <h3>🔒 Security Information</h3>
                <ul>
                    <li>Reset links expire after 1 hour for security</li>
                    <li>You can only have one active reset request at a time</li>
                    <li>If you don't receive an email, check your spam folder</li>
                    <li>Contact support if you continue having issues</li>
                </ul>
            </div>
        </div>
        
        <div class="help-section">
            <h3>Need Help?</h3>
            <div class="help-options">
                <a href="https://wa.me/919876543210" class="help-option" target="_blank">
                    <span class="icon">💬</span>
                    <div>
                        <h4>WhatsApp Support</h4>
                        <p>Get instant help via WhatsApp</p>
                    </div>
                </a>
                
                <a href="mailto:support@shubank.co" class="help-option">
                    <span class="icon">📧</span>
                    <div>
                        <h4>Email Support</h4>
                        <p>support@shubank.co</p>
                    </div>
                </a>
                
                <a href="contact.php" class="help-option">
                    <span class="icon">📝</span>
                    <div>
                        <h4>Contact Form</h4>
                        <p>Send us a detailed message</p>
                    </div>
                </a>
            </div>
        </div>
    </div>
</section>

<style>
.forgot-password {
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    min-height: 100vh;
    padding: 80px 0;
    display: flex;
    align-items: center;
}

.forgot-form-container {
    max-width: 500px;
    margin: 0 auto;
    background: white;
    border-radius: 20px;
    padding: 40px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.1);
    margin-bottom: 40px;
}

.forgot-header {
    text-align: center;
    margin-bottom: 40px;
}

.forgot-header .icon {
    margin-bottom: 20px;
}

.forgot-header h1 {
    font-size: 2rem;
    color: #005c97;
    margin-bottom: 15px;
}

.forgot-header p {
    color: #666;
    font-size: 1.1rem;
    line-height: 1.5;
}

.alert {
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 25px;
    font-weight: 500;
}

.alert-success {
    background: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
}

.alert-error {
    background: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

.form-group {
    margin-bottom: 25px;
    position: relative;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    color: #333;
}

.form-group input {
    width: 100%;
    padding: 15px 45px 15px 15px;
    border: 2px solid #e1e5e9;
    border-radius: 10px;
    font-size: 16px;
    transition: all 0.3s ease;
}

.form-group input:focus {
    outline: none;
    border-color: #005c97;
    box-shadow: 0 0 0 3px rgba(0,92,151,0.1);
}

.input-icon {
    position: absolute;
    right: 15px;
    top: 42px;
    pointer-events: none;
}

.btn-primary {
    width: 100%;
    background: linear-gradient(135deg, #005c97 0%, #17a2b8 100%);
    color: white;
    padding: 15px;
    border: none;
    border-radius: 10px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    margin-bottom: 25px;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(0,92,151,0.3);
}

.form-footer {
    text-align: center;
    padding-top: 25px;
    border-top: 1px solid #eee;
    margin-bottom: 30px;
}

.form-footer p {
    margin-bottom: 10px;
    color: #666;
}

.form-footer a {
    color: #005c97;
    text-decoration: none;
    font-weight: 600;
}

.form-footer a:hover {
    text-decoration: underline;
}

.security-info {
    background: #f8f9fa;
    padding: 20px;
    border-radius: 12px;
    border-left: 4px solid #ffd600;
}

.security-info h3 {
    color: #005c97;
    margin-bottom: 15px;
    font-size: 1.1rem;
}

.security-info ul {
    color: #666;
    padding-left: 20px;
}

.security-info li {
    margin-bottom: 8px;
    font-size: 0.9rem;
}

.help-section {
    max-width: 800px;
    margin: 0 auto;
    text-align: center;
}

.help-section h3 {
    font-size: 2rem;
    color: #005c97;
    margin-bottom: 40px;
}

.help-options {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
}

.help-option {
    display: flex;
    align-items: center;
    gap: 15px;
    background: white;
    padding: 25px;
    border-radius: 15px;
    text-decoration: none;
    color: inherit;
    transition: all 0.3s ease;
    box-shadow: 0 5px 20px rgba(0,0,0,0.1);
}

.help-option:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 30px rgba(0,0,0,0.15);
}

.help-option .icon {
    font-size: 2rem;
}

.help-option h4 {
    color: #005c97;
    margin-bottom: 5px;
    font-size: 1.1rem;
}

.help-option p {
    color: #666;
    font-size: 0.9rem;
}

/* Mobile Responsive */
@media (max-width: 768px) {
    .forgot-password {
        padding: 40px 0;
    }
    
    .forgot-form-container {
        margin: 0 20px 40px;
        padding: 30px 20px;
    }
    
    .forgot-header h1 {
        font-size: 1.5rem;
    }
    
    .help-options {
        grid-template-columns: 1fr;
        margin: 0 20px;
    }
    
    .help-option {
        padding: 20px;
    }
}
</style>

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

<!-- Create password_resets table if it doesn't exist -->
<?php
if (isset($conn)) {
    $create_resets_table = "CREATE TABLE IF NOT EXISTS password_resets (
        email VARCHAR(100) PRIMARY KEY,
        token VARCHAR(64) NOT NULL,
        expires_at TIMESTAMP NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )";
    mysqli_query($conn, $create_resets_table);
}
?>

</body>
</html>