<?php
/**
 * Services Section — Custom Cards Layout.
 *
 * @package design_studio
 */

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

// ── Section-level settings ────────────────────────────────────────────────────
$design_studio_sv_bg_text    = get_theme_mod( 'design_studio_services_bg_heading', '' );
$design_studio_sv_title      = get_theme_mod( 'design_studio_services_section_title', '' );
$design_studio_sv_desc       = get_theme_mod( 'design_studio_services_section_desc', '' );
$design_studio_sv_category   = get_theme_mod( 'design_studio_services_category', '' );

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

if ( ! empty( $design_studio_sv_category ) ) {
	$design_studio_query_args['category_name'] = sanitize_title( $design_studio_sv_category );
}

// ── Latest Services (up to 4 items) ────────────────────────────────────────
$design_studio_blog_query = new WP_Query(
	$design_studio_query_args
);
?>
<section id="design_studio_services_section" class="cm-services-section">
	<?php if ( is_customize_preview() ) : ?>
		<?php design_studio_section_link( 'design_studio_services_section' ); ?>
	<?php endif; ?>

	<div class="asterthemes-wrapper">

		<header class="cm-services__header">
			<?php if ( ! empty( $design_studio_sv_title ) ) : ?>
				<h2 class="cm-services__title"><?php echo esc_html( $design_studio_sv_title ); ?></h2>
			<?php endif; ?>
			<?php if ( ! empty( $design_studio_sv_desc ) ) : ?>
				<p class="cm-services__desc"><?php echo esc_html( $design_studio_sv_desc ); ?></p>
			<?php endif; ?>
			<?php if ( ! empty( $design_studio_sv_bg_text ) ) : ?>
				<span class="cm-services__bg-text" aria-hidden="true"><?php echo esc_html( $design_studio_sv_bg_text ); ?></span>
			<?php endif; ?>
		</header>

		<div class="cm-services__grid">
			<?php if ( $design_studio_blog_query->have_posts() ) : ?>
				<?php
				$design_studio_post_count = 0;
				while ( $design_studio_blog_query->have_posts() ) :
					$design_studio_blog_query->the_post();
					$design_studio_post_count++;
					$design_studio_number   = sprintf( '%02d.', $design_studio_post_count );
					$design_studio_image_id = get_post_thumbnail_id();
					$design_studio_has_image = ! empty( $design_studio_image_id );
					$design_studio_excerpt  = get_the_excerpt();
					$design_studio_desc     = ! empty( $design_studio_excerpt ) ? $design_studio_excerpt : wp_trim_words( wp_strip_all_tags( get_the_content() ), 20 );
				?>
				<article class="cm-services__card">

					<?php if ( $design_studio_has_image ) : ?>
						<div class="cm-services__card-bg" aria-hidden="true">
							<?php
							echo wp_get_attachment_image(
								$design_studio_image_id,
								'large',
								false,
								array(
									'class'    => 'cm-services__card-bg-img',
									'alt'      => '',
									'loading'  => 'lazy',
									'decoding' => 'async',
								)
							);
							?>
						</div>
					<?php endif; ?>

					<div class="cm-services__card-inner">
						<div class="cm-services__card-top-inner">
							<div class="cm-services__card-top">
								<span class="cm-services__card-number"><?php echo esc_html( $design_studio_number ); ?></span>
								<span class="cm-services__card-icon" aria-hidden="true"><?php esc_html_e( 'POST', 'design-studio' ); ?></span>
							</div>

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

							<?php if ( ! empty( $design_studio_desc ) ) : ?>
								<p class="cm-services__card-desc"><?php echo esc_html( $design_studio_desc ); ?></p>
							<?php endif; ?>
						</div>
					</div>

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

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