<?php 
/**
 * Template Name: Advance Search
 */
get_header();

// Get search keyword and selected category from POST
$coupon_flash_deals_search_keyword = isset($_POST['coupon_input']) ? sanitize_text_field($_POST['coupon_input']) : '';
$coupon_flash_deals_selected_category = isset($_POST['category_dropdown']) ? sanitize_text_field($_POST['category_dropdown']) : '';

// WP_Query arguments
$args = array(
    'post_type' => 'coupons',
    'post_status' => 'publish',
    's' => $coupon_flash_deals_search_keyword, // Search by title/content
    'tax_query' => array()
);

// Add category filter if selected
if (!empty($coupon_flash_deals_selected_category)) {
    $args['tax_query'][] = array(
        'taxonomy' => 'couponcategory',
        'field'    => 'slug',
        'terms'    => $coupon_flash_deals_selected_category,
    );
}

// Run query
$coupon_flash_deals_coupon_query = new WP_Query($args);
?>

<div id="blog-content" class="search-results-container">
    <div class="featured-img">
		<div class="post-thumbnail">
		    <?php
		    if ( has_post_thumbnail() ) :
		        the_post_thumbnail();
		    else :
		        // Get custom fallback image set from the Customizer
		        $coupon_flash_deals_custom_fallback_img = get_theme_mod( 'coupon_flash_deals_custom_fallback_img' );

		        if ( ! empty( $coupon_flash_deals_custom_fallback_img ) ) :
		            ?>
		            <img src="<?php echo esc_url( $coupon_flash_deals_custom_fallback_img ); ?>">
		        <?php else : ?>
		            <img src="<?php echo esc_url( get_template_directory_uri() . '/images/banner.png' ); ?>">
		        <?php endif;
		    endif;
		    ?>
		</div>
	    <div class="single-meta-box">
			<h2 class="my-3"><?php esc_html_e('Advance Search', 'coupon-flash-deals'); ?></h2>
			<?php if ( get_theme_mod('coupon_flash_deals_breadcrumb_setting',true) ) : ?>
	              <div class="bread_crumb text-center">
	                <?php coupon_flash_deals_breadcrumb();  ?>
	              </div>
	            <?php endif; ?>
		</div>
	</div>
   	<div class="container">
   		<div class="row m-0">
	    <?php if ($coupon_flash_deals_coupon_query->have_posts()) : ?>
	        <?php while ($coupon_flash_deals_coupon_query->have_posts()) : $coupon_flash_deals_coupon_query->the_post(); ?>
	        <div class="col-lg-4">
				<div class="tab-product text-center">
                    <div class="product-image box">
                        <figure class="mb-0">
                            <?php 
                                if ( has_post_thumbnail() ) {
                                  the_post_thumbnail();
                                } else {
                                  echo '<img src="' . esc_url( get_template_directory_uri() . '/assets/images/placeholder.png' ) . '" alt="No image">';
                            	}
                            ?>
                        </figure>
                        <div class="coupon-offer">
                            <?php echo get_post_meta(get_the_ID(),'coupon_amount', true); ?>
                        </div>
                    </div>
                    <div class="product-details">
                        <div class="px-2 py-2">
                          <h5 class="product-text mb-0">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                          </h5>
                          <?php the_excerpt(); ?>
                        </div>
                        <div class="offer-box">
                          <?php echo get_post_meta(get_the_ID(),'coupon_amount', true); ?>
                          <?php
                            $coupon_flash_deals_start_date_raw   = get_post_meta( get_the_ID(), 'start_date', true );
                            $coupon_flash_deals_expires_date_raw = get_post_meta( get_the_ID(), 'date_expires', true );

                            // Proceed only if both dates are set
                            if ( $coupon_flash_deals_start_date_raw && $coupon_flash_deals_expires_date_raw ) {
                                $coupon_flash_deals_current_date = current_time( 'Y-m-d' ); // WordPress current date
                                $coupon_flash_deals_current_timestamp = strtotime( $coupon_flash_deals_current_date );
                                $coupon_flash_deals_expires_timestamp = strtotime( $coupon_flash_deals_expires_date_raw );

                                if ( $coupon_flash_deals_expires_timestamp && $coupon_flash_deals_current_timestamp ) {
                                    $coupon_flash_deals_days_diff = ceil( ( $coupon_flash_deals_expires_timestamp - $coupon_flash_deals_current_timestamp ) / DAY_IN_SECONDS );

                                    if ( $coupon_flash_deals_days_diff > 0 ) {
                                        printf(
                                            esc_html__( 'off Expires in %d %s', 'coupon-flash-deals' ),
                                            intval( $coupon_flash_deals_days_diff ),
                                            esc_html( _n( 'day', 'days', $coupon_flash_deals_days_diff, 'coupon-flash-deals' ) )
                                        );
                                    } else {
                                        echo esc_html__( 'Coupon expired', 'coupon-flash-deals' );
                                    }
                                } else {
                                    echo esc_html__( 'Invalid date format', 'coupon-flash-deals' );
                                }
                            } else {
                                echo esc_html__( 'Date(s) missing', 'coupon-flash-deals' );
                            }
                            ?>
                        </div>
                    </div>
                </div>
	        </div>	
	        <?php endwhile; ?>
	    <?php else : ?>
	        <p><?php echo esc_html('No coupons found.','coupon-flash-deals'); ?></p>
	    <?php endif; ?>
		</div>

	    <?php wp_reset_postdata(); ?>
	</div>
</div>

<?php get_footer(); ?>
