<?php
@session_start();
include('includes/config.php');

$show_alert = false;
$alert_type = '';
$alert_message = '';

if(isset($_POST['submit']) && !empty($_POST['username'])){
    $U = mysqli_real_escape_string($tng_conn, $_POST['username']);

    // Prevent admin reset
    if(strtolower($U) == "admin"){
        $show_alert = true; 
        $alert_type = 'error';
        $alert_message = "You are not authorized to reset admin password.";
    } else {
        $Q = mysqli_query($tng_conn, "SELECT * FROM users WHERE phone='$U' OR email='$U'");

        if(mysqli_num_rows($Q) == 1){
            $D = mysqli_fetch_assoc($Q);

            if($D['status'] == 1){
                // Generate new password
                $NP = rand(10000000, 99999999);
                $HP = password_hash($NP, PASSWORD_DEFAULT);
                date_default_timezone_set("Asia/Kolkata");
                $TS = date('Y-m-d H:i:s');

                $IQ = mysqli_query($tng_conn, "UPDATE users SET password='$HP', date='$TS' WHERE phone='$U' OR email='$U'");

                if($IQ){
                    $WN = XyZ_val('brand');
                    $SV = $_SERVER['SERVER_NAME'];
                    $PP = "https://cdn.pixabay.com/animation/2022/07/31/05/09/05-09-53-216_512.gif";

                    $MSG = "🔒 Hello " . $D['name'] . "!\n\n"
                         . "🆕 Your new password is: *" . $NP . "*\n"
                         . "🌐 Login now: https://$SV\n\n" 
                         . "✨ Keep it safe and secure!";

                   
                        sendWhatsAppMessage($D['phone'], $MSG, $SV);
                    

                    $BODY = generatePasswordResetEmail($D['name'], $NP, $WN, $SV, $PP);
                    SendMail($D['email'], "Password Reset Successful", $BODY);
                    
                    $show_alert = true;
                    $alert_type = 'success';
                    $alert_message = "Password reset successfully. New password sent via email.";

                } else {
                    $show_alert = true;
                    $alert_type = 'error';
                    $alert_message = "Failed to update password. Please try again.";
                }

            } else {
                $show_alert = true;
                $alert_type = 'error';
                $alert_message = "Account not active. Contact support.";
            }

        } else {
            $show_alert = true;
            $alert_type = 'error';
            $alert_message = "Email or phone not registered.";
        }
    }
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="./template/tngweb/assets/images/favicon-32x32.png" type="image/png" />
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.4.14/dist/sweetalert2.all.min.js"></script>
    <script disable-devtool-auto src='https://cdn.jsdelivr.net/npm/disable-devtool@latest'></script>
    <title><?php echo XyZ_val('brand'); ?> | <?php echo XyZ_val('title'); ?></title>
    
    <!-- ========== ENHANCED MODERN CSS (preserves all original classes) ========== -->
    <style>
        /* Reset & Base */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            position: relative;
            overflow-x: hidden;
        }
        /* Animated Background Shapes */
        .bg-shape {
            position: fixed;
            filter: blur(80px);
            opacity: 0.35;
            z-index: 0;
            border-radius: 50%;
            animation: floatBg 18s infinite ease-in-out;
            pointer-events: none;
        }
        .shape-1 { top: -150px; left: -150px; width: 400px; height: 400px; background: radial-gradient(circle, #00ffff, #0080ff); }
        .shape-2 { bottom: -120px; right: -120px; width: 500px; height: 500px; background: radial-gradient(circle, #ff00ff, #ff0080); animation-delay: -6s; }
        .shape-3 { top: 40%; left: -80px; width: 300px; height: 300px; background: radial-gradient(circle, #ffd700, #ff6600); animation-delay: -12s; }
        @keyframes floatBg {
            0% { transform: translate(0, 0) scale(1); }
            50% { transform: translate(40px, 30px) scale(1.1); }
            100% { transform: translate(0, 0) scale(1); }
        }
        
        /* Navbar - original classes, upgraded look */
        .navbar {
            position: relative;
            z-index: 100;
            background: rgba(15, 12, 41, 0.7);
            backdrop-filter: blur(12px);
            border-bottom: 1px solid rgba(255,255,255,0.1);
            padding: 1rem 2rem;
        }
        .nav-container {
            max-width: 1300px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
        }
        .logo {
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 1.5rem;
            font-weight: 700;
            color: white;
            text-decoration: none;
            letter-spacing: -0.5px;
        }
        .logo img {
            height: 40px;
            width: auto;
            filter: drop-shadow(0 2px 5px rgba(0,0,0,0.2));
        }
        .mobile-toggle {
            display: none;
            background: none;
            border: none;
            color: white;
            font-size: 1.8rem;
            cursor: pointer;
        }
        .nav-links {
            display: flex;
            gap: 2rem;
            list-style: none;
            align-items: center;
        }
        .nav-links li a {
            color: rgba(255,255,255,0.85);
            text-decoration: none;
            font-weight: 500;
            transition: 0.2s;
            font-size: 0.95rem;
            padding: 0.5rem 0;
        }
        .nav-links li a:hover {
            color: white;
            text-shadow: 0 0 6px rgba(255,255,255,0.5);
        }
        .nav-links li a.active {
            color: #c084fc;
            border-bottom: 2px solid #c084fc;
        }
        @media (max-width: 768px) {
            .navbar { padding: 0.8rem 1.2rem; }
            .mobile-toggle { display: block; }
            .nav-links {
                display: none;
                width: 100%;
                flex-direction: column;
                gap: 1rem;
                padding: 1.2rem 0 0.5rem;
            }
            .nav-links.active {
                display: flex;
            }
            .nav-links li a {
                display: block;
                padding: 0.5rem;
                text-align: center;
            }
        }
        
        /* Forgot Password Container & Card - enhanced glassmorphism */
        .forgot-container {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 2rem 1.5rem;
            position: relative;
            z-index: 10;
        }
        .forgot-card {
            background: rgba(255, 255, 255, 0.07);
            backdrop-filter: blur(14px);
            border-radius: 2rem;
            border: 1px solid rgba(255, 255, 255, 0.2);
            box-shadow: 0 25px 45px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255,255,255,0.1);
            padding: 2rem;
            width: 100%;
            max-width: 480px;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        .forgot-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 30px 55px rgba(0, 0, 0, 0.4);
        }
        /* Shine effect on card */
        .forgot-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.12), transparent);
            transition: left 0.5s;
            pointer-events: none;
        }
        .forgot-card:hover::before {
            left: 100%;
        }
        
        .forgot-header {
            text-align: center;
            margin-bottom: 1.8rem;
        }
        .forgot-icon {
            font-size: 3rem;
            color: #c084fc;
            margin-bottom: 1rem;
            display: inline-block;
        }
        .forgot-header h1 {
            font-size: 1.8rem;
            font-weight: 700;
            background: linear-gradient(135deg, #fff, #c084fc);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            margin-bottom: 0.5rem;
        }
        .forgot-header p {
            color: rgba(255,255,255,0.7);
            font-size: 0.85rem;
        }
        
        .instructions {
            background: rgba(255,255,255,0.05);
            border-left: 3px solid #c084fc;
            padding: 0.8rem 1rem;
            border-radius: 1rem;
            margin-bottom: 1.5rem;
            font-size: 0.8rem;
            color: rgba(255,255,255,0.8);
        }
        
        .form-group {
            margin-bottom: 1.3rem;
        }
        .form-label {
            display: block;
            color: rgba(255,255,255,0.9);
            font-weight: 500;
            font-size: 0.8rem;
            margin-bottom: 0.5rem;
            letter-spacing: 0.5px;
        }
        .form-control {
            width: 100%;
            background: rgba(255,255,255,0.1);
            border: 1px solid rgba(255,255,255,0.2);
            border-radius: 1.2rem;
            padding: 0.85rem 1rem;
            font-size: 0.9rem;
            color: white;
            font-weight: 500;
            transition: 0.2s;
            outline: none;
            font-family: 'Poppins', sans-serif;
        }
        .form-control:focus {
            border-color: rgba(255,255,255,0.6);
            background: rgba(255,255,255,0.15);
            box-shadow: 0 0 10px rgba(255,255,255,0.15);
        }
        .form-control::placeholder {
            color: rgba(255,255,255,0.4);
        }
        
        .submit-btn {
            width: 100%;
            background: linear-gradient(95deg, #7c3aed, #a855f7);
            border: none;
            border-radius: 1.8rem;
            padding: 0.85rem;
            font-weight: 700;
            font-size: 0.95rem;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
            cursor: pointer;
            transition: 0.2s;
            position: relative;
            overflow: hidden;
            box-shadow: 0 6px 18px rgba(124,58,237,0.3);
        }
        .submit-btn:hover {
            transform: scale(1.02);
            box-shadow: 0 8px 22px rgba(124,58,237,0.5);
        }
        .back-to-login {
            text-align: center;
            margin-top: 1.3rem;
            font-size: 0.8rem;
            color: rgba(255,255,255,0.7);
        }
        .back-to-login a {
            color: #c084fc;
            text-decoration: none;
            font-weight: 600;
        }
        .back-to-login a:hover {
            text-shadow: 0 0 5px #c084fc;
        }
        
        /* Footer */
        .footer {
            text-align: center;
            padding: 1.2rem;
            font-size: 0.7rem;
            color: rgba(255,255,255,0.4);
            z-index: 10;
            position: relative;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        /* Alert container (if any) */
        .alert-container {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1000;
        }
        .alert {
            background: rgba(0,0,0,0.8);
            backdrop-filter: blur(8px);
            border-radius: 1rem;
            padding: 0.8rem 1.2rem;
            color: white;
            margin-bottom: 0.5rem;
            animation: slideIn 0.3s ease;
        }
        .alert-success {
            border-left: 4px solid #10b981;
        }
        .alert-error {
            border-left: 4px solid #ef4444;
        }
        @keyframes slideIn {
            from { transform: translateX(100%); opacity: 0; }
            to { transform: translateX(0); opacity: 1; }
        }
        
        /* Responsive */
        @media (max-width: 520px) {
            .forgot-card {
                padding: 1.5rem;
            }
            .forgot-header h1 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>

<body>
    <!-- Animated background shapes (added for glamour, does not affect functionality) -->
    <div class="bg-shape shape-1"></div>
    <div class="bg-shape shape-2"></div>
    <div class="bg-shape shape-3"></div>

    <!-- Alert Container -->
    <?php if ($show_alert): ?>
    <div class="alert-container">
        <div class="alert alert-<?php echo $alert_type; ?>">
            <?php echo $alert_message; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Navigation -->
    <nav class="navbar">
        <div class="nav-container">
            <a href="index.php" class="logo">
                <img src="<?php echo XyZ_val('logo'); ?>" alt="Logo">
                <?php echo XyZ_val('brand'); ?>
            </a>
            
            <button class="mobile-toggle" id="mobileToggle">
                <i class='bx bx-menu'></i>
            </button>
            
            <ul class="nav-links" id="navLinks">
                <li><a href="index.php">Home</a></li>
                <?php if (XyZ_val('register_status') === 'active'): ?>
                <li><a href="register.php">Register</a></li>
                <?php endif; ?>
                <li><a href="Forgot-password.php" class="active">Forgot Password</a></li>
                <li><a href="contact.php">Contact</a></li>
            </ul>
        </div>
    </nav>

    <!-- Forgot Password Content -->
    <div class="forgot-container">
        <div class="forgot-card">
            <div class="forgot-header">
                <i class='bx bx-lock-alt forgot-icon'></i>
                <h1>Reset Your Password</h1>
                <p>Enter your email or phone to receive reset instructions</p>
            </div>
            
            <div class="forgot-body">
                <div class="instructions">
                    <p>Enter the email address or phone number associated with your account, and we'll send you a new password via email<?php echo (XyZ_val('otp_option') === "yes" ? ' and WhatsApp' : ''); ?>.</p>
                </div>
                
                <form method="POST" action="">
                    <div class="form-group">
                        <label for="inputEmailAddress" class="form-label">Email or Phone *</label>
                        <input name="username" type="text" class="form-control" id="inputEmailAddress" placeholder="Enter your email or phone number" required value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>">
                    </div>
                    
                    <button type="submit" name="submit" class="submit-btn" id="submitBtn">
                        <i class='bx bx-send'></i> Reset Password
                    </button>
                    
                    <div class="back-to-login">
                        Remember your password? <a href="login.php">Back to Login</a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="footer">
        <div class="container">
            <p>&copy; <?php echo date("Y"); ?> <?php echo strtoupper($_SERVER['SERVER_NAME']); ?>. All rights reserved.</p>
        </div>
    </footer>
 
    <script>
        $(document).ready(function () {
            $("#show_hide_password a").on('click', function (event) {
                event.preventDefault();
                if ($('#show_hide_password input').attr("type") == "text") {
                    $('#show_hide_password input').attr('type', 'password');
                    $('#show_hide_password i').addClass("bx-hide");
                    $('#show_hide_password i').removeClass("bx-show");
                } else if ($('#show_hide_password input').attr("type") == "password") {
                    $('#show_hide_password input').attr('type', 'text');
                    $('#show_hide_password i').removeClass("bx-hide");
                    $('#show_hide_password i').addClass("bx-show");
                }
            });
        });
    </script>
</body>
</html>