<?php

class Intense_Faq extends Intense_Shortcode {

	function __construct() {
        parent::__construct();

		global $intense_visions_options;

		$this->title = __( 'FAQ', 'intense' );
		$this->description = __( 'Easily show off your FAQs with the use of the FAQ Custom Post Type and the FAQ shortcode.
			<br><br>
			Show off your FAQs with numerous different layouts. Use any of the templates provided for the Blog shortcode, or create your own ', 'intense' );
		$this->category = __( 'Posts', 'intense' );
		$this->show_preview = true;
	}

	function shortcode( $atts, $content = null ) {
		global $intense_visions_options;
		extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

		$output = '';
		$faqid = '';

		global $post, $wp_query;

		$padding_top = $intense_visions_options['intense_layout_row_default_padding']['padding-top'];
		$padding_bottom = $intense_visions_options['intense_layout_row_default_padding']['padding-bottom'];
		$margin_top = $intense_visions_options['intense_layout_row_default_margin']['margin-top'];
		$margin_bottom = $intense_visions_options['intense_layout_row_default_margin']['margin-bottom'];

		$layout_style = '';

		if ( isset( $padding_top ) ) {
			$layout_style .= 'padding-top: ' . $padding_top . 'px; ';
		}

		if ( isset( $padding_bottom ) ) {
			$layout_style .= 'padding-bottom: ' . $padding_bottom . 'px; ';
		}

		if ( isset( $margin_top ) ) {
			$layout_style .= 'margin-top: ' . $margin_top . 'px; ';
		}

		if ( isset( $margin_bottom ) ) {
			$layout_style .= 'margin-bottom: ' . $margin_bottom . 'px; ';
		}

		$no_layout_style = "padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;";
			
		$paged = ( !empty( $wp_query ) && get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;		
		$iscat_array = false;
		$cat_array = null;
		$faqid = ( !empty ( $id ) ) ? $id : 'faq_list_' . rand();

		$args = array(
			'post_type' => 'intense_faq',
			'orderby' => 'id',
			'order' => 'DESC',
			'paged'=> $paged,
			'nopaging' => ( $show_all == 0 ? false : true ),
			'tax_query' => '',
			'posts_per_page' => $posts_per_page
		);

		if ( $categories != '' ) {
			if ( strpos( $categories, ',' ) ) {
				$iscat_array = true;
				$cat_array = explode( ',', $categories );
				$args['tax_query'] = array(
					array(
						'taxonomy' => 'faq_category',
						'field'  => 'slug',
						'terms'  => $cat_array
					)
				);
			} else {
				$args['tax_query'] = array(
					array(
						'taxonomy' => 'faq_category',
						'field'  => 'slug',
						'terms'  => array( $categories )
					)
				);
			}
		}

		$post_query = new WP_Query( $args );

		//Setup filters
		if ( ( $show_filter && $categories == '' ) || ( $iscat_array && $show_filter ) ) {
			$faq_category = get_terms( 'faq_category' );
			$filter_shortcode = "";

			if ( $faq_category ) {
				$filter_shortcode .= "<nav role='navigation' id='nav-below' class='filter-post' style='margin-bottom: 10px;'>";
				$filter_shortcode .= "<ul class='intense pager " . ( is_rtl() ? "rtl " : "" ) . "'>";
				$filter_shortcode.= "<li><a class='filter btn-mini active' data-filter='all' href='#allfilter'>" . __( 'All', 'Intense' ) . "</a></li>";

				foreach ( $faq_category as $faq_cat ) {
					if ( $iscat_array ) {
						foreach ( $cat_array as &$value ) {
							if ( $value == $faq_cat->slug ) {
								$filter_shortcode .= "<li><a class='filter btn-mini' data-filter='" . $faq_cat->slug . "' href='#" . $faq_cat->slug . "filter'>" . esc_html( $faq_cat->name ) . "</a></li>";
							}
						}
					} else {
						$filter_shortcode .= "<li><a class='filter btn-mini' data-filter='" . $faq_cat->slug . "' href='#" . $faq_cat->slug . "filter'>" . esc_html( $faq_cat->name ) . "</a></li>";
					}
				}

				$filter_shortcode .= "</ul>";
				$filter_shortcode .= "</nav>";
			}

			$output .= do_shortcode( $filter_shortcode );
		}

		$i = 0;
		$j = 1;

		$layout_style .= ' margin-left: 0; margin-right: 0;';

		$collapsibles_shortcode = '<div id="' . $faqid . '" class="intense row' . ( !empty( $class ) ? ' ' . $class : '' ) . '" style="' . $layout_style . '"><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">';
		$collapsibles_shortcode .= '[intense_collapsibles single_toggle="' . $single_toggle . '" id="' . $faqid . '" title_border="' . $title_border . '" content_border="' . $content_border . '" expand_icon_source="' . $expand_icon_source . '" expand_icon="' . $expand_icon . '" collapse_icon_source="' . $collapse_icon_source . '" collapse_icon="' . $collapse_icon . '" icon_size="' . $icon_size . '"]';

		while ( $post_query->have_posts() ) {
			$post_query->the_post();
			$faq_link = '';
			$faq_link_target = '';
			$css_class = '';
			$set_title_background_color = '';
			$set_content_background_color = '';
			$set_title_font_color = '';
			$set_content_font_color = '';
			$set_active = '';

			if ( get_field( 'intense_faq_link' ) != '' ) {
				$faq_link = ' external_link="' . get_field( 'intense_faq_link' ) . '"';
			}

			if ( get_field( 'intense_faq_link_target' ) != '' ) {
				$faq_link_target = ' external_link_target="' . get_field( 'intense_faq_link_target' ) . '"';
			}

			if ( get_field( 'intense_faq_css_class' ) != '' ) {
				$css_class = ' class="' . get_field( 'intense_faq_css_class' ) . '"';
			}

			if ( get_field( 'intense_faq_title_background' ) != '' ) {
				$set_title_background_color = ' title_background_color="' . get_field( 'intense_faq_title_background' ) . '"';
			}

			if ( get_field( 'intense_faq_content_background' ) != '' ) {
				$set_content_background_color = ' content_background_color="' . get_field( 'intense_faq_content_background' ) . '"';
			}

			if ( get_field( 'intense_faq_title_font_color' ) != '' ) {
				$set_title_font_color = ' title_font_color="' . get_field( 'intense_faq_title_font_color' ) . '"';
			}

			if ( get_field( 'intense_faq_content_font' ) != '' ) {
				$set_content_font_color = ' content_font_color="' . get_field( 'intense_faq_content_font' ) . '"';
			}

			//override individual colors for title (background and font) and content (background and font) if they are included in the shortcode
			if ( $title_background_color != '' ) {
				$set_title_background_color = ' title_background_color="' . $title_background_color . '"';
			}

			if ( $title_font_color != '' ) {
				$set_title_font_color = ' title_font_color="' . $title_font_color . '"';
			}

			if ( $content_background_color != '' ) {
				$set_content_background_color = ' content_background_color="' . $content_background_color . '"';
			}

			if ( $content_font_color != '' ) {
				$set_content_font_color = ' content_font_color="' . $content_font_color . '"';
			}

			$item_classes = '';
			$item_cats = get_the_terms( $post->ID, 'faq_category' );

			if ( $item_cats ) {
				foreach ( $item_cats as $item_cat ) {
					$item_classes .= $item_cat->slug . ' ';
				}
			}

			if ( ( $show_filter && $categories == '' ) || ( $iscat_array && $show_filter ) ) {
				$item_classes .= 'intense mix ';
			}

			if ( !$single_toggle && $expand_all ) {
				$set_active = ' active="1"';
			}

			$collapsibles_shortcode .= '[intense_collapse title="' . the_title_attribute( 'echo=0' ) . '" class="' . $item_classes . '"' . $faq_link . $faq_link_target . $css_class . $set_title_background_color . $set_content_background_color . $set_title_font_color . $set_content_font_color . $set_active . ']' . do_shortcode( get_the_content() ) . '[/intense_collapse]';

			$i++;
		}

		$collapsibles_shortcode .= "[/intense_collapsibles]";

		$collapsibles_shortcode .= "</div></div>";

		$output .= do_shortcode( $collapsibles_shortcode );

		if ( ( $show_filter && $categories == '' ) || ( $iscat_array && $show_filter ) ) {
			// mixitup jquery plugin
			intense_add_script( 'mixitup' );

			$output .= "<script> jQuery(function($){";
			$output .= " $(document).ready(function() {";
			$output .= "  $('[id^=\"" . $faqid . "-accord\"]').mixitup({";

			if ( strlen( $filter_effects ) > 0 ) {
				$output .= "   effects: ['" . str_replace( ',', '\',\'', $filter_effects ) . "'],";
			} else {
				$output .= "   effects: ['fade','scale'],";
			}

			if ( strlen( $filter_easing ) > 0 ) {
				$output .= "   easing: '" . $filter_easing . "'";
			} else {
				$output .= "   easing: 'smooth'";
			}

			$output .= "  }); $('.mix').css('visibility', 'visible');";
			$output .= " });";
			$output .= "}); </script>";

			$output .= "<style type='text/css'>";
			$output .= " #" . $faqid . " {";
			$output .= "  overflow: hidden; }";
			$output .= " #" . $faqid . " .mix {";
			$output .= "  width: 100%;";
			$output .= "  opacity: 0;";
			$output .= "  display: none; }";
			$output .= " #" . $faqid . " h3, #" . $faqid . " h1 {";
			$output .= "  line-height: normal !important; }";
			$output .= " #" . $faqid . " div.mix {";
			$output .= "  vertical-align: top; }";
			$output .= "</style>";
		}

		$output .= intense_return_content_nav( 'nav-below', null, $post_query );

		return $output;
	}	
}
