<?php

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

if(!class_exists('FPD_Admin_Menus')) {

	class FPD_Admin_Menus {

		public function __construct() {

			add_action( 'admin_menu', array( &$this, 'add_menu_pages' ) );
			//add action links to plugins page
			add_filter( 'plugin_action_links_' . plugin_basename( FPD_PLUGIN_DIR ).'/fancy-product-designer.php', array( &$this, 'action_links' ) );

			add_action( 'fpd_backend_react_page_start', array( &$this, 'react_backend_head' ) );

		}

		public function add_menu_pages() {

			//add top level menu
			add_menu_page(
				__('Fancy Product Designer', 'radykal'),
				__('Fancy Product Designer', 'radykal'),
				Fancy_Product_Designer::CAPABILITY,
				'fancy_product_designer',
				array( $this, 'output_react' ),
				'data:image/svg+xml;base64, '
			);

			//add products menu
			add_submenu_page(
				'fancy_product_designer',
				 __('Products', 'radykal'),
				 __('Products', 'radykal'),
				 Fancy_Product_Designer::CAPABILITY,
				 'fancy_product_designer',
				 array( $this, 'output_react' )
			);

			//add product builder sub menu page to products menu
			add_submenu_page(
				'fancy_product_designer',
				 __('Product Builder', 'radykal'),
				 __('Product Builder', 'radykal'),
				 Fancy_Product_Designer::CAPABILITY,
				 'fpd_product_builder',
				 array( $this, 'output_react' )
			);

			//add ui&layou composer sub menu page to products menu
			add_submenu_page(
				'fancy_product_designer',
				 __('UI Composer', 'radykal'),
				 __('UI Composer', 'radykal'),
				 Fancy_Product_Designer::CAPABILITY,
				 'fpd_ui_layout_composer',
				 array( $this, 'output_react' )
			);

			//add designs sub menu page to products menu
			add_submenu_page(
				'fancy_product_designer',
				__('Designs', 'radykal'),
				__('Designs', 'radykal'),
				Fancy_Product_Designer::CAPABILITY,
				'fpd_manage_designs',
				array( $this, 'output_react' )
			);

			//add orders sub menu page to products menu
			add_submenu_page(
				'fancy_product_designer',
				__('Shortcode Orders', 'radykal'),
				__('Shortcode Orders', 'radykal'),
				Fancy_Product_Designer::CAPABILITY,
				'fpd_orders',
				array( $this, 'output_react' )
			);

			add_submenu_page(
				'fancy_product_designer',
				 __('Pricing Rules', 'radykal'),
				 __('Pricing Rules', 'radykal'),
				 Fancy_Product_Designer::CAPABILITY,
				 'fpd_pricing_builder',
				 array( $this, 'output_react' )
			);

			add_submenu_page(
				'fancy_product_designer',
				__('Settings', 'radykal'),
				__('Settings', 'radykal'),
				Fancy_Product_Designer::CAPABILITY,
				'fpd_settings',
				array( $this, 'output_react' )
			);

			add_submenu_page(
				'fancy_product_designer',
				__('Status & Tools', 'radykal'),
				__('Status & Tools', 'radykal'),
				Fancy_Product_Designer::CAPABILITY,
				'fpd_status',
				array( $this, 'status_page' )
			);

			add_submenu_page(
				'fancy_product_designer',
				__('Genius', 'radykal'),
				__('Genius', 'radykal'),
				Fancy_Product_Designer::CAPABILITY,
				'fpd_genius',
				array( $this, 'genius_page' )
			);

		}

		public function action_links( $links ) {

			return array_merge( array(
				'<a href="' . esc_url( 'http://support.fancyproductdesigner.com/' ) . '" target="_blank">' . __( 'Documentation', 'radykal' ) . '</a>',
			), $links );

		}

		public function output_react( ) {

			//FPD_Install::do_upgrade('6.1.0');

			if( isset($_GET['update']) ) {
				FPD_Install::do_upgrade($_GET['update']);
			}

			do_action( 'fpd_backend_react_page_start' );

			?>
			<div id="fpd-react-root"></div>
			<?php

			do_action( 'fpd_backend_react_page_end' );

		}

		public function status_page() {

			require_once(FPD_PLUGIN_ADMIN_DIR . '/views/status-tools.php' );

		}

		public function genius_page() {

			require_once(FPD_PLUGIN_ADMIN_DIR . '/views/genius.php' );

		}

		public function react_backend_head( $page ) {

			if( $_GET['page'] == 'fancy_product_designer' ) {

				$no_user_products = FPD_Product::get_products( array(
					'where' 	=> 'user_id IS NULL'
				) );

				//update V3.9.7
				if( isset($_POST['fpd_update_products_to_user']) ) {

					foreach( $no_user_products as $no_user_product ) {

						$fpd_update_product = new FPD_Product( $no_user_product->ID );
						$fpd_update_product->update( null, null, null, $_POST['fpd_update_products_to_user']);

					}

					update_option( 'fpd_updater_dokan_hidden', 'yes' );

				}

				if( sizeof($no_user_products) > 0 && get_option('fpd_updater_dokan_hidden', 'no') !== 'yes' ): ?>
				<div class="ui container">
					<h3>Update to V3.9.7</h3>
					<p>Your action is required to update to the latest version. Please select an user who will be assigned as creator for your current FPD products.</p>
					<form method="POST">
						<p>
							<select name="fpd_update_products_to_user">
								<?php

								$users = get_users( array('fields' => array('ID', 'user_nicename')) );

								foreach( $users as $user ) {
									echo '<option value="'.$user->ID.'">'.$user->user_nicename.'</option>';
								}

								?>
							</select>
							<input type="submit" name="run_updater" value="Run Updater" class="button-primary" style="margin-left: 20px;" />
						</p>
					</form>
				</div>
				<?php endif;

				require_once( FPD_PLUGIN_ADMIN_DIR.'/class-admin-import.php' );

				//test
				if ( isset($_GET['info']) && $_GET['info'] == 'activated' ) {
			        require_once(FPD_PLUGIN_ADMIN_DIR.'/views/modal-updated-installed-info.php');
			    }

				?>
				<form action="<?php echo admin_url('admin.php?page=fancy_product_designer'); ?>" method="POST" enctype="multipart/form-data" name="fpd_import" style="display: none !important;">
					<input type="checkbox" name="fpd_import_to_library" />
					<input name="fpd_import_file" type="file" accept=".zip" />
                    <?php wp_nonce_field( 'import', '_fpdnonce' ); ?>
				</form>
				<?php

			}
			else if( $_GET['page'] == 'fpd_orders' ) {

				echo '<div class="ui container">';
				fpd_output_admin_notice(
					'info',
					 __( 'WooCommerce Orders', 'radykal' ),
					 __( 'Orders made with WooCommerce can be viewed in the order details of a WooCommerce order!', 'radykal' ),
					 class_exists('WooCommerce'),
					 'wc_orders_in_sc_orders',
					 true
				);
				echo '</div>';

			}

		}

	}
}

new FPD_Admin_Menus();

?>