<?php
/**
 * Renders a Hub account settings field.
 *
 * @package northmill-online-shared
 */

declare(strict_types=1);

/**
 * View arguments.
 *
 * @var array $params
 */

defined( 'ABSPATH' ) || exit;

?>
<tbody
	<?php echo $params['tbody_id']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
		class="hub-account <?php echo esc_attr( $params['tbody_class'] ); ?>"
>
	<tr>
		<th scope="row">
				<span class="th-title">
					<?php echo esc_html( $params['row_title'] ); ?>
				</span>
			<?php echo $params['connection_status']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			<?php if ( ! $params['is_main'] ) : ?>
				<p>
					<a
						href="#"
						class="disconnect-backoffice-account"
						data-env="<?php echo esc_attr( $params['api_env'] ); ?>"
						data-existing="<?php echo 'template' !== $params['api_env'] ? 'yes' : 'no'; ?>"
					>
						<?php esc_html_e( 'Disconnect account', 'northmill-online-shared' ); ?>
					</a>
					<span class="disconnected-text" style="display: none; font-style: italic;">
						<?php esc_html_e( 'Account will be disconnected on save', 'northmill-online-shared' ); ?>
					</span>
				</p>
			<?php endif; ?>
			<p>
				<?php echo esc_html( $params['row_desc'] ); ?>
			</p>
		</th>
		<td>
			<label for="<?php echo esc_attr( $params['url_input_id'] ); ?>">
				<?php
				esc_html_e( 'API URL', 'northmill-online-shared' );
				echo ! $params['is_main'] ? '*' : '';
				?>
			</label>
			<select
				name="<?php echo esc_attr( $params['url_input_id'] ); ?>"
				id="<?php echo esc_attr( $params['url_input_id'] ); ?>"
				class="hub-account-url"
				<?php echo $params['make_input_required'] ? 'required' : ''; ?>
			>
				<?php
				foreach ( $params['api_url_options'] as $api_url ) {
					?>
					<option
						value="<?php echo esc_attr( $api_url ); ?>"
						<?php selected( $params['api_url'], $api_url ); ?>
					>
						<?php echo esc_html( $api_url ); ?>
					</option>
					<?php
				}
				?>
			</select>

			<label for="<?php echo esc_attr( $params['user_input_id'] ); ?>">
				<?php
				esc_html_e( 'Username', 'northmill-online-shared' );
				echo ! $params['is_main'] ? '*' : '';
				?>
			</label>
			<input
				type="text"
				id="<?php echo esc_attr( $params['user_input_id'] ); ?>"
				name="<?php echo esc_attr( $params['user_input_id'] ); ?>"
				class="regular-text code hub-account-user"
				value="<?php echo esc_attr( $params['api_user'] ); ?>"
				placeholder="<?php esc_attr_e( 'Username', 'northmill-online-shared' ); ?>"
				autocomplete="off"
				<?php echo $params['make_input_required'] ? 'required' : ''; ?>
			/>

			<label for="<?php echo esc_attr( $params['pass_input_id'] ); ?>">
				<?php
				esc_html_e( 'Password', 'northmill-online-shared' );
				echo ! $params['is_main'] ? '*' : '';
				?>
			</label>
			<input
				type="password"
				id="<?php echo esc_attr( $params['pass_input_id'] ); ?>"
				name="<?php echo esc_attr( $params['pass_input_id'] ); ?>"
				class="regular-text code hub-account-pass"
				value="<?php echo esc_attr( $params['api_pass'] ); ?>"
				placeholder="<?php esc_attr_e( 'Password', 'northmill-online-shared' ); ?>"
				autocomplete="off"
				<?php echo $params['make_input_required'] ? 'required' : ''; ?>
			/>

			<label
				for="<?php echo esc_attr( $params['display_name_input_id'] ); ?>"
				class="multi-account-field"
			>
				<?php
				esc_html_e( 'Display name', 'northmill-online-shared' );
				echo ! $params['is_main'] ? '*' : '';
				?>
			</label>
			<input
				type="text"
				id="<?php echo esc_attr( $params['display_name_input_id'] ); ?>"
				name="<?php echo esc_attr( $params['display_name_input_id'] ); ?>"
				class="regular-text code hub-account-display_name multi-account-field"
				value="<?php echo esc_attr( $params['display_name'] ); ?>"
				placeholder="<?php esc_attr_e( 'Display name', 'northmill-online-shared' ); ?>"
				<?php echo $params['make_input_required'] ? 'required' : ''; ?>
			/>

			<label
				for="<?php echo esc_attr( $params['shipping_methods_id'] ); ?>"
				class="multi-account-field"
			>
				<?php
				esc_html_e( 'Shipping methods', 'northmill-online-shared' );
				echo ! $params['is_main'] ? '*' : '';
				?>
			</label>
			<?php echo $params['shipping_methods_multi_select']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			<p class="description multi-account-field">
				<?php esc_html_e( 'Select all shipping methods allowed to use for this account.', 'northmill-online-shared' ); ?>
			</p>

			<?php if ( ! $params['is_main'] ) : ?>
				<label for="<?php echo esc_attr( $params['enabled_input_id'] ); ?>">
					<?php esc_html_e( 'Enabled', 'northmill-online-shared' ); ?>
				</label>
				<select
					name="<?php echo esc_attr( $params['enabled_input_id'] ); ?>"
					id="<?php echo esc_attr( $params['enabled_input_id'] ); ?>"
					class="hub-account-enabled"
				>
					<option value="enabled" <?php selected( $params['account_enabled'] ); ?>>
						<?php echo esc_html__( 'Enabled', 'northmill-online-shared' ); ?>
					</option>
					<option value="disabled" <?php selected( $params['account_enabled'], false ); ?>>
						<?php echo esc_html__( 'Disabled', 'northmill-online-shared' ); ?>
					</option>
				</select>
				<p class="description">
					<?php esc_html_e( 'If disabled, this account will not be used for any sync.', 'northmill-online-shared' ); ?>
				</p>
			<?php endif; ?>
		</td>
	</tr>
</tbody>
