<?php
/**
 * Admin view: edit an event, one subject per tab.
 *
 * Deliberately not a post editor — this is a purpose-built event form.
 *
 * Sixty-six fields on one page is a screen nobody reads to the bottom, and a
 * form that posts all sixty-six every time is a form that can quietly empty a
 * field it never showed. Each tab now declares the fields it owns; the server
 * writes those and leaves the rest of the event exactly as it was.
 *
 * Publishing is not one of the tabs. It is not a subject, it is a decision —
 * and it belongs where you take it, in the save bar, on every tab.
 *
 * @package BitForgeNet_Guest_Tickets
 *
 * @var object|null $event Event row, or null when creating.
 */

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

$is_new     = ! $event;
$organizers = BFNET_Organizer::all( '' );
$venues     = BFNET_Venue::all( '' );
$templates  = BFNET_Template::all();
$lineup     = $event ? BFNET_Event::lineup( $event ) : array();
$currencies = BFNET_Util::currencies();
$pages      = get_pages( array( 'number' => 200, 'sort_column' => 'post_title' ) );

/**
 * Format a MySQL datetime for a datetime-local input.
 *
 * @param string $value MySQL datetime.
 * @return string
 */
$bfnet_dt = static function ( $value ) {
	if ( empty( $value ) || '0000-00-00 00:00:00' === $value ) {
		return '';
	}
	return gmdate( 'Y-m-d\TH:i', strtotime( $value ) );
};

$cover_id  = $event ? (int) $event->cover_id : 0;
$cover_url = $cover_id ? wp_get_attachment_image_url( $cover_id, 'thumbnail' ) : '';

$bfnet_tabs = array(
	'event'   => array(
		'label' => __( 'Event', 'bitforgenet-guest-tickets' ),
		'blurb' => __( 'The name, the story and the picture. Reused on the booking page, in the emails and on every ticket.', 'bitforgenet-guest-tickets' ),
		'keys'  => array( 'title', 'slug', 'subtitle', 'series', 'edition', 'description', 'short_description', 'cover_id' ),
	),
	'when'    => array(
		'label' => __( 'When & where', 'bitforgenet-guest-tickets' ),
		'blurb' => __( 'Dates, doors, the room, and who is presenting the night.', 'bitforgenet-guest-tickets' ),
		'keys'  => array( 'starts_at', 'ends_at', 'doors_open_at', 'timezone', 'venue_id', 'organizer_id', 'lineup', 'dress_code', 'min_age', 'age_check_mode' ),
	),
	'pricing' => array(
		'label' => __( 'Pricing & capacity', 'bitforgenet-guest-tickets' ),
		'blurb' => __( 'What it costs, how many can come, and how the room is laid out.', 'bitforgenet-guest-tickets' ),
		'keys'  => array( 'price', 'currency', 'capacity', 'menu_enabled', 'menu_title', 'menu_body', 'sponsors_image' ),
	),
	'rules'   => array(
		'label' => __( 'Rules & terms', 'bitforgenet-guest-tickets' ),
		'blurb' => __( 'When the form opens and closes, and what a guest agrees to.', 'bitforgenet-guest-tickets' ),
		'keys'  => array( 'registration_opens_at', 'registration_closes_at', 'terms_page_id', 'regulation_page_id' ),
	),
	'payment' => array(
		'label' => __( 'Payment', 'bitforgenet-guest-tickets' ),
		'blurb' => __( 'How guests pay for this event, in their own words.', 'bitforgenet-guest-tickets' ),
		'keys'  => array( 'whatsapp_number', 'payment_instructions', 'terms' ),
	),
	'ticket'  => array(
		'label' => __( 'Ticket & link', 'bitforgenet-guest-tickets' ),
		'blurb' => __( 'The design the QR is printed on, and the link you share.', 'bitforgenet-guest-tickets' ),
		'keys'  => array( 'template_id', 'ticket_prefix', 'max_per_order', 'collect_guest_names' ),
	),
);

// phpcs:ignore WordPress.Security.NonceVerification -- read-only screen state.
$bfnet_tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'event';
if ( ! isset( $bfnet_tabs[ $bfnet_tab ] ) ) {
	$bfnet_tab = 'event';
}
$bfnet_panel = $bfnet_tabs[ $bfnet_tab ];

/**
 * The fields a tab is allowed to write.
 *
 * A module that adds fields to a tab has to add their names here too, or the
 * save politely refuses to write them — which is exactly what the list is for.
 *
 * @param array  $keys The tab's own fields.
 * @param string $tab  Which tab.
 */
$bfnet_panel['keys'] = (array) apply_filters( 'bfnet_event_tab_keys', $bfnet_panel['keys'], $bfnet_tab );
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = $event->title;
	$bfnet_subtitle = $bfnet_panel['blurb'];
	$bfnet_actions  = '<a href="' . esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events', array( 'action' => 'dashboard', 'event' => (int) $event->id ) ) ) . '" class="button">' . esc_html__( 'Event dashboard', 'bitforgenet-guest-tickets' ) . '</a>'
		. ' <a href="' . esc_url( BFNET_Util::event_url( $event ) ) . '" target="_blank" rel="noopener" class="button">' . esc_html__( 'View booking page', 'bitforgenet-guest-tickets' ) . '</a>';
	include BFNET_PATH . 'admin/views/partial-masthead.php';
	?>

	<div class="bfnet-tabs bfnet-tabs--settings">
		<?php foreach ( $bfnet_tabs as $bfnet_slug => $bfnet_meta ) : ?>
			<a class="<?php echo $bfnet_slug === $bfnet_tab ? 'is-active' : ''; ?>"
				href="<?php echo esc_url( BFNET_Util::admin_url( 'bitforgenet-guest-tickets-events', array( 'action' => 'edit', 'event' => (int) $event->id, 'tab' => $bfnet_slug ) ) ); ?>">
				<?php echo esc_html( $bfnet_meta['label'] ); ?>
			</a>
		<?php endforeach; ?>
	</div>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
		<?php wp_nonce_field( 'bfnet_save_event', '_bfnet_nonce' ); ?>
		<input type="hidden" name="page" value="bitforgenet-guest-tickets-events">
		<input type="hidden" name="bfnet_do" value="save_event">
		<input type="hidden" name="event_id" value="<?php echo (int) $event->id; ?>">
		<input type="hidden" name="bfnet_tab" value="<?php echo esc_attr( $bfnet_tab ); ?>">

		<?php /* The fields this tab is allowed to write. Nothing else is touched. */ ?>
		<?php foreach ( $bfnet_panel['keys'] as $bfnet_key ) : ?>
			<input type="hidden" name="bfnet_fields[]" value="<?php echo esc_attr( $bfnet_key ); ?>">
		<?php endforeach; ?>

		<div class="bfnet-stack">
			<?php include BFNET_PATH . 'admin/views/event/' . $bfnet_tab . '.php'; ?>
		</div>

		<div class="bfnet-savebar">
			<label class="bfnet-savebar__status" for="bfnet-status">
				<span><?php esc_html_e( 'Status', 'bitforgenet-guest-tickets' ); ?></span>
				<select id="bfnet-status" name="status">
					<?php foreach ( BFNET_Util::event_statuses() as $bfnet_key => $bfnet_label ) : ?>
						<option value="<?php echo esc_attr( $bfnet_key ); ?>" <?php selected( $event->status, $bfnet_key ); ?>><?php echo esc_html( $bfnet_label ); ?></option>
					<?php endforeach; ?>
				</select>
			</label>

			<button class="button button-primary button-large">
				<?php
				/* translators: %s: the name of the tab being saved. */
				echo esc_html( sprintf( __( 'Save %s', 'bitforgenet-guest-tickets' ), $bfnet_panel['label'] ) );
				?>
			</button>

			<span class="bfnet-savebar__note">
				<?php esc_html_e( 'Only this tab is written. The rest of the event stays exactly as it is.', 'bitforgenet-guest-tickets' ); ?>
			</span>
		</div>
	</form>
</div>
