<?php
/**
 * Checkout Form
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/checkout/form-checkout.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs, the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 9.4.0
 */

defined( 'ABSPATH' ) || exit;

$checkout = WC()->checkout();
?>

<?php do_action( 'woocommerce_before_checkout_form', $checkout ); ?>

<?php if ( WC()->cart->is_empty() ) : ?>
	<div class="woocommerce-checkout cart-empty-wrapper">
		<div class="empty-cart show">
			<div class="empty-icon">
				<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4Z"/><path d="M3 6h18"/><path d="M16 10a4 4 0 0 1-8 0"/></svg>
			</div>
			<h2><?php esc_html_e( 'Your Cart is Empty', 'shopbar' ); ?></h2>
			<p><?php esc_html_e( 'Looks like you haven\'t added anything to your cart yet. Explore our collections and find something you love.', 'shopbar' ); ?></p>
			<?php if ( wc_get_page_id( 'shop' ) > 0 ) : ?>
				<a class="btn-shop" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>">
					<?php esc_html_e( 'Start Shopping', 'shopbar' ); ?>
					<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></svg>
				</a>
			<?php endif; ?>
		</div>
	</div>

<?php else : ?>

<?php if ( ! $checkout->is_registration_enabled() && $checkout->is_registration_required() && ! is_user_logged_in() ) : ?>
	<?php esc_html_e( 'You must be logged in to checkout.', 'shopbar' ); ?>
	<?php return; ?>
<?php endif; ?>

<h1 class="page-title checkout-page-title"><?php esc_html_e( 'Checkout', 'shopbar' ); ?></h1>

<div class="steps-indicator">
	<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="step-dot completed">
		<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>
	</a>
	<span class="step-line completed"></span>
	<span class="step-dot active-step">2</span>
	<span class="step-line"></span>
	<span class="step-dot">3</span>
</div>
<div class="step-label-group">
	<span class="step-label" style="color:var(--text-primary);"><?php esc_html_e( 'Cart', 'shopbar' ); ?></span>
	<span class="step-label active-label"><?php esc_html_e( 'Checkout', 'shopbar' ); ?></span>
	<span class="step-label"><?php esc_html_e( 'Confirmation', 'shopbar' ); ?></span>
</div>

<div class="checkout-layout">

	<div class="checkout-main">

		<?php if ( $checkout->get_checkout_fields() ) : ?>

		<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data">

			<?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>

			<div class="checkout-section" id="section-shipping">
				<div class="section-title-row">
					<span class="section-number">1</span>
					<h2 class="section-heading"><?php esc_html_e( 'Shipping Information', 'shopbar' ); ?></h2>
				</div>

				<?php if ( ! is_user_logged_in() && $checkout->is_registration_enabled() ) : ?>
				<div class="login-prompt">
					<span><?php esc_html_e( 'Already have an account?', 'shopbar' ); ?></span>
					<a href="<?php echo esc_url( wp_login_url( wc_get_checkout_url() ) ); ?>">
						<?php esc_html_e( 'Log in', 'shopbar' ); ?>
						<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></svg>
					</a>
				</div>
				<?php endif; ?>

				<?php do_action( 'woocommerce_checkout_billing' ); ?>

				<?php if ( ! is_user_logged_in() && $checkout->is_registration_enabled() ) : ?>
				<div class="check-row" style="margin-top:16px;">
					<input type="checkbox" class="f-check" id="createaccount" name="createaccount" value="1" <?php checked( ( true === $checkout->get_value( 'createaccount' ) || ( true === apply_filters( 'woocommerce_create_account_default_checked', false ) ) ), true ); ?>>
					<label class="check-label" for="createaccount"><?php esc_html_e( 'Create an account for faster checkout', 'shopbar' ); ?></label>
				</div>
				<?php endif; ?>

				<div class="check-row ship-to-different" style="margin-top:12px;">
					<input type="checkbox" class="f-check" id="ship-to-different-address-checkbox" name="ship_to_different_address" value="1">
					<label class="check-label" for="ship-to-different-address-checkbox"><?php esc_html_e( 'Ship to a different address?', 'shopbar' ); ?></label>
				</div>

				<div class="shipping-address-fields" id="shipping-address-fields">
					<?php do_action( 'woocommerce_checkout_shipping' ); ?>
				</div>

				<?php do_action( 'woocommerce_checkout_after_customer_details' ); ?>
			</div>

			<div class="checkout-section" id="section-payment">
				<div class="section-title-row">
					<span class="section-number">2</span>
					<h2 class="section-heading"><?php esc_html_e( 'Payment Details', 'shopbar' ); ?></h2>
				</div>

				<?php do_action( 'woocommerce_checkout_before_order_review' ); ?>

				<div id="order_review" class="woocommerce-checkout-review-order">
					<?php
					wc_get_template( 'checkout/review-order.php' );

					$available_gateways = array();
					if ( WC()->cart && WC()->cart->needs_payment() ) {
						$available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
						WC()->payment_gateways()->set_current_gateway( $available_gateways );
					}

					wc_get_template(
						'checkout/payment.php',
						array(
							'checkout'           => WC()->checkout(),
							'available_gateways' => $available_gateways,
							'order_button_text'  => apply_filters( 'woocommerce_order_button_text', esc_html__( 'Place order', 'shopbar' ) ),
						)
					);
					?>
				</div>

				<?php do_action( 'woocommerce_checkout_after_order_review' ); ?>
			</div>

		</form>

		<?php endif; ?>

	</div>

	<aside class="order-summary" id="orderSummary">
		<h2 class="summary-title"><?php esc_html_e( 'Order Summary', 'shopbar' ); ?></h2>

		<div class="summary-items">
			<?php
			foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
				$_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
				$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

				if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 ) {
					$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
					?>
					<div class="summary-item">
						<div class="summary-item-img">
							<?php
							$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
							if ( ! $product_permalink ) {
								echo wp_kses_post( $thumbnail );
							} else {
								printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), wp_kses_post( $thumbnail ) );
							}
							?>
						</div>
						<div class="summary-item-details">
							<div class="summary-item-name">
								<?php
								if ( ! $product_permalink ) {
									echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) );
								} else {
									echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
								}
								?>
							</div>
							<?php
							$item_data = wc_get_formatted_cart_item_data( $cart_item );
							if ( $item_data ) :
							?>
								<div class="summary-item-variant"><?php echo wp_kses_post( $item_data ); ?></div>
							<?php endif; ?>
							<div class="summary-item-qty"><?php printf( esc_html__( 'Qty: %d', 'shopbar' ), $cart_item['quantity'] ); ?></div>
						</div>
						<div class="summary-item-price">
							<?php echo wp_kses_post( apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ) ); ?>
						</div>
					</div>
					<?php
				}
			}
			?>
		</div>

		<?php if ( wc_coupons_enabled() ) : ?>
		<div class="s-promo" id="sPromoBox">
			<div class="s-promo-form">
				<input type="text" name="coupon_code" class="s-promo-input" placeholder="<?php esc_attr_e( 'Discount code', 'shopbar' ); ?>" id="promo_code_input">
				<button type="button" class="s-promo-btn" id="apply_promo_btn"><?php esc_html_e( 'Apply', 'shopbar' ); ?></button>
			</div>
			<div class="s-promo-message" id="promoMessage"></div>
		</div>

		<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
		<div class="s-promo-applied show">
			<span><?php wc_cart_totals_coupon_label( $coupon ); ?></span>
			<span><?php wc_cart_totals_coupon_html( $coupon ); ?></span>
		</div>
		<?php endforeach; ?>
		<?php endif; ?>

		<div class="summary-divider"></div>

		<div class="summary-row">
			<span><?php esc_html_e( 'Subtotal', 'shopbar' ); ?></span>
			<span><?php wc_cart_totals_subtotal_html(); ?></span>
		</div>

		<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
		<div class="summary-row">
			<span><?php echo esc_html( $fee->name ); ?></span>
			<span><?php wc_cart_totals_fee_html( $fee ); ?></span>
		</div>
		<?php endforeach; ?>

		<?php if ( WC()->cart->needs_shipping() && WC()->cart->show_shipping() ) : ?>
		<div class="summary-row">
			<span><?php esc_html_e( 'Shipping', 'shopbar' ); ?></span>
			<span class="free-shipping"><?php echo wp_kses_post( WC()->cart->get_cart_shipping_total() ); ?></span>
		</div>
		<?php elseif ( WC()->cart->needs_shipping() ) : ?>
		<div class="summary-row">
			<span><?php esc_html_e( 'Shipping', 'shopbar' ); ?></span>
			<span><?php esc_html_e( 'Calculated at next step', 'shopbar' ); ?></span>
		</div>
		<?php endif; ?>

		<?php if ( wc_tax_enabled() && ! WC()->cart->display_prices_including_tax() ) : ?>
		<div class="summary-row">
			<span><?php echo esc_html( WC()->countries->tax_or_vat() ); ?></span>
			<span><?php wc_cart_totals_taxes_total_html(); ?></span>
		</div>
		<?php endif; ?>

		<div class="summary-row total">
			<span><?php esc_html_e( 'Total', 'shopbar' ); ?></span>
			<span><?php wc_cart_totals_order_total_html(); ?></span>
		</div>

		<?php if ( wc_tax_enabled() && WC()->cart->get_taxes_total() ) : ?>
		<p class="s-tax-note"><?php printf( esc_html__( 'Including %s in taxes', 'shopbar' ), wp_kses_post( WC()->cart->get_taxes_total() ) ); ?></p>
		<?php endif; ?>

	</aside>

</div>

<?php do_action( 'woocommerce_after_checkout_form', $checkout ); ?>

<?php endif; ?>
