<?php
/**
 * Wizard Step 2 — Connect to eMAG (IP whitelist + credentials + Test Connection).
 *
 * @package SkyEmagSync
 */

defined( 'ABSPATH' ) || exit;

$server_ip = SkyEmagSync\Admin\HelpPage::detect_server_ip();
$creds     = (array) SkyEmagSync\Options::get( SkyEmagSync\Options::API_CREDENTIALS, [] );
$username  = isset( $creds['username'] ) ? (string) $creds['username'] : '';
$region    = isset( $creds['region'] )   ? (string) $creds['region']   : 'ro';
$api_code  = isset( $creds['api_code'] ) ? (string) $creds['api_code'] : '';
$pwd_set   = ! empty( $creds['password'] );
?>
<h2><?php esc_html_e( 'Connect to eMAG', 'skyservers-product-sync-for-emag' ); ?></h2>

<p><strong><?php esc_html_e( "Your server's IP address:", 'skyservers-product-sync-for-emag' ); ?></strong></p>
<div class="skyemag-ip-display">
	<span id="skyemag-server-ip"><?php echo esc_html( $server_ip ?: __( 'Could not detect', 'skyservers-product-sync-for-emag' ) ); ?></span>
	<button type="button" class="button" data-skyemag-copy-ip><?php esc_html_e( 'Copy', 'skyservers-product-sync-for-emag' ); ?></button>
</div>
<p style="font-size:13px;color:#646970;margin:6px 0 12px;">
	<?php esc_html_e( 'eMAG will refuse every API call until this IP is whitelisted in your seller account.', 'skyservers-product-sync-for-emag' ); ?>
</p>

<details style="margin:8px 0 18px;">
	<summary style="cursor:pointer;font-weight:600;">
		<?php esc_html_e( 'Where to add it (takes 30 seconds)', 'skyservers-product-sync-for-emag' ); ?>
	</summary>
	<ol style="margin-top:8px;">
		<li><?php
			echo wp_kses_post( sprintf(
				/* translators: %s: link to marketplace.emag.ro */
				esc_html__( 'Open %s and log in.', 'skyservers-product-sync-for-emag' ),
				'<a href="https://marketplace.emag.ro" target="_blank" rel="noopener">marketplace.emag.ro</a>'
			) );
		?></li>
		<li><?php
			echo wp_kses_post( sprintf(
				/* translators: %1$s: bold "My Account → Technical Details"; %2$s: bold "IP Addresses" */
				esc_html__( 'Go to %1$s tab, scroll to %2$s.', 'skyservers-product-sync-for-emag' ),
				'<strong>' . esc_html__( 'My Account → Technical Details', 'skyservers-product-sync-for-emag' ) . '</strong>',
				'<strong>' . esc_html__( 'IP Addresses', 'skyservers-product-sync-for-emag' ) . '</strong>'
			) );
		?></li>
		<li><?php
			echo wp_kses_post( sprintf(
				/* translators: %s: bold button label "Add a new IP" */
				esc_html__( 'Click the green %s button.', 'skyservers-product-sync-for-emag' ),
				'<strong>' . esc_html__( 'Add a new IP', 'skyservers-product-sync-for-emag' ) . '</strong>'
			) );
		?></li>
		<li><?php esc_html_e( 'Paste the IP above and click Save.', 'skyservers-product-sync-for-emag' ); ?></li>
	</ol>
</details>

<?php SkyEmagSync\Admin\SetupWizard::open_step_form( 2 ); ?>

	<label>
		<input type="checkbox" name="ip_added">
		<?php esc_html_e( "I've added the IP in my eMAG account", 'skyservers-product-sync-for-emag' ); ?>
	</label>

	<h3><?php esc_html_e( 'eMAG account credentials', 'skyservers-product-sync-for-emag' ); ?></h3>

	<label for="skyemag-username"><?php esc_html_e( 'Username', 'skyservers-product-sync-for-emag' ); ?></label>
	<input id="skyemag-username" type="text" name="skyemag_username" value="<?php echo esc_attr( $username ); ?>" autocomplete="off">

	<label for="skyemag-password"><?php esc_html_e( 'Password', 'skyservers-product-sync-for-emag' ); ?></label>
	<input id="skyemag-password" type="password" name="skyemag_password" autocomplete="new-password" placeholder="<?php echo $pwd_set ? esc_attr__( '••• already set •••', 'skyservers-product-sync-for-emag' ) : ''; ?>">
	<p style="font-size:12px;color:#646970;"><?php esc_html_e( 'Stored AES-256-CBC encrypted.', 'skyservers-product-sync-for-emag' ); ?></p>

	<label for="skyemag-region"><?php esc_html_e( 'Region', 'skyservers-product-sync-for-emag' ); ?></label>
	<select id="skyemag-region" name="skyemag_region">
		<option value="ro" <?php selected( $region, 'ro' ); ?>><?php esc_html_e( 'Romania', 'skyservers-product-sync-for-emag' ); ?></option>
		<option value="bg" <?php selected( $region, 'bg' ); ?>><?php esc_html_e( 'Bulgaria', 'skyservers-product-sync-for-emag' ); ?></option>
		<option value="hu" <?php selected( $region, 'hu' ); ?>><?php esc_html_e( 'Hungary', 'skyservers-product-sync-for-emag' ); ?></option>
	</select>

	<label for="skyemag-api-code"><?php esc_html_e( 'API code', 'skyservers-product-sync-for-emag' ); ?></label>
	<input id="skyemag-api-code" type="text" name="skyemag_api_code" value="<?php echo esc_attr( $api_code ); ?>">
	<p style="font-size:12px;color:#646970;"><?php esc_html_e( 'Reference only — not used in authentication.', 'skyservers-product-sync-for-emag' ); ?></p>

	<p>
		<button type="button" class="button" data-skyemag-test-connection>
			<?php esc_html_e( 'Test Connection', 'skyservers-product-sync-for-emag' ); ?>
		</button>
		<span id="skyemag-test-result" style="margin-left:12px;"></span>
	</p>
	<?php SkyEmagSync\Admin\SetupWizard::render_step_buttons( 2 ); ?>
</form>
