<?php
/**
 * Template part for displaying slider section
 *
 * @package Crowdfunding Nonprofit
 * @subpackage crowdfunding_nonprofit
 */

$crowdfunding_nonprofit_slider_arrows = get_theme_mod('crowdfunding_nonprofit_slider_arrows', true);

$crowdfunding_nonprofit_static_image1 = get_stylesheet_directory_uri() . '/assets/images/dedicate-team1.png';
?>
<?php if ($crowdfunding_nonprofit_slider_arrows) : ?>
  <section id="slider">
    <div class="container">
      <div class="item">
        <div class="row m-0">
          <div class="col-lg-6 col-md-6 col-12 slider-content-col align-self-center">
            <div class="carousel-caption">
              <div class="inner_carousel">
                <?php if (get_theme_mod('crowdfunding_nonprofit_slider_short_heading')) : ?>
                  <p class="slidetop-text mb-2 text-capitalize"><?php echo esc_html(get_theme_mod('crowdfunding_nonprofit_slider_short_heading')); ?></p>
                <?php endif; ?>
                <?php if ( get_theme_mod( 'crowdfunding_nonprofit_show_slider_title' ) ) :
                  $crowdfunding_nonprofit_title  = get_theme_mod( 'crowdfunding_nonprofit_show_slider_title' );
                  $crowdfunding_nonprofit_words  = explode( ' ', $crowdfunding_nonprofit_title );

                  foreach ( $crowdfunding_nonprofit_words as $crowdfunding_nonprofit_i => $word ) {
                      $crowdfunding_nonprofit_n = $crowdfunding_nonprofit_i + 1; // 1-based index

                      if ( $crowdfunding_nonprofit_n === 2 ) {
                          $crowdfunding_nonprofit_class = 'slider-second-word';
                      } elseif ( $crowdfunding_nonprofit_n === 6 || $crowdfunding_nonprofit_n === 7 ) {
                          $crowdfunding_nonprofit_class = 'slider-middle-word';
                      } else {
                          $crowdfunding_nonprofit_class = '';
                      }

                      if ( $crowdfunding_nonprofit_class ) {
                          $crowdfunding_nonprofit_words[ $crowdfunding_nonprofit_i ] = '<span class="' . esc_attr( $crowdfunding_nonprofit_class ) . '">' . esc_html( $word ) . '</span>';
                      } else {
                          $crowdfunding_nonprofit_words[ $crowdfunding_nonprofit_i ] = esc_html( $word );
                      }
                  }
                    $crowdfunding_nonprofit_modified_title = implode( ' ', $crowdfunding_nonprofit_words );
                    ?>
                    <h1 class="mb-md-3 mb-2"><?php echo wp_kses_post( $crowdfunding_nonprofit_modified_title ); ?></h1>
                <?php endif; ?>

                <?php if (get_theme_mod('crowdfunding_nonprofit_show_slider_content')) : ?>
                  <p class="slide-content"><?php echo esc_html(get_theme_mod('crowdfunding_nonprofit_show_slider_content')); ?></p>
                <?php endif; ?>
                <div class="more-btn mt-md-4 mt-2 mb-4">
                  <?php if(get_theme_mod('crowdfunding_nonprofit_product_btn_text1','Explore more') != '' && get_theme_mod('crowdfunding_nonprofit_product_btn_link1')){ ?>
                    <a class="text-capitalize slider-btn1" href="<?php echo esc_url(get_theme_mod('crowdfunding_nonprofit_product_btn_link1')); ?>"><?php echo esc_html(get_theme_mod('crowdfunding_nonprofit_product_btn_text1','Explore more')); ?><span class="screen-reader-text"><?php echo esc_html(get_theme_mod('crowdfunding_nonprofit_product_btn_text1','Explore more')); ?></span></a>
                  <?php }?>
                </div>
              </div>
            </div>
            <div class="donation-map">
              <?php
              $crowdfunding_nonprofit_received_raw = get_theme_mod( 'crowdfunding_nonprofit_donation_received', '' );
              $crowdfunding_nonprofit_goal_raw     = get_theme_mod( 'crowdfunding_nonprofit_donation_goal', '' );
              if ( $crowdfunding_nonprofit_received_raw !== '' && $crowdfunding_nonprofit_goal_raw !== '' && (int) $crowdfunding_nonprofit_goal_raw > 0 ) :

                  $crowdfunding_nonprofit_received = (int) $crowdfunding_nonprofit_received_raw;
                  $goal     = (int) $crowdfunding_nonprofit_goal_raw;

                  $crowdfunding_nonprofit_progress = min( 100, max( 0, ( $crowdfunding_nonprofit_received / $goal ) * 100 ) );

                  $crowdfunding_nonprofit_donate_text = get_theme_mod(
                      'crowdfunding_nonprofit_donate_button_text',
                      __( 'Donate Now', 'crowdfunding-nonprofit' )
                  );
                  $crowdfunding_nonprofit_donate_url  = get_theme_mod( 'crowdfunding_nonprofit_donate_button_url', '' );
                  ?>
                  <div class="donation-progress-wrapper">
                      <!-- Top labels -->
                      <div class="donation-label donation-label-left">
                          <span><?php esc_html_e( 'Donation Received', 'crowdfunding-nonprofit' ); ?></span>
                      </div>
                      <div class="donation-label donation-label-right">
                          <span><?php esc_html_e( 'Goal', 'crowdfunding-nonprofit' ); ?></span>
                      </div>
                      <!-- Outer black capsule -->
                      <div class="progress-outer position-relative">
                        <!-- White rail + orange bar -->
                        <div class="progress" style="height: 30px; width: 70%;">
                          <div class="progress-bar bg-warning text-center"
                                 role="progressbar"
                                 style="width: <?php echo esc_attr( $crowdfunding_nonprofit_progress ); ?>%;"></div>
                        </div>
                        <!-- Received amount over orange -->
                        <div class="progress-bar-value">
                            <span class="progress-bar-value-recieved">
                                <?php echo esc_html( '$' . number_format( $crowdfunding_nonprofit_received, 2 ) ); ?>
                            </span>
                        </div>
                        <!-- Goal pill on right -->
                        <div class="goal-wrap">
                            <span class="goal-pill">
                                <?php echo esc_html( '$' . number_format( $goal, 2 ) ); ?>
                            </span>
                        </div>
                      </div>
                      <?php if ( ! empty( $crowdfunding_nonprofit_donate_text ) ) : ?>
                        <a class="donation-cta-btn"
                           href="<?php echo esc_url( $crowdfunding_nonprofit_donate_url ? $crowdfunding_nonprofit_donate_url : '#' ); ?>">
                            <?php echo esc_html( $crowdfunding_nonprofit_donate_text ); ?>
                        </a>
                      <?php endif; ?>
                  </div>
              <?php endif; ?>
            </div>
          </div>
          <div class="col-lg-6 col-md-6 slider-img-col p-0">
            <div class="img-bg-slide">
              <?php
              $crowdfunding_nonprofit_default_banner = get_stylesheet_directory_uri() . '/assets/images/post-img1.png';
              $crowdfunding_nonprofit_banner_main_img = get_theme_mod(
                  'crowdfunding_nonprofit_banner_main_img',
                  $crowdfunding_nonprofit_default_banner
              );
              ?>
            <div class="<?php echo empty( $crowdfunding_nonprofit_banner_main_img ) ? 'no-image1' : ''; ?>">
              <?php if ( ! empty( $crowdfunding_nonprofit_banner_main_img ) ) : ?>
                <img class="main-img" src="<?php echo esc_url( $crowdfunding_nonprofit_banner_main_img ); ?>" alt="<?php esc_attr_e( 'Background Image 1', 'crowdfunding-nonprofit' ); ?>" />
              <?php else : ?>
                <img class="main-img" src="<?php echo esc_url( $crowdfunding_nonprofit_default_banner ); ?>" alt="<?php esc_attr_e( 'Default Background Image 1', 'crowdfunding-nonprofit' ); ?>" />
              <?php endif; ?>
            </div>
            <?php
            $crowdfunding_nonprofit_story_reel_text = get_theme_mod(
                'crowdfunding_nonprofit_story_reel_text',
                __( 'Watch our story reel', 'crowdfunding-nonprofit' )
            );
            $crowdfunding_nonprofit_story_reel_url = get_theme_mod( 'crowdfunding_nonprofit_story_reel_url', '' );
            if ( ! empty( $crowdfunding_nonprofit_story_reel_url ) ) : ?>
              <a class="story-reel-btn"
                 href="<?php echo esc_url( $crowdfunding_nonprofit_story_reel_url ); ?>"
                 target="_blank" rel="noopener noreferrer">
                <span class="story-reel-text"><?php echo esc_html( $crowdfunding_nonprofit_story_reel_text ); ?></span>
                <span class="story-reel-icon"><i class="fas fa-play"></i></span>
              </a>
            <?php endif; ?>
            </div>
              <!-- absolute text -->
              <?php
              $crowdfunding_nonprofit_main_team1 = get_theme_mod( 'crowdfunding_nonprofit_main_team1', '' );
              ?>
              <div class="team-box1">
                <?php
                $crowdfunding_nonprofit_default_img1 = get_stylesheet_directory_uri() . '/assets/images/team1.png';
                $crowdfunding_nonprofit_short_img_banner1 = get_theme_mod(
                    'crowdfunding_nonprofit_short_img_banner1',
                    $crowdfunding_nonprofit_default_img1
                );
                ?>
                <div class="team-box-content1">
                  <div class="team-tab-details1 align-items-center">
                    <div class="<?php echo empty( $crowdfunding_nonprofit_short_img_banner1 ) ? 'no-image1' : ''; ?>">
                      <?php if ( ! empty( $crowdfunding_nonprofit_short_img_banner1 ) ) : ?>
                        <img src="<?php echo esc_url( $crowdfunding_nonprofit_short_img_banner1 ); ?>" 
                               alt="<?php esc_attr_e( 'Background Image 1', 'crowdfunding-nonprofit' ); ?>" />
                      <?php else : ?>
                        <img src="<?php echo esc_url( $crowdfunding_nonprofit_default_img1 ); ?>" 
                               alt="<?php esc_attr_e( 'Default Background Image 1', 'crowdfunding-nonprofit' ); ?>" />
                      <?php endif; ?>
                    </div>
                    <?php if ( ! empty( $crowdfunding_nonprofit_main_team1 ) ) : ?>
                      <div class="team-content1 ms-lg-3 ms-2 text-start">
                        <h4 class="m-0 text-left"><?php echo esc_html( $crowdfunding_nonprofit_main_team1 ); ?></h4>
                      </div>
                    <?php endif; ?>
                  </div>
                </div>
              </div>
              <?php
              $crowdfunding_nonprofit_main_team2 = get_theme_mod( 'crowdfunding_nonprofit_main_team2', '' );
              ?>
              <div class="team-box2">
                <?php
                $crowdfunding_nonprofit_default_img2 = get_stylesheet_directory_uri() . '/assets/images/team2.png';
                $crowdfunding_nonprofit_short_img_banner2 = get_theme_mod(
                    'crowdfunding_nonprofit_short_img_banner2',
                    $crowdfunding_nonprofit_default_img2
                );
                ?>
                <div class="team-box-content2">
                  <div class="team-tab-details2 align-items-center">
                    <?php if ( ! empty( $crowdfunding_nonprofit_main_team2 ) ) : ?>
                      <div class="team-content2 me-lg-3 me-2 text-end">
                        <h4 class="m-0"><?php echo esc_html( $crowdfunding_nonprofit_main_team2 ); ?></h4>
                      </div>
                    <?php endif; ?>
                    <div class="<?php echo empty( $crowdfunding_nonprofit_short_img_banner2 ) ? 'no-image1' : ''; ?>">
                      <?php if ( ! empty( $crowdfunding_nonprofit_short_img_banner2 ) ) : ?>
                        <img src="<?php echo esc_url( $crowdfunding_nonprofit_short_img_banner2 ); ?>" alt="<?php esc_attr_e( 'Background Image 2', 'crowdfunding-nonprofit' ); ?>" />
                      <?php else : ?>
                        <img src="<?php echo esc_url( $crowdfunding_nonprofit_default_img2 ); ?>" alt="<?php esc_attr_e( 'Default Background Image 2', 'crowdfunding-nonprofit' ); ?>" />
                      <?php endif; ?>
                    </div>
                  </div>
                </div>
              <div class="img-donate-btn">
                <?php if(get_theme_mod('crowdfunding_nonprofit_donate_btn_text1','Donate') != '' && get_theme_mod('crowdfunding_nonprofit_donate_btn_link1')){ ?>
                  <a class="text-capitalize donate-btn" href="<?php echo esc_url(get_theme_mod('crowdfunding_nonprofit_donate_btn_link1')); ?>"><?php echo esc_html(get_theme_mod('crowdfunding_nonprofit_donate_btn_text1','Donate')); ?><span class="screen-reader-text"><?php echo esc_html(get_theme_mod('crowdfunding_nonprofit_donate_btn_text1','Donate')); ?></span></a>
                <?php }?>
              </div>
              <?php
              // Check if ANY content exists before rendering the dedicated-box
              $crowdfunding_nonprofit_has_title = get_theme_mod('crowdfunding_nonprofit_show_dedicated_title');
              $crowdfunding_nonprofit_has_detail = get_theme_mod('crowdfunding_nonprofit_show_dedicated_detail');
              $crowdfunding_nonprofit_has_rating = get_theme_mod('crowdfunding_nonprofit_customer_review');
              // Check if category posts exist (without full loop)
              $crowdfunding_nonprofit_selected_category = get_theme_mod('crowdfunding_nonprofit_about_catData', 'select');
              $crowdfunding_nonprofit_has_posts = false;
              if ($crowdfunding_nonprofit_selected_category != 'select') {
                  $crowdfunding_nonprofit_query_args = array(
                      'category_name' => esc_attr($crowdfunding_nonprofit_selected_category),
                      'posts_per_page' => 1, // Just check 1 post exists
                      'fields' => 'ids',     // Faster query, only get IDs
                  );
                  $crowdfunding_nonprofit_post_check = new WP_Query($crowdfunding_nonprofit_query_args);
                  $crowdfunding_nonprofit_has_posts = $crowdfunding_nonprofit_post_check->have_posts();
                  wp_reset_postdata();
              }
              $crowdfunding_nonprofit_has_content = !empty($crowdfunding_nonprofit_has_title) || !empty($crowdfunding_nonprofit_has_detail) || !empty($crowdfunding_nonprofit_has_rating) || $crowdfunding_nonprofit_has_posts;

              if ($crowdfunding_nonprofit_has_content) : ?>
                <div class="dedicated-box">
                    <?php if ($crowdfunding_nonprofit_has_title) : ?>
                        <h2 class="dedicated-title mb-lg-3 mb-2"><?php echo esc_html($crowdfunding_nonprofit_has_title); ?></h2>
                    <?php endif; ?>
                    <?php if ($crowdfunding_nonprofit_has_detail) : ?>
                      <p class="dedicated-content"><?php echo esc_html($crowdfunding_nonprofit_has_detail); ?></p>
                    <?php endif; ?>
                    <div class="rating-col">
                      <div class="row rating-box">
                          <div class="col-lg-8 col-md-7 col-7 align-self-center position-relative">
                            <?php if ($crowdfunding_nonprofit_selected_category != 'select' && $crowdfunding_nonprofit_has_posts) : ?>
                            <div class="abt-cat">
                              <div class="owl-carousel m-0">
                                <?php
                                // Now run full query since we know posts exist
                                $crowdfunding_nonprofit_abtpage_query = new WP_Query(array(
                                    'category_name' => esc_attr($crowdfunding_nonprofit_selected_category),
                                    'posts_per_page' => -1,
                                ));
                                if ($crowdfunding_nonprofit_abtpage_query->have_posts()) :
                                    while ($crowdfunding_nonprofit_abtpage_query->have_posts()) : 
                                        $crowdfunding_nonprofit_abtpage_query->the_post(); 
                                ?>
                                  <div class="abt-imagebox">
                                      <?php if (has_post_thumbnail()) : ?>
                                          <a href="<?php the_permalink(); ?>">
                                              <img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title_attribute(); ?>" />
                                          </a>
                                      <?php else : ?>
                                          <a href="<?php the_permalink(); ?>">
                                              <img src="<?php echo esc_url($crowdfunding_nonprofit_static_image1); ?>" alt="<?php esc_attr_e('Default image', 'crowdfunding-nonprofit'); ?>" />
                                          </a>
                                      <?php endif; ?>
                                  </div>
                                <?php 
                                    endwhile;
                                    wp_reset_postdata();
                                endif; 
                                ?>
                              </div>
                            </div>
                            <?php endif; ?>
                          </div>
                          <div class="col-lg-4 col-md-5 col-5 align-self-center">
                            <?php if ($crowdfunding_nonprofit_has_rating) : ?>
                            <div class="customzer-rating">
                              <span class="rate-no"><?php echo esc_html($crowdfunding_nonprofit_has_rating); ?></span>
                            </div>
                            <?php endif; ?>
                          </div>
                      </div>
                    </div>
                </div>
              <?php endif; ?>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="clearfix"></div>
  </section>
<?php endif; ?>