<?php
/**
 * The template for displaying the footer.
 *
 * Contains the closing of the #content div and all content after.
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package plumber_handyman_lite
 */
$plumber_handyman_lite_scroll_top  = get_theme_mod( 'plumber_handyman_lite_scroll_to_top', true );
$plumber_handyman_lite_footer_background = get_theme_mod('plumber_handyman_lite_footer_background_image');
$plumber_handyman_lite_footer_background_url = '';
if(!empty($plumber_handyman_lite_footer_background)){
    $plumber_handyman_lite_footer_background = absint($plumber_handyman_lite_footer_background);
    $plumber_handyman_lite_footer_background_url = wp_get_attachment_url($plumber_handyman_lite_footer_background);
}

$plumber_handyman_lite_footer_background_color = get_theme_mod('plumber_handyman_lite_footer_background_color', 'var(--secondary-color)'); // New line

$plumber_handyman_lite_footer_background_style = '';
if (!empty($plumber_handyman_lite_footer_background_url)) {
    $plumber_handyman_lite_footer_background_style = ' style="background-image: url(\'' . esc_url($plumber_handyman_lite_footer_background_url) . '\'); background-repeat: no-repeat; background-size: cover;"';
} else {
    $plumber_handyman_lite_footer_background_style = ' style="background-color: ' . esc_attr($plumber_handyman_lite_footer_background_color) . ';"'; // Updated line
}
?>

</div>
</div>
</div>
</div>

<footer class="site-footer" <?php echo $plumber_handyman_lite_footer_background_style; ?>>
    <?php 
    $plumber_handyman_lite_active_areas = get_theme_mod('plumber_handyman_lite_footer_widget_areas', 4);
    if (
        is_active_sidebar('footer-1') ||
        is_active_sidebar('footer-2') ||
        is_active_sidebar('footer-3') ||
        is_active_sidebar('footer-4')
    ) : ?>
        <div class="footer-t">
            <div class="container">
                <div class="row">
                    <?php 
                    for ($plumber_handyman_lite_i = 1; $plumber_handyman_lite_i <= $plumber_handyman_lite_active_areas; $plumber_handyman_lite_i++) {

                        if (is_active_sidebar('footer-' . $plumber_handyman_lite_i)) {

                            $plumber_handyman_lite_col = 12 / $plumber_handyman_lite_active_areas;

                            echo '<div class="col-xl-' . $plumber_handyman_lite_col . ' col-lg-' . $plumber_handyman_lite_col . ' col-md-6 col-sm-6">';
                            dynamic_sidebar('footer-' . $plumber_handyman_lite_i);
                            echo '</div>';
                        }
                    }
                    ?>
                </div>
            </div>
        </div>

    <?php else : ?>

        <!-- Default Widget Content -->
        <div class="footer-t">
            <div class="container">
                <div class="row">

                    <?php 
                    // Dynamic column width
                    $plumber_handyman_lite_col = 12 / $plumber_handyman_lite_active_areas;
                    ?>

                    <!-- Archive -->
                    <aside class="widget widget_archive col-xl-<?php echo $plumber_handyman_lite_col; ?> col-lg-<?php echo $plumber_handyman_lite_col; ?> col-md-6 col-sm-6">
                        <h2 class="widget-title"><?php esc_html_e('Archive List', 'plumber-handyman-lite'); ?></h2>
                        <ul><?php wp_get_archives('type=monthly'); ?></ul>
                    </aside>

                    <!-- Recent Posts -->
                    <aside class="widget widget_recent_posts col-xl-<?php echo $plumber_handyman_lite_col; ?> col-lg-<?php echo $plumber_handyman_lite_col; ?> col-md-6 col-sm-6">
                        <h2 class="widget-title"><?php esc_html_e('Recent Posts', 'plumber-handyman-lite'); ?></h2>
                        <ul>
                            <?php
                            $args = array('post_type' => 'post', 'posts_per_page' => 5);
                            $recent_posts = new WP_Query($args);
                            while ($recent_posts->have_posts()) : $recent_posts->the_post(); ?>
                                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                            <?php endwhile; wp_reset_postdata(); ?>
                        </ul>
                    </aside>

                    <!-- Categories -->
                    <aside class="widget widget_categories col-xl-<?php echo $plumber_handyman_lite_col; ?> col-lg-<?php echo $plumber_handyman_lite_col; ?> col-md-6 col-sm-6">
                        <h2 class="widget-title"><?php esc_html_e('Categories', 'plumber-handyman-lite'); ?></h2>
                        <ul><?php wp_list_categories(array('title_li' => '')); ?></ul>
                    </aside>

                    <!-- Tags -->
                    <aside class="widget widget_tags col-xl-<?php echo $plumber_handyman_lite_col; ?> col-lg-<?php echo $plumber_handyman_lite_col; ?> col-md-6 col-sm-6">
                        <h2 class="widget-title"><?php esc_html_e('Tags', 'plumber-handyman-lite'); ?></h2>
                        <div class="tag-cloud"><?php wp_tag_cloud(); ?></div>
                    </aside>

                </div>
            </div>
        </div>

    <?php endif; ?>

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

    <?php if ($plumber_handyman_lite_scroll_top) : ?>
        <a id="button">
            <i class="<?php echo esc_attr(get_theme_mod('plumber_handyman_lite_scroll_icon', 'fas fa-arrow-up')); ?>"></i>
        </a>
    <?php endif; ?>

</footer>

</div>
</div>

<?php wp_footer(); ?>

</body>
</html>