<?php
/**
 * Load admin view for WooCommerceRegistrationForm.
 *
 * @package miniorange-sms-order-notification-otp-verification/controllers/forms/
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! current_user_can( 'manage_options' ) ) {
	wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'miniorange-sms-order-notification-otp-verification' ) );
}

use WCSMSOTP\Handler\Forms\WooCommerceRegistrationForm;
use WCSMSOTP\Helper\MoUtility;


$mowc_handler                  = WooCommerceRegistrationForm::instance();
$mowc_woocommerce_registration = (bool) $mowc_handler->is_form_enabled() ? 'checked' : '';
$mowc_wc_hidden                = 'checked' === $mowc_woocommerce_registration ? '' : 'hidden';
$mowc_wc_enable_type           = $mowc_handler->get_otp_type_enabled();
$mowc_wc_restrict_duplicates   = (bool) $mowc_handler->restrict_duplicates() ? 'checked' : '';
$mowc_wc_reg_type_phone        = $mowc_handler->get_phone_html_tag();
$mowc_wc_reg_type_email        = $mowc_handler->get_email_html_tag();
$mowc_wc_reg_type_both         = $mowc_handler->get_both_html_tag();
$mowc_form_name                = $mowc_handler->get_form_name();
$mowc_redirect_page            = sanitize_text_field( $mowc_handler->redirectToPage() );
$mowc_redirect_page_id         = '';
if ( ! MoUtility::is_blank( $mowc_redirect_page ) ) {
	$mowc_query = new \WP_Query(
		array(
			'post_type'              => 'page',
			'post_status'            => 'publish',
			'posts_per_page'         => 1,
			'no_found_rows'          => true,
			'ignore_sticky_posts'    => true,
			'update_post_term_cache' => false,
			'update_post_meta_cache' => false,
			's'                      => $mowc_redirect_page,
		)
	);
	if ( $mowc_query->have_posts() ) {
		while ( $mowc_query->have_posts() ) {
			$mowc_query->the_post();
			if ( get_the_title() === $mowc_redirect_page ) {
				$mowc_redirect_page_id = absint( get_the_ID() );
				break;
			}
		}
		wp_reset_postdata();
	}
}
$mowc_is_ajax_form                           = $mowc_handler->is_ajax_form();
$mowc_is_ajax_mode_enabled                   = $mowc_is_ajax_form ? 'checked' : '';
$mowc_wc_button_text                         = $mowc_handler->get_button_text();
$mowc_is_redirect_after_registration_enabled = $mowc_handler->isredirectToPageEnabled() ? 'checked' : '';

$mowc_registration_form_file = MOV_WC_DIR . 'views/forms/mowoocommerceregistrationform.php';
if ( ! MoUtility::mo_require_file( $mowc_registration_form_file, MOV_WC_DIR ) ) {
	return;
}
require $mowc_registration_form_file;
