<?php
/**
* Template hooks
* @package real-estate-vital
*/


//Top Bar
if ( ! function_exists( 'real_estate_vital_topbar_settings' ) ) {
	function real_estate_vital_topbar_settings() {
		if ( get_theme_mod( 'real_estate_salient_topbar_enable', true ) ): ?>
		<div class="top-bar-vital">
			<div class="container">									
				<div class="row align-items-center">					
					<div class="top-bar-text col-sm">
						<?php if ( get_theme_mod( 'real_estate_vital_top_mobile', __( '0000000000', 'real-estate-vital' ) ) ) : ?>
							<i class="fa-solid fa-phone"></i>
							<p><?php echo esc_html( get_theme_mod( 'real_estate_vital_top_mobile'), __( '0000000000', 'real-estate-vital' ) ); ?></p>
						<?php endif; ?>
						<?php if ( get_theme_mod( 'real_estate_vital_top_email', __( 'yourmail@example.com', 'real-estate-vital' ) ) ) : ?>
							<i class="fa-regular fa-envelope"></i>
							<p><?php echo esc_html( get_theme_mod( 'real_estate_vital_top_email', __( 'yourmail@example.com', 'real-estate-vital' ) ) ); ?></p>
						<?php endif; ?>				
					</div>			
				</div>
			</div>			
		</div>
		<?php endif;
	}
}


if ( ! function_exists( 'real_estate_vital_custom_logo' ) ) :
/**
 * Displays the optional custom logo.
 * Does nothing if the custom logo is not available.
 */
function real_estate_vital_custom_logo() {
	if ( function_exists( 'the_custom_logo' ) ) {
		the_custom_logo();
	}
}
endif;


/* -- Logo */
if ( ! function_exists( 'real_estate_vital_logo_settings' ) ) {
	function real_estate_vital_logo_settings() {
		?>
	    <?php 
		/**
		 * Header and Navigation menu for normal screens
		 */
		?>
		<div class="container logo-area-vital clearfix">
			<div class="row align-items-center clearfix">					
				<div class="logo col-md-3 col-lg-3 col-sm-6 col">
					<?php real_estate_vital_custom_logo(); 
					if ( is_front_page() && is_home() ) : ?>		
						<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
					<?php else : ?>
						<p class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
					<?php endif;

					$description = get_bloginfo( 'description', 'display' );
					if ( $description || is_customize_preview() ) : ?>
						<p class="site-description"><?php echo $description; ?></p>
					<?php endif; ?>					
				</div>
				<div class="col-md-9 col-lg-9 col-sm-6 col clearfix">
					<nav id="site-navigation" class="main-navigation clearfix">
						<div class="main-navigation-links">
							<?php 
								/**
								* Main Navigation menu
								*/						
								wp_nav_menu( array(
									'theme_location' => 'catnav',
									'fallback_cb'    => false, // Do not fall back to wp_page_menu()
								) );
							?>
						</div>
					</nav><!-- #site-navigation -->
					<span class="menu-icon">
						<button aria-label="Open menu" class="menu-toggle"></button>	
					</span>
					<div id="caeliora-mobile-header">
						<a href="#" aria-label="Close menu" class="close-mobile-menu"></a>			
					</div>					
					
				</div>
			</div>				
		</div>
		<?php
	}
}



/* -- Slider */
if ( ! function_exists( 'real_estate_vital_slider_content' ) ) {
	function real_estate_vital_slider_content() {
		?>
		<?php if( get_theme_mod( 'real_estate_salient_slide_enable', true ) ) : ?>
		<section class="font-page-slider">
			<div class="overlay slider-wrap">
				<div class="">
					<div class="swiper mySwiper">
					    <div class="swiper-wrapper">
							<?php
							$slide_type = 'post';
							if ( get_theme_mod( 'real_estate_salient_slide_type' ) == "property" and class_exists( 'Essential_Real_Estate' ) ){
								$slide_type = 'property';
							}
							$args = array(
							'post_type' => $slide_type,
							'ignore_sticky_posts' => true,
							'posts_per_page' => 3,
							'orderby' => 'date',
							'order' => 'DESC',
							'post_status' => 'publish',
							'meta_query' => array(
								array(
								 'key' => '_thumbnail_id',
								 'compare' => 'EXISTS'
								),
							)
							);

							

							$latestloop = new WP_Query($args);

							if (get_theme_mod('real_estate_salient_slide_type') == "property" and class_exists('Essential_Real_Estate')) :
							if ($latestloop->have_posts()) :  while ($latestloop->have_posts()) : $latestloop->the_post();
			
							$property_id = get_the_ID();
							$price = get_post_meta($property_id, ERE_METABOX_PREFIX . 'property_price', true);
							$beds = get_post_meta($property_id, ERE_METABOX_PREFIX . 'property_bedrooms', true);
							$bath = get_post_meta($property_id, ERE_METABOX_PREFIX . 'property_bathrooms', true);
							$size = get_post_meta($property_id, ERE_METABOX_PREFIX . 'property_land', true);
							$property_address = get_post_meta($property_id, ERE_METABOX_PREFIX . 'property_address', true);
							$price_prefix = get_post_meta($property_id, ERE_METABOX_PREFIX . 'property_price_prefix', true);
							$price_unit = get_post_meta($property_id, ERE_METABOX_PREFIX . 'property_price_unit', true);
							$price_short = get_post_meta($property_id, ERE_METABOX_PREFIX . 'property_price_short', true);
							$price_postfix = get_post_meta($property_id, ERE_METABOX_PREFIX . 'property_price_postfix', true);
							?>


							<div class="swiper-slide">
						      	<div class="swiper-slider-content">
						      		<img src="<?php echo esc_url( get_the_post_thumbnail_url( $property_id, 'real-estate-vital-slider' ) ); ?>" >
						      		<div class="overlay swiper-slider-text">
						      			<div class="swiper-slider-content container">
						      				<h2 class="slider-headings"><a href="<?php the_permalink(); ?>"><?php echo the_title();?></a></h2>
						      				<div class="swiper-slider-meta">
						      					<?php 
							      					// Display bed counts in slider
							      					if( !empty( $beds) ) : 
							      				?>
						      					<span><i class="fa-solid fa-bed"></i></span>
						      					<span class="slider-meta-count"><?php echo esc_html( $beds ); ?></span>
						      					<?php 
						      						endif; 
						      						// Display bath counter
						      						if( !empty( $bath) ) : 
						      					?>
						      					<span><i class="fa-solid fa-bath"></i></span>
						      					<span class="slider-meta-count"><?php echo esc_html($bath); ?></span>
						      					<?php 
						      						endif; 
						      						// Display price
						      						if( !empty( $price) ) : 
						      					?>
						      					<span><i class="fa-solid fa-tag"></i></span>
						      					<span class="slider-meta-count"><?php echo esc_html($price_prefix).' '.esc_html(ere_get_format_money($price_short, $price_unit)).' '.esc_html($price_postfix); ?></span>
						      					<?php 
						      						endif; 
						      						// Display address
						      						if( !empty( $property_address) ) : 
						      					?>
						      					<p class="swiper-meta-address"><i class="fas fa-map-marker-alt"></i><?php echo esc_html( $property_address );?></p>
						      					<?php 
						      						endif; 
						      					?>
						      				</div>
						      			</div>
						      		</div>
						      	</div>
						    </div>
							<?php  
							    endwhile;
							    wp_reset_postdata();
							    endif;
							    endif;
							?>


							<?php 
							// Post Slider
							if ( get_theme_mod( 'real_estate_salient_slide_type') == "post") :
								$property_id = get_the_ID();
								
								if ($latestloop->have_posts()) :  while ($latestloop->have_posts()) : $latestloop->the_post(); ?>
								<div class="swiper-slide">
						      		<div class="swiper-slider-content">
							      		<?php the_post_thumbnail( 'real-estate-vital-slider' ); ?>
							      		<div class="overlay swiper-slider-text">
							      			<div class="swiper-slider-content container">
							      				<h2 class="slider-headings"><a href="<?php the_permalink(); ?>"><?php echo the_title();?></a></h2>
							      			</div>
							      		</div>
							      	</div>
								</div>
								<?php 
								    endwhile;
								    endif;
								    wp_reset_postdata();
							endif;								    
							?>
		

						</div>
					<div class="swiper-pagination"></div>
			    	<div class="autoplay-progress">
			      		<svg viewBox="0 0 48 48">
			        		<circle cx="24" cy="24" r="20"></circle>
			      		</svg>
			      		<span></span>
			    	</div>
			  </div>
		</div>
	</div>
</section>
		<?php endif;
	}
}


/* -- Home Properties */
if ( ! function_exists( 'real_estate_vital_home_properties' ) ) {
	function real_estate_vital_home_properties() {
		?>
		<?php if ( get_theme_mod( 'real_estate_salient_home_properties_enable', true ) AND class_exists('Essential_Real_Estate')) : ?>
			<section class="home-rooms-grid" id="content">

				<div class="home-rooms-head container">
					<div>
						<div class="row align-items-center">
							<div class="home-rooms-head-left">
								<div class="home-rooms-head-left-content">
									<?php if( get_theme_mod('real_estate_salient_home_properties_desc', __( 'You are seeing recently added properties','real-estate-vital' ) )) : ?>
										<p><?php echo esc_html(get_theme_mod('real_estate_salient_home_properties_desc', __('You are seeing recently added properties','real-estate-vital'))); ?></p>
									<?php endif; ?>
									<?php if(get_theme_mod( 'real_estate_salient_home_properties_title', __( 'Recent Properties','real-estate-vital' ))) : ?>
										<h3 class="home-section-header"><?php echo esc_html(get_theme_mod('real_estate_salient_home_properties_title', __('Recent Properties','real-estate-vital'))); ?></h3>
									<?php endif; ?>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="container">					
					<div class="ere-property-wrap">
						<div class="ere-property clearfix property-grid  col-gap-30">
							<div class="property-content columns-3 columns-md-3 columns-sm-2 columns-xs-1 columns-mb-1">
								<?php
								// Property item class define
								$custom_property_layout_style = ere_get_option('archive_property_layout_style', 'property-grid');
								$custom_property_items_amount = ere_get_option('archive_property_items_amount', '6');
								$custom_property_image_size = ere_get_option( 'archive_property_image_size', '330x180' );
								$custom_property_columns = ere_get_option('archive_property_columns', '3');
								$custom_property_columns_gap = ere_get_option('archive_property_columns_gap', 'col-gap-30');
								$custom_property_items_md = ere_get_option('archive_property_items_md', '3');
								$custom_property_items_sm = ere_get_option('archive_property_items_sm', '2');
								$custom_property_items_xs = ere_get_option('archive_property_items_xs', '1');
								$custom_property_items_mb = ere_get_option('archive_property_items_mb', '1');

								$property_item_class = array();
								if (isset($_SESSION["property_view_as"]) && !empty($_SESSION["property_view_as"]) && in_array($_SESSION["property_view_as"], array('property-list', 'property-grid'))) {
								    $custom_property_layout_style = $_SESSION["property_view_as"];
								}

								$wrapper_classes = array(
								    'ere-property clearfix',
								    $custom_property_layout_style,
								    $custom_property_columns_gap
								);

								if ($custom_property_layout_style == 'property-list') {
								    $wrapper_classes[] = 'list-1-column';
								}

								if ($custom_property_columns_gap == 'col-gap-30') {
								    $property_item_class[] = 'mg-bottom-30';
								} elseif ($custom_property_columns_gap == 'col-gap-20') {
								    $property_item_class[] = 'mg-bottom-20';
								} elseif ($custom_property_columns_gap == 'col-gap-10') {
								    $property_item_class[] = 'mg-bottom-10';
								} 
								$args = array(
								    'post_type' => 'property',
								    'ignore_sticky_posts' => true,
								    'posts_per_page' => 6,
								    'orderby' => 'date',
								    'order' => 'DESC',
								    'post_status' => 'publish',
								);
								$latestloop = new WP_Query($args);
								if ($latestloop->have_posts()) :  while ($latestloop->have_posts()) : $latestloop->the_post(); ?>
								<div class="ere-property ere-item-wrap clearfix">						
									<div class="mg-bottom-30 ere-property-featured">
									<?php ere_get_template('content-property.php', array(
			                            'property_item_class' => $property_item_class,
			                            'custom_property_image_size' => $custom_property_image_size
			                        )); ?>
			                        </div>
		                        </div>
								<?php
							    endwhile;
							    wp_reset_postdata();
							    endif;
							    ?>
					    	</div>
						</div>
					</div>
				</div>
			</section>

		<?php endif;
	}
}

/* -- Home posts */
if ( ! function_exists( 'real_estate_vital_home_posts' ) ) {
	function real_estate_vital_home_posts() {
		?>
		<?php if ( get_theme_mod( 'real_estate_vital_recent_post', true ) ) : ?>
		<section class="frontpage-latestposts-fluid container-fluid">
			<div class="home-rooms-head container">
					<div>
						<div class="row align-items-center">
							<div class="home-rooms-head-left">
								<div class="home-rooms-head-left-content">
									<?php if( get_theme_mod( 'real_estate_vital_recent_post_desc', __( 'You are seeing recently added posts','real-estate-vital' ) )) : ?>
										<p><?php echo esc_html(get_theme_mod('real_estate_vital_recent_post_desc', __( 'You are seeing recently added posts', 'real-estate-vital'))); ?></p>
									<?php endif; ?>
									<?php if(get_theme_mod( 'real_estate_vital_recent_post_title', __( 'Recent News','real-estate-vital' ))) : ?>
										<h3 class="home-section-header"><?php echo esc_html(get_theme_mod('real_estate_vital_recent_post_title', __('Recent News','real-estate-vital'))); ?></h3>
									<?php endif; ?>
								</div>
							</div>
						</div>
					</div>
				</div>
			<div class="frontpage-latestposts container">
				<div class="frontpage-offer-row row clearfix">
					<?php                 		             
					$args = array (
					  'post_type'	=> 'post',
					  'ignore_sticky_posts' => true,
					  'showposts'	=> 3,
					  'meta_query' => array(
						array(
						 'key' => '_thumbnail_id',
						 'compare' => 'EXISTS'
						),
					)
					);
					$latestloop = new WP_Query($args);
					if ($latestloop->have_posts()) :  while ($latestloop->have_posts()) : $latestloop->the_post();
					?>

		            <div class="col-md-4 col-sm-4">
		                <div class="frontpage-gridposts">
		                    <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) the_post_thumbnail('real-estate-vital-slider'); ?></a>
		                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
		                    <div class="frontpage-gridposts-icons"><i class="fa fa-calendar"></i> <?php the_time(get_option('date_format')); ?></div>
		                    <p><?php echo wp_trim_words( strip_tags(get_the_content()), 19, '' ); ?></p>
		                    <a class="frontpage-gridposts-readmore" href="<?php the_permalink(); ?>"><?php esc_html__('Read more','real-estate-vital'); ?></a>
		                </div>
		            </div>
		    		<?php
		    		endwhile;
		    		wp_reset_postdata();
		    		endif;
		    		?>
				</div>
			</div>
		</section>
		<?php
		endif;
	}
}

function real_estate_vital_remove_actions() {
	remove_action( 'real_estate_salient_topbar', 'real_estate_salient_topbar_settings', 0 );
	remove_action( 'real_estate_salient_header', 'real_estate_salient_logo_settings', 0 );
	remove_action( 'real_estate_salient_slider', 'real_estate_salient_slider_content', 0 );
	remove_action( 'real_estate_salient_recent_properties', 'real_estate_salient_home_properties', 0 );
	remove_action( 'real_estate_salient_recent_posts', 'real_estate_salient_home_posts', 0 );

};
add_action( 'init', 'real_estate_vital_remove_actions');
add_action( 'real_estate_salient_topbar', 'real_estate_vital_topbar_settings', 10 );
add_action( 'real_estate_salient_header', 'real_estate_vital_logo_settings', 10 );
add_action( 'real_estate_salient_slider', 'real_estate_vital_slider_content', 10 );
add_action( 'real_estate_salient_recent_properties', 'real_estate_vital_home_properties', 10 );
add_action( 'real_estate_salient_recent_posts', 'real_estate_vital_home_posts', 10 );