<?php
/**
 * The Template Name: Home Page
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package Garden Farm
 */

get_header(); ?>

<div id="content" >
    <?php
        $garden_farm_hidepageboxes = get_theme_mod('garden_farm_slider', true);
        $garden_farm_catData = get_theme_mod('garden_farm_slider_cat');
        if ($garden_farm_hidepageboxes && $garden_farm_catData) { ?>
        <section id="slider-cat" class="position-relative">
            <div class="owl-carousel m-0">
                <?php
                    $garden_farm_page_query = new WP_Query(
                        array(
                            'category_name' => esc_attr($garden_farm_catData),
                            'posts_per_page' => -1,
                        )
                    );
                    while ($garden_farm_page_query->have_posts()) : $garden_farm_page_query->the_post(); ?>
                        <div class="imagebox">
                            <?php if(has_post_thumbnail()){
                                the_post_thumbnail('full', array('class' => 'post-image'));
                            } else { ?>
                                <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/slider.png" alt="<?php echo esc_attr( 'slider', 'garden-farm'); ?>" class="post-image"/>
                            <?php } ?>
                            <div class="slider-overlay"></div>
                            <div class="container">
                                <div class="text-content position-absolute">
                                    <?php if(get_theme_mod('garden_farm_slider_subhead') != ''){ ?>
                                        <p class="slider-subhead mb-2"><?php echo esc_html(get_theme_mod ('garden_farm_slider_subhead','garden-farm')); ?></p>
                                    <?php } ?>
                                    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                                    <?php
                                        $garden_farm_trimexcerpt  = get_the_excerpt();
                                        $garden_farm_shortexcerpt = wp_trim_words($garden_farm_trimexcerpt, $garden_farm_num_words = 25);
                                        echo '<p class="slider-content my-3">' . esc_html($garden_farm_shortexcerpt) . '</p>';
                                    ?>
                                    <div class="sliderbtn">
                                        <?php 
                                            $garden_farm_button_text = get_theme_mod('garden_farm_button_text', 'Discover More');
                                            $garden_farm_icon = get_theme_mod('garden_farm_button_icon', 'fa-solid fa-arrow-right');
                                            $garden_farm_button_link_slider = get_theme_mod('garden_farm_button_link_slider', ''); 
                                            if (empty($garden_farm_button_link_slider)) {
                                                $garden_farm_button_link_slider = esc_url(get_permalink());
                                            }
                                            if ($garden_farm_button_text || !empty($garden_farm_button_link_slider)) { ?>
                                            <?php if(get_theme_mod('garden_farm_button_text', 'Discover More') != ''){ ?>
                                                <a href="<?php echo esc_url($garden_farm_button_link_slider); ?>">
                                                    <?php echo esc_html($garden_farm_button_text); ?>
                                                    <span class="screen-reader-text"><?php echo esc_html($garden_farm_button_text); ?></span>
                                                    <i class="<?php echo esc_attr($garden_farm_icon); ?>"></i>
                                                </a>
                                            <?php } ?>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endwhile;
                    wp_reset_postdata();
                ?>
            </div>
        </section>
    <?php } ?>

    <!-- Our Team Section -->
    <?php
        $garden_farm_hide_team_section = get_theme_mod('garden_farm_disabled_our_team_section', true);
        $garden_farm_post_cat = get_theme_mod('garden_farm_posts'); 
        if ($garden_farm_hide_team_section){ ?>
        <section id="team-section" class="py-5">
            <div class="container">
                <div class="blog-bx mb-4">
                    <?php if (get_theme_mod('garden_farm_our_team_text') != "") { ?>
                        <p class="mb-2 text-uppercase text-center team-text"><?php echo esc_html(get_theme_mod('garden_farm_our_team_text', 'garden-farm')); ?></p>
                    <?php } ?>
                    <?php if (get_theme_mod('garden_farm_our_team_title') != "") { ?>
                        <h2 class="team-title mb-1 text-uppercase text-center"><?php echo esc_html(get_theme_mod('garden_farm_our_team_title', 'garden-farm')); ?></h2>
                    <?php } ?>
                </div> 
                <div class="container">
                    <div class="row">
                        <?php
                            $garden_farm_page_query = new WP_Query(
                                array(
                                    'category_name' => esc_attr($garden_farm_post_cat),
                                    'posts_per_page' => 3,
                                )
                            );
                            $garden_farm_post_counter = 1;
                            while ($garden_farm_page_query->have_posts()) : $garden_farm_page_query->the_post(); ?>
                                    <div class="col-xl-4 col-lg-4 col-md-6 col-12 align-self-center mb-3 books">
                                        <div class="team-content">
                                            <div class="team-image mb-2">
                                                <?php if(has_post_thumbnail()){
                                                  the_post_thumbnail('full');
                                                  } else{?>
                                                  <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/dummy-post.png" alt="<?php echo esc_attr( 'dummy-post', 'garden-farm'); ?>"/>
                                                <?php } ?>
                                                <div class="image-overlay"></div>
                                            </div>
                                            <div class="team-text-content">
                                                <h3 class="mb-1"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                                <?php
                                                    $garden_farm_categories = get_the_category();
                                                    if (!empty($garden_farm_categories)) {
                                                        // Get the first (latest) category
                                                        $garden_farm_latest_category = $garden_farm_categories[0];
                                                        
                                                        echo '<div class="post-categories mb-1">';
                                                        echo '<span class="category-item">';
                                                        echo esc_html($garden_farm_latest_category->name);
                                                        echo '</span>';
                                                        echo '</div>';
                                                    }
                                                    $garden_farm_trimexcerpt  = get_the_excerpt();
                                                    $garden_farm_shortexcerpt = wp_trim_words($garden_farm_trimexcerpt, $garden_farm_num_words = 45);
                                                    echo '<p class="post-text">' . esc_html($garden_farm_shortexcerpt) . '</p>';
                                                ?>
                                                <div class="social-icons">
                                                    <?php 
                                                        $garden_farm_post_id = get_the_ID(); // Get current post ID
                                                        for ($garden_farm_i = 1; $garden_farm_i <= 5; $garden_farm_i++) {
                                                            // Get the social link and icon for this post
                                                            $garden_farm_social_link = get_theme_mod('garden_farm_social_link_' . $garden_farm_post_id . '_' . $garden_farm_i, '');
                                                            $garden_farm_social_icon = get_theme_mod('garden_farm_social_icon_' . $garden_farm_post_id . '_' . $garden_farm_i, '');
                                                            
                                                            if (!empty($garden_farm_social_link)) { ?>
                                                                <a title="<?php echo esc_attr('Link ' . $garden_farm_i, 'garden-farm'); ?>" target="_blank" href="<?php echo esc_url($garden_farm_social_link); ?>">
                                                                    <i class="<?php echo esc_attr($garden_farm_social_icon); ?>"></i>
                                                                </a>
                                                            <?php }
                                                        }
                                                    ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php $garden_farm_post_counter++; ?>
                        <?php endwhile;
                        wp_reset_postdata();
                        ?>
                    </div>
                </div>
            </div>
        </section>
    <?php } ?>
</div>
<?php get_footer(); ?>
