<?php 
$nonprofit_give_slider = get_theme_mod('nonprofit_give_slider_setting', true);

if ($nonprofit_give_slider == '1') :
?>

<section id="slider">
    <div id="main-slide">
        <?php
        $nonprofit_give_slide_pages = array();
        for ($nonprofit_give_count = 1; $nonprofit_give_count <= 1; $nonprofit_give_count++) {
            $nonprofit_give_mod = intval(get_theme_mod('nonprofit_give_slider_page'));
            if ('page-none-selected' !== $nonprofit_give_mod && $nonprofit_give_mod > 0) {
                $nonprofit_give_slide_pages[] = $nonprofit_give_mod;
            }
        }

        if (!empty($nonprofit_give_slide_pages)) :
            $nonprofit_give_args = array(
                'post_type' => 'page',
                'post__in' => $nonprofit_give_slide_pages,
                'orderby' => 'post__in'
            );
            $nonprofit_give_query = new WP_Query($nonprofit_give_args);
            if ($nonprofit_give_query->have_posts()) :
                while ($nonprofit_give_query->have_posts()) : $nonprofit_give_query->the_post(); ?>
                    <div class="item">
                        <?php if (has_post_thumbnail()) { ?>
                            <img src="<?php echo esc_url(get_the_post_thumbnail_url(get_the_ID(), 'full')); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" />
                        <?php } else { ?>
                            <img src="<?php echo esc_url(get_stylesheet_directory_uri() . '/assets/images/slider-img.png'); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" />
                        <?php } ?>
                        <div class="container">
                            <div class="carousel-caption">
                                <div class="inner_carousel wow fadeInRight" data-wow-delay="0.5s">
                                    <?php if (get_theme_mod('nonprofit_give_slider_short_heading') != '') { ?>
                                        <p class="slidetop-text mb-1 text-uppercase"><?php echo esc_html(get_theme_mod('nonprofit_give_slider_short_heading', '')); ?></p>
                                    <?php } ?>
                                    <h1 class="custom-title mb-2">
                                      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h1>
                                    <p class="mb-2 slider-content"><?php echo esc_html(wp_trim_words(get_the_content(), 25)); ?></p>
                                    <div class="slider-mainbtn mt-md-4 mt-3 mb-3">
                                        <!-- First button -->
                                        <span class="slide-btn1">
                                            <a href="<?php the_permalink(); ?>" class="btn-text me-md-3 mb-2">
                                                <?php echo esc_html__('Donate Now', 'nonprofit-give'); ?>
                                            </a>
                                        </span>
                                        <!-- Second button  -->
                                        <span class="slide-btn2">
                                            <?php
                                            $nonprofit_give_slider_btn_text = get_theme_mod('nonprofit_give_slider_btn_text', 'Join Us');
                                            $nonprofit_give_slider_btn_link = get_theme_mod('nonprofit_give_slider_btn_link');

                                            if (!empty($nonprofit_give_slider_btn_text) && !empty($nonprofit_give_slider_btn_link)) { ?>
                                                <a href="<?php echo esc_url($nonprofit_give_slider_btn_link); ?>" class="btn-text">
                                                    <?php echo esc_html($nonprofit_give_slider_btn_text); ?>
                                                </a>
                                            <?php } ?>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endwhile;
                wp_reset_postdata(); ?>
            <?php else : ?>
                <div class="no-postfound"></div>
            <?php endif;
        endif; ?>
    </div>

    <div class="donation-map">
      <?php
        $nonprofit_give_total_goal_raw        = get_theme_mod( 'nonprofit_give_total_goal', '' );
        $nonprofit_give_total_raised_raw      = get_theme_mod( 'nonprofit_give_total_raised', '' );
        $nonprofit_give_total_supporters_raw  = get_theme_mod( 'nonprofit_give_total_supporters', '' );

        $nonprofit_give_total_goal        = (int) preg_replace( '/[^0-9]/', '', $nonprofit_give_total_goal_raw );
        $nonprofit_give_total_raised      = (int) preg_replace( '/[^0-9]/', '', $nonprofit_give_total_raised_raw );
        $nonprofit_give_total_supporters  = (int) preg_replace( '/[^0-9]/', '', $nonprofit_give_total_supporters_raw );

        if ( $nonprofit_give_total_goal > 0 ) :
          $nonprofit_give_progress = min( 100, max( 0, ( $nonprofit_give_total_raised / $nonprofit_give_total_goal ) * 100 ) );
      ?>
        <div class="donation-progress-card">
          
          <div class="donation-top">
            <div class="donation-top-left">
              <span class="donation-label">
                <?php esc_html_e( 'Total Donations Raised', 'nonprofit-give' ); ?>
              </span>
              <span class="donation-amount">
                <?php echo esc_html( '₹' . number_format_i18n( $nonprofit_give_total_raised ) ); ?>
              </span>
            </div>

            <div class="donation-top-right">
              <span class="goal-text">
                <?php esc_html_e( 'Goal:', 'nonprofit-give' ); ?>
                <?php echo esc_html( '₹' . number_format_i18n( $nonprofit_give_total_goal ) ); ?>
              </span>
            </div>
          </div>

          <div class="donation-progress-bar">
            <span class="donation-progress-fill" style="width: <?php echo esc_attr( $nonprofit_give_progress ); ?>%;"></span>
          </div>

          <div class="donation-supporters">
            <?php
              echo esc_html__(
                'Supporters:',
                'nonprofit-give'
              ) . ' ' . esc_html( number_format_i18n( $nonprofit_give_total_supporters ) ) . ' ' . esc_html__( 'Donors', 'nonprofit-give' );
            ?>
          </div>

        </div>
      <?php endif; ?>
    </div>

</section>
<?php endif; ?>
