<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! isset( $coupon_rows ) || ! is_array( $coupon_rows ) ) {
	$coupon_rows = array();
}

$payment_settings = SftcyWordformBase::wordform_settings_page_payment_config();
$currency_symbol  = SftcyWordformBuildForm::wordform_get_payment_currency_symbol( isset( $payment_settings['payment_currency'] ) ? $payment_settings['payment_currency'] : 'USD' );

foreach ( $coupon_rows as $coupon ) :
	$coupon_id           = absint( $coupon['id'] );
	$status_class        = ( 'inactive' === $coupon['coupon_status'] ) ? 'is-inactive' : 'is-active';
	$discount_suffix     = ( 'percentage' === $coupon['discount_type'] ) ? '%' : '';
	$discount_value_text = ( 'percentage' === $coupon['discount_type'] ) ? number_format_i18n( (float) $coupon['discount_value'], 2 ) . '%' : $currency_symbol . number_format_i18n( (float) $coupon['discount_value'], 2 );
	$usage_limit         = absint( $coupon['usage_limit'] );
	$usage_count         = absint( $coupon['usage_count'] );
	$usage_text          = ( $usage_limit > 0 ) ? sprintf( '%1$d / %2$d', $usage_count, $usage_limit ) : sprintf( __( '%d used', 'wordform-pro' ), $usage_count );
	$allowed_forms_text  = SftcyWordformBase::wordform_get_coupon_allowed_forms_text( $coupon['allowed_forms'] );
	$validity_text       = SftcyWordformBase::wordform_get_coupon_validity_text( $coupon );
	?>
	<tr
		data-coupon-id="<?php echo esc_attr( $coupon_id ); ?>"
		data-coupon='<?php echo esc_attr( wp_json_encode( $coupon ) ); ?>'
	>
		<td><strong><?php echo esc_html( $coupon['coupon_code'] ); ?></strong></td>
		<td><?php echo esc_html( ucfirst( $coupon['discount_type'] ) ); ?></td>
		<td><?php echo esc_html( $discount_value_text ); ?></td>
		<td><span class="wordform-coupon-status-badge <?php echo esc_attr( $status_class ); ?>"><?php echo esc_html( ucfirst( $coupon['coupon_status'] ) ); ?></span></td>
		<td><?php echo esc_html( $usage_text ); ?></td>
		<td><?php echo esc_html( $validity_text ); ?></td>
		<td><?php echo esc_html( $allowed_forms_text ); ?></td>
		<td class="wordform-coupon-actions-cell">
			<button type="button" class="button button-secondary button-small sc-wordform-edit-coupon"><?php esc_html_e( 'Edit', 'wordform-pro' ); ?></button>
			<button type="button" class="button button-link-delete sc-wordform-delete-coupon"><?php esc_html_e( 'Delete', 'wordform-pro' ); ?></button>
		</td>
	</tr>
<?php endforeach; ?>
