<?php

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

use StoreEngine\Utils\Helper;
use StoreEngine\Utils\Template;

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

$available_gateways    = Helper::get_payment_gateways()->get_available_payment_gateways();
$order_button_classes  = 'storeengine-btn storeengine-btn--preset-blue';
$order_button_disabled = empty( $available_gateways ) && 0 < storeengine()->cart->get_total( 'edit' );

if ( $order_button_disabled ) {
	$order_button_classes .= ' storeengine-btn--disabled';
}

?>
<div class="storeengine-ajax-checkout-form__payments">
	<?php if ( $needs_payment ?? false ) { ?>
		<div class="storeengine-checkout-available-payments">
			<h6><?php esc_html_e( 'Payment Method', 'storeengine' ); ?></h6>
			<?php
			if ( ! empty( $available_gateways ) ) {
				foreach ( $available_gateways as $gateway ) {
					Template::get_template( 'checkout/payment-method.php', [ 'gateway' => $gateway ] );
				}
			} else {
				$notice = apply_filters( 'storeengine/checkout_page/no_payment_method_notice', [
					'message' => __( 'No payment methods are available right now. Please contact support and we’ll assist you in completing your payment.', 'storeengine' ),
					'title'   => __( 'Unable to Process Payment', 'storeengine' ),
				] );

				storeengine_show_notice( $notice['message'], [ 'title' => $notice['title'], 'type' => 'secondary' ] );
			}
			?>
		</div>
	<?php } ?>

	<div class="storeengine-place-order">
		<noscript>
			<?php
				storeengine_show_notice(
					__( 'It looks like JavaScript is disabled in your browser. Please enable JavaScript and reload this page to continue, or contact support for assistance.', 'storeengine' ),
					[
						'title' => __( 'JavaScript is required to complete checkout.', 'storeengine' ),
						'type'  => 'secondary',
					]
				);
			?>
		</noscript>

		<?php do_action( 'storeengine/checkout_page/before_submit' ); ?>

		<div class="storeengine-payment-elements storeengine-checkout__order-btn" id="storeengine-payment-elements">
			<button type="submit" class="<?php echo esc_attr( $order_button_classes ); ?>" name="storeengine_checkout_place_order" id="storeengine_place_order"<?php disabled( $order_button_disabled ); ?>><?php echo esc_html( $place_order_label ?? __('Place Order', 'storeengine') ); ?></button>
		</div>

		<?php do_action( 'storeengine/checkout_page/after_submit' ); ?>
	</div>
</div>
