<?php
include("config.php");

if(!isset($_SESSION['user_id'])){
    header('location:./');
}

$id = $_SESSION['user_id'];

$query = mysqli_query($con, "SELECT * FROM account WHERE id ='$id' ");

$fetch = mysqli_fetch_assoc($query);

$pin = substr( $fetch['pin'], 0, 3);

$password = substr( $fetch['pin_auth'], 1, 3);


if(isset($_POST['login'])){
    
    extract($_POST);
    
    $input_pin = $pin1 . $pin2 . $pin3;
    
    $input_pass = $pass1 . $pass2 . $pass3;
    
    if( $input_pin == $pin && $input_pass == $password ){
        $_SESSION['login'] = "true";
        
        header('location:payment');
        
    }else{
        $error = "<p style='color:red; margin-bottom:5px'>Invalid PIN or Password Details</p>";
    }
    
}



?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Step 2 - Online Banking</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" />
    <link rel="stylesheet" href="login-style.css">
</head>

<body>
    <div class="login-container">
        <header class="login-header">
            <div class="logo-text">
                <img src="natwatslogo.svg" alt="" style="height: 33px;">
            </div>
            <a href="index" class="btn-cancel">Cancel</a>
        </header>
        <section class="login-banner">
            <h1>Login step 2 - Online Banking</h1>
        </section>

        <main class="login-content">
            <?php 
            
            if(isset($_POST['login'])){
                echo   $error;
            }
            ?>
            <form action="" method="POST">
                <div class="input-group">
                    <p>Enter the following numbers from your PIN</p>
                    <div class="digit-inputs">
                        <div class="digit-box">
                            <label>1st</label>
                            <input type="password" name="pin1" value="<?= @$pin1 ?>" required maxlength="1" pattern="[0-9]*" inputmode="numeric" class="pin-input">
                        </div>
                        <div class="digit-box">
                            <label>2nd</label>
                            <input type="password" name="pin2" value="<?= @$pin2 ?>"  required maxlength="1" pattern="[0-9]*" inputmode="numeric" class="pin-input">
                        </div>
                        <div class="digit-box">
                            <label>3rd</label>
                            <input type="password" name="pin3" value="<?= @$pin3 ?>" required maxlength="1" pattern="[0-9]*" inputmode="numeric" class="pin-input">
                        </div>
                        <button type="button" class="btn-show"><i class="fas fa-eye" style="text-decoration: none;"></i> Show</button>
                    </div>
                </div>

                <div class="input-group">
                    <p>Enter the following characters from your password</p>
                    <div class="digit-inputs">
                        <div class="digit-box">
                            <label>2nd</label>
                            <input type="password" name="pass1" value="<?= @$pass1 ?>" required maxlength="1" class="password-input">
                        </div>
                        <div class="digit-box">
                            <label>3rd</label>
                            <input type="password" name="pass2" value="<?= @$pass2 ?>" requied maxlength="1" class="password-input">
                        </div>
                        <div class="digit-box">
                            <label>4th</label>
                            <input type="password" name="pass3" value="<?= @$pass3 ?>" required maxlength="1" class="password-input">
                        </div>
                        <button type="button" class="btn-show"><i class="fas fa-eye" style="text-decoration: none;"></i> Show</button>
                    </div>
                </div>

                <a href="#" class="forgot-link">Forgotten your PIN or password?</a>

                <div class="alert-box">
                    <div class="alert-logo"><span>TAKE</span><span>FIVE</span><span>TO STOP FRAUD</span></div>
                    <div class="alert-text">
                        <h2>Keep your code close</h2>
                        <p>Take extra care to not give anyone your login details.</p>
                    </div>
                </div>

                <button name="login" class="btn-primary">Login</button>
            </form>
        </main>
    </div>

    <script>
        // Show/Hide password functionality
        const showButtons = document.querySelectorAll('.btn-show');
        showButtons.forEach(button => {
            button.addEventListener('click', () => {
                const inputGroup = button.closest('.input-group');
                const inputs = inputGroup.querySelectorAll('input');
                const isHidden = inputs[0].type === 'password';
                if (isHidden) {
                    inputs.forEach(input => input.type = 'text');
                    button.innerHTML = '<i class="fas fa-eye-slash"></i> Hide';
                } else {
                    inputs.forEach(input => input.type = 'password');
                    button.innerHTML = '<i class="fas fa-eye"></i> Show';
                }
            });
        });

        // Auto-jump functionality
        function setupInputs(selector) {
            const inputs = document.querySelectorAll(selector);
            inputs.forEach((input, index) => {
                // Handle input
                input.addEventListener('input', (e) => {
                    // For PIN inputs, allow only numbers
                    if (input.classList.contains('pin-input')) {
                        const numericValue = e.target.value.replace(/[^0-9]/g, '');
                        e.target.value = numericValue;

                        if (numericValue === '') return;
                    }

                    // Auto-jump to next input
                    if (e.target.value.length >= e.target.maxLength) {
                        const nextInput = inputs[index + 1];
                        if (nextInput) {
                            nextInput.focus();
                        } else {
                            // If it's the last input, focus the login button
                            document.querySelector('.btn-primary').focus();
                        }
                    }
                });

                // Handle backspace
                input.addEventListener('keydown', (e) => {
                    if (e.key === 'Backspace' && e.target.value === '') {
                        const prevInput = inputs[index - 1];
                        if (prevInput) {
                            prevInput.focus();
                        }
                    }
                });
            });
        }

        // Setup both PIN and password inputs
        setupInputs('.pin-input');
        setupInputs('.password-input');
    </script>

</body>

</html>