<?php
/**
 * Services Section — Static Grid Layout.
 *
 * @package events_and_celebrations
 */

if ( ! get_theme_mod( 'events_and_celebrations_enable_services_section', false ) ) {
	return;
}

// ── Section-level settings ────────────────────────────────────────────────────
$events_and_celebrations_sv_bg_text    = get_theme_mod( 'events_and_celebrations_services_bg_heading', '' );
$events_and_celebrations_flower_image = get_theme_mod( 'events_and_celebrations_services_flower_image' );
$events_and_celebrations_sv_title      = get_theme_mod( 'events_and_celebrations_services_section_title', '' );
$events_and_celebrations_sv_category   = get_theme_mod( 'events_and_celebrations_services_category', '' );
$events_and_celebrations_sv_count      = absint( get_theme_mod( 'events_and_celebrations_services_posts_count', 4 ) );

if ( $events_and_celebrations_sv_count < 1 ) {
	$events_and_celebrations_sv_count = 1;
} elseif ( $events_and_celebrations_sv_count > 12 ) {
	$events_and_celebrations_sv_count = 12;
}

$events_and_celebrations_query_args = array(
	'post_type'           => 'post',
	'post_status'         => 'publish',
	'posts_per_page'      => $events_and_celebrations_sv_count,
	'ignore_sticky_posts' => true,
	'no_found_rows'       => true,
);

if ( ! empty( $events_and_celebrations_sv_category ) ) {
	$events_and_celebrations_query_args['category_name'] = sanitize_title( $events_and_celebrations_sv_category );
}

// ── Services posts ────────────────────────────────────────────────────────────
$events_and_celebrations_blog_query = new WP_Query( $events_and_celebrations_query_args );
?>
<section id="events_and_celebrations_services_section" class="cm-services-section">
	<?php if ( is_customize_preview() ) : ?>
		<?php events_and_celebrations_section_link( 'events_and_celebrations_services_section' ); ?>
	<?php endif; ?>

	<div class="asterthemes-wrapper">

		<header class="cm-services__header">
			<div class="cm-services__header-main">

				<div class="cm-services__flower">
					<img
						src="<?php echo esc_url( get_template_directory_uri() . '/resource/img/flower.png' ); ?>"
						alt="<?php esc_attr_e( 'Flower Decoration', 'events-and-celebrations' ); ?>"
						class="cm-services__flower-img"
					>
				</div>

				<?php if ( ! empty( $events_and_celebrations_sv_bg_text ) ) : ?>
					<span class="cm-services__badge"><?php echo esc_html( $events_and_celebrations_sv_bg_text ); ?></span>
				<?php endif; ?>
				<?php if ( ! empty( $events_and_celebrations_sv_title ) ) : ?>
					<h2 class="cm-services__title"><?php echo esc_html( $events_and_celebrations_sv_title ); ?></h2>
				<?php endif; ?>
			</div>
		</header>

		<?php if ( $events_and_celebrations_blog_query->have_posts() ) : ?>
		<div class="cm-services__grid">
			<?php
			while ( $events_and_celebrations_blog_query->have_posts() ) :
				$events_and_celebrations_blog_query->the_post();
				$events_and_celebrations_image_id = get_post_thumbnail_id();

			?>
			<div class="cm-services__slide">
				<article class="cm-services__card">

					<div class="cm-services__card-bg">
						<a href="<?php the_permalink(); ?>">
							<?php
							if ( ! empty( $events_and_celebrations_image_id ) ) {
								echo wp_get_attachment_image(
									$events_and_celebrations_image_id,
									'large',
									false,
									array(
										'class'    => 'cm-services__card-bg-img',
											'alt'      => esc_attr( get_the_title() ),
										'loading'  => 'lazy',
										'decoding' => 'async',
									)
								);
							} else {
								?>
								<img 
									src="<?php echo esc_url( get_template_directory_uri() . '/resource/img/services-default.png' ); ?>" 
									class="cm-services__card-bg-img" 
									alt="<?php echo esc_attr( get_the_title() ); ?>"
									loading="lazy"
									decoding="async"
								/>
								<?php
							}
							?>
						</a>
					</div>

					<div class="cm-services__card-info">
						<div class="cm-services__card-head">
							<div class="cm-services__card-title-div">
								<h3 class="cm-services__card-title">
									<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
								</h3>

								 <?php
								$categories = get_the_category();
								if ( ! empty( $categories ) ) :
									$category = $categories[0];
								?>
									<a class="cm-services__card-category" href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
										<?php echo esc_html( $category->name ); ?>
									</a>
								<?php endif; ?>
							</div>
						</div>
					</div>

				</article>
			</div>
			<?php endwhile; ?>
			<?php wp_reset_postdata(); ?>
		</div><!-- .cm-services__grid -->
		<?php endif; ?>

	</div><!-- .asterthemes-wrapper -->
</section><!-- #events_and_celebrations_services_section -->