<?php
/**
 * Checkout Form
 *
 * @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', 'pawwell' ); ?></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.', 'pawwell' ); ?></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', 'pawwell' ); ?>
					<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.', 'pawwell' ); ?>
	<?php return; ?>
<?php endif; ?>

<?php
$co_item_count = ( function_exists( 'WC' ) && WC()->cart ) ? WC()->cart->get_cart_contents_count() : 0;
$co_count_label = sprintf(
	/* translators: %d: number of items in cart */
	_n( '%d item', '%d items', $co_item_count, 'pawwell' ),
	$co_item_count
);
?>

<!-- ============ CHECKOUT HERO ============ -->
<?php if ( true === get_theme_mod( 'pawwell_co_hero_enable', true ) ) : ?>
<section class="checkout-hero">
	<div class="container">
		<div class="checkout-hero-row">
			<div class="checkout-hero-text">
				<?php
				$co_eyebrow = get_theme_mod( 'pawwell_co_hero_eyebrow', __( 'Final Step', 'pawwell' ) );
				$co_title   = get_theme_mod( 'pawwell_co_hero_title', __( 'Secure', 'pawwell' ) );
				$co_accent  = get_theme_mod( 'pawwell_co_hero_title_accent', __( 'Checkout', 'pawwell' ) );
				$co_sub     = get_theme_mod( 'pawwell_co_hero_subtitle', __( 'Review your details, pick a delivery method and complete your purchase. Your payment is encrypted end-to-end.', 'pawwell' ) );
				?>
				<?php if ( ! empty( $co_eyebrow ) ) : ?>
					<div class="checkout-eyebrow"><?php echo esc_html( $co_eyebrow ); ?></div>
				<?php endif; ?>
				<h1 class="checkout-hero-title"><?php echo esc_html( $co_title ); ?> <span class="checkout-accent"><?php echo esc_html( $co_accent ); ?></span></h1>
				<?php if ( ! empty( $co_sub ) ) : ?>
					<p class="checkout-hero-sub"><?php echo esc_html( $co_sub ); ?></p>
				<?php endif; ?>
			</div>
			<?php if ( true === get_theme_mod( 'pawwell_co_hero_stat_enable', true ) ) : ?>
				<div class="checkout-hero-stat">
					<div class="chs-icon">
						<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="10" width="16" height="11" rx="2"/><path d="M8 10V7a4 4 0 0 1 8 0v3"/><path d="M12 15v2"/></svg>
					</div>
					<div class="chs-text">
						<strong><?php echo esc_html( get_theme_mod( 'pawwell_co_hero_stat_badge', __( '256-bit', 'pawwell' ) ) ); ?></strong>
						<span><?php echo esc_html( get_theme_mod( 'pawwell_co_hero_stat_text', __( 'encrypted & secure', 'pawwell' ) ) ); ?></span>
					</div>
				</div>
			<?php endif; ?>
		</div>
	</div>
</section>
<?php endif; ?>

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

<!-- Steps indicator -->
<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', 'pawwell' ); ?></span>
	<span class="step-label active-label"><?php esc_html_e( 'Checkout', 'pawwell' ); ?></span>
	<span class="step-label"><?php esc_html_e( 'Confirmation', 'pawwell' ); ?></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' ); ?>

			<!-- ============ STEP 1 — DETAILS & SHIPPING ============ -->
			<div class="checkout-section co-card" id="section-shipping">
				<div class="co-card-head section-title-row">
					<span class="co-step-num section-number">1</span>
					<div class="co-card-titles">
						<h2 class="section-heading"><?php esc_html_e( 'Contact & Shipping Details', 'pawwell' ); ?></h2>
						<div class="co-sub"><?php esc_html_e( 'We\'ll send your order confirmation and delivery here', 'pawwell' ); ?></div>
					</div>
				</div>

				<?php if ( ! is_user_logged_in() && $checkout->is_registration_enabled() ) : ?>
				<div class="login-prompt">
					<span><?php esc_html_e( 'Already have an account?', 'pawwell' ); ?></span>
					<a href="<?php echo esc_url( wp_login_url( wc_get_checkout_url() ) ); ?>">
						<?php esc_html_e( 'Log in', 'pawwell' ); ?>
						<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', 'pawwell' ); ?></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?', 'pawwell' ); ?></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>

			<!-- ============ STEP 2 — PAYMENT ============ -->
			<div class="checkout-section co-card" id="section-payment">
				<div class="co-card-head section-title-row">
					<span class="co-step-num section-number">2</span>
					<div class="co-card-titles">
						<h2 class="section-heading"><?php esc_html_e( 'Delivery & Payment', 'pawwell' ); ?></h2>
						<div class="co-sub"><?php esc_html_e( 'Choose a delivery speed and a secure payment method', 'pawwell' ); ?></div>
					</div>
				</div>

				<?php if ( WC()->cart->needs_shipping() && WC()->cart->show_shipping() ) : ?>
				<div class="checkout-shipping-methods" id="checkoutShippingMethods">
					<?php wc_cart_totals_shipping_html(); ?>
				</div>
				<?php endif; ?>

				<?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', 'pawwell' ) ),
						)
					);
					?>
				</div>

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

		</form>

		<?php endif; ?>

	</div>

	<!-- ============ ORDER SUMMARY SIDEBAR ============ -->
	<aside class="order-summary co-aside" id="orderSummary">
		<div class="summary-head co-summary-head">
			<div class="sh-icon">
				<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="5" y="5" width="14" height="16" rx="2"/><path d="M9 3h6v4H9zM9 12h6M9 16h4"/></svg>
			</div>
			<div>
				<h2 class="summary-title"><?php esc_html_e( 'Order Summary', 'pawwell' ); ?></h2>
				<div class="sh-sub"><?php echo esc_html( $co_count_label ); ?></div>
			</div>
		</div>

		<div class="order-items 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="order-item summary-item">
						<div class="oi-img 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 ) );
							}
							?>
							<span class="oi-qty"><?php echo esc_html( $cart_item['quantity'] ); ?></span>
						</div>
						<div class="oi-info summary-item-details">
							<div class="oi-name 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="oi-meta summary-item-variant"><?php echo wp_kses_post( $item_data ); ?></div>
							<?php endif; ?>
						</div>
						<div class="oi-price 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', 'pawwell' ); ?>" id="promo_code_input">
				<button type="button" class="s-promo-btn" id="apply_promo_btn"><?php esc_html_e( 'Apply', 'pawwell' ); ?></button>
			</div>
			<div class="s-promo-message" id="promoMessage"></div>
		</div>
		<?php endif; ?>

		<?php
		/*
		 * Coupon-applied rows and all totals live in order-summary-totals.php
		 */
		wc_get_template( 'checkout/order-summary-totals.php' );
		?>

		<?php
	$co_trust_any = get_theme_mod( 'pawwell_co_trust_secure_enable', true )
		|| get_theme_mod( 'pawwell_co_trust_returns_enable', true )
		|| get_theme_mod( 'pawwell_co_trust_delivery_enable', true );
	?>
	<?php if ( $co_trust_any ) : ?>
	<div class="co-trust">
		<?php if ( get_theme_mod( 'pawwell_co_trust_secure_enable', true ) ) : ?>
			<div><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3 5 6v6c0 4 3 7 7 9 4-2 7-5 7-9V6l-7-3z"/><path d="m9 12 2 2 4-4"/></svg> <?php echo esc_html( get_theme_mod( 'pawwell_co_trust_secure_text', __( 'Secure Payment', 'pawwell' ) ) ); ?></div>
		<?php endif; ?>
		<?php if ( get_theme_mod( 'pawwell_co_trust_returns_enable', true ) ) : ?>
			<div><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M4 9a8 8 0 0 1 14-3l2 2"/><path d="M20 4v4h-4"/><path d="M20 15a8 8 0 0 1-14 3l-2-2"/><path d="M4 20v-4h4"/></svg> <?php echo esc_html( get_theme_mod( 'pawwell_co_trust_returns_text', __( 'Easy Returns', 'pawwell' ) ) ); ?></div>
		<?php endif; ?>
		<?php if ( get_theme_mod( 'pawwell_co_trust_delivery_enable', true ) ) : ?>
			<div><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3 7h11v9H3zM14 10h4l3 3v3h-7"/><circle cx="7" cy="18" r="1.8"/><circle cx="17.5" cy="18" r="1.8"/></svg> <?php echo esc_html( get_theme_mod( 'pawwell_co_trust_delivery_text', __( 'Fast Delivery', 'pawwell' ) ) ); ?></div>
		<?php endif; ?>
	</div>
	<?php endif; ?>

	<?php
	$co_pay_any = get_theme_mod( 'pawwell_co_pay_visa_enable', true )
		|| get_theme_mod( 'pawwell_co_pay_mc_enable', true )
		|| get_theme_mod( 'pawwell_co_pay_paypal_enable', true );
	?>
	<?php if ( $co_pay_any ) : ?>
	<div class="co-pay-row" aria-hidden="true">
		<?php if ( get_theme_mod( 'pawwell_co_pay_visa_enable', true ) ) : ?>
			<span class="co-pay-chip"><?php echo esc_html( get_theme_mod( 'pawwell_co_pay_visa_text', __( 'VISA', 'pawwell' ) ) ); ?></span>
		<?php endif; ?>
		<?php if ( get_theme_mod( 'pawwell_co_pay_mc_enable', true ) ) : ?>
			<span class="co-pay-chip"><?php echo esc_html( get_theme_mod( 'pawwell_co_pay_mc_text', __( 'MC', 'pawwell' ) ) ); ?></span>
		<?php endif; ?>
		<?php if ( get_theme_mod( 'pawwell_co_pay_paypal_enable', true ) ) : ?>
			<span class="co-pay-chip"><?php echo esc_html( get_theme_mod( 'pawwell_co_pay_paypal_text', __( 'PayPal', 'pawwell' ) ) ); ?></span>
		<?php endif; ?>
		</div>
	<?php endif; ?>

	</aside>

</div>

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

<?php endif;
