<?php
	//Get all settings
	$settings = $this->get_settings();
?>

<div class="course_grid_container">
<?php
	$widget_id = $this->get_id();
	
	$no_hover_class = '';
	if($settings['hover_animation'] != 'yes')
	{
		$no_hover_class = 'no-hover-animation';
	}
	
	//For pagination
	if(is_front_page())
	{
	    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
	}
	else
	{
	    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	}

	$args = array( 
		'post_type' => 'meeting',
		'suppress_filters' => false,
		'post_status' => 'publish',
		'posts_per_page' => $settings['posts_per_page']['size'],
		'paged' => $paged,
		'orderby' => 'menu_order',
		'order' => 'ASC',
	);
	
	if(!empty($settings['meeting_category']))
	{
		$args['tax_query'][] = array( 
		    'taxonomy' => 'meeting_category',
		    'field' => 'id', 
		    'terms' => $settings['meeting_category']
		);
	}
	
	switch($settings['sort_by'])
	{
		case 'default':
			$args['orderby'] = 'menu_order';
			$args['order'] = 'ASC';
		break;
		
		case 'published':
			$args['orderby'] = 'date';
			$args['order'] = 'DESC';
		break;
		
		case 'title':
			$args['orderby'] = 'title';
			$args['order'] = 'ASC';
		break;
		
		case 'start_date':
			$args['orderby'] = 'meta_value';
			$args['meta_key'] = 'meeting_date_raw';
			$args['order'] = 'ASC';
		break;
	}
	
	query_posts($args);
	$count = 0;
	
	//Get spacing class
	$spacing_class = '';
	if($settings['spacing'] != 'yes')
	{
		$spacing_class = 'has_no_space';
	}
	
	$column_class = 1;
	$thumb_image_name = 'dotlife-gallery-grid';
	
	//Start displaying gallery columns
	switch($settings['columns']['size'])
	{
		case 2:
	   		$column_class = 'tg_two_cols';
	   	break;
	   	
	   	case 3:
	   	default:
	   		$column_class = 'tg_three_cols';
	   	break;
	   	
	   	case 4:
	   		$column_class = 'tg_four_cols';
	   	break;
	   	
	   	case 5:
	   		$column_class = 'tg_five_cols';
	   	break;
	   	
	   	case 6:
	   		$column_class = 'tg_six_cols';
	   	break;
	}
?>
<div class="portfolio_classic_content_wrapper portfolio_classic layout_<?php echo esc_attr($column_class); ?> <?php echo esc_attr($spacing_class); ?>" data-cols="<?php echo esc_attr($settings['columns']['size']); ?>">
<?php
	if (have_posts()) : while (have_posts()) : the_post();
		$post_ID = get_the_ID();		
		
		$last_class = '';
		$count++;
		
		if($count%$settings['columns']['size'] == 0)
		{
			$last_class = 'last';
		}
		
		$meeting_featured_img_url = get_the_post_thumbnail_url($post_ID, $thumb_image_name);
		$meeting_featured_img_alt = get_post_meta(get_post_thumbnail_id($post_ID), '_wp_attachment_image_alt', true);
		$meeting_url = get_permalink($post_ID);
		$meeting_title= get_the_title();
?>
		 <div class="portfolio_classic_grid_wrapper <?php echo esc_attr($no_hover_class); ?> <?php echo esc_attr($column_class); ?> <?php echo esc_attr($last_class); ?>  portfolio-<?php echo esc_attr($count); ?> tile scale-anm">
			<div class="card__img" style="background-image:url(<?php echo esc_url($meeting_featured_img_url); ?>);"></div>
			 
			 <a href="<?php echo esc_url($meeting_url); ?>" class="card_link">
				<div class="card__img--hover" style="background-image:url(<?php echo esc_url($meeting_featured_img_url); ?>);"></div>
			</a>

			<div class="card__info">
				<h3 class="card__title"><a href="<?php echo esc_url($meeting_url); ?>"><?php echo esc_html($meeting_title); ?></a></h3>
				<?php
					if(isset($settings['excerpt_length']['size']) && !empty($settings['excerpt_length']['size']))
					{
				?>
				<div class="card__excerpt"><?php echo dotlife_limit_get_excerpt(strip_tags(get_the_excerpt()), $settings['excerpt_length']['size'], '...'); ?></div>
				<?php
					}
				?>
				
				<?php
					//Get meeting start date
					$meeting_date = get_post_meta($post_ID, 'meeting_date', true);
					
					if($settings['show_date'] == 'yes' && !empty($meeting_date))
					{
				?>
					<div class="card__meta">
						<span class="ti-calendar"></span>&nbsp;
						<span class="card__lesson"><?php echo $meeting_date; ?></span>
					</div>
				<?php
					}
				?>
				
				<?php
					//Get meeting duration
					$meeting_duration = get_post_meta($post_ID, 'meeting_duration', true);
					
					if($settings['show_duration'] == 'yes' && !empty($meeting_duration))
					{
				?>
					<div class="card__meta">
						<span class="ti-time"></span>&nbsp;
						<span class="card__student"><?php echo esc_html($meeting_duration); ?></span>
					</div>
				<?php
					}
				?>
		  	</div>
		</div>
<?php
	endwhile; endif;
	
	if($settings['spacing'] == 'yes')
	{
?>
<br class="clear"/>
<?php
	}
?>
</div>
</div>
<?php
	if($settings['show_pagination'] == 'yes')
	{
		global $wp_query;
		if($wp_query->max_num_pages > 1)
	    {
	    	if (function_exists("dotlife_pagination")) 
	    	{
	    	    dotlife_pagination($wp_query->max_num_pages, 4, 'course-posts-grid' );
	    	}
	    	else
	    	{
?>
	    		<div class="pagination"><p><?php posts_nav_link(''); ?></p></div>
<?php
	    	}
?>
			<div class="pagination_detail">
		    	<?php esc_html_e('Page', 'dotlife-elementor' ); ?> <?php echo esc_html($paged); ?> <?php esc_html_e('of', 'dotlife-elementor' ); ?> <?php echo esc_html($wp_query->max_num_pages); ?>
		    </div>
<?php
	    }
	}
	
	wp_reset_query();	
?>
<br class="clear"/>