<?php
/**
 * Theme footer template
 *
 * @package KsPortfolio
 * @since 1.0.0
 */

namespace KsPortfolio;

$footerLogo = getFieldValue( 'footer.footer_logo.url' ) ? getFieldValue( 'footer.footer_logo.url' ) : '';
$footerDesc = getFieldValue( 'footer.footer_description' ) ? getFieldValue( 'footer.footer_description' ) : '';

$copyright_text = getFieldValue( 'footer.copyright_text' ) ? getFieldValue( 'footer.copyright_text' ) : 'Copyright &copy; %Y ' . get_bloginfo( 'name' );

$copyright = str_replace( '%Y', gmdate( 'Y' ), $copyright_text );

$locations = get_nav_menu_locations();
$footerNav = ! empty( $locations['footer_navigation'] )
    ? ( wp_get_nav_menu_items( (int) $locations['footer_navigation'] ) ? wp_get_nav_menu_items( (int) $locations['footer_navigation'] ) : array() )
    : array();
?>

	</main>

	<footer role="contentinfo" class="site-footer">
		<div class="container mx-auto py-12">
			<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-8">
				<div class="site-footer__brand max-w-[40ch]">
					<h2 class="site-footer__title">
						<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
							<?php if ( $footerLogo ) { ?>
								<img src="<?php echo esc_url( $footerLogo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" />
								<?php
							} else {
								echo esc_html( get_bloginfo( 'name' ) );
							}
							?>
						</a>
					</h2>
					<?php if ( $footerDesc ) : ?>
						<p class="site-footer__desc"><?php echo wp_kses_post( $footerDesc ); ?></p>
					<?php endif; ?>
				</div>

				<?php
				for ( $i = 1; $i <= 3; $i++ ) :
					$area_id = 'footer-' . $i;
					if ( ! is_active_sidebar( $area_id ) ) {
						continue;
					}
					?>
					<div class="site-footer__area">
						<?php dynamic_sidebar( $area_id ); ?>
					</div>
				<?php endfor; ?>

				<?php if ( $footerNav ) : ?>
					<nav class="site-footer__nav" aria-label="<?php echo esc_attr__( 'Footer navigation', 'ks-portfolio' ); ?>">
						<h3><?php echo esc_html__( 'Navigation', 'ks-portfolio' ); ?></h3>
						<ul>
							<?php foreach ( $footerNav as $item ) : ?>
								<li>
									<a href="<?php echo esc_url( $item->url ); ?>">
										<?php echo esc_html( $item->title ); ?>
									</a>
								</li>
							<?php endforeach; ?>
						</ul>
					</nav>
				<?php endif; ?>
			</div>
		</div>

		<div class="site-footer__copyright">
			<?php echo wp_kses_post( $copyright ); ?>
		</div>
	</footer>

	<?php wp_footer(); ?>
</body>
</html>
