<?php
/**
 * The template for displaying the footer.
 *
 * @package FansPaysite
 */

$fanspaysite_layout  = get_theme_mod( 'fanspaysite_layout', 'left-sidebar' );
$footer_cols         = (int) get_theme_mod( 'fanspaysite_footer_columns', 1 );
$show_footer_menu    = get_theme_mod( 'fanspaysite_show_footer_menu', true );
$show_copyright      = get_theme_mod( 'fanspaysite_show_copyright', true );
$footer_html_snippet = get_theme_mod( 'fanspaysite_footer_html_snippet', '' );
$has_footer_menu     = $show_footer_menu && has_nav_menu( 'footer-menu' );
$has_widgets         = false;

if ( $footer_cols > 0 ) {
	for ( $i = 1; $i <= $footer_cols; $i++ ) {
		if ( is_active_sidebar( 'fanspaysite_footer_' . $i ) ) {
			$has_widgets = true;
			break;
		}
	}
}
?>

<?php if ( $has_widgets ) : ?>
	<div class="fps-footer-widgets" style="display:grid; grid-template-columns: repeat(<?php echo esc_attr( $footer_cols ); ?>, 1fr); gap: var(--space-md, 16px);">
		<?php for ( $i = 1; $i <= $footer_cols; $i++ ) : ?>
			<div class="fps-footer-col">
				<?php if ( is_active_sidebar( 'fanspaysite_footer_' . $i ) ) : ?>
					<?php dynamic_sidebar( 'fanspaysite_footer_' . $i ); ?>
				<?php endif; ?>
			</div>
		<?php endfor; ?>
	</div>
<?php endif; ?>

<footer id="colophon" class="site-footer<?php echo $has_footer_menu ? ' has-footer-menu' : ''; ?>" role="contentinfo">
	<?php if ( $show_copyright || $has_footer_menu ) : ?>
		<div class="site-footer-inner">
			<?php if ( $show_copyright ) : ?>
				<div class="site-info">
					&copy; <?php echo esc_html( date( 'Y' ) ); ?> <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a>. <?php esc_html_e( 'All rights reserved.', 'fanspaysite' ); ?>
				</div>
			<?php endif; ?>

			<?php if ( $has_footer_menu ) : ?>
				<nav class="footer-navigation" aria-label="<?php esc_attr_e( 'Footer navigation', 'fanspaysite' ); ?>">
					<?php
					wp_nav_menu(
						array(
							'theme_location' => 'footer-menu',
							'menu_id'        => 'footer-menu',
							'container'      => false,
							'depth'          => 1,
							'fallback_cb'    => false,
						)
					);
					?>
				</nav>
			<?php endif; ?>
		</div>
	<?php endif; ?>
	<?php if ( $footer_html_snippet ) : ?>
		<div class="footer-html-snippet">
			<?php echo $footer_html_snippet; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- sanitized when saved; privileged users may intentionally add scripts. ?>
		</div>
	<?php endif; ?>
</footer>

</main><!-- .site-content -->

<?php
$should_render_right_sidebar = fanspaysite_should_render_right_sidebar();

if ( 'top-bar' === $fanspaysite_layout && $should_render_right_sidebar ) {
	?>
	<aside class="site-sidebar-right">
		<?php if ( is_active_sidebar( 'fanspaysite_left_sidebar' ) ) : ?>
			<div class="sidebar-relocated-widgets">
				<?php dynamic_sidebar( 'fanspaysite_left_sidebar' ); ?>
			</div>
		<?php endif; ?>
		<?php if ( is_active_sidebar( 'fanspaysite_right_sidebar' ) ) : ?>
			<?php dynamic_sidebar( 'fanspaysite_right_sidebar' ); ?>
		<?php endif; ?>
	</aside>
	</div><!-- .site-content-wrapper -->
	<?php
} elseif ( $should_render_right_sidebar ) {
	?>
	<aside class="site-sidebar-right">
		<?php if ( is_active_sidebar( 'fanspaysite_right_sidebar' ) ) : ?>
			<?php dynamic_sidebar( 'fanspaysite_right_sidebar' ); ?>
		<?php endif; ?>
	</aside>
	<?php
}

$background_image = get_background_image();
?>

</div><!-- .site-wrapper -->

<?php if ( get_theme_mod( 'fanspaysite_show_back_to_top', true ) ) : ?>
	<button id="fanspaysiteBackToTop" class="fanspaysite-back-to-top" type="button" aria-label="<?php echo esc_attr__( 'Back to top', 'fanspaysite' ); ?>" title="<?php echo esc_attr__( 'Back to top', 'fanspaysite' ); ?>">
		<i class="fas fa-arrow-up" aria-hidden="true"></i>
	</button>
<?php endif; ?>

<?php if ( $background_image ) : ?>
	<style type="text/css">
		.site-content {
			background-image: url(<?php echo esc_url( $background_image ); ?>);
			background-size: cover;
			background-position: center;
			background-repeat: no-repeat;
		}
	</style>
<?php endif; ?>

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