<?php
session_start();
include('./includes/config.php');
include('./template/ahkweb/authentication-signin.php');

if (isset($_POST['username']) && !empty($_POST['username'])) {
    $username = mysqli_real_escape_string($ahk_conn, $_POST['username']);
    $password = mysqli_real_escape_string($ahk_conn, $_POST['password']);

    $fetch = mysqli_query($ahk_conn, "SELECT * FROM users WHERE phone='$username' OR email='$username'");

    if ($fetch && mysqli_num_rows($fetch) === 1) {
        $userData = mysqli_fetch_assoc($fetch);

        // First check if account is active
        if ($userData['status'] == 1) {
            // Then check password
            if (password_verify($password, $userData['password'])) {

                // Set session variables
                $_SESSION['phone'] = $userData['phone'];
                $_SESSION['name'] = $userData['name'];

                $user = $userData['name'];
                $login_time = date('Y-m-d H:i:s');
                $login_ip = $_SERVER['REMOTE_ADDR'];
                $site = "https://panel.sss00.xyz";

                // WhatsApp Notification
                $number = $userData['phone'];
                $message = urlencode("Dear $user, you have successfully logged in at $login_time. Login IP: $login_ip. If this wasn't you, please change your password immediately. $site");
                $whatsapp_url = "https://web.tgekyc.site/send-message?api_key=dfg&sender=9154564&number=91$number&message=$message";
                ?>

                <script>
                    $(function () {
                        Swal.fire({
                            title: "✅ Login Successful",
                            text: "<?php echo ahkweb('webname'); ?> - Welcome back!",
                            icon: "success",
                            timer: 2000,
                            showConfirmButton: false,
                            timerProgressBar: true
                        });

                        setTimeout(() => {
                            window.location.href = 'admin';
                        }, 1800);

                        fetch("<?php echo $whatsapp_url; ?>");
                    });
                </script>

                <?php
            } else {
                // Incorrect password
                ?>
                <script>
                    $(function () {
                        Swal.fire({
                            title: "❌ Incorrect Password",
                            text: "The password you entered is incorrect.",
                            icon: "error",
                            confirmButtonColor: "#d33"
                        });
                    });
                </script>
                <?php
            }
        } else {
            // Account not active
            ?>
            <script>
                $(function () {
                    Swal.fire({
                        title: "🚫 Account Inactive",
                        text: "Your account is not active. Please contact support.",
                        icon: "warning",
                        confirmButtonColor: "#f39c12"
                    });
                });
            </script>
            <?php
        }
    } else {
        // No such user
        ?>
        <script>
            $(function () {
                Swal.fire({
                    title: "📛 Not Registered",
                    text: "The email or phone number is not registered.",
                    icon: "error",
                    confirmButtonColor: "#d33"
                });
            });
        </script>
        <?php
    }
}
?>
