<?php
/**
 * This class creates setting page functionality for Product enquiry
 *
 * @package PE/Admin
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
 * Class for PEFree Settings page.
 */
class PE_Admin_Settings_Products {
	/**
	 * The single instance of the class.
	 *
	 * @var pe_instance
	 */
	protected static $instance = null;
	/**
	 * __constructor
	 */
	public function __construct() {
		add_action( 'admin_menu', array( $this, 'pefree_menu_page' ) );
	}

	/**
	 * Ensures only one instance of class is loaded or can be loaded.
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Adds PFREE settings menu page.
	 *
	 * @return void
	 */
	public function pefree_menu_page() {
		// Fine original icon here. WDM_PE_PLUGIN_URL . 'assets/admin/img/pep-icon.svg'
		$pep_menu_icon = ' ';

		add_menu_page( __( 'Product Enquiry', 'product-enquiry-for-woocommerce' ), __( 'Product Enquiry', 'product-enquiry-for-woocommerce' ), 'manage_options', 'product-enquiry-for-woocommerce', array( $this, 'add_ask_product_settings' ), 'data:image/svg+xml;base64,' . $pep_menu_icon, 59 );
	}
	/**
	 * Function to addproduct settings to admin menu this is callback method called on add_menu_page method.
	 */
	public function add_ask_product_settings() {

		wp_enqueue_script( 'wdm_wpi_validation' );
		$nonce = wp_create_nonce( 'wdm-wpi-validation-nonce' );
		wp_localize_script( 'wdm_wpi_validation', 'wdm_wpi_validation_nonce', array( 'nonce' => $nonce ) );
		wp_enqueue_script( 'postbox' );
		?>
		<div class="wrap wdm_leftwrap">
			<?php
			if ( isset( $_POST['save_settings_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['save_settings_nonce'] ), 'wdm-wpi-validation-nonce' ) && isset( $_GET['tab'] ) ) {
				$active_tab = sanitize_text_field( $_GET['tab'] );
			} else {
				$active_tab = 'form';
			}

			$dashboard_tabs = array(
				'form'             => array(
					'title' => __( 'Enquiry Settings', 'product-enquiry-for-woocommerce' ),
					'class' => 'enquiry-settings-tab',
				),
				'quotation'        => array(
					'title' => __( 'Quotation', 'product-enquiry-for-woocommerce' ),
					'class' => 'quotation-tab',
				),
				'entry'            => array(
					'title' => __( 'Enquiry Details', 'product-enquiry-for-woocommerce' ),
					'class' => 'enquiry-details-tab',
				),
				'other_extensions' => array(
					'title' => __( 'Other Extensions', 'product-enquiry-for-woocommerce' ),
					'class' => 'other-extensions-tab',
				),
			);
			$dashboard_tabs = apply_filters( 'product_enquiry_dashboard_tab_titles', $dashboard_tabs );
			?>
		<h2 class="nav-tab-wrapper">  
			<?php
			foreach ( $dashboard_tabs as $single_tab_slug => $single_tab_title ) {
				/* translators: sup HTML tag */
				$sup_pro = ( 'quotation' === $single_tab_slug ) || ( 'entry' === $single_tab_slug ) ? sprintf( __( '%1$s PRO %2$s', 'product-enquiry-for-woocommerce' ), '<sup>', '</sup>' ) : '';
				?>
				<a href="admin.php?page=product-enquiry-for-woocommerce&tab=<?php echo esc_attr( $single_tab_slug ); ?>" class="nav-tab <?php echo esc_attr( $active_tab === $single_tab_slug ? 'nav-tab-active' : '' ); ?> <?php echo esc_attr( $single_tab_title['class'] ); ?>">
					<?php
						echo esc_html( $single_tab_title['title'] );
						echo wp_kses_post( $sup_pro );
					?>
				</a>
			<?php } ?>
			<a href="admin.php?page=product-enquiry-for-woocommerce&tab=premium" class="premium nav-tab <?php echo esc_attr( ( 'premium' === $active_tab ) ? 'nav-tab-active' : '' ); ?>"><?php esc_attr_e( 'Premium Version', 'product-enquiry-for-woocommerce' ); ?></a>
		</h2>  
			<?php
			// Enqueue upsell assets on settings pages
			wp_enqueue_style( 'pefree-upsell' );
			wp_enqueue_script( 'pefree-upsell' );
			wp_localize_script(
				'pefree-upsell',
				'pefreeUpsell',
				array(
					'title'   => __( 'Go Pro to unlock this setting', 'product-enquiry-for-woocommerce' ),
					'message' => __( 'This option is available in the Pro version. Upgrade to enable quotation workflow, PDF quotes, multiproduct enquiries, and more.', 'product-enquiry-for-woocommerce' ),
					'ctaText' => __( 'Purchase Pro', 'product-enquiry-for-woocommerce' ),
					'demoText'=> __( 'View Demo', 'product-enquiry-for-woocommerce' ),
					'proUrl'  => 'https://wisdmlabs.com/woocommerce-quotation-and-inquiry/?utm_source=pefree_admin&utm_medium=upsell_modal&utm_campaign=pefree_admin',
					'demoUrl' => 'https://wisdmlabs.com/pep-demo/',
				)
			);
			if ( 'entry' === $active_tab ) {
				$form_tab = PE_Admin_Settings_Enquiry_Details_Tab::instance();
				$form_tab->entry_tab_functionality_helper();
			} elseif ( 'other_extensions' === $active_tab ) {
				$form_tab = PE_Admin_Settings_Other_Extensions_Tab::instance();
				$form_tab->other_extensions_tab_functionality_helper();
			} elseif ( 'premium' === $active_tab ) {
				$form_tab = PE_Admin_Settings_Premium_Tab::instance();
				$form_tab->premium_tab_functionality_helper();

			} elseif ( 'form' === $active_tab ) {
				render_pro_banner();
				$form_tab = PE_Admin_Settings_Form_Tab::instance();
				$form_tab->form_tab_functionality_helper();
			} elseif ( 'quotation' === $active_tab ) {
				render_pro_banner();
				$form_tab = PE_Admin_Settings_Quotation_Tab::instance();
				$form_tab->quotation_tab_functionality_helper();
			}
			do_action( 'product_enquiry_tab_content', $active_tab );
			?>
		</div>
		<?php
		PE_Admin_Newsletter_Subcribe::generate_form();
		// add styles for settings page.
		wp_enqueue_style( 'wdm-admin-css', WDM_PE_PLUGIN_URL . 'assets/admin/css/wpi_admin.css', array(), PEFREE_VERSION );
	}
}
