<?php

if ( ! class_exists( 'Give' ) || ! get_theme_mod( 'charity_elite_enable_causes_section', false ) ) {
	return;
}

$charity_elite_content_ids   = array();
$charity_elite_content_count = get_theme_mod( 'charity_elite_causes_counter', 3 );
for ( $charity_elite_i = 1; $charity_elite_i <= $charity_elite_content_count; $charity_elite_i++ ) {
	$charity_elite_content_ids[] = get_theme_mod( 'charity_elite_causes_donation_form_' . $charity_elite_i );
}

$charity_elite_args = array(
	'post_type'           => 'give_forms',
	'post__in'            => array_filter( $charity_elite_content_ids ),
	'orderby'             => 'post__in',
	'posts_per_page'      => absint( $charity_elite_content_count ),
	'post_status'         => 'publish',
	'ignore_sticky_posts' => true,
);

$charity_elite_args = apply_filters( 'charity_elite_causes_section_args', $charity_elite_args );

charity_elite_render_causes_section( $charity_elite_args );

/**
 * Render Causes Section.
 */
function charity_elite_render_causes_section( $charity_elite_args ) {
	$charity_elite_cause_subtitle     = get_theme_mod( 'charity_elite_causes_subtitle', __( 'Give your help today', 'charity-elite' ) );
	$charity_elite_cause_title        = get_theme_mod( 'charity_elite_causes_title', __( 'Help Poor people', 'charity-elite' ) );
	$charity_elite_cause_button_label = get_theme_mod( 'charity_elite_causes_button_label', __( 'Donate Now', 'charity-elite' ) );
	?>

	<section id="charity_elite_causes_section" class="ascendoor-frontpage-section section-has-background causes-section causes-style-1">
		<?php
		if ( is_customize_preview() ) :
			charity_elite_section_link( 'charity_elite_causes_section' );
		endif;
		?>
		<div class="ascendoor-wrapper">
			<?php if ( ! empty( $charity_elite_cause_subtitle || $charity_elite_cause_title ) ) { ?>
				<div class="section-header-subtitle">
					<p class="section-subtitle"><?php echo esc_html( $charity_elite_cause_subtitle ); ?></p>
					<h3 class="section-title"><?php echo esc_html( $charity_elite_cause_title ); ?></h3>
				</div>
				<?php
			}
			$charity_elite_query = new WP_Query( $charity_elite_args );
			if ( $charity_elite_query->have_posts() ) :
				?>
				<div class="section-body">
					<div class="causes-section-wrapper">
						<?php
						while ( $charity_elite_query->have_posts() ) :
							$charity_elite_query->the_post();
							?>
							<div class="cause-single">
								<div class="cause-img">
									<?php the_post_thumbnail( 'post-thumbnail' ); ?>
								</div>
								<div class="cause-detail">
									<h3 class="cause-title">
										<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									</h3>
									<?php the_excerpt(); ?>
									<?php
									$charity_elite_form_id         = get_the_ID();
									$charity_elite_goal_stats      = give_goal_progress_stats( $charity_elite_form_id );
									$charity_elite_currency_symbol = give_currency_symbol();
									?>
									<div class="cause-progress">
										<div class="progress-bar">
											<div class="progress-bar-background">
												<span class="progressbar-completed" style="width:<?php echo esc_attr( $charity_elite_goal_stats['progress'] ); ?>%" ;></span>
											</div>
										</div>
										<div class="progress-detail">
											<span class="goal">
												<?php echo esc_html__( 'Goal: ', 'charity-elite' ); ?>
												<span class="amount"><?php echo esc_html( $charity_elite_currency_symbol . $charity_elite_goal_stats['raw_goal'] ); ?></span>
											</span>
											<span class="goal">
												<?php echo esc_html__( 'Raised: ', 'charity-elite' ); ?>
												<span class="amount"><?php echo esc_html( $charity_elite_currency_symbol . $charity_elite_goal_stats['raw_actual'] ); ?></span>
											</span>
										</div>
									</div>
									<?php if ( ! empty( $charity_elite_cause_button_label ) ) : ?>
										<div class="cause-button">
											<a href="<?php the_permalink(); ?>" class="ascendoor-button"><?php echo esc_html( $charity_elite_cause_button_label ); ?></a>
										</div>
									<?php endif; ?>
								</div>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
			<?php endif; ?>
		</div>
	</section>
	<?php
}
