<?php
/**
 * Admin view: CSV export, with a column picker.
 *
 * @package BitForgeNet_Guest_Tickets
 */

defined( 'ABSPATH' ) || exit;

// This file is a template: it is included from a method -- never at file
// scope -- so everything declared here belongs to that method's variable
// scope and not to the global one. PHPCS reads each file on its own and has
// no way to see where the include happens, so it reads these as globals.
// Stated once, for the whole file, per this plugin's suppression rule.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$bfnet_what  = isset( $_GET['what'] ) && 'guests' === sanitize_key( wp_unslash( $_GET['what'] ) ) ? 'guests' : 'orders'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen state.
$bfnet_event = isset( $_GET['event'] ) ? (int) $_GET['event'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen state.

$bfnet_defaults  = BFNET_Export::defaults( $bfnet_what );
$bfnet_remember  = get_user_meta( get_current_user_id(), 'bfnet_export_fields_' . $bfnet_what, true );
$bfnet_selected  = is_array( $bfnet_remember ) && $bfnet_remember ? BFNET_Export::chosen( $bfnet_what, $bfnet_remember ) : $bfnet_defaults;
$bfnet_catalogue = BFNET_Export::catalogue( $bfnet_what );
$bfnet_groups    = BFNET_Export::group_labels();
$bfnet_events    = BFNET_Event::query( array( 'limit' => 100, 'orderby' => 'starts_at', 'order' => 'DESC' ) );
$bfnet_total     = count( BFNET_Export::available( $bfnet_what ) );
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = __( 'Export', 'bitforgenet-guest-tickets' );
	$bfnet_subtitle = __( 'Tick the columns you want. The essentials are already chosen; everything else is here when you need it.', 'bitforgenet-guest-tickets' );
	$bfnet_actions  = '<a class="button" href="' . esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders' ) ) . '">' . esc_html__( 'Back to subscribers', 'bitforgenet-guest-tickets' ) . '</a>';
	include BFNET_PATH . 'admin/views/partial-masthead.php';
	?>

	<div class="bfnet-panel">
		<div class="bfnet-panel__head">
			<div class="bfnet-tabs">
				<?php foreach ( BFNET_Export::sets() as $bfnet_key => $bfnet_label ) : ?>
					<a class="<?php echo $bfnet_key === $bfnet_what ? 'is-active' : ''; ?>"
						href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-orders', array( 'action' => 'export', 'what' => $bfnet_key, 'event' => $bfnet_event ) ) ); ?>">
						<?php echo esc_html( $bfnet_label ); ?>
					</a>
				<?php endforeach; ?>
			</div>
		</div>

		<form method="post" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" data-bfnet-fields
			data-bfnet-fields-default="<?php echo esc_attr( wp_json_encode( $bfnet_defaults ) ); ?>">
			<?php wp_nonce_field( 'bfnet_export_csv', '_bfnet_nonce' ); ?>
			<input type="hidden" name="page" value="bitforgenet-guest-tickets-orders">
			<input type="hidden" name="bfnet_do" value="export_csv">
			<input type="hidden" name="what" value="<?php echo esc_attr( $bfnet_what ); ?>">

			<div class="bfnet-panel__body">
				<div class="bfnet-field" style="max-width:420px">
					<label class="bfnet-label" for="bfnet-x-event"><?php esc_html_e( 'Event', 'bitforgenet-guest-tickets' ); ?></label>
					<select id="bfnet-x-event" name="event">
						<option value="0"><?php esc_html_e( 'All events', 'bitforgenet-guest-tickets' ); ?></option>
						<?php foreach ( $bfnet_events as $bfnet_row ) : ?>
							<option value="<?php echo (int) $bfnet_row->id; ?>" <?php selected( $bfnet_event, (int) $bfnet_row->id ); ?>><?php echo esc_html( $bfnet_row->title ); ?></option>
						<?php endforeach; ?>
					</select>
				</div>

				<div class="bfnet-fields__bar">
					<div class="bfnet-inline">
						<button type="button" class="button button-small" data-bfnet-fields-preset="essentials"><?php esc_html_e( 'Essentials', 'bitforgenet-guest-tickets' ); ?></button>
						<button type="button" class="button button-small" data-bfnet-fields-preset="all"><?php esc_html_e( 'Everything', 'bitforgenet-guest-tickets' ); ?></button>
						<button type="button" class="button button-small" data-bfnet-fields-preset="none"><?php esc_html_e( 'Clear', 'bitforgenet-guest-tickets' ); ?></button>
					</div>
					<p class="description" data-bfnet-fields-count>
						<?php
						/* translators: 1: chosen columns, 2: available columns. */
						echo esc_html( sprintf( __( '%1$d of %2$d columns', 'bitforgenet-guest-tickets' ), count( $bfnet_selected ), $bfnet_total ) );
						?>
					</p>
				</div>
			</div>

			<div class="bfnet-panel__body" style="padding-top:0">
				<?php foreach ( $bfnet_catalogue as $bfnet_group => $bfnet_keys ) : ?>
					<?php // A group holding a ticked column opens: a checked box hidden behind a summary is a box nobody knows about. ?>
					<?php $bfnet_open = (bool) array_intersect( $bfnet_keys, $bfnet_selected ); ?>
					<details class="bfnet-more" <?php echo $bfnet_open ? 'open' : ''; ?>>
						<summary>
							<?php echo esc_html( isset( $bfnet_groups[ $bfnet_group ] ) ? $bfnet_groups[ $bfnet_group ] : $bfnet_group ); ?>
							<span class="bfnet-table__sub">
								<?php
								$bfnet_on = count( array_intersect( $bfnet_keys, $bfnet_selected ) );
								echo esc_html( $bfnet_on ? sprintf( '%d / %d', $bfnet_on, count( $bfnet_keys ) ) : sprintf( '%d', count( $bfnet_keys ) ) );
								?>
							</span>
						</summary>

						<div class="bfnet-more__body">
						<div class="bfnet-inline" style="margin:0 0 10px">
							<button type="button" class="button button-small" data-bfnet-fields-group="on"><?php esc_html_e( 'Select all', 'bitforgenet-guest-tickets' ); ?></button>
							<button type="button" class="button button-small" data-bfnet-fields-group="off"><?php esc_html_e( 'None', 'bitforgenet-guest-tickets' ); ?></button>
						</div>

						<div class="bfnet-fields">
							<?php foreach ( $bfnet_keys as $bfnet_key ) : ?>
								<label class="bfnet-fields__item">
									<input type="checkbox" name="fields[]" value="<?php echo esc_attr( $bfnet_key ); ?>" <?php checked( in_array( $bfnet_key, $bfnet_selected, true ) ); ?>>
									<span>
										<?php echo esc_html( BFNET_Export::label( $bfnet_key ) ); ?>
										<em><?php echo esc_html( $bfnet_key ); ?></em>
									</span>
								</label>
							<?php endforeach; ?>
						</div>
						</div>
					</details>
				<?php endforeach; ?>
			</div>

			<div class="bfnet-panel__head" style="border-top:1px solid #dcdcde;border-bottom:0">
				<button class="button button-primary"><?php esc_html_e( 'Download CSV', 'bitforgenet-guest-tickets' ); ?></button>
				<p class="description"><?php esc_html_e( 'Your choice is remembered for next time.', 'bitforgenet-guest-tickets' ); ?></p>
			</div>
		</form>
	</div>
</div>
