<?php
if ( ! get_theme_mod( 'electrical_services_enable_banner_section', false ) ) {
	return;
}

/**
 * Render Banner Section.
 */
$electrical_services_banner_section_sub_title     = get_theme_mod( 'electrical_services_banner_section_sub_title' );
$electrical_services_banner_section_title     = get_theme_mod( 'electrical_services_banner_section_title' );
$electrical_services_banner_section_description = get_theme_mod( 'electrical_services_banner_section_description' );
$electrical_services_button_label               = get_theme_mod( 'electrical_services_banner_button_label', '' );
$electrical_services_button_link                = get_theme_mod( 'electrical_services_banner_button_link', '' );
$electrical_services_button_label_second        = get_theme_mod( 'electrical_services_banner_second_button_label', '' );
$electrical_services_button_link_second         = get_theme_mod( 'electrical_services_banner_second_button_link', '' );

$electrical_services_default_main_image_url  = get_template_directory_uri() . '/resource/img/default1.png';
$electrical_services_default_small_image_url = get_template_directory_uri() . '/resource/img/default2.png';

$electrical_services_banner_section_image = get_theme_mod( 'electrical_services_banner_section_image', $electrical_services_default_main_image_url );
$electrical_services_discount             = get_theme_mod( 'electrical_services_banner_years_experience', '' );
$electrical_services_discount_lbl         = get_theme_mod( 'electrical_services_banner_years_experience_label', '' );
$electrical_services_secondary_stat_label = get_theme_mod( 'electrical_services_banner_secondary_stat_label', '' );
$electrical_services_badge_value          = get_theme_mod( 'electrical_services_banner_badge_value', '' );
$electrical_services_badge_label          = get_theme_mod( 'electrical_services_banner_badge_label', '' );

$electrical_services_badge_images = array();
for ( $i = 1; $i <= 4; $i++ ) {
	$image = get_theme_mod( 'electrical_services_banner_badge_image_' . $i );
	if ( ! empty( $image ) ) {
		$electrical_services_badge_images[] = $image;
	}
}

$electrical_services_small_visual_image_fallback = ! empty( $electrical_services_badge_images ) ? $electrical_services_badge_images[0] : $electrical_services_default_small_image_url;
$electrical_services_small_visual_image          = get_theme_mod( 'electrical_services_banner_small_image', $electrical_services_small_visual_image_fallback );

$electrical_services_banner_image_alt = '';
$electrical_services_banner_image_id  = attachment_url_to_postid( $electrical_services_banner_section_image );

if ( $electrical_services_banner_image_id ) {
	$electrical_services_banner_image_alt = get_post_meta( $electrical_services_banner_image_id, '_wp_attachment_image_alt', true );
}

if ( empty( $electrical_services_banner_image_alt ) ) {
	$electrical_services_banner_image_alt = $electrical_services_banner_section_title;
}

$electrical_services_stat_value = ! empty( $electrical_services_discount ) ? $electrical_services_discount : $electrical_services_badge_value;
$electrical_services_stat_label = ! empty( $electrical_services_discount_lbl ) ? $electrical_services_discount_lbl : $electrical_services_badge_label;

$electrical_services_has_stat = ! empty( $electrical_services_stat_value ) || ! empty( $electrical_services_stat_label );
?>
<section id="electrical_services_banner_section" class="sale-banner sale-banner--angled">
	<div class="asterts-wrapper">
		<div class="sale-banner__inner">
			<div class="sale-banner__content">
				<?php if ( ! empty( $electrical_services_banner_section_sub_title ) ) : ?>
					<p class="sale-banner__kicker"><?php echo esc_html( $electrical_services_banner_section_sub_title ); ?></p>
				<?php endif; ?>

				<?php if ( ! empty( $electrical_services_banner_section_title ) ) : ?>
					<h1 class="sale-banner__title"><?php echo esc_html( $electrical_services_banner_section_title ); ?></h1>
				<?php endif; ?>

				<?php if ( ! empty( $electrical_services_secondary_stat_label ) ) : ?>
					<p class="sale-banner__subtitle"><?php echo esc_html( $electrical_services_secondary_stat_label ); ?></p>
				<?php endif; ?>

				<?php if ( ! empty( $electrical_services_banner_section_description ) ) : ?>
					<p class="sale-banner__description"><?php echo esc_html( $electrical_services_banner_section_description ); ?></p>
				<?php endif; ?>

				<div class="sale-banner__actions">
					<?php if ( ! empty( $electrical_services_button_label ) ) : ?>
						<a href="<?php echo esc_url( $electrical_services_button_link ); ?>" class="sale-banner__button sale-banner__button--primary">
							<?php echo esc_html( $electrical_services_button_label ); ?>
						</a>
					<?php endif; ?>
					<?php if ( ! empty( $electrical_services_button_label_second ) ) : ?>
						<a href="<?php echo esc_url( ! empty( $electrical_services_button_link_second ) ? $electrical_services_button_link_second : '#' ); ?>" class="sale-banner__button sale-banner__button--ghost">
							<?php echo esc_html( $electrical_services_button_label_second ); ?>
						</a>
					<?php endif; ?>
				</div>
			</div>

			<div class="sale-banner__media" aria-hidden="true">
				<div class="sale-banner__mediadivtwo" aria-hidden="true">
					<div class="sale-banner__hero-shell">
						<div class="sale-banner__hero-image">
							<?php if ( $electrical_services_banner_image_id ) : ?>
								<?php
								echo wp_get_attachment_image(
									$electrical_services_banner_image_id,
									'full',
									false,
									array(
										'class'         => 'sale-banner__model-image',
										'alt'           => esc_attr( $electrical_services_banner_image_alt ),
										'loading'       => 'eager',
										'decoding'      => 'async',
										'fetchpriority' => 'high',
									)
								);
								?>
							<?php else : ?>
								<img class="sale-banner__model-image" src="<?php echo esc_url( $electrical_services_banner_section_image ); ?>" alt="<?php echo esc_attr( $electrical_services_banner_image_alt ); ?>" loading="eager" decoding="async" fetchpriority="high">
							<?php endif; ?>
						</div>
					</div>

					<?php if ( $electrical_services_has_stat ) : ?>
						<div class="sale-banner__experience-badge">
							<div class="sale-banner__experience-image">
								<?php if ( ! empty( $electrical_services_stat_value ) ) : ?>
									<strong class="sale-banner__experience-value"><?php echo esc_html( $electrical_services_stat_value ); ?></strong>
								<?php endif; ?>
								<?php if ( ! empty( $electrical_services_stat_label ) ) : ?>
									<span class="sale-banner__experience-label"><?php echo esc_html( $electrical_services_stat_label ); ?></span>
								<?php endif; ?>
							</div>
						</div>
					<?php endif; ?>

					<div class="sale-banner__mini-shell">
						<div class="sale-banner__mini-image">
							<img src="<?php echo esc_url( $electrical_services_small_visual_image ); ?>" alt="<?php esc_attr_e( 'Electrical Services', 'electrical-services' ); ?>" loading="lazy" decoding="async">
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>