<?php
/**
 * Outputs the Import and Export screen.
 *
 * @package WPZincDashboardWidget
 * @author WP Zinc
 */

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

?>
<div class="wrap">
	<h1 class="wp-heading-inline">
		<?php echo esc_html( $this->plugin->displayName ); ?>

		<span>
			<?php esc_html_e( 'Import &amp; Export', 'media-library-organizer' ); ?>
		</span>
	</h1>

	<hr class="wp-header-end" />

	<?php
	// Notices.
	if ( isset( $this->message ) ) {
		?>
		<div class="updated notice"><p><?php echo esc_html( $this->message ); ?></p></div>  
		<?php
	}
	if ( isset( $this->error_message ) ) {
		?>
		<div class="error notice"><p><?php echo esc_html( $this->error_message ); ?></p></div>  
		<?php
	}
	?>

	<div class="wrap-inner">
		<!-- Tabs -->
		<h2 class="nav-tab-wrapper wpzinc-horizontal-tabbed-ui">
			<a href="<?php echo esc_url( $this->plugin->documentation_url ); ?>" class="nav-tab last documentation" rel="noopener" target="_blank">
				<?php esc_html_e( 'Documentation', 'media-library-organizer' ); ?>
				<span class="dashicons dashicons-admin-page"></span>
			</a>
		</h2>

		<form name="post" method="post" action="<?php echo esc_url( add_query_arg( 'page', 'media-library-organizer-import-export', admin_url( 'admin.php' ) ) ); ?>" id="<?php echo esc_attr( $this->plugin->name ); ?>" enctype="multipart/form-data">
			<div id="poststuff">
				<div id="post-body" class="metabox-holder columns-1">
					<!-- Content -->
					<div id="post-body-content">
						<div id="normal-sortables" class="meta-box-sortables ui-sortable publishing-defaults">  
							<div class="postbox wpzinc-vertical-tabbed-ui">
								<!-- Second level tabs -->
								<ul class="wpzinc-nav-tabs wpzinc-js-tabs" data-panels-container="#import-export-container" data-panel=".panel" data-active="wpzinc-nav-tab-vertical-active">
									<li class="wpzinc-nav-tab download">
										<a href="#import" class="wpzinc-nav-tab-vertical-active">
											<?php esc_html_e( 'Import', 'media-library-organizer' ); ?>
										</a>
									</li>
									<?php
									if ( is_array( $import_sources ) && count( $import_sources ) > 0 ) {
										foreach ( $import_sources as $import_source ) {
											?>
											<li class="wpzinc-nav-tab download <?php echo esc_attr( $import_source['name'] ); ?>">
												<a href="#<?php echo esc_attr( $import_source['name'] ); ?>">
													<?php echo esc_html( $import_source['label'] ); ?>
												</a>
											</li>
											<?php
										}
									}
									?>
									<li class="wpzinc-nav-tab upload">
										<a href="#export">
											<?php esc_html_e( 'Export', 'media-library-organizer' ); ?>
										</a>
									</li>
								</ul>

								<!-- Content -->
								<div id="import-export-container" class="wpzinc-nav-tabs-content no-padding">
									<!-- Import -->
									<div id="import" class="panel">
										<div class="postbox">
											<header>
												<h3><?php esc_html_e( 'Import', 'media-library-organizer' ); ?></h3>
												<p class="description">
													<?php esc_html_e( 'Upload a file generated by this Plugin\'s export functionality (JSON or zipped JSON).  This will overwrite any existing settings stored on this installation.', 'media-library-organizer' ); ?>
												</p>
											</header>

											<div class="wpzinc-option">
												<div class="left">
													<label for="file"><?php esc_html_e( 'JSON File', 'media-library-organizer' ); ?></label>
												</div>
												<div class="right">
													<input type="file" id="file" name="import" />
												</div>
											</div>

											<div class="wpzinc-option">
												<input name="import" type="submit" class="button button-primary" value="<?php esc_attr_e( 'Import', 'media-library-organizer' ); ?>" />              
											</div>
										</div>
									</div>

									<?php
									if ( is_array( $import_sources ) && count( $import_sources ) > 0 ) {
										foreach ( $import_sources as $import_source ) {
											include_once $import_source['view'];
										}
									}
									?>

									<!-- Export -->
									<div id="export" class="panel">
										<div class="postbox">
											<header>
												<h3><?php esc_html_e( 'Export', 'media-library-organizer' ); ?></h3>
												<p class="description">
													<?php esc_html_e( 'To export this Plugin\'s settings, choose which item(s) to export, and click the Export button below.', 'media-library-organizer' ); ?>
													<br />
													<?php esc_html_e( 'You can then import the generated file into another Plugin installation.', 'media-library-organizer' ); ?>
													<br />
													<?php esc_html_e( 'Including this file in a support request? We recommend setting the Format option to "Export as JSON, Zipped".', 'media-library-organizer' ); ?>
												</p>
											</header>

											<?php
											/**
											 * Add any form options to the Export screen
											 *
											 * @since   1.0.0
											 */
											do_action( str_replace( '-', '_', $this->plugin->name ) . '_export_view' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName
											?>

											<div class="wpzinc-option">
												<div class="left">
													<label for="format"><?php esc_html_e( 'Format', 'media-library-organizer' ); ?></label>
												</div>
												<div class="right">
													<select name="format" id="format" size="1">
														<option value="json"><?php esc_html_e( 'JSON', 'media-library-organizer' ); ?></option>
														<option value="zip"><?php esc_html_e( 'JSON, Zipped', 'media-library-organizer' ); ?></option>
													</select>
												</div>
											</div>

											<div class="wpzinc-option">
												<input name="export" type="submit" class="button button-primary" value="<?php esc_attr_e( 'Export', 'media-library-organizer' ); ?>" />              
											</div>
										</div>
									</div>
								</div>
							</div>

							<?php
							wp_nonce_field( $this->plugin->name, $this->plugin->name . '_nonce' );
							?>
						</div>
						<!-- /normal-sortables -->
					</div>
					<!-- /post-body-content -->
				</div>
			</div> 
			<!-- /poststuff -->
		</form>
	</div><!-- /.wrap-inner -->
</div>
