<?php
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
$options = get_option( 'bwd_userfronted_options' );
if ( !current_user_can( 'cv_editor' ) ) {
    if ( !is_user_logged_in() ) {
        $show_registration_form = apply_filters( 'bwdcv_show_registration_form', true );
        if ( $show_registration_form ) {?>
           <div class="bwd-tab-content">
               <div id="tab-2" class="bwd-custom-registration-form">
                   <?php
                   $raw_errors     = isset( $_SESSION['bwd_registration_errors'] ) && is_array( $_SESSION['bwd_registration_errors'] )
                  ? $_SESSION['bwd_registration_errors']
                  : [];

                   $error_messages = array_map( 'sanitize_text_field', $raw_errors );
                   unset( $_SESSION['bwd_registration_errors'] );

                   // For math captcha
                   $num1 = rand(1, 9);
                   $num2 = rand(1, 9);
                   $operator = self::OPERATORS[array_rand(self::OPERATORS)];
                   $num1_in_words = rand(0, 1) ? $this->toWords($num1) : $num1;
                   $num2_in_words = rand(0, 1) ? $this->toWords($num2) : $num2;
                   switch ($operator) {
                       case '+':
                           $result = $num1 + $num2;
                           break;
                       case '-':
                           $result = $num1 - $num2;
                           break;
                       case '*':
                           $result = $num1 * $num2;
                           break;
                       default:
                           $result = 0;
                   }
                   ?>
                   <form method="post" action="<?php echo esc_url( sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); ?>" id="custom_register_form">
                       <?php wp_nonce_field( 'custom_registration_nonce_action', 'custom_registration_nonce' );
                       $username = apply_filters( 'bwdcv_custom_registration_username', true );
                       if($username) {?>
                           <div class="form-group">
                           <label for="user_username"><?php esc_html_e( 'Username', 'cv-builder' );?></label>
                           <input type="text" name="user_username" id="user_username" required />
                           <?php if ( isset( $error_messages['username_exists'] ) && !empty( $error_messages['username_exists'] ) ): ?>
                               <p class="error-message"><?php echo esc_html( $error_messages['username_exists'] ); ?></p>
                           <?php endif;?>
                       </div>
                       <?php
                       }
                       ?>

                       <div class="form-group">
                           <label for="user_email"><?php esc_html_e( 'Email', 'cv-builder' );?></label>
                           <input type="email" name="user_email" id="user_email" required />
                           <?php if ( isset( $error_messages['email_exists'] ) && !empty( $error_messages['email_exists'] ) ): ?>
                               <p class="error-message"><?php echo esc_html( $error_messages['email_exists'] ); ?></p>
                           <?php endif;?>
                       </div>
                       <div class="form-group">
                           <label for="user_password"><?php esc_html_e( 'Password', 'cv-builder' );?></label>
                           <div class="bwd-pass-show">
                               <input type="password" name="user_password" id="user_password" required />
                               <span class="bwd-show-pass-icon" onclick="togglePassword('user_password', this)">
                                   <i class="fas fa-eye" aria-hidden="true"></i>
                               </span>
                           </div>
                           <button type="button" id="generate_password" class="bwd-generate-pass"><?php esc_html_e('Generate Password', 'cv-builder'); ?></button>
                       </div>
                       <div class="form-group">
                           <label for="user_password_repeat"><?php esc_html_e( 'Repeat Password', 'cv-builder' );?></label>
                           <div class="bwd-pass-show">
                               <input type="password" name="user_password_repeat" id="user_password_repeat" required />
                               <span class="bwd-show-pass-icon" onclick="togglePassword('user_password_repeat', this)">
                                   <i class="fas fa-eye" aria-hidden="true"></i>
                               </span>
                           </div>
                           <?php if ( isset( $error_messages['password_mismatch'] ) && !empty( $error_messages['password_mismatch'] ) ): ?>
                               <p class="error-message"><?php echo esc_html( $error_messages['password_mismatch'] ); ?></p>
                           <?php endif;?>
                       </div>
                       <div class="form-group">
                           <label><?php esc_html_e( 'What is', 'cv-builder' );?> <strong style="color:red;"><?php echo sprintf( esc_html__( '%s %s %s', 'cv-builder' ), ucfirst($num1_in_words), $operator, ucfirst($num2_in_words) ); ?></strong> = ?</label>
                           <input type="text" name="captcha_answer" id="captcha_answer" required />
                       </div>
                       <div class="form-group">
                           <input type="hidden" name="captcha_result" value="<?php echo $result; ?>">
                           <input type="submit" name="custom_register_form" class="btn btn-primary" value="<?php esc_html_e( 'Register', 'cv-builder' );?>" />
                       </div>
                   </form>
                   <?php echo '<a href="'.esc_url(get_permalink($options['login_page'])).'" class="bwdcv-reg-log-extra">' . esc_html__( 'Login', 'cv-builder' ) . '</a>'; ?>
                  <?php echo $this->display_social_login_btn(); ?>
               </div>
               <script type="text/javascript">
                   document.getElementById("custom_register_form").addEventListener("submit", function(e) {
                       var captchaAnswer = document.getElementById("captcha_answer").value;
                       var correctAnswer = "<?php echo $result; ?>";
                       if (captchaAnswer != correctAnswer) {
                           e.preventDefault();
                           alert("Incorrect CAPTCHA answer. Please try again.");
                       }
                   });
               </script>
           </div>

           <?php
        }
    } else {
        echo '<h2>Only CV register users can see the page.</h2> <p><a href="' . esc_url( wp_logout_url( get_permalink($options['registration_page']) ) ) . '">Leave</a> from current user.</p>';
    }
} else {
    ?>
    <div class="bwd-custom-registration-form bwdcv-login-user-guid bwdcv-already-loged-in">
        <p><?php esc_html_e( 'You are already register.', 'cv-builder' ); ?></p>
        <a href="<?php echo esc_url(get_permalink($options['dashboard_page'])); ?>" class="btn btn-primary bwdcv-my-account"><?php esc_html_e( 'CV Dashboard', 'cv-builder' ); ?></a>
        <a href="<?php echo wp_logout_url(home_url()); ?>" class="btn btn-secondary bwdcv-log-out"><?php esc_html_e( 'Log Out', 'cv-builder' ); ?></a>
    </div>
    <?php
}
