<?php

/**
 * Template Name: Advance Search
 */
get_header();?>
    
<div id="content-vw">
   <div class="container"> 
    <div class="container">
      <div id="search" class="backgroung-wrap">
        <form action="<?php if(get_theme_mod('advance_education_pro_search_page_settings') != ''){
        echo esc_url(get_permalink(get_theme_mod('advance_education_pro_search_page_settings')));
      }?>" method="POST" class="search_tour">
        <div class="container">
          <div class="row">
            <div class="col-md-3 p-2">
              <select id="course_categories" name="course_categories">
                <option value=""><?php echo esc_html(get_theme_mod('advance_education_pro_default_dropdown_title')); ?></option>
                <?php $taxonomies = get_terms('createcategory');
                      if ($taxonomies) {
                           foreach ($taxonomies as $taxonomy ) { ?>
                             <option value="<?php echo esc_attr($taxonomy->name); ?>"><?php echo esc_html($taxonomy->name); ?></option> <?php
                           }
                       }
                      ?>
              </select>        
            </div>
            <div class="col-md-2 p-2">
               <input type="number" id="price" placeholder="<?php echo esc_html(get_theme_mod('advance_education_pro_search_price'))?>" name="price">
            </div>
            <div class="col-md-4 p-2">
              <input type="text" id="course_keyword" placeholder="<?php echo esc_html(get_theme_mod('advance_education_pro_search_course_keyword'))?>" name="course_keyword">
            </div>
            <div class="col-md-3 p-2">
              <input type="submit" value="<?php echo esc_html(get_theme_mod('advance_education_pro_search_courses_label'));?>">
            </div>
          </div>
      </div>
    </form>
        <div class="clearfix"></div>
      </div>
      <!-- Search Fields Conditions -->
      <div class="middle-align"> 
          <?php 
          // $course_categories = esc_html( $_POST['course_categories'] );
          // $course_price = intval( $_POST['price'] );
          // $course_keyword = esc_html( $_POST['course_keyword']);
          $course_categories = isset($_POST["course_categories"]) ?$_POST["course_categories"] : "" ;
           $course_price = isset($_POST["course_price"]) ?$_POST["course_price"] : "" ;
           $course_keyword = isset($_POST["course_keyword"]) ?$_POST["course_keyword"] : "" ;
           $args="";
          if($course_categories != '' && $course_price != '' && $course_keyword != ''){
            $args = array( 'post_type' => 'courses',
              's' => $course_keyword,
              'createcategory' => $course_categories,
              'meta_query' => array(
                array(
                    'key' => 'price',
                    'value' => $course_price,
                    'compare' => '<=',
                    'type' => 'numeric'
                )
              )
            );
          }
          elseif($course_categories != '' && $course_price != '' && $course_keyword == ''){
            $args = array( 'post_type' => 'courses',
              'createcategory' => $course_categories,
              'meta_query' => array(
                array(
                    'key' => 'price',
                    'value' => $course_price,
                    'compare' => '<=',
                    'type' => 'numeric'
                )
              )
            );
          }
          elseif($course_categories != '' && $course_price == '' && $course_keyword != ''){
            $args = array('post_type' => 'courses',
              's' => $course_keyword,
              'createcategory' => $course_categories
            );
          }
          elseif($course_categories == '' && $course_price != '' && $course_keyword != ''){
            $args = array('post_type' => 'courses',
              's' => $course_keyword,
              'meta_query' => array(
                array(
                    'key' => 'price',
                    'value' => $course_price,
                    'compare' => '<=',
                    'type' => 'numeric'
                )
              )
            );
          } 
          elseif($course_categories == '' && $course_price == '' && $course_keyword != ''){
            $args = array('post_type' => 'courses',
              's' => $course_keyword,
            );
          }   
          elseif($course_categories == '' && $course_price != '' && $course_keyword == ''){
            $args = array('post_type' => 'courses',
              'meta_query' => array(
                array(
                    'key' => 'price',
                    'value' => $course_price,
                    'compare' => '<=',
                    'type' => 'numeric'
                )
              )
            );
          } 
          elseif($course_categories != '' && $course_price == '' && $course_keyword == ''){
            $args = array('post_type' => 'courses',
              'createcategory' => $course_categories
            );
          }     
        
            $i=1;
            //$data = get_post_meta($show_courses);
            $show_courses = new WP_Query( $args );
            $data = get_post_meta($show_courses);?>
            <div class="row"> 
            <?php if ( $show_courses -> have_posts() ):
              while ( $show_courses -> have_posts() ) :
                $show_courses->the_post();
                ?>
                <?php $meta = get_post_meta(get_the_ID(), '', true);
                ?>
                <div class="col-md-4 backgrndwrapper mt-2">
                  <div class="col-md-12 adjust">
                    <img src="<?php the_post_thumbnail_url('full'); ?>"  width="100%">
                  </div>
                  <div class="col-md-12 font_large">
                    <div id="courses" class="our_courses_details">  
                      <div class="our_courses_content">
                        <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                        <p class="subtitle"><?php $excerpt = get_the_excerpt(); echo esc_html(advance_education_pro_string_limit_words($excerpt,12)); ?></p>
                        <?php if(get_post_meta($post->ID,'price',true)){?>
                        <p class="price"><?php echo esc_html(get_post_meta($post->ID,'price',true)); ?></p>
                        <?php }?>
                        <div class="popular_courses_details">
                          <?php if(get_post_meta($post->ID,'course-duration',true) || get_post_meta($post->ID,'degree_level',true) || get_post_meta($post->ID,'price',true)){?>
                              <div class="row">
                                <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                                  <?php if(get_post_meta($post->ID,'course_duration',true)){?>
                                  <p class="course_duration"><?php echo esc_html(get_post_meta($post->ID,'course_duration',true)); ?></p>
                                  <?php }?>
                                </div>
                                <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                                  <?php if(get_post_meta($post->ID,'degree_level',true)){?>
                                  <p class="price"><?php echo esc_html(get_post_meta($post->ID,'degree_level',true)); ?></p>
                                  <?php }?>
                                </div>
                                <div class="col-lg-6 col-md-6 col-sm-12 col-12">
                                  <?php if(get_post_meta($post->ID,'custom_link',true) !='') { $custom_link = get_post_meta($post->ID,'custom_link',true); } else {
                                    $custom_link = get_the_permalink(); } ?>
                                 <?php if(get_post_meta($post->ID,'apply_now',true)){?>
                                  <div class="apply-now"><a href="<?php echo esc_url($custom_link); ?>"class="apply"><?php echo esc_html(get_post_meta($post->ID,'apply_now',true)); ?></a></div>
                                 <?php } ?>
                                </div>
                              </div>
                          <?php }?>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="clearfix"></div> 
                </div>
                <?php if($i%3 == 0){ echo '<div class="clearfix"></div>';}
              endwhile;
            else:
                // Insert any content or load a template for no posts found.
                echo "<h2>There is nothing found.Please search again.</h2>";
            endif;
              wp_reset_query();
            ?>
          </div>
        <div class="clearfix"></div>
      </div>
    </div>
</div>
<?php get_footer(); ?>