<?php $options = kaveh_get_options(); ?>
<?php
$page = get_page_by_path('klogin');
if (!empty($page)) {
  $page_id = $page->ID;
}
$account_page_id = get_option('woocommerce_myaccount_page_id');
?>
<?php if (!is_user_logged_in() && !is_page($page_id)): ?>
  <div id="auth-gajmarket" class="auth__modal">
    <div class="auth__modal__dialog">
        <button type="button" class="auth__modal__close auth__absolute auth__flex auth__items__center auth__content__center auth__b__white" aria-label="close">&times;</button>
        <div class="auth__modal__content">
            <div class="auth__modal__body">
                <div class="auth__page__twelve auth__mx__auto loadlogin">
                    <!-- Start Logo -->
                    <a href="<?php echo home_url(); ?>" class="auth__page__twelve__logo auth__block auth__mx__auto auth__w__fit auth__absolute">
                        <img src="<?php echo $options['kaveh_logo_up'] ?>" alt="logo" width="127" height="40">
                    </a>
                    <!-- End Logo -->
                    <div class="auth__page__twelve__wrapper auth__relative">
                        <!-- Start Tabs -->
                        <ul class="auth__page__twelve__tabs auth__flex auth__items__center auth__content__center">
                            <li class="active auth__ease__in__out" data-auth-toggle="tab" data-auth-target="login"><?php esc_html_e('Login with SMS', 'kavehTheme'); ?></li>
                            <li class="auth__ease__in__out" data-auth-toggle="tab" data-auth-target="register"><?php esc_html_e('Login with Password', 'kavehTheme'); ?></li>
                        </ul>
                        <!-- End Tabs -->
                        <!-- Start Content -->
                        <div class="auth__page__twelve__content">
                            <!-- Start Tabs Contents -->
                            <div class="auth__tab__content">
                                <?php if ($options['kaveh_login_type'] === 'option-1' || $options['kaveh_login_type'] === 'option-3'): ?>
                                    <!-- Start Tab Content Login -->
                                    <div id="login" class="active auth__tab__pane">
                                        <img src="<?php echo KAVEH_TEMPLATE_URI . '/assets/images/user-avatar.svg'; ?>" alt="user" width="105" height="105" class="auth__block auth__mx__auto">
                                        <form class="smslogin" method="post" action="#">
                                            <div class="auth__page__form__group position-relative">
                                                <i class="isax isax-user iabs"></i>
                                                <input id="phone" type="text" class="auth__page__form__control auth__ease__in__out" placeholder="<?php esc_attr_e('Enter your phone number...', 'kavehTheme'); ?>">
                                            </div>
                                            <br>
                                            <button type="submit" class="auth__page__btn auth__page__btn__success auth__w__fit auth__block">
                                                <?php esc_html_e('Get Verification Code', 'kavehTheme'); ?>
                                            </button>
                                        </form>
                                    </div>
                                    <!-- End Tab Content Login -->
                                    <!-- Start Tab Content Register -->
                                    <div id="register" class="auth__tab__pane">
                                        <img src="<?php echo KAVEH_TEMPLATE_URI . '/assets/images/user-avatar.svg'; ?>" alt="user" width="105" height="105" class="auth__block auth__mx__auto">
                                        <form class="passlogin" method="post" action="#">
                                            <div class="auth__page__form__group position-relative">
                                                <i class="iconly iconly-Message iabs"></i>
                                                <input type="text" id="email-login" class="auth__page__form__control auth__ease__in__out" placeholder="<?php esc_attr_e('Enter a valid mobile number or email', 'kavehTheme'); ?>">
                                            </div>
                                            <div class="auth__page__form__group position-relative">
                                                <i class="iconly iconly-Password iabs"></i>
                                                <input type="password" id="password" class="auth__page__form__control auth__ease__in__out" placeholder="<?php esc_attr_e('Enter your password...', 'kavehTheme'); ?>">
                                            </div>
                                            <a href="javascript:void(0)" class="link auth__flex auth__items__center auth__ease__in__out forgotk">
                                                <i class="isax isax-lock-1"></i>
                                                <?php esc_html_e('Forgot your password?', 'kavehTheme'); ?>
                                            </a>
                                            <button type="submit" class="auth__page__btn auth__page__btn__success auth__w__fit auth__block">
                                                <?php esc_html_e('Login / Register', 'kavehTheme'); ?>
                                            </button>
                                        </form>
                                    </div>
                                    <!-- End Tab Content Register -->
                                <?php elseif ($options['kaveh_login_type'] === 'option-2'): ?>
                                    <!-- Start Tab Content Login -->
                                    <div id="login" class="auth__tab__pane">
                                        <img src="<?php echo KAVEH_TEMPLATE_URI . '/assets/images/user-avatar.svg'; ?>" alt="user" width="105" height="105" class="auth__block auth__mx__auto">
                                        <form class="smslogin" method="post" action="#">
                                            <div class="auth__page__form__group position-relative">
                                                <i class="isax isax-user iabs"></i>
                                                <input id="phone" type="text" class="auth__page__form__control auth__ease__in__out" placeholder="<?php esc_attr_e('Enter your phone number...', 'kavehTheme'); ?>">
                                            </div>
                                            <br>
                                            <button type="submit" class="auth__page__btn auth__page__btn__success auth__w__fit auth__block">
                                                <?php esc_html_e('Get Verification Code', 'kavehTheme'); ?>
                                            </button>
                                        </form>
                                    </div>
                                    <!-- End Tab Content Login -->
                                    <!-- Start Tab Content Register -->
                                    <div id="register" class="active auth__tab__pane">
                                        <img src="<?php echo KAVEH_TEMPLATE_URI . '/assets/images/user-avatar.svg'; ?>" alt="user" width="105" height="105" class="auth__block auth__mx__auto">
                                        <form class="passlogin" method="post" action="#">
                                            <div class="auth__page__form__group position-relative">
                                                <i class="iconly iconly-Message iabs"></i>
                                                <input type="text" id="email-register" class="auth__page__form__control auth__ease__in__out" placeholder="<?php esc_attr_e('Enter a valid mobile number or email', 'kavehTheme'); ?>">
                                            </div>
                                            <div class="auth__page__form__group position-relative">
                                                <i class="iconly iconly-Password iabs"></i>
                                                <input type="password" id="password" class="auth__page__form__control auth__ease__in__out" placeholder="<?php esc_attr_e('Enter your password...', 'kavehTheme'); ?>">
                                            </div>
                                            <a href="javascript:void(0)" class="link auth__flex auth__items__center auth__ease__in__out forgotk">
                                                <i class="isax isax-lock-1"></i>
                                                <?php esc_html_e('Forgot your password?', 'kavehTheme'); ?>
                                            </a>
                                            <button type="submit" class="auth__page__btn auth__page__btn__success auth__w__fit auth__block">
                                                <?php esc_html_e('Login / Register', 'kavehTheme'); ?>
                                            </button>
                                        </form>
                                    </div>
                                    <!-- End Tab Content Register -->
                                <?php endif; ?>
                            </div>
                            <!-- End Tabs Contents -->
                        </div>
                        <!-- End Content -->
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    document.getElementById('phone').addEventListener('input', function(e) {
    const phoneRegex = /^09\d{9}$/;
    const isValid = phoneRegex.test(e.target.value);
    
    e.target.setCustomValidity(isValid ? '' : '<?php echo esc_js(__('Mobile number format is incorrect', 'kavehTheme')); ?>');
});

// Prevent form submission if invalid
const form = document.getElementById('phone').closest('form');
if (form) {
    form.addEventListener('submit', function(e) {
        const phone = document.getElementById('phone');
        if (!phone.checkValidity()) {
            e.preventDefault();
            phone.reportValidity();
        }
    });
}
</script>

<?php endif; ?>