<?php
function tdg_render_post_and_term_items($type, $tax = '', $id)
{
    $thumb = '';
    $title = '';
    $url = '';

    if ($type == 'post') {
        $featured_img = get_the_post_thumbnail_url($id, 'full');
        if ($featured_img) {
            $thumb = $featured_img;
        } else {
            $gallery = get_field('gallery', $id);
            if (!empty($gallery)) {
                $thumb = $gallery[0]['url'];
            }
        }

        $title = get_the_title($id);
        $url = get_the_permalink($id);

    } elseif ($type == 'term') {
        $gallery = get_field('gallery_tax', $tax . '_' . $id);
        if (!empty($gallery)) {
            $thumb = $gallery[0]['url'];
        }

        $title = get_term($id)->name;
        $url = get_term_link($id);
    }
    ?>
    <div class="item-cm-tax">
        <div class="item-cm-tax-wrap">
            <?php if (!empty($thumb)) { ?>
                <div class="img">
                    <img src="<?php echo $thumb; ?>" alt="<?php echo esc_attr($title); ?>">
                </div>
            <?php } ?>

            <div class="box-hover">
                <div class="icon">
                    <img src="<?php echo get_stylesheet_directory_uri() . '/package-main/assets/images/icon.png' ?>">
                </div>

                <?php if (!empty($title)) { ?>
                    <h3 class="title-hover"><?php echo $title; ?></h3>
                <?php } ?>

                <?php if ($url) { ?>
                    <div class="button">
                        <a href="<?php echo $url; ?>">
                            <span><?php echo esc_html('Read more'); ?></span>
                        </a>
                    </div>
                <?php } ?>

            </div>

            <div class="title-box">
                <h3><?php echo $title; ?></h3>
            </div>
        </div>
    </div>
    <?php
}


function render_social_single($post_id)
{
    $title = get_the_title($post_id);
    $url = get_the_permalink($post_id);
    ?>
    <ul class="shareicon">
        <li>
            <a href="https://twitter.com/intent/tweet?url=<?php echo $url; ?>&text=<?php echo $title; ?>" target="_blank">
                <i class="ti-twitter"></i>
            </a>
        </li>
        <li>
            <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $url; ?>" target="_blank">
                <i class="ti-facebook"></i>
            </a>
        </li>
        <li>
            <a href="https://www.linkedin.com/sharing/share-offsite/?url=<?php echo $url; ?>" target="_blank">
                <i class="ti-linkedin"></i>
            </a>
        </li>
    </ul>
    <?php
}

if ( !function_exists('tdg_socials_global_function') ) {

    function tdg_socials_global_function() {

        $globalField = get_fields('option');
        $social_list = $globalField['social_list'];
        $enable = $social_list['enable'];
        $list = $social_list['list'];

        if( is_home(  ) ){
            return;
        }
    
        if( !empty($list) ): ?>
            <div class="left-panel">
                <div class="social-list">
                    <?php foreach ($list as $key => $item) {
                    $icon = $item['icon'];
                    $url = $item['url'];
                    ?>
                    <a class="social-item" href="<?php echo $url; ?>" target="_blank">
                        <img src="<?php echo $icon; ?>" alt="">
                    </a>
                    <?php
                    } ?>
                </div>
            </div>
        <?php 
        endif;
    }
}

if ( !function_exists('tdg_search_form_function') ) {

    function tdg_search_form_function() {
        ?>
        <div class="search-modal" id="search-modal">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-body">
                        <form method="get" id="search-form" class="form-inline" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
                            <input class="form-control" name="s" type="search" placeholder="Search" aria-label="Search">
                            <button class="form-button" type="submit"><?php echo __( 'Search', 'hello-thedavanigroup' ); ?></button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }

}

if ( !function_exists('tdg_recent_posts_function') ) {

    function tdg_recent_posts_function() {
        global $post;

        $post_type = get_post_type( $post );
        $args = array(
            'post_type' => $post_type,
            'posts_per_page' => 5,
            'post_status' => 'publish',
        );
        $posts = new WP_Query( $args );

        if ( $posts->have_posts(  ) ) {
            echo '<div class="recent-posts">'; 
            while ($posts->have_posts(  )) {
                $posts->the_post();
                $p_title = get_the_title( get_the_ID() );
                $p_link = get_the_permalink( get_the_ID() );
                $p_thumb = get_the_post_thumbnail_url( get_the_ID() );
                ?>
                <div class="recent-post-item">
                    <?php if (!empty($p_thumb)) {
                       ?>
                       <div class="recent-post-item-thumb">
                            <img src="<?php echo $p_thumb ?>">
                       </div>
                       <?php
                    } ?>
                    <a href="<?php echo $p_link; ?>" class="recent-post-item-link">
                        <?php echo $p_title; ?>
                    </a>
                </div>
                <?php
            }
            echo '</div>'; 

            wp_reset_postdata(  );
        }


    }
}

