<?php
/**
 * @var Cart[] $recurring_carts
 */

use StoreEngine\Addons\Subscription\Classes\Utils;
use StoreEngine\Classes\Cart;
use StoreEngine\Classes\Countries;
use StoreEngine\Utils\Formatting;
use StoreEngine\Utils\Helper;
use StoreEngine\Utils\TaxUtil;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

?>

<tr class="recurring-totals">
	<th colspan="2">
		<strong><?php esc_html_e( 'Recurring totals', 'storeengine' ); ?></strong>
	</th>
</tr>

<?php
$display_heading = true;

foreach ( $recurring_carts as $recurring_cart_key => $recurring_cart ) { ?>
	<tr class="cart-subtotal recurring-total">
	<?php if ( $display_heading ) { ?>
		<?php $display_heading = false; ?>
		<th rowspan="<?php echo esc_attr( count( $recurring_carts ) ); ?>"><?php esc_html_e( 'Subtotal', 'storeengine' ); ?></th>
		<td data-title="<?php esc_attr_e( 'Subtotal', 'storeengine' ); ?>"><?php Utils::cart_totals_subtotal_html( $recurring_cart ); ?></td>
	<?php } else { ?>
		<th></th>
		<td><?php Utils::cart_totals_subtotal_html( $recurring_cart ); ?></td>
	<?php } ?>
	</tr>
	<?php
}

$display_heading = true;

foreach ( storeengine_cart()->get_coupons() as $code => $coupon ) {
	foreach ( $recurring_carts as $recurring_cart_key => $recurring_cart ) {
		foreach ( $recurring_cart->get_coupons() as $recurring_code => $recurring_coupon ) {
			if ( $recurring_code !== $code ) {
				continue;
			}
			?>
			<tr class="cart-discount coupon-<?php echo esc_attr( $code ); ?> recurring-total">
				<?php if ( $display_heading ) { ?>
					<?php $display_heading = false; ?>
					<th rowspan="<?php echo esc_attr( count( $recurring_carts ) ); ?>"><?php Formatting::cart_totals_coupon_label( $coupon ); ?></th>
					<td data-title="<?php Formatting::cart_totals_coupon_label( $coupon ); ?>"><?php
						/**
						 * @see wcs_cart_totals_coupon_html
						 * @see wcs_cart_coupon_remove_link_html
						 */
						Formatting::cart_totals_coupon_html( $coupon );
					?></td>
				<?php } else { ?>
					<th></th>
					<td><?php Formatting::cart_totals_coupon_html( $coupon ); ?></td>
				<?php } ?>
			</tr>
			<?php
		}
	}
}

foreach ( $recurring_carts as $recurring_cart_key => $recurring_cart ) {
	foreach ( $recurring_cart->get_fees() as $recurring_fee ) { ?>
		<tr class="fee recurring-total">
			<th scope="row"><?php Formatting::cart_totals_fee_label( $recurring_fee ); ?></th>
			<td data-title="<?php esc_attr( Formatting::cart_totals_fee_label( $recurring_fee, false ) ); ?>"><?php echo Formatting::price( $recurring_fee->amount ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></td>
		</tr>
		<?php
	}
}

if ( TaxUtil::is_tax_enabled() && ! Helper::cart()->display_prices_including_tax() ) {
	if ( 'itemized' === Helper::get_settings( 'tax_total_display' ) ) {
		$display_heading = true;
		foreach ( storeengine_cart()->get_taxes() as $tax_id => $tax_total ) {
			foreach ( $recurring_carts as $recurring_cart_key => $recurring_cart ) {
				foreach ( $recurring_cart->get_tax_totals() as $recurring_code => $recurring_tax ) {
					if ( ! isset( $recurring_tax->tax_rate_id ) || $recurring_tax->tax_rate_id !== $tax_id ) {
						continue;
					}

					/**
					 * Allow third-parties to filter the tax displayed.
					 *
					 * @param string The recurring cart's tax total price string for this tax code.
					 * @param Cart $recurring_cart The recurring cart.
					 * @param string  $recurring_code The tax code.
					 * @param object  $recurring_tax  The recurring tax data generated by @see WC_Cart::get_tax_totals()
					 */
					$tax_amount = wp_kses_post( apply_filters( 'storeengine/subscription/cart/recurring_cart_itemized_tax_totals_html', Utils::prepare_cart_price_html( $recurring_tax->formatted_amount, $recurring_cart ), $recurring_cart, $recurring_code, $recurring_tax ) );

					// If the returned amount is empty, skip it.
					if ( empty( $tax_amount ) ) {
						continue;
					}
					?>
					<tr class="tax-rate tax-rate-<?php echo esc_attr( sanitize_title( $recurring_code ) ); ?> recurring-total">
					<?php if ( $display_heading ) { ?>
						<?php $display_heading = false; ?>
						<th><?php echo esc_html( $recurring_tax->label ); ?></th>
						<td data-title="<?php echo esc_attr( $recurring_tax->label ); ?>"><?php echo $tax_amount; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- XSS ok ?></td>
					<?php } else { ?>
						<th></th>
						<td><?php echo $tax_amount; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- XSS ok ?></td>
					<?php } ?>
					</tr>
					<?php
				}
			}

			$display_heading = true;
		}
	} else {
		$display_heading = true;

		foreach ( $recurring_carts as $recurring_cart_key => $recurring_cart ) {
			/**
			 * Allow third-parties to filter the tax displayed.
			 *
			 * @param string The recurring cart's total tax price string.
			 * @param Cart $recurring_cart The recurring cart.
			 */
			$tax_amount = wp_kses_post( apply_filters( 'storeengine/subscription/cart/recurring_cart_tax_totals_html', Utils::prepare_cart_price_html( $recurring_cart->get_taxes_total(), $recurring_cart ), $recurring_cart ) );

			// Skip the tax if there's nothing to display.
			if ( empty( $tax_amount ) ) {
				continue;
			} ?>

			<tr class="tax-total recurring-total">
			<?php if ( $display_heading ) { ?>
				<?php $display_heading = false; ?>
				<th><?php echo esc_html( Countries::init()->tax_or_vat() ); ?></th>
				<td data-title="<?php echo esc_attr( Countries::init()->tax_or_vat() ); ?>"><?php echo $tax_amount; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- XSS ok. ?></td>
			<?php } else { ?>
				<th></th>
				<td><?php echo $tax_amount; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- XSS ok. ?></td>
			<?php } ?>
			</tr>
			<?php
		}
	}
}

$display_heading = true;

foreach ( $recurring_carts as $recurring_cart_key => $recurring_cart ) { ?>
	<tr class="order-total recurring-total">
		<?php if ( $display_heading ) { ?>
			<?php $display_heading = false; ?>
			<th rowspan="<?php echo esc_attr( count( $recurring_carts ) ); ?>"><?php esc_html_e( 'Recurring Total', 'storeengine' ); ?></th>
			<td data-title="<?php esc_attr_e( 'Recurring Total', 'storeengine' ); ?>"><?php Utils::cart_totals_order_total_html( $recurring_cart ); ?></td>
		<?php } else { ?>
			<th></th>
			<td><?php Utils::cart_totals_order_total_html( $recurring_cart ); ?></td>
		<?php } ?>
	</tr>
	<?php
}

// End of file cart-recurring-total.php
