<?php
global $wpdb, $post, $wp_query, $opt_themes;  
?>

	<!-- moddroid -->
	<div id="accordion" class="accordion-container mb-2 ">
	
	<?php
	// Get the downloads data - fallback to repeatable_fields if group_downloads_links is empty
	/* $downloads_link = get_post_meta(get_the_ID(), 'group_downloads_links', true);
	if (empty($downloads_link)) {
		$downloads_link = get_post_meta(get_the_ID(), 'repeatable_fields', true);
	} */

	$downloads_link = get_post_meta(get_the_ID(), 'repeatable_fields', true);
	
	if (!empty($downloads_link) && is_array($downloads_link)) {
		$i = 1;
		foreach ($downloads_link as $key => $dw) {
			// Extract file details with proper fallbacks
			$file_link = !empty($dw['url']) ? $dw['url'] : (!empty($dw['links']) ? $dw['links'] : '');
			$file_name = !empty($dw['name']) ? $dw['name'] : (!empty($dw['title']) ? $dw['title'] : 'Download File');
			$file_size = !empty($dw['sizes']) ? $dw['sizes'] : (!empty($dw['sizes1']) ? $dw['sizes1'] : (!empty($dw['size']) ? $dw['size'] : ''));
			$file_type = !empty($dw['tipes']) ? $dw['tipes'] : (!empty($dw['tipe']) ? $dw['tipe'] : '');
			$file_note = !empty($dw['notes']) ? $dw['notes'] : '';

			// Skip if no valid link
			if (empty($file_link)) continue; 
			?>

			<!-- Download item <?php echo $i; ?> -->
			<h4 class="accordion-title js-accordion-title h6 rounded text-white bg-primary py-2 px-3">
				<?php echo esc_html($file_name); ?>
			</h4>

			<div class="accordion-content p-3 border rounded mb-3">
				<?php if ($file_note) : ?>
					<div class="small bg-light p-2 mb-3">
						<?php echo wp_kses_post($file_note); ?>
					</div>
				<?php endif; ?>
 
				<a class="btn btn-secondary btn-block text-left" 
				   href="<?php echo esc_url(get_permalink() . '/file/?link=' . ($key + 1)); ?>" 
				   target="_self" 
				   >
					<?php if (!empty($opt_themes['svg_icon_downloadx'])) echo $opt_themes['svg_icon_downloadx']; ?>
					<span class="align-middle">
						<?php echo esc_html(!empty($opt_themes['exthemes_Download']) ? $opt_themes['exthemes_Download'] : 'Download'); ?>
						<?php if ($file_type) echo ' ' . esc_html($file_type); ?>
						<?php if ($file_size) echo ' (' . esc_html($file_size) . ')'; ?>
					</span>
				</a>
			</div>

			<?php $i++;
		}
	}  else {
		// Optional: Show a message if no downloads exist
		echo '<p class="text-muted">No download links available.</p>';
	}
	?>
	</div>  
	
	<style>
	.bg-primary {
	  background-color: var(--color_button)!important;
	}
	</style>