<?php

if(!function_exists('mascot_core_educacion_sc_blog_vc_map_modifier')) {
	/**
	 * Shortcodes vc_map modifier
	 */
	function mascot_core_educacion_sc_blog_vc_map_modifier( $shortcode_base ) {
		//vc_remove_param( $shortcode_base, "display_type" );
		//vc_add_param( $shortcode_base, $attributes );
		//vc_add_params( $shortcode_base, $attributes );
	}
}



if(!function_exists('mascot_core_educacion_sc_blog_render')) {
	/**
	 * Renders shortcodes HTML
	 */
	function mascot_core_educacion_sc_blog_render( $attr, $content = null ) {
		$class_instance =  '';
		$args = array(
			'custom_css_class' => '',
			'design_style' => 'blog-style1-simple',
			'display_type' => '',
			'columns' => '3',

			'show_navigation' => 'true',
			'show_bullets' => 'true',
			'bullets_skin' => 'owl-dots-light-skin',
			'bullets_alignment' => 'owl-dots-center',
			'carousel_autoplay' => 'true',
			'carousel_loop' => 'true',
			'animation_speed' => '6000',
			'smart_speed' => '2000',
			'margin' => '30',
			'stagepadding' => '0',
			'laptop' => '',
			'tablet' => '',

			'total_items' => '3',
			'category' => '',
			'order_by' => 'date',
			'order' => 'DESC',

			'show_title' => 'true',
			'title_tag' => 'h5',
			'show_description'	=> 'true',
			'show_featured_image' => 'true',
			'featured_image_size' => 'post-thumbnail',
			'show_post_meta' => 'true',
			'post_meta_options' => 'show-post-date',
			'style_classic_show_post_meta_beside_read_more_btn' => 'show-post-by-author',
			'style_modern_show_post_meta_beside_read_more_btn' => 'show-post-by-author',
			'style_horizontal_show_post_meta_beside_read_more_btn' => 'show-post-by-author',



			
			'show_view_details_button' => 'true',
			'view_details_button_text' => esc_html__( 'View Details', 'educacion-wp' ),


			//button options
			'btn_design_style' => 'default',
			'button_size' => '',
			'btn_outlined' => false,
			'btn_round' => false,
			'btn_flat' => false,
			'btn_block' => false,
			'btn_threed_effect' => false,
			'button_alignment' => '',
			'button_hover_animation_effect' => '',


			//loadmore button options
			'loadmore_show_view_details_button' => 'false',
			'loadmore_view_details_button_text' => esc_html__( 'Load More', 'educacion-wp' ),
			'loadmore_button_loading_text' => esc_html__( 'Loading...', 'educacion-wp' ),
			'loadmore_btn_design_style' => 'default',
			'loadmore_button_size' => '',
			'loadmore_btn_outlined' => false,
			'loadmore_btn_round' => false,
			'loadmore_btn_flat' => false,
			'loadmore_btn_block' => false,
			'loadmore_btn_threed_effect' => false,
			'loadmore_button_alignment' => '',
			'loadmore_button_hover_animation_effect' => '',

			//cat filter
			'show_cat_filter' => 'false',
			'cat_filter_style' => '',
			'cat_filter_alignment' => '',
			'cat_filter_theme_colored' => '',

			'css' => '',

		);
		$params = shortcode_atts($args, $attr);
		$params['holder_id'] = educacion_mascot_get_isotope_holder_ID('blog');
		

		if (  $params['loadmore_show_view_details_button'] === 'true' ) {
			$holder_id = str_replace('-', '_', $params['holder_id']);
			$params['holder_id_underline'] = $holder_id;
			$params['wp_ajax_load_for_cpt'] = 'post';

			wp_enqueue_script( 'educacion-mascot-custom-loadmore', EDUCACION_MASCOT_TEMPLATE_URI . '/assets/js/tm-custom-loadmore.js', array('jquery'), false, true );
			wp_localize_script( 'educacion-mascot-custom-loadmore', 'tm_loadmore_params_'.$holder_id, array(
				'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php',

				'class_instance' => $class_instance,
				'params' => $params,

				'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
			) );
			wp_enqueue_script( 'educacion-mascot-custom-loadmore' );
		}


		return educacion_mascot_sc_blog_render_output( $class_instance, $params );
	}
}


if(!function_exists('educacion_mascot_sc_blog_render_output')) {
	/**
	 * Output render
	 */
	function educacion_mascot_sc_blog_render_output( $class_instance, $params ) {
		$paged = isset($params['paged']) ? $params['paged'] : '';
		
		//query args
		$args = array(
			'orderby' => $params['order_by'],
			'order' => $params['order'],
			'posts_per_page' => $params['total_items'],
			'paged' => $paged,
		);

		//if category selected
		if( $params['category'] ) {
			$args['category_name'] = $params['category'];
		}

		$the_query = new \WP_Query( $args );
		$params['the_query'] = $the_query;


		if ( !$the_query->have_posts() && $params['from_loadmore_ajax_handler'] === true ) {
			return;
		}


		//button classes
		$params['btn_classes'] = educacion_mascot_prepare_button_classes_from_params( $params );
		$params['loadmore_btn_classes'] = educacion_mascot_prepare_button_classes_from_params( $params, 'loadmore_' );
		
		$params['params_array'] = $params;


		//classes
		$classes = array();
		$classes[] = $params['design_style'];
		if( $params['display_type'] == 'carousel' ) {
			$classes[] = $params['bullets_skin'];
			$classes[] = $params['bullets_alignment'];
		}
		$classes[] = $params['custom_css_class'];
		$classes[] = apply_filters( VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, vc_shortcode_custom_css_class( $params['css'], ' ' ) );
		$params['classes'] = $classes;
		
		//Owl Carousel Data
		$params['owl_carousel_data_info'] = educacion_mascot_prepare_owlcarousel_data_from_params( $params );

		//Produce HTML version by using the parameters (filename, variation, folder name, parameters, shortcode_ob_start)
		$html = educacion_mascot_get_shortcode_template_part( 'blog', $params['display_type'], 'blog/tpl/type', $params, true );
		
		return $html;
	}
}