<?php
/**
 * Accounts list view template.
 *
 * Variables in scope (passed by Admin_Settings::render_list_view()):
 *
 * @var string                                                   $add_url
 * @var \DonationRouter\Account_List_Table     $list_table
 * @var array{available:bool,hint?:string,action?:string,nonce_action?:string,merchant_id?:string,country?:string,mode?:string} $import
 *
 * @since   1.0.0
 * @package DonationRouter
 */

declare( strict_types=1 );

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

?>
<div class="ador-list-header">
	<a href="<?php echo esc_url( $add_url ); ?>" class="page-title-action">
		<?php esc_html_e( 'Add New Account', 'alamin-donation-router' ); ?>
	</a>
		<?php if ( ! empty( $import['available'] ) ) : ?>
			<button type="button" class="page-title-action ador-import-trigger" aria-haspopup="dialog" aria-controls="ador-import-modal">
				<?php esc_html_e( 'Import from GiveWP', 'alamin-donation-router' ); ?>
			</button>
		<?php else : ?>
			<button type="button" class="page-title-action ador-import-trigger" disabled aria-disabled="true" title="<?php echo esc_attr( (string) ( $import['hint'] ?? '' ) ); ?>">
				<?php esc_html_e( 'Import from GiveWP', 'alamin-donation-router' ); ?>
			</button>
			<?php if ( ! empty( $import['hint'] ) ) : ?>
				<span class="ador-import-hint description"><?php echo esc_html( (string) $import['hint'] ); ?></span>
			<?php endif; ?>
		<?php endif; ?>
</div>

<?php if ( ! empty( $import['available'] ) ) : ?>
	<div class="ador-modal" id="ador-import-modal" hidden>
		<div class="ador-modal__overlay" data-ador-modal-close></div>
		<div class="ador-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="ador-import-title">
			<button type="button" class="ador-modal__close" data-ador-modal-close aria-label="<?php esc_attr_e( 'Close', 'alamin-donation-router' ); ?>">&times;</button>
			<h2 id="ador-import-title"><?php esc_html_e( 'Import your connected GiveWP PayPal account', 'alamin-donation-router' ); ?></h2>
			<p>
				<?php
				printf(
					/* translators: 1: masked merchant id, 2: PayPal environment (live/sandbox). */
					esc_html__( 'GiveWP already has a PayPal account connected (merchant %1$s, %2$s mode). Import it to route donations to it and customise its receipts — no new PayPal connection needed.', 'alamin-donation-router' ),
					esc_html( '' !== $import['merchant_id'] ? '…' . substr( $import['merchant_id'], -6 ) : __( 'unknown', 'alamin-donation-router' ) ),
					esc_html( $import['mode'] )
				);
				?>
			</p>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
				<?php wp_nonce_field( $import['nonce_action'], 'ador_nonce' ); ?>
				<input type="hidden" name="action" value="<?php echo esc_attr( $import['action'] ); ?>" />
				<p>
					<label>
						<?php esc_html_e( 'Account label', 'alamin-donation-router' ); ?><br />
						<input type="text" name="account_label" value="<?php echo esc_attr__( 'PayPal (imported)', 'alamin-donation-router' ); ?>" class="regular-text" />
					</label>
				</p>
				<?php if ( '' === $import['country'] ) : ?>
				<p>
					<label>
						<?php esc_html_e( 'Account country (ISO 3166-1 alpha-2)', 'alamin-donation-router' ); ?><br />
						<input type="text" name="account_country" maxlength="2" class="small-text" />
					</label>
				</p>
				<?php else : ?>
				<input type="hidden" name="account_country" value="<?php echo esc_attr( $import['country'] ); ?>" />
				<?php endif; ?>
				<p>
					<label>
						<?php esc_html_e( 'Legal entity name', 'alamin-donation-router' ); ?><br />
						<input type="text" name="legal_entity_name" class="regular-text" required />
					</label>
				</p>
				<p>
					<label>
						<?php esc_html_e( 'Tax ID / EIN', 'alamin-donation-router' ); ?><br />
						<input type="text" name="legal_entity_tax_id" class="regular-text" required />
					</label>
				</p>
				<p>
					<label>
						<?php esc_html_e( 'Legal entity address', 'alamin-donation-router' ); ?><br />
						<textarea name="legal_entity_address" class="large-text" rows="2" required></textarea>
					</label>
				</p>
				<p class="ador-modal__actions">
					<button type="button" class="button" data-ador-modal-close>
						<?php esc_html_e( 'Cancel', 'alamin-donation-router' ); ?>
					</button>
					<button type="submit" class="button button-primary">
						<?php esc_html_e( 'Import connected account', 'alamin-donation-router' ); ?>
					</button>
				</p>
			</form>
		</div>
	</div>
<?php endif; ?>

<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
	<?php wp_nonce_field( 'ador_bulk_deactivate', 'ador_bulk_nonce' ); ?>
	<?php $list_table->display(); ?>
	<?php
	/*
	 * Route the bulk submit to our admin-post handler. WP_List_Table::display()
	 * emits its own bulk <select name="action"> (and name="action2"), which
	 * collides with this routing field. admin-post.php dispatches on
	 * $_REQUEST['action'] and PHP keeps the LAST value of a repeated key, so this
	 * input MUST be printed after the table to win over the list table's own
	 * "deactivate" value — otherwise admin-post finds no matching handler and dies
	 * with an empty error page. Printing it last also lets both the top and bottom
	 * Apply buttons route correctly.
	 */
	?>
	<input type="hidden" name="action" value="ador_bulk_deactivate_accounts" />
</form>
