<?php
/**
 * Loads the tav view and initializes other controllers.
 *
 * @package miniorange-sms-order-notification-otp-verification/controllers/
 */

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\MoActionHandlerHandler;
use WCSMSOTP\Helper\MoUtility;
use WCSMSOTP\Objects\TabDetails;
use WCSMSOTP\Objects\SubTabDetails;

$mowc_registered               = MoUtility::micr();
$mowc_activated                = MoUtility::mclv();
$mowc_gatewayconfigured        = MoUtility::is_gateway_config();
$mowc_plan                     = MoUtility::micv();
$mowc_disabled                 = ( ( $mowc_registered && $mowc_activated ) || ( strcmp( MOV_WC_TYPE, 'MiniOrangeGateway' ) === 0 ) ) ? '' : 'disabled';
$mowc_mo_current_user          = wp_get_current_user();
$mowc_email                    = get_mo_wc_option( 'admin_email' );
$mowc_phone                    = get_mo_wc_option( 'admin_phone' );
$mowc_controller               = MOV_WC_DIR . 'controllers/';
$mowc_notifications_controller = MOV_WC_DIR . 'notifications/wcsmsnotification/controllers/';
$mowc_admin_handler            = MoActionHandlerHandler::instance();
$mowc_req_url                  = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
$mowc_addon                    = add_query_arg( array( 'page' => 'addon' ), remove_query_arg( 'addon', $mowc_req_url ) );



$mowc_tab_details     = TabDetails::instance();
$mowc_sub_tab_details = SubTabDetails::instance();

echo "<div id='main-outer-div' class='w-full flex flex-col border bg-white'>";

$mowc_titlebar_file = $mowc_controller . 'titlebar.php';
if ( ! MoUtility::mo_require_file( $mowc_titlebar_file, MOV_WC_DIR ) ) {
	return;
}
require $mowc_titlebar_file;
$mowc_errormessage_file = $mowc_controller . 'errormessage.php';
if ( ! MoUtility::mo_require_file( $mowc_errormessage_file, MOV_WC_DIR ) ) {
	return;
}
require $mowc_errormessage_file;

echo '<div id="nav-and-view" class="flex">';

		$mowc_navbar_file = $mowc_controller . 'navbar.php';
if ( ! MoUtility::mo_require_file( $mowc_navbar_file, MOV_WC_DIR ) ) {
	return;
}
		require $mowc_navbar_file;

echo "  <div class='flex-1'>
            <div id='moblock' class='mo_customer_validation-modal-backdrop dashboard'>" .
				"<img src='" . esc_url( MOV_WC_LOADER_URL ) . "'>" .
		'</div>';
		$mowc_subtabs_file = $mowc_controller . 'subtabs.php';
if ( ! MoUtility::mo_require_file( $mowc_subtabs_file, MOV_WC_DIR ) ) {
	return;
}
		require $mowc_subtabs_file;
if ( isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended -- Reading GET parameter from the URL for checking the tab name, doesn't require nonce verification.

	foreach ( $mowc_tab_details->tab_details as $mowc_mo_tabs ) {
		if ( sanitize_text_field( wp_unslash( $_GET['page'] ) ) === $mowc_mo_tabs->menu_slug ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended -- Reading GET parameter from the URL for checking the tab name, doesn't require nonce verification.
			$mowc_view_file = $mowc_controller . $mowc_mo_tabs->view;
			if ( ! MoUtility::mo_require_file( $mowc_view_file, MOV_WC_DIR ) ) {
				continue;
			}
			include $mowc_view_file;
		}
	}

	do_action( 'mo_otp_verification_add_on_controller' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Hook is used for extensibility/backward compatibility.
	$mowc_support_file = $mowc_controller . 'support.php';
	if ( ! MoUtility::mo_require_file( $mowc_support_file, MOV_WC_DIR ) ) {
		return;
	}
	include $mowc_support_file;
}

echo '  </div>';
echo '</div>';

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