<?php

function servicesShortCode($pageId = null, $isCustomizer = false, $i = null) {

    ob_start();

    $args = array('post_type' => 'our-services');
    if (!empty($pageId)) {
        $args['page_id'] = absint($pageId);
    }
    $args['posts_per_page'] = -1;
    $colCls = '';
    // if($isCustomizer == true){
    $cols = get_theme_mod('lz_fitness_services_npp_count',2);  
    ++$cols;
    switch ($cols) {
            case 1:
            $colCls = 'col-md-12 col-sm-12';
            break;
            case 2:
            $colCls = 'col-md-6 col-sm-6';
            break;
            case 4:
            case 8:
            case 7:
            case 10:
            case 11:
            case 12:
            $colCls = 'col-md-3 col-sm-6';
            break;
            default: 
            $colCls = 'col-md-4 col-sm-6';
            break;
    }
    // }
    $text = '';
    $query = new WP_Query($args);
    if ($query->have_posts()):
        $postN = 0;

        while ($query->have_posts()) : $query->the_post();
            $lz_fitness_image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'total-services-thumb');
            $post = get_post();

            if (($postN % 3) == 0 && $isCustomizer == false) {
                ?>
                <div class="clearfix paddingBot20"></div>
                </div>
                <div class="row">
                    <?php
                }
                ++$postN;
                ?>
      
                <div class="<?php echo $colCls;?>">
                <div class="services-post servpost text-center">
                    <div class="services-icon">
                      <?php
                                if (has_post_thumbnail()) {
                                    $image_url = $lz_fitness_image[0];
                                } else {
                                    $image_url = get_template_directory_uri() . '/images/nothumb-related.png';
                                }
                                ?>
                                <img class="secondry-bg img-responsive" src="<?php echo esc_url($image_url); ?>" alt="<?php the_title(); ?>" />

                    </div>
                    <div class="services-data text-center">
                        <h5><?php the_title(); ?></h5>
                        <div class="services-excerpt">
                         <?php

                            if(has_excerpt()){
                              echo get_the_excerpt();
                             }else{
                              echo lz_fitness_excerpt( get_the_content() , 80 );
                             } 
                        ?>
                        </div>      
                         <?php
                            $ser_button1 = get_theme_mod('ser_button1', 'Read More');
                        ?>      
                           <?php if($ser_button1) { ?>                          
                        <div class="services-readmore">
                            <a class="btn " href="<?php the_permalink(); ?>"><?php echo $ser_button1 ?> </a>
                        </div>
                        <?php }?>
                    </div>
                    <div class="clearfix"></div>                                                
                </div>       
                   
            </div>


                <?php
            endwhile;
            $text = ob_get_contents();
            ob_clean();
        endif;
        wp_reset_postdata();
        return $text;
}




function servicespageShortCode($pageId = null, $isCustomizer = false, $i = null) {

    ob_start();

    $args = array('post_type' => 'our-services');
    if (!empty($pageId)) {
        $args['page_id'] = absint($pageId);
    }
    $args['posts_per_page'] = -1;
    $colCls = '';
    // if($isCustomizer == true){
    $cols = get_theme_mod('lz_fitness_services_npp_count',2);  
    ++$cols;
    switch ($cols) {
            case 1:
            $colCls = 'col-md-12 col-sm-12';
            break;
            case 2:
            $colCls = 'col-md-6 col-sm-6';
            break;
            case 4:
            case 8:
            case 7:
            case 10:
            case 11:
            case 12:
            $colCls = 'col-md-3 col-sm-6';
            break;
            default: 
            $colCls = 'col-md-4 col-sm-6';
            break;
    }
    // }
    $text = '';
    $query = new WP_Query($args);
    if ($query->have_posts()):
        $postN = 0;

        while ($query->have_posts()) : $query->the_post();
            $lz_fitness_image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'total-services-thumb');
            $post = get_post();


                ?>
      
                <div class="<?php echo $colCls;?>">
                <div service-match-height="groupName" class="services-post servpost text-center">
                    <div class="services-icon">
                           <a href="<?php the_permalink(); ?>">
                      <?php
                            if (has_post_thumbnail()) {
                                $image_url = $lz_fitness_image[0];
                            } else {
                                $image_url = get_template_directory_uri() . '/images/nothumb-related.png';
                            }
                            ?>
                            <img class="secondry-bg img-responsive" src="<?php echo esc_url($image_url); ?>" alt="<?php the_title(); ?>" />
                         </a>
                    </div>
                       <div class="sec-icn"> 
                                <a href="<?php the_permalink(); ?>"><i class="fa fa-home" aria-hidden="true"></i></a>
                            </div> 
                    <div class="services-data text-center">
                        <h5><?php the_title(); ?></h5>
                        <div class="services-excerpt">
                         <?php

                            if(has_excerpt()){
                              echo get_the_excerpt();
                             }else{
                              echo lz_fitness_excerpt( get_the_content() , 80 );
                             } 
                        ?>
                        </div>      
                         <?php
                            $ser_button1 = get_theme_mod('ser_button1', 'Read More');
                        ?>      
                           <?php if($ser_button1) { ?>                          
                        <div class="services-readmore">
                            <a class="btn " href="<?php echo esc_url(get_permalink()); ?>"><?php echo $ser_button1 ?> </a>
                        </div>
                        <?php }?>
                    </div>
                    <div class="clearfix"></div>                                                
                </div>    
              
                    <script>
    var matchHeight = function () {
    
    function init() {
        eventListeners();
        matchHeight();
    }
    
    function eventListeners(){
        $(window).on('resize', function() {
            matchHeight();
        });
    }
    
    function matchHeight(){
        var groupName = $('[service-match-height]');
        var groupHeights = [];
        
        groupName.css('min-height', 'auto');
        
        groupName.each(function() {
            groupHeights.push($(this).outerHeight());
        });
        
        var maxHeight = Math.max.apply(null, groupHeights);
        groupName.css('min-height', maxHeight);
    };
    
    return {
        init: init
    };
    
} ();

$(document).ready(function() {
    matchHeight.init();
});
</script>     
            </div>

                                
                <?php
            endwhile;
            $text = ob_get_contents();
            ob_clean();
        endif;
        wp_reset_postdata();
        return $text;
}

add_shortcode('SERVICE', 'servicespageShortCode');