<?php

/* Template Name: NFT Items */

get_header();
?>

<?php

$nft_items_page_title_show = ethrik_get_option('nft_items_page_title_show', true);
$nft_items_title_text = ethrik_get_option('nft_items_title_text');
$brdcrmb_nft_items_text = ethrik_get_option('brdcrmb_nft_items_text', true);

$nft_price = ethrik_get_option('nft_price', true);
$show_bid_btn = ethrik_get_option('show_bid_btn', true);
$bid_btn_text = ethrik_get_option('bid_btn_text');

?>

<!-- Breadcrumb -->
<div class="theme-breadcrumb__Wrapper theme-breacrumb-area">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-12">

                <?php if ($nft_items_page_title_show == true) : ?>
                    <h1 class="theme-breacrumb-title">
                        <?php esc_html_e($nft_items_title_text, 'ethrik'); ?>
                    </h1>
                <?php endif; ?>

                <div class="breadcrumb-nav-top">
                    <ul>
                        <li class="breadcrumb-home-menu"><a href="<?php echo esc_url(home_url('/')); ?>"><?php esc_html_e('Home', 'ethrik'); ?></a></li>
                        <li class="breadcrumb-item-menu"><?php esc_html_e($brdcrmb_nft_items_text, 'ethrik'); ?></li>
                    </ul>
                </div>

            </div>
        </div>
    </div>
</div>
<!-- Breadcrumb End -->

<section id="nft-page" class="nft-page py-5">
    <div class="container">
        <div class="row">

            <?php
            $args = array(
                'post_type' => 'nft',
                'post_status' => 'publish',
                'order' => 'DESC',
                'posts_per_page' => 10,
                'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
            );

            $query = new WP_Query($args);
            if ($query->have_posts()) {
                while ($query->have_posts()) {
                    $query->the_post();

                    $nft_single_item_meta = get_post_meta(get_the_ID(), 'ethrik_nft_single_meta', true);

                    if (array_key_exists('nft-price-eth', $nft_single_item_meta)) {
                        $nft_item_price_eth = $nft_single_item_meta['nft-price-eth'];
                    } else {
                        $nft_item_price_eth = '';
                    }
            ?>

                    <!-- NFT Item Single -->
                    <div class="col-lg-3 col-md-6 col-sm-12">
                        <div class="nft-item-single">
                            <div class="product-media">
                                <a href="<?php the_permalink(); ?>">    
                                    <img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title_attribute(); ?>">
                                </a>
                            </div>
                            <div class="product-content">
                                <h2 class="product-title">
                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                </h2>
                                <div class="nft-grid-inner-bottom d-flex justify-content-between align-items-center mt-3">
                                    <?php if ($nft_price == true) : ?>
                                        <h1 class="current-price-eth">
                                            <?php echo esc_attr($nft_single_item_meta['nft-price-eth']); ?>
                                        </h1>
                                    <?php endif; ?>
                                    <?php if ($show_bid_btn == true) : ?>
                                        <div class="nft-bottom-right">
                                            <a href="<?php the_permalink(); ?>" class="discover-nft-btn"><?php esc_html_e($bid_btn_text, 'ethrik'); ?></a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                <?php
                }
            } else {
                ?>

                <div class="col-lg-12">
                    <div class="error-page text-center">
                        <img src="<?php echo ETHRIK_IMG . '/empty-nft.jpg' ?>" alt="empty-nft-image">
                        <div class="error-message">
                            <h3><?php esc_html_e('Sorry!...No Items Found!', 'ethrik'); ?></h3>
                        </div>
                        <div class="error-body">
                            <a href="<?php echo esc_url(home_url('/')); ?>" class="error-btn-custom"><i class="icofont-home"></i> <?php esc_html_e('Go Home Page', 'ethrik'); ?></a>
                        </div>
                    </div>
                </div>

            <?php } ?>

            <!-- Pagination -->
            <div class="col-lg-12">
                <nav class="navigation pagination">
                    <?php
                    $total_pages = $query->max_num_pages;
                    if ($total_pages > 1) {
                        $current_page = max(1, get_query_var('paged'));
                        echo paginate_links(array(
                            'base' => get_pagenum_link(1) . '%_%',
                            'format' => '/page/%#%',
                            'current' => $current_page,
                            'total' => $total_pages,
                            'prev_text'    => __('<i class="icofont-long-arrow-left"></i>'),
                            'next_text'    => __('<i class="icofont-long-arrow-right"></i>'),
                        ));
                    }
                    ?>
                </nav>
            </div>

        </div>
    </div>
</section>

<?php get_footer(); ?>