<?php
/**
 * Sparta - Premium Furniture/Interior Wordpress Theme functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Sparta
 */
include_once 'inc/merlin-config.php';
get_template_part('sparta/core');

function sparta_get_link_page_booking()
{
    $link = '#';
    $pageslug = get_theme_mod('sparta_hotel_book_now_page', '');
    if ($pageslug !== '') {
        $link = get_permalink($pageslug);
    }

    return $link;
}

function sparta_get_related_room($id)
{
    global $relatedcatargs, $relatedargs;
    $limit_post = 3;
    $my_query = '';
    $rel_tagnames = $rel_catnames = array();
    $rel_tags = get_the_terms($id, 'mphb_room_type_tag');

    if ($rel_tags && !is_wp_error($rel_tags)) {
        // Get list of tag names and set arguments for loop
        foreach ($rel_tags as $rel_tag) {
            $rel_tagnames[] = $rel_tag->slug;
        }
        $relatedargs = array(
            'post_type' => 'mphb_room_type',
            'post__not_in' => array($id),
            'posts_per_page' => $limit_post,
            'tax_query' => array(
                array(
                    'taxonomy' => 'mphb_room_type_tag',
                    'field' => 'slug',
                    'terms' => $rel_tagnames,
                )
            ),

        );
        $my_query = new WP_Query($relatedargs);
    } else {

        $post_cats = get_the_terms($id, 'mphb_room_type_category');
        if ($post_cats && !is_wp_error($post_cats)) {
            foreach ($post_cats as $post_cat) {
                $rel_catnames[] = $post_cat->slug;
            }

            $relatedcatargs = array(
                'post_type' => 'mphb_room_type',
                'post__not_in' => array($id),
                'posts_per_page' => $limit_post,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'mphb_room_type_category',
                        'field' => 'slug',
                        'terms' => $rel_catnames,
                    )
                ),
            );
            $my_query = new WP_Query($relatedcatargs);
        }
    }

    //If there are posts
    if ($my_query !== '' && $my_query->have_posts()) :
        ?>
        <div class="sparta-related-room">
            <div class="col-lg-12">
                <div class="row vk-rooms-grid-content">
                    <div class="vk-rooms-grid-header col-md-12">
                        <h2><?php echo esc_html__('Related Rooms', 'sparta'); ?></h2>
                        <div class="vk-rooms-grid-border"></div>
                    </div>
                    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                        <div class="col-lg-4 col-md-6">
                            <div class="item">
                                <div class="vk-sparta-item-content">
                                    <?php
                                    if (has_post_thumbnail()) :?>
                                        <div class="vk-item-img">
                                            <?php
                                            echo '<a href="' . esc_url(get_permalink()) . '"  class="img-responsive">';
                                                the_post_thumbnail('sparta-thumb-grid');
                                            echo '</a>';
                                            ?>
                                        </div>
                                    <?php endif; ?>
                                    <div class="vk-item-text">
                                        <?php the_title('<h2><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h2>'); ?>
                                        <ul>
                                            <li>
                                                <p><?php echo esc_html__('Starting Form :', 'sparta'); ?></p>
                                            </li>
                                            <li>
                                                <?php
                                                if (function_exists('mphb_tmpl_the_room_type_default_price')) :
                                                    mphb_tmpl_the_room_type_default_price();
                                                endif;
                                                ?>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endwhile;
                    wp_reset_postdata();
                    ?>
                </div>
            </div>
        </div>
    <?php
    endif;

}

if ( ! function_exists( 'sparta_comments' ) ) :

    function sparta_comments( $comment, $args, $depth ) {

        $allowed_html_array = array(
            'a'      => array(
                'href'  => array(),
                'title' => array(),
            ),
            'br'     => array(),
            'cite'   => array(),
            'em'     => array(),
            'strong' => array(),
        );

        $GLOBALS['comment'] = $comment; ?>

        <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
            <article id="comment-<?php comment_ID(); ?>" class="comment-body">
                <footer class="comment-meta">
                    <div class="comment-author vcard">
                        <?php
                        echo get_avatar( $comment, $size = '43' ); ?>
                        <?php printf( wp_kses( __( '<b class="fn">%s</b> ', 'sparta' ), $allowed_html_array ), get_comment_author_link() ); ?>


                    </div><!-- .comment-author -->

                    <div class="comment-metadata">
                        <a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>">
                            <?php printf( esc_html__( ' %1$s at %2$s', 'sparta' ), get_comment_date(), get_comment_time() ); ?>
                        </a>

                        </a>
                        <span class="edit-link"><?php edit_comment_link( esc_html__( 'Edit', 'sparta' ), '', '' ); ?></span>

                    </div><!-- .comment-metadata -->

                </footer><!-- .comment-meta -->

                <div class="comment-content">
                    <?php if ( $comment->comment_approved === '0' ) : ?>
                        <span class="moderation"><?php esc_html_e( 'Awaiting Moderation', 'sparta' ); ?></span>
                    <?php endif; ?>
                    <?php comment_text(); ?>
                    <?php
                    comment_reply_link(
                        array_merge(
                            $args, array(
                                'before' => '',
                                'depth'  => $depth,
                                'max_depth' => $args['max_depth'],
                            )
                        )
                    );
                    ?>
                </div><!-- .comment-content -->

            </article>


        </li>
        <?php
    }
endif;


