<?php
/**
 * Pay with Credits Template
 *
 * Displays the checkbox and information for using affiliate credits at checkout.
 *
 * @since 2.15.0
 *
 * @package wc-affiliate
 * @var string $checked Whether the checkbox should be checked
 * @var string $currency_symbol The currency symbol
 * @var array $credit_info Array containing total_credits and eligible_amount
 */
?>

<div id="wca-pay-with-credits-wrapper" class="wca-pay-with-credits">
	<h3><?php esc_html_e( 'Pay with Affiliate Credits', 'wc-affiliate' ); ?></h3>
	<p>
		<label>
			<input type="checkbox" name="wca_pay_with_credits" id="wca_pay_with_credits"
					value="1" <?php echo esc_attr( $checked ); ?> />
			<?php
			echo wp_kses_post(
				sprintf(
					/* translators: 1: currency symbol, 2: total credits, 3: currency symbol, 4: eligible amount */
					__( 'You have %1$s%2$s in credits. Pay %3$s%4$s with credits.', 'wc-affiliate' ),
					$currency_symbol,
					number_format( $credit_info['total_credits'], 2 ),
					$currency_symbol,
					number_format( $credit_info['eligible_amount'], 2 )
				)
			);
			?>
		</label>
	</p>
</div>
<script>
( function ( $ ) {
	// Delegated on document.body so the handler survives WC fragment replacements.
	// Namespace (.wca_credits) prevents duplicates when the fragment re-executes this script.
	$( document.body ).off( 'change.wca_credits' ).on( 'change.wca_credits', '#wca_pay_with_credits', function () {
		$( document.body ).trigger( 'update_checkout' );
	} );
} )( jQuery );
</script>
