<?php
// Build an inline style attribute for the banner background.
// Prefer the post featured image, then any supplied $img_url, then banner_transparent option, then attachment thumb, then a default placeholder.
$thumb_id         = get_post_thumbnail_id( get_the_ID() );
$menu_transparent = st()->get_option( 'menu_transparent', '' );
$banner_transparent = st()->get_option( 'banner_transparent', '' );

$bg_url = '';
// 1) Featured image
if ( has_post_thumbnail( get_the_ID() ) ) {
	$bg_url = get_the_post_thumbnail_url( get_the_ID(), 'full' );
}

// 2) If no featured image, check $img_url passed into template
if ( empty( $bg_url ) && ! empty( $img_url ) ) {
	$bg_url = $img_url;
}

// 3) If still empty, use banner_transparent option when menu transparent is enabled
if ( empty( $bg_url ) && $menu_transparent === 'on' && ! empty( $banner_transparent ) ) {
	$bg_url = $banner_transparent;
}

// 4) If still empty and thumb_id exists, use attachment URL
if ( empty( $bg_url ) && ! empty( $thumb_id ) ) {
	$bg_url = wp_get_attachment_image_url( $thumb_id, 'full' );
}

// 5) Fallback to theme logo or no-image
if ( empty( $bg_url ) ) {
	$bg_url = st()->get_option( 'logo', get_template_directory_uri() . '/img/no-image.png' );
}

// Prepare inline style (background-size and position added)
$inner_style_attr = '';
if ( ! empty( $bg_url ) ) {
	$inner_style_attr = "background-image: url('" . esc_url( $bg_url ) . "'); background-size: cover; background-position: center;";
}

$posts_type      = get_post_type( get_the_ID() );
$style           = get_post_meta( get_the_ID(), 'st_custom_layout_new', true ) ?? 1;
$check_tour_solo = $posts_type == 'st_tours' && $style == 10;

if ( ( $menu_transparent == 'on' && ! $check_tour_solo ) || is_page_template( 'template-blog.php' ) || is_archive() ) {?>
<div class="banner st-bg-feature" style="<?php echo esc_attr( $inner_style_attr ); ?>">
	<div class="container">
		<div class="st-banner-search-form style_2">
			<?php if ( empty( $type_page ) ) { ?>
				<h1 class="st-banner-search-form__title">
					<?php
					if ( is_archive() ) {
						the_archive_title( '', '' );
					} elseif ( is_search() ) {
						echo sprintf( __( 'Search results : "%s"', 'traveler' ), esc_html( STInput::get( 's', '' ) ) );
					} else {
						echo get_the_title();
					}
					?>
				</h1>
				<?php echo st_breadcrumbs_new(); ?>
			<?php } else { ?>
				<h1 class="st-banner-search-form__title">
					<?php echo __( 'Partner Page', 'traveler' ); ?>
				</h1>
			<?php } ?>

		</div>
	</div>
</div>
<?php } else {
	st_breadcrumbs_new();
}
?>
