<?php
/**
 * Forms tab. $plugin is in scope (Ffrd_Admin::render).
 *
 * @package Form_Flight_Recorder
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$ffrd_registry = $plugin->get( 'registry' );
$ffrd_events   = $plugin->get( 'events' );
$ffrd_forms    = $ffrd_registry->all();
$ffrd_settings = $plugin->settings();
?>

<div class="ffrd-panel">
	<h2><?php esc_html_e( 'Known forms', 'form-flight-recorder' ); ?></h2>
	<p class="description">
		<?php esc_html_e( '"Expects notification": auto = trust what the form plugin\'s settings say at submission time; on = always expect one; off = this form intentionally sends no email (silences missing-notification alerts). "Silence threshold": hours of quiet before the heartbeat flags this form (0 = site default; only forms averaging 3+ submissions/week are ever flagged).', 'form-flight-recorder' ); ?>
	</p>

	<?php if ( $ffrd_forms ) : ?>
	<form method="post">
		<?php wp_nonce_field( 'ffrd_save_forms' ); ?>
		<input type="hidden" name="ffrd_action" value="save_forms" />
		<input type="hidden" name="ffrd_tab" value="forms" />

		<table class="widefat striped">
			<thead><tr>
				<th><?php esc_html_e( 'Form', 'form-flight-recorder' ); ?></th>
				<th><?php esc_html_e( 'Plugin', 'form-flight-recorder' ); ?></th>
				<th><?php esc_html_e( 'Last submission (UTC)', 'form-flight-recorder' ); ?></th>
				<th><?php esc_html_e( 'Baseline', 'form-flight-recorder' ); ?></th>
				<th><?php esc_html_e( 'Expects notification', 'form-flight-recorder' ); ?></th>
				<th>
					<?php
					printf(
						/* translators: %d: default hours. */
						esc_html__( 'Silence threshold (h, 0 = default %d)', 'form-flight-recorder' ),
						(int) $ffrd_settings['silence_hours']
					);
					?>
				</th>
			</tr></thead>
			<tbody>
			<?php foreach ( $ffrd_forms as $ffrd_key => $ffrd_form ) : ?>
				<?php $ffrd_baseline = $ffrd_registry->baseline( $ffrd_events, $ffrd_key ); ?>
				<tr>
					<td><?php echo esc_html( '' !== $ffrd_form['title'] ? $ffrd_form['title'] : $ffrd_key ); ?></td>
					<td><?php echo esc_html( $ffrd_form['source'] ); ?></td>
					<td><?php echo esc_html( $ffrd_form['last_submission'] ? gmdate( 'Y-m-d H:i', (int) $ffrd_form['last_submission'] ) : '—' ); ?></td>
					<td>
						<?php
						printf(
							/* translators: %s: submissions per week. */
							esc_html__( '~%s/week', 'form-flight-recorder' ),
							esc_html( (string) $ffrd_baseline['per_week'] )
						);
						?>
					</td>
					<td>
						<select name="ffrd_forms[<?php echo esc_attr( $ffrd_key ); ?>][expects]">
							<option value="" <?php selected( null === $ffrd_form['expects'] ); ?>><?php esc_html_e( 'auto', 'form-flight-recorder' ); ?></option>
							<option value="on" <?php selected( 'on' === $ffrd_form['expects'] ); ?>><?php esc_html_e( 'on', 'form-flight-recorder' ); ?></option>
							<option value="off" <?php selected( 'off' === $ffrd_form['expects'] ); ?>><?php esc_html_e( 'off', 'form-flight-recorder' ); ?></option>
						</select>
					</td>
					<td><input type="number" min="0" max="2160" class="small-text" name="ffrd_forms[<?php echo esc_attr( $ffrd_key ); ?>][silence_hours]" value="<?php echo esc_attr( (string) (int) $ffrd_form['silence_hours'] ); ?>" /></td>
				</tr>
			<?php endforeach; ?>
			</tbody>
		</table>

		<?php submit_button( __( 'Save per-form settings', 'form-flight-recorder' ), 'primary', 'submit', false ); ?>
	</form>
	<?php else : ?>
	<p><?php esc_html_e( 'No forms known yet.', 'form-flight-recorder' ); ?></p>
	<?php endif; ?>

	<form method="post" class="ffrd-inline-form">
		<?php wp_nonce_field( 'ffrd_discover_now' ); ?>
		<input type="hidden" name="ffrd_action" value="discover_now" />
		<input type="hidden" name="ffrd_tab" value="forms" />
		<?php submit_button( __( 'Discover forms now', 'form-flight-recorder' ), 'secondary', 'submit', false ); ?>
	</form>
</div>
