<?php
/**
 * Data Export screen template
 *
 * @link       https://personalizewp.com/
 * @since      3.3.0
 *
 * @package    PersonalizeWP
 * @subpackage PersonalizeWP/Admin
 */

namespace PersonalizeWP\Admin;

use PersonalizeWP\Admin\Utils;
use PersonalizeWP\Admin\PageDataExport;
use PersonalizeWP\Admin\ListTableExportRequests;

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

// Pre-selected visitor.
$user_id = isset( $_GET['user_id'] ) ? (int) sanitize_text_field( wp_unslash( $_GET['user_id'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: used as intval
// Default export to all visitors.
$export_default = 'all_visitors';
if ( $user_id ) {
	$export_default = 'single_visitor';
}
?>

<main class="section entity-panel">

	<?php require_once plugin_dir_path( __DIR__ ) . '/_other/contacts-nav.php'; ?>

	<section>

		<!-- Export Form -->
		<div class="export-form-section pwp-panel">
			<div class="section-description">
				<p><?php esc_html_e( 'Choose from the options below to export Visitor Profile data from the database. When you click the export button below, PersonalizeWP will create a CSV file and send a link to the email address you enter so that you can download it to your computer.', 'personalizewp' ); ?></p>
			</div>
			<form class="pwp-form flow" method="post" action="<?php echo esc_url( admin_url( PageDataExport::$request_export ) ); ?>">
				<?php wp_nonce_field( 'pwp-export-request' ); ?>

				<fieldset class="flow">
					<label for="export-type"><?php esc_html_e( 'Select Data to Export', 'personalizewp' ); ?></label>
					<select data-selection-conditions name="export_type" id="export-type" class="form-control">
					<?php
					/**
					 * Filter the available export types.
					 *
					 * @since 3.3.0
					 *
					 * @param array $export_types Array of export types to export.
					 */
					$export_types = apply_filters(
						'personalizewp_export_data_types',
						array(
							'all_visitors'   => __( 'All Users', 'personalizewp' ),
							'single_visitor' => __( 'Single Visitor', 'personalizewp' ),
						)
					);
					/**
					 * Filter the default selected export type.
					 *
					 * @since 3.4.0
					 *
					 * @param string $export_default Selected default export type.
					 */
					$export_default = apply_filters(
						'personalizewp_export_default_export_type',
						$export_default
					);
					foreach ( $export_types as $key => $label ) :
						?>
						<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $export_default ); ?>><?php echo esc_html( $label ); ?></option>
						<?php
					endforeach;
					?>
					</select>

					<?php
					/**
					 * Action for additional fields within export form.
					 *
					 * @since 3.3.0
					 */
					do_action( 'personalizewp_export_after_select_data_types' );
					?>

					<div class="flow" <?php echo 'single_visitor' !== $export_default ? 'hidden' : ''; ?>
						data-selection-condition="single_visitor">
						<label for="visitor-search"><?php esc_html_e( 'Select User', 'personalizewp' ); ?></label>
						<div class="condition">
							<auto-complete
								mode="user"
								placeholder="<?php esc_attr_e( 'Start typing user name...', 'personalizewp' ); ?>"
								class="visitor-autocomplete single-selection"
								label-id="visitor-search"
								max-items="1"
								content="<?php
								// Do we have a pre-selected user?
								if ( ! empty( $user_id ) ) :
									$_user = get_user( $user_id );
									if ( $_user ) {
										$selected_users = array(
											array(
												'value' => $user_id,
												'text'  => $_user->display_name,
											),
										);
										echo esc_attr( wp_json_encode( $selected_users ) );
									}
								endif;
								?>">
								<template hidden>
									<input type="hidden" name="wp_user_id" value="" class="visitor-id-input" />
									<span></span>
									<button type="button" class="button" aria-label="<?php esc_attr_e( 'Remove', 'personalizewp' ); ?>">
										<?php Utils::svg_icon( 'cross' ); ?>
									</button>
								</template>
							</auto-complete>
						</div>
					</div>

					<div class="flow" <?php echo 'all_visitors' !== $export_default ? 'hidden' : ''; ?>
						data-selection-condition="all_visitors">
						<label for="include-anonymous-visitors">
							<input
								type="checkbox" role="switch"
								id="include-anonymous-visitors"
								name="include_anonymous"
								checked
							/>
							<?php esc_html_e( 'Include anonymous visitors', 'personalizewp' ); ?></label>
					</div>
				</fieldset>

				<fieldset class="flow">
					<label for="event-filter"><?php esc_html_e( 'Select Events', 'personalizewp' ); ?></label>
					<select data-selection-conditions name="event_filter" id="event-filter" class="form-control">
						<option value="all_events" selected><?php esc_html_e( 'All Events', 'personalizewp' ); ?></option>
						<option value="single_event"><?php esc_html_e( 'Single Event', 'personalizewp' ); ?></option>
					</select>

					<div class="flow" data-selection-condition="single_event" hidden>
						<label for="event-type"><?php esc_html_e( 'Select Event Type', 'personalizewp' ); ?></label>
						<select name="event_type" id="event-type" class="form-control">
							<option value="" selected><?php esc_html_e( 'Event Type', 'personalizewp' ); ?></option>
							<?php
							// Use all registered events.
							$all_event_types = personalizewp()->get_registered_event_types();
							if ( ! empty( $all_event_types ) ) {
								$event_types = wp_list_pluck( $all_event_types, 'label', 'name' );
							}

							/**
							 * Filter event types for export.
							 *
							 * @since 3.3.0
							 *
							 * @param array $event_types Event types
							 */
							$event_types = apply_filters( 'personalizewp_export_event_types', $event_types );

							// Order alphabetically.
							asort( $event_types );

							foreach ( $event_types as $key => $label ) :
								?>
								<option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $label ); ?></option>
								<?php
							endforeach;
							?>
						</select>
					</div>
				</fieldset>

				<fieldset class="flow">
					<label for="date-range"><?php esc_html_e( 'Select Date Range', 'personalizewp' ); ?></label>
					<div class="cluster">
						<div class="date-field">
							<label for="export-start-date" class="screen-reader-text"><?php esc_html_e( 'Start Date', 'personalizewp' ); ?></label>
							<input type="date" name="start_date" id="export-start-date" class="form-control" placeholder="<?php esc_attr_e( 'Start Date', 'personalizewp' ); ?>" max="<?php echo esc_attr( current_time( 'Y-m-d' ) ); ?>" />
						</div>
						<div class="date-field">
							<label for="export-end-date" class="screen-reader-text"><?php esc_html_e( 'End Date', 'personalizewp' ); ?></label>
							<input type="date" name="end_date" id="export-end-date" class="form-control" placeholder="<?php esc_attr_e( 'End Date', 'personalizewp' ); ?>" max="<?php echo esc_attr( current_time( 'Y-m-d' ) ); ?>" />
						</div>
					</div>
				</fieldset>

				<fieldset class="flow">
					<label for="email"><?php esc_html_e( 'Enter email address', 'personalizewp' ); ?></label>
					<input type="email" name="email" id="email" class="form-control" placeholder="<?php esc_attr_e( 'Enter email address', 'personalizewp' ); ?>" />
				</fieldset>

				<?php
				/**
				 * Action for additional fields within export form, immediately before submit button.
				 *
				 * @since 3.3.0
				 */
				do_action( 'personalizewp_export_before_submit_button' );
				?>

				<div class="flow">
					<input type="submit" name="submit_export" class="button button-primary primary btn" value="<?php esc_attr_e( 'Download Export', 'personalizewp' ); ?>" />
				</div>
			</form>
		</div>
	</section>

	<!-- Previous Exports Section -->
	<section class="previous-exports">
		<header>
			<h2><?php esc_html_e( 'Previous Exports', 'personalizewp' ); ?></h2>
		</header>

		<?php
		$export_list_table = new ListTableExportRequests();
		$export_list_table->prepare_items();
		?>
		<form method="post" data-pwp-exports>
			<input type="hidden" name="page" value="<?php echo esc_attr( PageDataExport::SLUG ); ?>" />
			<?php $export_list_table->display(); ?>
		</form>
	</section>

</main>
