<?php 

defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
?>
<div class="my-campaign wfp-content-padding">
	<h2 class="dashboard-right-section--title"> <?php echo esc_html( apply_filters( 'wfp_dashboard_mycampaign_heading', __( 'My campaigns ', 'wp-fundraising-donation' ) ) ); ?></h2>
	
	<div class="campaign-tab-container">	
		
		<?php


			// Verify nonce
		if ( isset( $_GET['nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'wp_rest' ) ) {
			$wfpGetType = isset( $_GET['type'] ) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : 'all';
		} else {
			$wfpGetType = 'all';
		}

		?>
		<div class="campaign-tab">
			<ul class="top-menu-dashboard">
				<li class="<?php echo esc_attr( ( $wfpGetType == 'all' ) ? 'active' : '' ); ?>" > <a href="?wfp-page=my-campaign&type=all&nonce=<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>"> <?php echo esc_html__( 'All', 'wp-fundraising-donation' ); ?> </a></li>
				<li class="<?php echo esc_attr( ( $wfpGetType == 'publish' ) ? 'active' : '' ); ?>" > <a href="?wfp-page=my-campaign&type=publish&nonce=<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>"> <?php echo esc_html__( 'Publish', 'wp-fundraising-donation' ); ?> </a></li>
				<li class="<?php echo esc_attr( ( $wfpGetType == 'review' ) ? 'active' : '' ); ?>" > <a href="?wfp-page=my-campaign&type=review&nonce=<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>"> <?php echo esc_html__( 'Review', 'wp-fundraising-donation' ); ?> </a></li>
				<li class="<?php echo esc_attr( ( $wfpGetType == 'draft' ) ? 'active' : '' ); ?>" > <a href="?wfp-page=my-campaign&type=draft&nonce=<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>"> <?php echo esc_html__( 'Draft', 'wp-fundraising-donation' ); ?> </a></li>
			</ul>
		</div>
		<div class="campaign-body">
			<?php

			$wfp_args['author'] = $wfpUserId;
			if ( $wfpGetType == 'all' ) {
				$wfp_args['post_status'] = array( 'draft', 'publish', 'pending' );
			} elseif ( $wfpGetType == 'review' ) {
				$wfp_args['post_status'] = 'pending';
			} else {
				$wfp_args['post_status'] = ( $wfpGetType == 'publish' ) ? 'publish' : 'draft';
			}
			$wfp_args['post_type'] = self::post_type();

			$wfp_args['orderby'] = 'post_date';
			$wfp_args['order']   = 'DESC';

			$wfp_the_query = new \WP_Query( $wfp_args );
			?>
			
			<?php
			if ( $wfp_the_query->have_posts() ) :
				global $wpdb;
				?>
				<?php
				while ( $wfp_the_query->have_posts() ) :
					$wfp_the_query->the_post();
					?>
					
					
					<div class="campaign-blog">
						<div class="campaign-blog-content">
							<h3 class="campaign-blog--title"><a class="campaign-blog--title__link" href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title(); ?> </a></h3>
							<!--<p><?php // the_excerpt(); ?> </p> -->
							
							<div class="campaign-blog--raised"> 
							<?php
							esc_html_e( 'Raised Amount : ', 'wp-fundraising-donation' );
							//phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
							$wfp_raised_amount = $wpdb->get_var( $wpdb->prepare( "SELECT SUM(donate_amount) FROM {$wpdb->prefix}wdp_fundraising WHERE form_id = %d AND status = 'Active' AND payment_gateway NOT IN ('test_payment')", get_the_ID() ) );
							?>
							 
								<span class="campaign-blog--raised__price"><?php echo esc_html( WfpFundraising\Apps\Settings::wfp_number_format_currency_icon( 'left', $wfp_defaultUse_space ) ); ?><strong><?php echo esc_html( WfpFundraising\Apps\Settings::wfp_number_format_currency( $wfp_raised_amount ) ); ?></strong><em class="wfp-currency-symbol"><?php echo esc_html( WfpFundraising\Apps\Settings::wfp_number_format_currency_icon( 'right', $wfp_defaultUse_space ) ); ?></em> </span>
							</div>
							
							<div class="campaign-blog--date"><?php echo esc_html__( 'Publish Date : ', 'wp-fundraising-donation' ); ?><time class="campaign-blog--date__text" datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>" itemprop="datePublished"><?php echo esc_html( get_the_date() ); ?></time> </div>
						</div>
						
						<div class="campaign-blog-edit-btn">
							<?php if ( in_array( get_post_status(), array( 'draft', 'publish', 'pending' ) ) ) : ?>
								<a class="xs-btn xs-btn-outline-primary xs-btn-sm campaign-blog--edit" name="filter_my_campaign" href="?wfp-page=campaign&camp=<?php echo esc_attr( get_the_ID() ); ?>&nonce=<?php echo esc_attr( wp_create_nonce( 'wp_nonce' ) ); ?>" > <?php echo esc_html__( 'Edit : ', 'wp-fundraising-donation' ); ?> </a>
							<?php endif; ?>
						</div>
						
					</div>
				<?php endwhile; ?>
				<?php wp_reset_postdata(); ?>
			<?php else : ?>
				<p><?php esc_html_e( 'Sorry, not found any campaign.', 'wp-fundraising-donation' ); ?></p>
			<?php endif; ?>
		</div>
	</div>
</div>
