<?php 
if (is_singular('downloads')) {
 get_header(); 
    include get_template_directory() . '/inc/menus/menu.php'; 

    $post_id = get_the_ID();
            
    $file_url       =  get_post_meta($post_id, '_file_url', true);
    $file_size      =  get_post_meta($post_id, '_file_size', true);
    $downloadCount = get_post_meta($post_id, '_download_count', true);
	$link		   = get_post_meta($post_id, '_external_file', true);
	$publisherDate = get_post_meta($post_id, '_publication_date', true);
	$publisherName = get_post_meta($post_id, '_publication_publisher', true);
	$publisherAuthors = get_post_meta($post_id, '_publication_authors', true);
	$publisherIsban = get_post_meta($post_id, '_publication_isbn', true);
	$publisherDoi = get_post_meta($post_id, '_publication_doi', true);
	$publisherPages = get_post_meta($post_id, '_publication_pages', true);
	$publisherLanguage = get_post_meta($post_id, '_publication_language', true);
    
    // Get the file size
    $file_size = '';
    if ($file_url) {
        $headers = get_headers($file_url, 1);
        $file_size = isset($headers['Content-Length']) ? size_format($headers['Content-Length']) : '';
        }
    ?>
	<main role="main">
		<?php $backgroundImg =  get_template_directory_uri()."/assets/images/Kwe2-Kwettu-Africa1.jpg"; ?>
		<section class="header-programs" style="background: linear-gradient(to top, rgba(255, 255, 255, 1), rgba(255, 255, 255, 1)), url('<?php echo get_template_directory_uri()."/assets/images/Kwe2-Kwettu-Africa1.jpg"; ?>') no-repeat;background-size: cover;background-position: center center; background-blend-mode: color;">
		    <div class="overlay">
		    <div class="page-title">
        		  <div class="container">
                    <div class="row">
						<div class="col-md-7" data-aos="fade-up"> 
							<h1 class="entry-title"><?php the_title(); ?></h1>
							<p><?php the_excerpt(); ?></p>
							<?php custom_breadcrumb(); ?>
						</div>
						<div class="col-md-5 d-none d-md-block">&nbsp;</div>
                    </div>
                </div>
		    </div>
		    </div>
        </section><!-- Header Section !-->
		<section>
		    <div class="container-xxl page-content2">
		        <div class="post-content">
		            <div class="row justify-content-between pub-single">
		            <div class="col-lg-5 col-md-5 col-sm-5" data-aos="fade up">
		        <?php if (have_posts()): while (have_posts()) : the_post(); ?>
			<!-- article -->
			<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
		            <!-- post thumbnail -->
        			<div class="post-thumbnail">
        			<?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
        				<?php the_post_thumbnail(); // Fullsize image for the single post ?>
        			<?php endif; ?>
        			</div>
		        </div>
		        <div class="col-lg-7 col-md-7 col-sm-7">
		            <div class="about-side-pub">
		                <!-- /post thumbnail -->
                <h1 class="entry-title"><?php the_title(); ?></h1>
				<h4 class="pub-summ">Overview</h4>
				<div class="pub-desc"><?php the_content(); ?></div>
				<hr />
				<div class="pub-download">
					<?php if($file_url):?>
						<a href="<?= $file_url; ?>" target="_blank" onclick="updateDownloadCount(<?= $post_id; ?>)"> Download</button></a>
					<?php else:?>
						<a href="<?= $link; ?>" target="_blank"> Visit KOHA</button></a>
					<?php endif;?>
				</div> 
				<div class="pub-stats py-3"> <i class="bi bi-bar-chart-fill"></i> <small>No. Of Downloads: <?= $downloadCount; ?></small> <i class="bi bi-file-earmark-pdf"></i> <small>File Size: <?= $file_size; ?> </small> </div>   
						
						<!-- Publication Details  -->
						<div class="pub-meta-container mt-4">
							<h4 class="pub-meta-title">Publication Details</h4>
							
							<div class="row">
								<!-- Left Column -->
								<div class="col-md-6">
									<div class="pub-meta-item">
										<span class="pub-meta-label"><i class="bi bi-person-fill"></i> Author(s):</span>
										<span class="pub-meta-value"><?= $publisherAuthors ? esc_html($publisherAuthors) : 'N/A'; ?></span>
									</div>
									
									<div class="pub-meta-item">
										<span class="pub-meta-label"><i class="bi bi-building"></i> Publisher:</span>
										<span class="pub-meta-value"><?= $publisherName ? esc_html($publisherName) : 'N/A'; ?></span>
									</div>
									
									<div class="pub-meta-item">
										<span class="pub-meta-label"><i class="bi bi-calendar-event"></i> Published:</span>
										<span class="pub-meta-value">
											<?= $publisherDate ? esc_html(date('F j, Y', strtotime($publisherDate))) : 'N/A'; ?>
										</span>
									</div>
									
									<div class="pub-meta-item">
										<span class="pub-meta-label"><i class="bi bi-translate"></i> Language:</span>
										<span class="pub-meta-value"><?= $publisherLanguage ? esc_html($publisherLanguage) : 'N/A'; ?></span>
									</div>
								</div>
								
								<!-- Right Column -->
								<div class="col-md-6">
									<?php if($publisherIsban): ?>
									<div class="pub-meta-item">
										<span class="pub-meta-label"><i class="bi bi-bookmark-fill"></i> ISBN/ISSN:</span>
										<span class="pub-meta-value"><?= esc_html($publisherIsban); ?></span>
									</div>
									<?php endif; ?>
									
									<?php if($publisherDoi): ?>
									<div class="pub-meta-item">
										<span class="pub-meta-label"><i class="bi bi-fingerprint"></i> DOI:</span>
										<span class="pub-meta-value">
											<a href="https://doi.org/<?= esc_attr($publisherDoi); ?>" target="_blank">
												<?= esc_html($publisherDoi); ?>
											</a>
										</span>
									</div>
									<?php endif; ?>
									
									<?php if($publisherPages): ?>
									<div class="pub-meta-item">
										<span class="pub-meta-label"><i class="bi bi-file-text-fill"></i> Pages:</span>
										<span class="pub-meta-value"><?= esc_html($publisherPages); ?></span>
									</div>
									<?php endif; ?>
									
									<div class="pub-meta-item">
										<span class="pub-meta-label"><i class="bi bi-file-earmark-arrow-down-fill"></i> File Size:</span>
										<span class="pub-meta-value"><?= $file_size ? esc_html($file_size) : 'N/A'; ?></span>
									</div>
								</div>
							</div>
						</div><!-- Publication details !-->
						<!-- social media sharing !-->
						<div class="d-flex justify-content-start bd-highlight mb-5">
                            <div class="bd-highlight p-1">
                            <small class="text-muted">share this publication</small>
                            </div>
                            <div class="bd-highlight p-1">
                                <a class="btn btn-sm btn-facebook" href="https://www.facebook.com/sharer/sharer.php?u=<?= esc_url(get_permalink()); ?>" target="_blank" rel="noopener noreferrer" title="Facebook">
                                <small>Share on <i class="bi bi-facebook"></i> </small>
                                </a>
                            </div><!-- Facebook !-->
                            <div class="bd-highlight p-1">
                                <a class="btn btn-sm btn-twitter" href="https://twitter.com/intent/tweet?url=<?= esc_url(get_permalink()); ?>&text=<?= the_title(); ?>" target="_blank" rel="noopener noreferrer" title="Twitter">
                                <small> Share on <i class="bi bi-twitter-x"></i></small>
                                </a>
                            </div><!-- Twitter !-->
                            <div class="bd-highlight p-1">
                                <a class="btn btn-sm btn-whatsapp" href="whatsapp://send?text=Check out this product: <?= the_title(); ?> - <?= esc_url(get_permalink());?>" data-action="share/whatsapp/share" title="WhatsApp">
                                <small><i class="bi bi-whatsapp"></i> WhatsApp</small>
                                </a>
                            </div><!-- WhatsApp !-->
                            <div class="bd-highlight p-1">
                                <?php do_action('back_button'); ?>
                            </div><!-- Back button !-->
                        </div><!-- social sharing !-->
				<!-- End of Social Media sharing !-->
				
		            </div>
		        </div>
		        </div>
		        </div>
		        
				<?php edit_post_link(); ?>

			</article>
			<!-- /article -->

		<?php endwhile; ?>

		<?php else: ?>

			<!-- article -->
			<article>

				<h2><?php _e( 'Sorry, nothing to display.', 'kipya' ); ?></h2>

			</article>
			<!-- /article -->

		<?php endif; ?>
		    </div>
		</section>
		
		<!-- /section -->
	<script>
    function updateDownloadCount(post_id) {
        jQuery.ajax({
            type: 'POST',
            url: '<?php echo admin_url("admin-ajax.php"); ?>',
            data: {
                action: 'update_download_count',
                post_id: post_id
            },
            success: function(response) {
                // Optionally handle success response
                console.log('Download count updated successfully.');
            }
        });
    }
</script>
 </main>
 <?php
    } else {
    // If the post type is not "downloads", you can display a message or redirect to another page
    echo '<p>Sorry, this page is only for downloads.</p>';
    }

 get_footer(); ?>