<?php
/**
 * Public branch locations grid.
 *
 * @package Lucrativeit_Subscription_Recurring_Billing
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Included template; variables are local to the including scope.

$instance_id = isset( $instance_id ) ? $instance_id : 'lucrsure-locations';
$heading     = isset( $heading ) ? $heading : '';
$subtitle    = isset( $subtitle ) ? $subtitle : '';
$branches    = isset( $branches ) ? $branches : array();
?>

<section class="lucrsure-locations" id="<?php echo esc_attr( $instance_id ); ?>">
	<?php if ( $heading || $subtitle ) : ?>
		<header class="lucrsure-locations-header">
			<?php if ( $heading ) : ?>
				<h2 class="lucrsure-locations-title"><?php echo esc_html( $heading ); ?></h2>
			<?php endif; ?>
			<?php if ( $subtitle ) : ?>
				<p class="lucrsure-locations-subtitle"><?php echo esc_html( $subtitle ); ?></p>
			<?php endif; ?>
		</header>
	<?php endif; ?>

	<?php if ( empty( $branches ) ) : ?>
		<p class="lucrsure-public-notice"><?php esc_html_e( 'No locations are available right now.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	<?php else : ?>
		<div class="lucrsure-locations-grid">
			<?php foreach ( $branches as $branch ) : ?>
				<?php
				$image_url  = LUCRSURE_Branch_Repository::image_url( $branch, 'large' );
				$amenities  = LUCRSURE_Branch_Repository::amenities( $branch );
				$address    = trim( (string) ( $branch['address'] ?? '' ) );
				$region     = trim( (string) ( $branch['region'] ?? '' ) );
				if ( '' === $address || false !== stripos( $address, 'default shipping' ) ) {
					$address = $region;
				}
				$hours      = lucrsure_format_branch_hours(
					isset( $branch['opening_time'] ) ? $branch['opening_time'] : '',
					isset( $branch['closing_time'] ) ? $branch['closing_time'] : ''
				);
				?>
				<article class="lucrsure-location-card">
					<div class="lucrsure-location-media">
						<?php if ( $image_url ) : ?>
							<img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $branch['name'] ); ?>" loading="lazy" />
						<?php else : ?>
							<div class="lucrsure-location-media-placeholder" aria-hidden="true"></div>
						<?php endif; ?>
					</div>
					<div class="lucrsure-location-body">
						<h3 class="lucrsure-location-name"><?php echo esc_html( $branch['name'] ); ?></h3>
						<ul class="lucrsure-location-meta">
							<?php if ( $address ) : ?>
								<li>
									<span class="lucrsure-location-icon" aria-hidden="true">
										<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 21s7-4.5 7-11a7 7 0 1 0-14 0c0 6.5 7 11 7 11z"/><circle cx="12" cy="10" r="2.5"/></svg>
									</span>
									<span><?php echo esc_html( $address ); ?></span>
								</li>
							<?php endif; ?>
							<?php if ( ! empty( $branch['phone'] ) ) : ?>
								<li>
									<span class="lucrsure-location-icon" aria-hidden="true">
										<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.8 19.8 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6A19.8 19.8 0 0 1 2.08 4.18 2 2 0 0 1 4.06 2h3a2 2 0 0 1 2 1.72c.12.86.32 1.7.6 2.5a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.58-1.12a2 2 0 0 1 2.11-.45c.8.28 1.64.48 2.5.6A2 2 0 0 1 22 16.92z"/></svg>
									</span>
									<a href="tel:<?php echo esc_attr( preg_replace( '/[^\d+]/', '', $branch['phone'] ) ); ?>"><?php echo esc_html( $branch['phone'] ); ?></a>
								</li>
							<?php endif; ?>
							<?php if ( $hours ) : ?>
								<li>
									<span class="lucrsure-location-icon" aria-hidden="true">
										<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>
									</span>
									<span><?php echo esc_html( $hours ); ?></span>
								</li>
							<?php endif; ?>
						</ul>
						<?php if ( ! empty( $amenities ) ) : ?>
							<div class="lucrsure-location-amenities">
								<?php foreach ( $amenities as $amenity ) : ?>
									<span class="lucrsure-location-tag"><?php echo esc_html( $amenity ); ?></span>
								<?php endforeach; ?>
							</div>
						<?php endif; ?>
					</div>
				</article>
			<?php endforeach; ?>
		</div>
	<?php endif; ?>
</section>
