<?php
/**
 * Advanced settings tab.
 *
 * @package MacaHold
 *
 * @var array<string, mixed> $settings
 * @var string               $message
 * @var WP_Post[]            $pages
 */

defined( 'ABSPATH' ) || exit;

$maca_hold_messages = array(
	'saved' => __( 'Settings saved.', 'maca-hold' ),
);

$social_links         = is_array( $settings['social_links'] ?? null ) ? array_values( $settings['social_links'] ) : array();
$social_rows          = array();
$selected_page_ids    = array_map( 'absint', (array) ( $settings['page_exception_ids'] ?? array() ) );
$page_exception_mode  = (string) ( $settings['page_exception_mode'] ?? '' );
$pages                = isset( $pages ) && is_array( $pages ) ? $pages : array();
$social_networks      = Maca_Hold_Social_Networks::get_networks();
$social_links_display = (string) ( $settings['social_links_display'] ?? 'icons' );

for ( $maca_hold_i = 0; $maca_hold_i < 6; $maca_hold_i++ ) {
	$social_rows[] = $social_links[ $maca_hold_i ] ?? array(
		'network'  => '',
		'label'    => '',
		'url'      => '',
		'icon_url' => '',
		'new_tab'  => true,
	);
}
?>
<div class="maca-hold-advanced-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" />
		<input type="hidden" name="maca_hold_advanced_save" value="1" />
		<?php wp_nonce_field( 'maca_hold_save_settings' ); ?>

		<h2 class="title"><?php echo esc_html__( 'Page exceptions', 'maca-hold' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php echo esc_html__( 'Per-page maintenance', 'maca-hold' ); ?></th>
				<td>
					<fieldset class="maca-hold-page-exceptions">
						<legend class="screen-reader-text"><?php echo esc_html__( 'Page exceptions', 'maca-hold' ); ?></legend>
						<label>
							<input type="radio" name="page_exception_mode" value="" <?php checked( '' === $page_exception_mode ); ?> />
							<?php echo esc_html__( 'No page exceptions', 'maca-hold' ); ?>
						</label><br />
						<label>
							<input type="radio" name="page_exception_mode" value="open" <?php checked( 'open' === $page_exception_mode ); ?> />
							<?php echo esc_html__( 'Keep selected pages open while the rest of the site is in maintenance', 'maca-hold' ); ?>
						</label><br />
						<label>
							<input type="radio" name="page_exception_mode" value="closed" <?php checked( 'closed' === $page_exception_mode ); ?> />
							<?php echo esc_html__( 'Show maintenance on selected pages only', 'maca-hold' ); ?>
						</label>
						<?php if ( empty( $pages ) ) : ?>
							<p class="description"><?php echo esc_html__( 'Create at least one WordPress page to use this feature.', 'maca-hold' ); ?></p>
						<?php else : ?>
							<div class="maca-hold-page-list">
								<?php foreach ( $pages as $page ) : ?>
									<label class="maca-hold-page-option">
										<input type="checkbox" name="page_exception_ids[]" value="<?php echo esc_attr( (string) $page->ID ); ?>" <?php checked( in_array( (int) $page->ID, $selected_page_ids, true ) ); ?> />
										<?php echo esc_html( $page->post_title ); ?>
									</label>
								<?php endforeach; ?>
							</div>
						<?php endif; ?>
					</fieldset>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_max_links"><?php echo esc_html__( 'Maximum links', 'maca-hold' ); ?></label></th>
				<td>
					<input type="number" min="5" step="1" class="small-text" id="maca_hold_max_links" name="max_links" value="<?php echo esc_attr( $settings['max_links'] ); ?>" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_custom_background_url"><?php echo esc_html__( 'Custom background URL', 'maca-hold' ); ?></label></th>
				<td>
					<input type="url" class="regular-text" id="maca_hold_custom_background_url" name="custom_background_url" value="<?php echo esc_url( $settings['custom_background_url'] ); ?>" placeholder="https://example.com/background.jpg" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_custom_background_label"><?php echo esc_html__( 'Custom background label', 'maca-hold' ); ?></label></th>
				<td>
					<input type="text" class="regular-text" id="maca_hold_custom_background_label" name="custom_background_label" value="<?php echo esc_attr( $settings['custom_background_label'] ); ?>" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_favicon_url"><?php echo esc_html__( 'Favicon URL', 'maca-hold' ); ?></label></th>
				<td>
					<input type="url" class="regular-text" id="maca_hold_favicon_url" name="favicon_url" value="<?php echo esc_url( $settings['favicon_url'] ); ?>" placeholder="https://example.com/favicon.ico" />
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_head_code"><?php echo esc_html__( 'Head code', 'maca-hold' ); ?></label></th>
				<td>
					<textarea class="large-text code" rows="5" id="maca_hold_head_code" name="head_code"><?php echo esc_textarea( $settings['head_code'] ); ?></textarea>
					<p class="description"><?php echo esc_html__( 'Optional HTML for analytics or meta tags on the maintenance page.', 'maca-hold' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_hold_custom_css"><?php echo esc_html__( 'Custom CSS', 'maca-hold' ); ?></label></th>
				<td>
					<textarea class="large-text code" rows="8" id="maca_hold_custom_css" name="custom_css" placeholder=".maca-hold-card { border-radius: 2rem; }"><?php echo esc_textarea( $settings['custom_css'] ); ?></textarea>
				</td>
			</tr>
		</table>

		<h2 class="title"><?php echo esc_html__( 'Social media', 'maca-hold' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php echo esc_html__( 'Display', 'maca-hold' ); ?></th>
				<td>
					<fieldset class="maca-hold-social-display">
						<label><input type="radio" name="social_links_display" value="icons" <?php checked( 'icons' === $social_links_display ); ?> /> <?php echo esc_html__( 'Icons only', 'maca-hold' ); ?></label><br />
						<label><input type="radio" name="social_links_display" value="icons_text" <?php checked( 'icons_text' === $social_links_display ); ?> /> <?php echo esc_html__( 'Icons and labels', 'maca-hold' ); ?></label>
					</fieldset>
					<table class="widefat striped maca-hold-social-table">
						<thead>
							<tr>
								<th scope="col"><?php echo esc_html__( 'Network', 'maca-hold' ); ?></th>
								<th scope="col"><?php echo esc_html__( 'Profile URL', 'maca-hold' ); ?></th>
								<th scope="col"><?php echo esc_html__( 'Icon image URL', 'maca-hold' ); ?></th>
								<th scope="col"><?php echo esc_html__( 'Custom label', 'maca-hold' ); ?></th>
								<th scope="col"><?php echo esc_html__( 'New tab', 'maca-hold' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $social_rows as $maca_hold_index => $maca_hold_social_row ) : ?>
								<?php
								$maca_hold_row_network = sanitize_key( $maca_hold_social_row['network'] ?? '' );
								if ( ! Maca_Hold_Social_Networks::is_valid( $maca_hold_row_network ) ) {
									$maca_hold_row_network = '';
								}
								$maca_hold_row_placeholder = '' !== $maca_hold_row_network && isset( $social_networks[ $maca_hold_row_network ] )
									? $social_networks[ $maca_hold_row_network ]['placeholder']
									: 'https://';
								?>
								<tr>
									<td>
										<select name="social_links[<?php echo esc_attr( (string) $maca_hold_index ); ?>][network]">
											<option value=""><?php echo esc_html__( '— Select —', 'maca-hold' ); ?></option>
											<?php foreach ( $social_networks as $maca_hold_network_slug => $maca_hold_network_data ) : ?>
												<option value="<?php echo esc_attr( $maca_hold_network_slug ); ?>" <?php selected( $maca_hold_row_network, $maca_hold_network_slug ); ?>><?php echo esc_html( $maca_hold_network_data['label'] ); ?></option>
											<?php endforeach; ?>
										</select>
									</td>
									<td><input type="url" class="regular-text" name="social_links[<?php echo esc_attr( (string) $maca_hold_index ); ?>][url]" value="<?php echo esc_url( $maca_hold_social_row['url'] ); ?>" placeholder="<?php echo esc_attr( $maca_hold_row_placeholder ); ?>" /></td>
									<td><input type="url" class="regular-text" name="social_links[<?php echo esc_attr( (string) $maca_hold_index ); ?>][icon_url]" value="<?php echo esc_url( $maca_hold_social_row['icon_url'] ?? '' ); ?>" placeholder="<?php echo esc_attr__( 'Optional PNG/SVG', 'maca-hold' ); ?>" /></td>
									<td><input type="text" class="regular-text" name="social_links[<?php echo esc_attr( (string) $maca_hold_index ); ?>][label]" value="<?php echo esc_attr( $maca_hold_social_row['label'] ); ?>" /></td>
									<td><label><input type="checkbox" name="social_links[<?php echo esc_attr( (string) $maca_hold_index ); ?>][new_tab]" value="1" <?php checked( ! empty( $maca_hold_social_row['new_tab'] ) ); ?> /> <?php echo esc_html__( 'Open in new tab', 'maca-hold' ); ?></label></td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				</td>
			</tr>
		</table>

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