<?php
namespace CustomLoginLogoLink;

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

/**
 * Admin UI for Custom Login Logo Link module.
 *
 * @var string $module_id The ID of the current module.
 * @var array  $module_meta The metadata for the current module.
 * @since 1.0.0
 */

if ( ! class_exists( 'CustomLoginLogoLink\Module' ) ) {
	$module_file = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'module.php';
	if ( file_exists( $module_file ) ) {
		require_once $module_file;
	}
}

$default_settings = [
	'logo_link_url' => home_url( '/' ),
];

if ( class_exists( 'CustomLoginLogoLink\Module' ) ) {
	try {
		$module_instance = new Module();
		if ( method_exists( $module_instance, 'getDefaultSettings' ) ) {
			$default_settings = $module_instance->getDefaultSettings();
		}
	} catch ( \Exception $e ) {
		// Use defaults if module can't be instantiated.
	}
}

$settings = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
if ( ! is_array( $settings ) ) {
	$settings = [];
}
$settings = array_merge( $default_settings, $settings );

$customizor_on = OptionStoreModuleSettingsRepository::isModuleEnabled( 'login-page-customizor' );

?>
<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Change the login logo link URL. By default, the login logo links to WordPress.org. You can change it to point to your site homepage or any custom URL.', 'wp-powersuite' ); ?>
	</p>

	<?php if ( $customizor_on ) : ?>
		<div class="notice notice-info inline" style="margin: 0 0 16px 0;">
			<p>
				<?php esc_html_e( 'Login Page Customizer (Pro): if its Form Customization Logo Link field is filled in, that URL takes priority over this module.', 'wp-powersuite' ); ?>
			</p>
		</div>
	<?php endif; ?>

	<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">
			<tbody>
				<tr>
					<th scope="row">
						<label for="<?php echo esc_attr( $module_id . '_logo_link_url' ); ?>">
							<?php esc_html_e( 'Logo Link URL:', 'wp-powersuite' ); ?>
						</label>
					</th>
					<td>
						<input type="url"
							id="<?php echo esc_attr( $module_id . '_logo_link_url' ); ?>"
							name="logo_link_url"
							class="wp-powersuite-advanced-setting regular-text"
							data-setting-key="logo_link_url"
							value="<?php echo esc_url( $settings['logo_link_url'] ?? home_url( '/' ) ); ?>"
							placeholder="<?php echo esc_attr( home_url( '/' ) ); ?>"
						/>
						<p class="description">
							<?php esc_html_e( 'Enter the URL where the login logo should link to. Default is your site homepage. Pair with Custom Login Logo to replace the image as well.', 'wp-powersuite' ); ?>
						</p>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
