<?php
/**
 * Admin settings page view.
 *
 * @package MacaHold
 */

defined( 'ABSPATH' ) || exit;

$maca_hold_messages = array(
	'saved'             => __( 'Settings saved.', 'maca-hold' ),
	'imported'          => __( 'Settings imported.', 'maca-hold' ),
	'token_regenerated' => __( 'Secret bypass link regenerated.', 'maca-hold' ),
);
$maca_hold_saved_links = is_array( $settings['links'] ?? null ) ? array_values( $settings['links'] ) : array();
$maca_hold_max_links   = (int) apply_filters( 'maca_hold_max_links', 5 );

if ( empty( $maca_hold_saved_links ) ) {
	$maca_hold_link_rows = array(
		array(
			'label'   => '',
			'url'     => '',
			'new_tab' => false,
		),
	);
} else {
	$maca_hold_link_rows = array_slice( $maca_hold_saved_links, 0, $maca_hold_max_links );
}
?>
<div class="maca-hold-settings-tab">
	<?php if ( isset( $maca_hold_messages[ $message ] ) ) : ?>
		<div class="notice notice-success is-dismissible">
			<p><?php echo esc_html( $maca_hold_messages[ $message ] ); ?></p>
		</div>
	<?php endif; ?>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="maca-hold-form">
		<input type="hidden" name="action" value="maca_hold_save_settings" />
		<?php wp_nonce_field( 'maca_hold_save_settings' ); ?>
		<input type="hidden" name="setup_dismissed" value="1" />
		<input type="hidden" name="enabled" id="maca_hold_enabled_field" value="<?php echo ! empty( $settings['enabled'] ) ? '1' : '0'; ?>" />

		<h2 class="title"><?php echo esc_html__( 'General', 'maca-hold' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php echo esc_html__( 'Maintenance mode', 'maca-hold' ); ?></th>
				<td>
					<p class="description">
						<?php echo esc_html__( 'Use the switch at the top of this page to enable or disable maintenance immediately.', 'maca-hold' ); ?>
					</p>
					<p class="description">
						<?php echo esc_html__( 'When scheduling is enabled, the schedule overrides the switch.', 'maca-hold' ); ?>
					</p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_retry_after"><?php echo esc_html__( 'Retry-After (seconds)', 'maca-hold' ); ?></label></th>
				<td>
					<input type="number" min="0" step="1" class="small-text" id="maca_hold_retry_after" name="retry_after" value="<?php echo esc_attr( $settings['retry_after'] ); ?>" />
					<p class="description"><?php echo esc_html__( 'Sent with the HTTP 503 response. Use 0 to omit the header.', 'maca-hold' ); ?></p>
				</td>
			</tr>
		</table>

		<h2 class="title"><?php echo esc_html__( 'Appearance', 'maca-hold' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><label for="maca_hold_title"><?php echo esc_html__( 'Title', 'maca-hold' ); ?></label></th>
				<td><input type="text" class="regular-text" id="maca_hold_title" name="title" value="<?php echo esc_attr( $settings['title'] ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_subtitle"><?php echo esc_html__( 'Subtitle', 'maca-hold' ); ?></label></th>
				<td>
					<input type="text" class="regular-text" id="maca_hold_subtitle" name="subtitle" value="<?php echo esc_attr( $settings['subtitle'] ); ?>" />
					<p class="description"><?php echo esc_html__( 'Optional line shown below the title.', 'maca-hold' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_message"><?php echo esc_html__( 'Message', 'maca-hold' ); ?></label></th>
				<td>
					<textarea class="large-text" rows="5" id="maca_hold_message" name="message"><?php echo esc_textarea( $settings['message'] ); ?></textarea>
					<p class="description"><?php echo esc_html__( 'Basic HTML is allowed in the message and footer text.', 'maca-hold' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_badge_text"><?php echo esc_html__( 'Status badge', 'maca-hold' ); ?></label></th>
				<td>
					<input type="text" class="regular-text" id="maca_hold_badge_text" name="badge_text" value="<?php echo esc_attr( $settings['badge_text'] ); ?>" placeholder="<?php echo esc_attr__( 'Under maintenance', 'maca-hold' ); ?>" />
					<p class="description"><?php echo esc_html__( 'Leave empty to use the default badge on color-only layouts. Hidden when a background image is used unless you enter custom text.', 'maca-hold' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_logo_url"><?php echo esc_html__( 'Logo URL', 'maca-hold' ); ?></label></th>
				<td>
					<input type="url" class="regular-text" id="maca_hold_logo_url" name="logo_url" value="<?php echo esc_url( $settings['logo_url'] ); ?>" placeholder="<?php echo esc_attr__( 'https://example.com/logo.png', 'maca-hold' ); ?>" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_logo_alt"><?php echo esc_html__( 'Logo alt text', 'maca-hold' ); ?></label></th>
				<td>
					<input type="text" class="regular-text" id="maca_hold_logo_alt" name="logo_alt" value="<?php echo esc_attr( $settings['logo_alt'] ); ?>" placeholder="<?php echo esc_attr__( 'Logo', 'maca-hold' ); ?>" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_template"><?php echo esc_html__( 'Template', 'maca-hold' ); ?></label></th>
				<td>
					<select id="maca_hold_template" name="template">
						<?php foreach ( Maca_Hold_Settings::get_templates() as $maca_hold_template_slug => $maca_hold_template_label ) : ?>
							<option value="<?php echo esc_attr( $maca_hold_template_slug ); ?>" <?php selected( $settings['template'], $maca_hold_template_slug ); ?>>
								<?php echo esc_html( $maca_hold_template_label ); ?>
							</option>
						<?php endforeach; ?>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php echo esc_html__( 'Background image', 'maca-hold' ); ?></th>
				<td>
					<fieldset class="maca-hold-background-picker">
						<legend class="screen-reader-text"><?php echo esc_html__( 'Background image', 'maca-hold' ); ?></legend>

						<label class="maca-hold-background-option">
							<input type="radio" name="background_image" value="" <?php checked( empty( $settings['background_image'] ) ); ?> />
							<span class="maca-hold-background-preview maca-hold-background-preview--none">
								<span><?php echo esc_html__( 'Color only', 'maca-hold' ); ?></span>
							</span>
						</label>

						<?php foreach ( $backgrounds as $maca_hold_background ) : ?>
							<label class="maca-hold-background-option">
								<input
									type="radio"
									name="background_image"
									value="<?php echo esc_attr( $maca_hold_background['id'] ); ?>"
									<?php checked( $settings['background_image'], $maca_hold_background['id'] ); ?>
								/>
								<span class="maca-hold-background-preview">
									<img src="<?php echo esc_url( $maca_hold_background['url'] ); ?>" alt="<?php echo esc_attr( $maca_hold_background['label'] ); ?>" />
									<span><?php echo esc_html( $maca_hold_background['label'] ); ?></span>
								</span>
							</label>
						<?php endforeach; ?>
					</fieldset>

					<p class="maca-hold-background-status" id="maca_hold_background_status" aria-live="polite"></p>

					<?php if ( empty( $backgrounds ) ) : ?>
						<p class="description"><?php echo esc_html__( 'Add hold1.png, hold2.png, and similar files to the plugin assets folder.', 'maca-hold' ); ?></p>
					<?php else : ?>
						<p class="description"><?php echo esc_html__( 'Choose a bundled background or use the background color only. Changes apply immediately.', 'maca-hold' ); ?></p>
					<?php endif; ?>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php echo esc_html__( 'Colors', 'maca-hold' ); ?></th>
				<td class="maca-hold-colors">
					<label>
						<?php echo esc_html__( 'Background', 'maca-hold' ); ?>
						<input type="color" name="background_color" value="<?php echo esc_attr( $settings['background_color'] ); ?>" />
					</label>
					<label>
						<?php echo esc_html__( 'Text', 'maca-hold' ); ?>
						<input type="color" name="text_color" value="<?php echo esc_attr( $settings['text_color'] ); ?>" />
					</label>
					<label>
						<?php echo esc_html__( 'Accent', 'maca-hold' ); ?>
						<input type="color" name="accent_color" value="<?php echo esc_attr( $settings['accent_color'] ); ?>" />
					</label>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php echo esc_html__( 'Preview', 'maca-hold' ); ?></th>
				<td>
					<a class="button button-secondary" href="<?php echo esc_url( $preview_url ); ?>" target="_blank" rel="noopener noreferrer">
						<?php echo esc_html__( 'Preview maintenance page', 'maca-hold' ); ?>
					</a>
				</td>
			</tr>
		</table>

		<h2 class="title"><?php echo esc_html__( 'Links & footer', 'maca-hold' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php echo esc_html__( 'Action links', 'maca-hold' ); ?></th>
				<td>
					<table class="maca-hold-link-table widefat">
						<thead>
							<tr>
								<th scope="col"><?php echo esc_html__( 'Label', 'maca-hold' ); ?></th>
								<th scope="col"><?php echo esc_html__( 'URL', 'maca-hold' ); ?></th>
								<th scope="col"><?php echo esc_html__( 'New tab', 'maca-hold' ); ?></th>
							</tr>
						</thead>
						<tbody id="maca_hold_links_body">
							<?php foreach ( $maca_hold_link_rows as $maca_hold_link_index => $maca_hold_link_row ) : ?>
								<?php
								$maca_hold_link_removable = count( $maca_hold_link_rows ) > 1;
								include MACA_HOLD_PATH . 'admin/views/partials/link-row.php';
								?>
							<?php endforeach; ?>
						</tbody>
					</table>
					<p class="maca-hold-link-toolbar">
						<button
							type="button"
							class="button button-secondary maca-hold-add-link"
							id="maca_hold_add_link"
							aria-label="<?php echo esc_attr__( 'Add link', 'maca-hold' ); ?>"
							<?php disabled( count( $maca_hold_link_rows ) >= $maca_hold_max_links ); ?>
						>
							<span aria-hidden="true">+</span>
							<?php echo esc_html__( 'Add link', 'maca-hold' ); ?>
						</button>
					</p>
					<p class="description">
						<?php
						printf(
							/* translators: %d: maximum number of links */
							esc_html__( 'Add buttons or text links below the message. Up to %d links.', 'maca-hold' ),
							(int) $maca_hold_max_links
						);
						?>
					</p>
					<template id="maca_hold_link_row_template">
						<?php
						$maca_hold_link_index     = '__INDEX__';
						$maca_hold_link_row       = array(
							'label'   => '',
							'url'     => '',
							'new_tab' => false,
						);
						$maca_hold_link_removable = true;
						include MACA_HOLD_PATH . 'admin/views/partials/link-row.php';
						?>
					</template>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_footer_text"><?php echo esc_html__( 'Footer text', 'maca-hold' ); ?></label></th>
				<td>
					<textarea class="large-text" rows="3" id="maca_hold_footer_text" name="footer_text"><?php echo esc_textarea( $settings['footer_text'] ); ?></textarea>
					<p class="description"><?php echo esc_html__( 'Optional small print shown below links and countdown.', 'maca-hold' ); ?></p>
				</td>
			</tr>
		</table>

		<h2 class="title"><?php echo esc_html__( 'Access', 'maca-hold' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php echo esc_html__( 'Bypass roles', 'maca-hold' ); ?></th>
				<td>
					<fieldset>
						<?php foreach ( $roles as $maca_hold_role_key => $role ) : ?>
							<label>
								<input type="checkbox" name="bypass_roles[]" value="<?php echo esc_attr( $maca_hold_role_key ); ?>" <?php checked( in_array( $maca_hold_role_key, (array) $settings['bypass_roles'], true ) ); ?> />
								<?php echo esc_html( translate_user_role( $role['name'] ) ); ?>
							</label><br />
						<?php endforeach; ?>
					</fieldset>
					<p class="description"><?php echo esc_html__( 'Logged-in users with these roles can browse the site normally.', 'maca-hold' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php echo esc_html__( 'Login link', 'maca-hold' ); ?></th>
				<td>
					<?php
					$maca_hold_switch_id          = 'maca_hold_show_login_link';
					$maca_hold_switch_name        = 'show_login_link';
					$maca_hold_switch_checked     = ! empty( $settings['show_login_link'] );
					$maca_hold_switch_label       = __( 'Show login icon on the maintenance page', 'maca-hold' );
					$maca_hold_switch_description = '';
					include MACA_HOLD_PATH . 'admin/views/partials/setting-switch.php';
					?>
					<p>
						<label for="maca_hold_login_link_label"><?php echo esc_html__( 'Link label', 'maca-hold' ); ?></label><br />
						<input
							type="text"
							class="regular-text"
							id="maca_hold_login_link_label"
							name="login_link_label"
							value="<?php echo esc_attr( $settings['login_link_label'] ); ?>"
							placeholder="<?php echo esc_attr__( 'Log in', 'maca-hold' ); ?>"
						/>
					</p>
					<p class="description"><?php echo esc_html__( 'Links to the WordPress login page. Leave the label empty to show the icon only in the top-right corner.', 'maca-hold' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_ip_whitelist"><?php echo esc_html__( 'IP whitelist', 'maca-hold' ); ?></label></th>
				<td>
					<textarea class="large-text code" rows="4" id="maca_hold_ip_whitelist" name="ip_whitelist" placeholder="<?php echo esc_attr__( '203.0.113.10', 'maca-hold' ); ?>"><?php echo esc_textarea( $settings['ip_whitelist'] ); ?></textarea>
					<p class="description"><?php echo esc_html__( 'One IP address per line. These visitors bypass maintenance mode.', 'maca-hold' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php echo esc_html__( 'Secret bypass link', 'maca-hold' ); ?></th>
				<td>
					<input type="hidden" name="secret_token" value="<?php echo esc_attr( $settings['secret_token'] ); ?>" />
					<input type="text" class="large-text code" readonly value="<?php echo esc_url( $bypass_url ); ?>" id="maca_hold_bypass_url" />
					<p>
						<button type="button" class="button button-secondary" data-copy-target="maca_hold_bypass_url">
							<?php echo esc_html__( 'Copy link', 'maca-hold' ); ?>
						</button>
						<a class="button button-secondary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=maca_hold_regenerate_token' ), 'maca_hold_regenerate_token' ) ); ?>">
							<?php echo esc_html__( 'Regenerate link', 'maca-hold' ); ?>
						</a>
					</p>
					<p class="description"><?php echo esc_html__( 'Anyone with this link can view the site. Keep it private.', 'maca-hold' ); ?></p>
				</td>
			</tr>
		</table>

		<h2 class="title"><?php echo esc_html__( 'Schedule', 'maca-hold' ); ?></h2>
		<p class="description">
			<?php
			printf(
				/* translators: %s: WordPress site timezone */
				esc_html__( 'Schedule times use your WordPress site timezone: %s', 'maca-hold' ),
				esc_html( Maca_Hold_Scheduler::get_timezone_label() )
			);
			?>
		</p>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php echo esc_html__( 'Scheduled maintenance', 'maca-hold' ); ?></th>
				<td>
					<?php
					$maca_hold_switch_id          = 'maca_hold_schedule_enabled';
					$maca_hold_switch_name        = 'schedule_enabled';
					$maca_hold_switch_checked     = ! empty( $settings['schedule_enabled'] );
					$maca_hold_switch_label       = __( 'Use schedule instead of the manual toggle', 'maca-hold' );
					$maca_hold_switch_description = $schedule_sum;
					include MACA_HOLD_PATH . 'admin/views/partials/setting-switch.php';
					?>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_schedule_start"><?php echo esc_html__( 'Start', 'maca-hold' ); ?></label></th>
				<td>
					<input type="datetime-local" id="maca_hold_schedule_start" name="schedule_start" value="<?php echo esc_attr( Maca_Hold_Scheduler::format_for_input( $settings['schedule_start'] ) ); ?>" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_schedule_end"><?php echo esc_html__( 'End', 'maca-hold' ); ?></label></th>
				<td>
					<input type="datetime-local" id="maca_hold_schedule_end" name="schedule_end" value="<?php echo esc_attr( Maca_Hold_Scheduler::format_for_input( $settings['schedule_end'] ) ); ?>" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_countdown_label"><?php echo esc_html__( 'Countdown label', 'maca-hold' ); ?></label></th>
				<td>
					<input type="text" class="regular-text" id="maca_hold_countdown_label" name="countdown_label" value="<?php echo esc_attr( $settings['countdown_label'] ); ?>" placeholder="<?php echo esc_attr__( 'Back soon in', 'maca-hold' ); ?>" />
					<p class="description"><?php echo esc_html__( 'Shown above the countdown timer when scheduling is enabled.', 'maca-hold' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php echo esc_html__( 'Countdown', 'maca-hold' ); ?></th>
				<td>
					<?php
					$maca_hold_switch_id          = 'maca_hold_show_countdown';
					$maca_hold_switch_name        = 'show_countdown';
					$maca_hold_switch_checked     = ! empty( $settings['show_countdown'] );
					$maca_hold_switch_label       = __( 'Show countdown to scheduled end time', 'maca-hold' );
					$maca_hold_switch_description = __( 'Requires an end time in the schedule section. Works with background images and color-only layouts.', 'maca-hold' );
					include MACA_HOLD_PATH . 'admin/views/partials/setting-switch.php';
					?>
				</td>
			</tr>
		</table>

		<?php submit_button( __( 'Save settings', 'maca-hold' ) ); ?>
	</form>

	<hr />

	<h2><?php echo esc_html__( 'Import / Export', 'maca-hold' ); ?></h2>
	<p><?php echo esc_html__( 'Export your settings for backup or migration, or import a JSON file from another site.', 'maca-hold' ); ?></p>

	<p>
		<a class="button button-secondary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=maca_hold_export_settings' ), 'maca_hold_export_settings' ) ); ?>">
			<?php echo esc_html__( 'Export settings', 'maca-hold' ); ?>
		</a>
	</p>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" enctype="multipart/form-data" class="maca-hold-import-form">
		<input type="hidden" name="action" value="maca_hold_import_settings" />
		<?php wp_nonce_field( 'maca_hold_import_settings' ); ?>
		<input type="file" name="maca_hold_import_file" accept="application/json,.json" required />
		<?php submit_button( __( 'Import settings', 'maca-hold' ), 'secondary', 'submit', false ); ?>
	</form>
</div>
