<?php
 /**
  * Handles the POPUP after plugin activation
  *
  * @package StockSyncWithGoogleSheetForWooCommerce
  * @since 1.3.2
  */
 
 // Namespace.
 namespace StockSyncWithGoogleSheetForWooCommerce;
 
 // Exit if accessed directly.
 defined('ABSPATH') || exit;
 if ( ! class_exists('\StockSyncWithGoogleSheetForWooCommerce\DOKAN') ) {
	/**
	 * Add submenu in the dokan  dashboard
	 *
	 * @varsion 1.0.0
	 */
	class DOKAN {
		/**
		 * Object Calling method
		 *
		 * @varsion 1.0.0
		 */
		public function __construct() {
			add_filter( 'dokan_query_var_filter', [ $this, 'flexstock_dokan_load_document_menu' ] );
			add_filter( 'dokan_get_dashboard_nav', [ $this, 'flexstock_dokan_add_subscriber_menu' ] );
			add_action( 'dokan_load_custom_template', [ $this, 'flexstock_dokan_load_template' ] );
			add_filter( 'dokan_dashboard_nav_settings_key', [ $this, 'flexstock_key' ], 10, 1 );
			add_action( 'admin_init', [ $this, 'maybe_flush_rewrite_rules' ] );
		}
		public function maybe_flush_rewrite_rules() {
			if ( get_transient( 'ssgsw_flush_rewrite_rules_on_next_load' ) ) {
				flush_rewrite_rules( true );
				delete_transient( 'ssgsw_flush_rewrite_rules_on_next_load' ); // Clear the flag
			}
    	}
		/**
		 * Push flexstock key in dokan dashboard menu
		 *
		 * @param array $menu dokan menu key list.
		 * @varsion 1.0.0
		 */
		public function flexstock_key( $menu ) {
			global $wp;
			if ( isset( $wp->query_vars['flexstock'] ) ) {
				return 'flexstock';
			}
			return $menu;
		}
		/**
		 * Load flexstock document menu in dokan multivendor
		 *
		 * @param array $query_vars push flexstock url.
		 * @varsion 1.0.0
		 */
		public function flexstock_dokan_load_document_menu( $query_vars ) {
			$query_vars['flexstock'] = 'flexstock';

			return $query_vars;
		}
		/**
		 * Add Stock Menu in dokan multivendor.
		 *
		 * @param array $menus dokan multivendor urls.
		 * @varsion 1.0.0
		 */
		public function flexstock_dokan_add_subscriber_menu( $menus ) {
		     $title = get_option('ssgsw_vendor_menu_name','Sync GSheet');
			$custom_menus = [
				'flexstock' => [
					'title'   => $title.'<span class="flexstock_badge">New</span>',
					 'icon'  => sprintf(
						'<span class="snw-dokan-icon" style="width: 20px; height: 21px; margin-right: 5px; margin-bottom: -4px; background-image: url(\'%s/images/multi-dashboard.svg\'); background-size: contain; background-repeat: no-repeat; color: transparent;">New</span>',
						SSGSW_PUBLIC
					 ),
					'url'     => dokan_get_navigation_url( 'flexstock' ),
					'pos'     => 60,
				],
			];
			return array_merge( $menus, $custom_menus );
		}
		/**
		 * Load Stock Notifier Template.
		 *
		 * @param array $query_vars dokan query vars list.
		 */
		public function flexstock_dokan_load_template( $query_vars ) {
			if ( isset( $query_vars['flexstock'] ) ) {
				?>
				<?php
				/**
				 *  Dokan Dashboard Template
				 *
				 *  Dokan Main Dahsboard template for Fron-end
				 *
				 * @since 2.4
				 *
				 * @package dokan
				 */
				?>
				<div class="dokan-dashboard-wrap" xmlns="http://www.w3.org/1999/html">
					<style>
						.dokan-dashboard .dokan-dashboard-content{
							overflow: visible !important;
						}
						.ssgsw-wrapper .start-setup, .ssgsw-wrapper .install-woocommerce {
							width: 100% !important;
							height: 100% !important;
						}
					</style>
					<?php
					/**
					 * Dokan_dashboard_content_before hook
					 *
					 * @hooked get_dashboard_side_navigation
					 *
					 * @since 2.4
					 */
					do_action( 'dokan_dashboard_content_before' );
					?>

					<div class="dokan-dashboard-content">
						<article class="help-content-area">                            
							<?php
							/**
							 * Load flexStock setting
							*
							* @version 1.0.0
							*/
								do_action('flex_stock_vendor_setting');
							?> 
						</article><!-- .dashboard-content-area -->

						<?php
						/**
						 * Dokan_dashboard_content_inside_after hook
						 *
						 * @since 2.4
						 */
						do_action( 'flexstock_dokan_dashboard_content_inside_after' );
						?>
					</div><!-- .dokan-dashboard-content -->
					<?php
					/**
					 * Dokan_dashboard_content_after hook
					 *
					 * @since 2.4
					 */
					do_action( 'flexstock_dokan_dashboard_content_after' );
					?>
				</div><!-- .dokan-dashboard-wrap -->

				<?php
			}
			
		}
	}
	new DOKAN();
}
