<?php

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

if ( ! current_user_can( 'manage_options' ) ) {
	return;
}

if ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = 'custom-admin-footer-text';
}

$settings = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
$settings = is_array( $settings ) ? $settings : [];

$footer_left = isset( $settings['footer_left'] ) && is_string( $settings['footer_left'] )
	? $settings['footer_left']
	: '';
$footer_right = isset( $settings['footer_right'] ) && is_string( $settings['footer_right'] )
	? $settings['footer_right']
	: '';

// Legacy single-field setting (shown in the form so users can edit migrated values).
if ( $footer_left === ''
	&& isset( $settings['footer_text'] )
	&& is_string( $settings['footer_text'] )
	&& trim( $settings['footer_text'] ) !== ''
) {
	$footer_left = $settings['footer_text'];
}

global $wp_version;
$wp_version_label = is_string( $wp_version ) && $wp_version !== '' ? $wp_version : '';
?>

<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Customize the left and right text shown at the bottom of every wp-admin screen. Basic HTML (e.g. links) is allowed.', 'wp-powersuite' ); ?>
	</p>

	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>

		<table class="form-table" role="presentation">
			<tbody>
				<tr>
					<th scope="row">
						<label for="<?php echo esc_attr( $module_id ); ?>_footer_left"><?php esc_html_e( 'Left footer text', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<textarea
							id="<?php echo esc_attr( $module_id ); ?>_footer_left"
							name="footer_left"
							class="large-text wp-powersuite-advanced-setting"
							data-setting-key="footer_left"
							rows="3"
							placeholder="<?php esc_attr_e( 'e.g. Thank you for using My Site.', 'wp-powersuite' ); ?>"
						><?php echo esc_textarea( $footer_left ); ?></textarea>
						<p class="description">
							<?php esc_html_e( 'Leave empty to keep the default WordPress “Thank you for creating with WordPress” message.', 'wp-powersuite' ); ?>
						</p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="<?php echo esc_attr( $module_id ); ?>_footer_right"><?php esc_html_e( 'Right footer text', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<textarea
							id="<?php echo esc_attr( $module_id ); ?>_footer_right"
							name="footer_right"
							class="large-text wp-powersuite-advanced-setting"
							data-setting-key="footer_right"
							rows="3"
							placeholder="<?php esc_attr_e( 'e.g. Version 1.0 · Support', 'wp-powersuite' ); ?>"
						><?php echo esc_textarea( $footer_right ); ?></textarea>
						<p class="description">
							<?php
							if ( $wp_version_label !== '' ) {
								printf(
									/* translators: %s: WordPress version number */
									esc_html__( 'Leave empty to hide the default version text (Version %s). The Footer Timezone Display module can append to custom right-side text.', 'wp-powersuite' ),
									esc_html( $wp_version_label )
								);
							} else {
								esc_html_e( 'Leave empty to hide the default WordPress version text. The Footer Timezone Display module can append to custom right-side text.', 'wp-powersuite' );
							}
							?>
						</p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Example', 'wp-powersuite' ); ?></th>
					<td>
						<p class="description" style="margin-top: 0;">
							<code>&lt;a href="https://example.com" target="_blank" rel="noopener noreferrer"&gt;<?php esc_html_e( 'Visit our website', 'wp-powersuite' ); ?>&lt;/a&gt;</code>
						</p>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
