<?php
/**
 * Page footer template
 *
 * @package Multi
 */

?>

<?php get_template_part( 'template-parts/go-top-link' ); ?>
</div>
<?php
if ( is_single() || is_page() ) {
	$show_above_footer_widgets = get_post_meta( get_queried_object_id(), 'multi_show_above_footer_widgets', true );
}
if ( empty( $show_above_footer_widgets ) ) {
	$show_above_footer_widgets = get_theme_mod( 'multi_show_above_footer_widgets', MULTIMAGAZINE_SHOW_ABOVE_FOOTER_WIDGETS );
}
if ( 'on' === $show_above_footer_widgets ) {
	get_template_part( 'template-parts/above-footer-widgets' );
}

$show_footer = '';
if ( is_single() || is_page() ) {
	$show_footer = get_post_meta( get_queried_object_id(), 'multi_show_footer', true );
}
if ( empty( $show_footer ) ) {
	$show_footer = get_theme_mod( 'multi_footer_display', MULTIMAGAZINE_FOOTER_DISPLAY );
}

$layout       = get_theme_mod( 'multi_layout_type', MULTIMAGAZINE_LAYOUT_TYPE );
$fixed_footer = get_theme_mod( 'multi_fixed_footer', MULTIMAGAZINE_FIXED_FOOTER );

if ( 'wide' === $layout && $fixed_footer ) :
	?>
	</div>
	<?php
endif;
?>
</div>
<?php

if ( 'on' === $show_footer ) :

	$footer_classes            = '';
	$full_width_widgets        = get_theme_mod( 'multi_footer_widgets_width', MULTIMAGAZINE_FOOTER_WIDGETS_WIDTH );
	$full_width_bottom         = get_theme_mod( 'multi_footer_bottom_width', MULTIMAGAZINE_FOOTER_BOTTOM_WIDTH );
	$full_width_bottom_content = get_theme_mod( 'multi_footer_bottom_content_width', MULTIMAGAZINE_FOOTER_BOTTOM_CONTENT_WIDTH );
	$column_count              = get_theme_mod( 'multi_footer_column_count', MULTIMAGAZINE_FOOTER_COLUMN_COUNT );

	$footer_classes .= ' footer-columns-' . $column_count;

	$fixed = get_theme_mod( 'multi_fixed_footer', MULTIMAGAZINE_FIXED_FOOTER );
	if ( $fixed ) {
		$footer_classes .= ' fixed';
	}

	$footer_shadow = get_theme_mod( 'multi_footer_shadow', MULTIMAGAZINE_FOOTER_SHADOW );
	if ( $footer_shadow ) {
		$footer_classes .= ' with-shadow';
	}

	$footer_separator = get_theme_mod( 'multi_footer_bottom_separator', MULTIMAGAZINE_FOOTER_BOTTOM_SEPARATOR );
	if ( $footer_separator ) {
		$footer_classes .= ' with-separator';
	}
	?>
		<footer class="site-footer <?php echo esc_attr( $footer_classes ); ?>">
			<?php
			$widget_classes = array();
			$column_classes = array();
			$bottom_classes = array(
				'bottom',
				$full_width_bottom ? 'full-width' : '',
			);

			if ( multimagazine_is_sidebar_active( 'footer-widget-area' ) ) :
				$separators     = get_theme_mod( 'multi_footer_widget_separator', MULTIMAGAZINE_FOOTER_WIDGET_SEPARATOR );
				$widget_classes = array(
					'widgets',
					$full_width_widgets ? 'full-width' : '',
					'heading-style-' . multimagazine_heading_style(),
				);
				$column_classes = array(
					'columns',
					$separators ? 'border-vertical' : '',
				);
				?>
			<div class="<?php echo esc_attr( implode( ' ', $widget_classes ) ); ?>">
				<div class="<?php echo esc_attr( implode( ' ', $column_classes ) ); ?>">
					<?php dynamic_sidebar( 'footer-widget-area' ); ?>
				</div>
			</div>
			<?php endif; ?>

			<div class="<?php echo esc_attr( implode( ' ', $bottom_classes ) ); ?>">
				<div class="bottom-content
					<?php
					if ( $full_width_bottom_content ) :
						?>
						full-width
						<?php
					endif;
					?>
				">
					<?php
					$elements  = array(
						0 => get_theme_mod( 'multi_footer_priority1', MULTIMAGAZINE_FOOTER_PRIORITY1 ),
						1 => get_theme_mod( 'multi_footer_priority2', MULTIMAGAZINE_FOOTER_PRIORITY2 ),
						2 => get_theme_mod( 'multi_footer_priority3', MULTIMAGAZINE_FOOTER_PRIORITY3 ),
					);
					$positions = array(
						0 => get_theme_mod( 'multi_footer_' . $elements[0] . '_position', constant( 'MULTIMAGAZINE_FOOTER_' . strtoupper( $elements[0] ) . '_POSITION' ) ),
						1 => get_theme_mod( 'multi_footer_' . $elements[1] . '_position', constant( 'MULTIMAGAZINE_FOOTER_' . strtoupper( $elements[1] ) . '_POSITION' ) ),
						2 => get_theme_mod( 'multi_footer_' . $elements[2] . '_position', constant( 'MULTIMAGAZINE_FOOTER_' . strtoupper( $elements[2] ) . '_POSITION' ) ),
					);

					for ( $row = 0; $row < 3; $row ++ ) :
						if ( multimagazine_is_footer_row_empty( $row, $positions ) ) {
							continue;
						}

						$row_classes = 'footer-row footer-row' . $row;
						$no_first    = multimagazine_is_footer_col_empty( $row, 0, $positions );
						$no_middle   = multimagazine_is_footer_col_empty( $row, 1, $positions );
						$no_last     = multimagazine_is_footer_col_empty( $row, 2, $positions );

						if ( true === $no_first ) {
							$row_classes .= ' no-first';
						}
						if ( true === $no_last ) {
							$row_classes .= ' no-last';
						}
						if ( true === $no_middle ) {
							$row_classes .= ' no-middle';
						}
						?>
						<div class="<?php echo esc_attr( $row_classes ); ?>">
						<?php
						for ( $col = 0; $col < 3; $col ++ ) :
							if ( 0 === $col && true === $no_first ) {
								continue;
							}
							if ( 1 === $col && true === $no_middle ) {
								continue;
							}
							if ( 2 === $col && true === $no_last ) {
								break;
							}
							$current_pos = $row . ',' . $col;
							?>
							<div class="footer-cell footer-cell<?php echo esc_attr( $col ); ?>">
							<?php
							for ( $el = 0; $el < 3; $el ++ ) {
								if ( $positions[ $el ] === $current_pos && $elements[ $el ] ) {
									get_template_part( 'template-parts/footer-' . $elements[ $el ] );
									$elements[ $el ]  = 0;
									$positions[ $el ] = 0;
								}
							}
							?>
							</div>
							<?php
						endfor;
						?>
					</div>
						<?php
					endfor;
					?>
				</div>
			</div>

		</footer>

	<?php endif; ?>

<?php do_action( 'multimagazine_page_end' ); ?>

<?php
$search_in_menu              = get_theme_mod( 'multimagazine_search_in_menu', MULTIMAGAZINE_SEARCH_IN_MENU ) !== 'off' ? 1 : 0;
$multimagazine_heading_style = multimagazine_heading_style();
?>
<dialog class="multi-popup popup-search" id="popup-search" closedby="any" role="dialog" aria-modal="true">
	<a href="#" class="m-icon m-icon-close popup-close" autofocus aria-label="<?php esc_attr_e( 'Close pop-up', 'multimagazine' ); ?>"></a>
	<h3 class="spec-heading <?php echo esc_attr( $multimagazine_heading_style ); ?>"><span><?php esc_html_e( 'Search', 'multimagazine' ); ?></span></h3>
	<?php get_search_form(); ?>
</dialog>
<?php do_action( 'multimagazine_after_footer' ); ?>
<?php wp_footer(); ?>
</body>
</html>
