<?php
/**
 * Admin view: the Email panel of the settings screen.
 *
 * Included by admin/views/settings.php inside that tab's own form, so the
 * closures and the $s array defined there are in scope here.
 *
 * @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
?>
	<div class="bfnet-panel">
		<div class="bfnet-panel__head"><h2><?php esc_html_e( 'Email', 'bitforgenet-guest-tickets' ); ?></h2></div>
		<div class="bfnet-panel__body">
			<div class="bfnet-row">
				<div class="bfnet-field">
					<label for="s-fromname"><?php esc_html_e( 'From name', 'bitforgenet-guest-tickets' ); ?></label>
					<input type="text" id="s-fromname" name="bfnet_settings[email_from_name]" value="<?php echo esc_attr( $s['email_from_name'] ); ?>">
				</div>
				<div class="bfnet-field">
					<label for="s-fromemail"><?php esc_html_e( 'From address', 'bitforgenet-guest-tickets' ); ?></label>
					<input type="email" id="s-fromemail" name="bfnet_settings[email_from_address]" value="<?php echo esc_attr( $s['email_from_address'] ); ?>">
				</div>
			</div>
			<div class="bfnet-field">
				<label for="s-replyto"><?php esc_html_e( 'Reply-to address', 'bitforgenet-guest-tickets' ); ?></label>
				<input type="email" id="s-replyto" name="bfnet_settings[email_reply_to]" value="<?php echo esc_attr( $s['email_reply_to'] ); ?>" placeholder="hello@example.com">
				<p class="description"><?php esc_html_e( 'Where a guest\'s reply lands. Worth setting: it survives even when another plugin rewrites the sender.', 'bitforgenet-guest-tickets' ); ?></p>
			</div>

			<?php
			/**
			 * Where a delivery module warns about the From address.
			 *
			 * Diagnosing which other mail plugin holds the connection is the
			 * business of the module that cares; the panel itself does not need
			 * to know such a module exists.
			 */
			do_action( 'bfnet_settings_panel_email_top', $s );
			?>


			<?php if ( 'inherit' !== $s['email_sender_mode'] && ! empty( $bfnet_state['routes'] ) ) : ?>
				<div class="bfnet-callout bfnet-callout--warn">
					<strong><?php esc_html_e( 'The sender mode is working against your connection.', 'bitforgenet-guest-tickets' ); ?></strong>
					<p>
						<?php
						printf(
							/* translators: %s: name of the mail plugin found. */
							esc_html__( '%s already sends each message through the connection that matches its From address — it is not imposing a sender, so there is nothing here to step around. Sending BitForgeNet Guest Tickets\'s email ourselves skips that connection and posts the message from this server instead, which is exactly what a connected mailbox exists to avoid. Set the mode back to "Let the site decide".', 'bitforgenet-guest-tickets' ),
							esc_html( $bfnet_state['plugin'] )
						);
						?>
					</p>
				</div>
			<?php endif; ?>

			<?php if ( $bfnet_state['plugin'] ) : ?>
				<?php
				$bfnet_tone = '';
				if ( ! empty( $bfnet_state['routes'] ) ) {
					$bfnet_tone = ' bfnet-callout--good';
				}
				if ( $bfnet_state['overrides'] || ! empty( $bfnet_state['conflict'] ) ) {
					$bfnet_tone = ' bfnet-callout--warn';
				}
				?>
				<div class="bfnet-callout<?php echo esc_attr( $bfnet_tone ); ?>">
					<strong>
						<?php
						/* translators: %s: name of the mail plugin found. */
						printf( esc_html__( '%s is handling this site\'s email', 'bitforgenet-guest-tickets' ), esc_html( $bfnet_state['plugin'] ) );
						?>
					</strong>
					<p><?php echo esc_html( $bfnet_state['advice'] ); ?></p>
					<?php if ( ! empty( $bfnet_state['addresses'] ) ) : ?>
						<p>
							<?php esc_html_e( 'Connected addresses:', 'bitforgenet-guest-tickets' ); ?>
							<?php foreach ( $bfnet_state['addresses'] as $bfnet_addr ) : ?>
								<code><?php echo esc_html( $bfnet_addr ); ?></code>
							<?php endforeach; ?>
						</p>
					<?php endif; ?>
					<?php if ( ! empty( $bfnet_state['conflict'] ) ) : ?>
						<p><strong><?php echo esc_html( $bfnet_state['conflict'] ); ?></strong></p>
					<?php endif; ?>
					<?php if ( $bfnet_state['sender'] ) : ?>
						<p>
							<?php
							/* translators: %s: the sender that plugin will use. */
							printf( esc_html__( 'Its sender is %s.', 'bitforgenet-guest-tickets' ), '<code>' . esc_html( $bfnet_state['sender'] ) . '</code>' );
							?>
						</p>
					<?php endif; ?>
				</div>
			<?php endif; ?>

			<?php
			/**
			 * Where a delivery module puts its own panel.
			 *
			 * The Google mailbox block used to be written out here, which meant
			 * this view could not be rendered without that module present. It now
			 * prints itself, and a build without it simply shows nothing here.
			 */
			do_action( 'bfnet_settings_panel_email', $s );
			?>



			<div class="bfnet-field">
				<label for="s-bcc"><?php esc_html_e( 'Team notification address', 'bitforgenet-guest-tickets' ); ?></label>
				<input type="email" id="s-bcc" name="bfnet_settings[email_bcc]" value="<?php echo esc_attr( $s['email_bcc'] ); ?>">
				<p class="description"><?php esc_html_e( 'Gets a copy of guest emails and an alert on every new booking.', 'bitforgenet-guest-tickets' ); ?></p>
			</div>
			<div class="bfnet-field">
				<label for="s-footer"><?php esc_html_e( 'Email footer', 'bitforgenet-guest-tickets' ); ?></label>
				<input type="text" id="s-footer" name="bfnet_settings[email_footer]" value="<?php echo esc_attr( $s['email_footer'] ); ?>">
			</div>
			<?php
			$bfnet_check( 'send_pending_email', __( 'Send a "reservation received" email', 'bitforgenet-guest-tickets' ), '', $s );
			$bfnet_check( 'send_paid_email', __( 'Send the tickets when payment is confirmed', 'bitforgenet-guest-tickets' ), '', $s );
			$bfnet_check( 'attach_individual_pdfs', __( 'Attach one PDF per guest', 'bitforgenet-guest-tickets' ), '', $s );
			$bfnet_check( 'attach_combined_pdf', __( 'Attach a single PDF containing every ticket', 'bitforgenet-guest-tickets' ), __( 'Recommended: one file instead of five keeps the message well under any size limit.', 'bitforgenet-guest-tickets' ), $s );
			$bfnet_check( 'email_download_links', __( 'Always include a download button per guest', 'bitforgenet-guest-tickets' ), __( 'Leave this on. Mail servers strip attachments far more often than they admit, and this is what saves the booking when they do.', 'bitforgenet-guest-tickets' ), $s );
			?>

			<div class="bfnet-row">
				<div class="bfnet-field">
					<label for="s-maxatt"><?php esc_html_e( 'Attachment ceiling (MB)', 'bitforgenet-guest-tickets' ); ?></label>
					<input type="number" id="s-maxatt" name="bfnet_settings[email_max_attachment_mb]" min="1" max="25" value="<?php echo (int) $s['email_max_attachment_mb']; ?>">
					<p class="description"><?php esc_html_e( 'Above this, the plugin sends download links instead of files rather than risk a stripped message.', 'bitforgenet-guest-tickets' ); ?></p>
				</div>
				<div class="bfnet-field">
					<label for="s-linkdays"><?php esc_html_e( 'Download links valid for (days)', 'bitforgenet-guest-tickets' ); ?></label>
					<input type="number" id="s-linkdays" name="bfnet_settings[download_link_days]" min="1" max="365" value="<?php echo (int) $s['download_link_days']; ?>">
				</div>
			</div>
		</div>
	</div>
