<?php

function display_file_download_popup() {

    $id = get_the_ID();
    
	$downloadable_catalogue = get_field('downloadable_catalogue', $id);
	$downloadable_spec = get_field('downloadable_spec', $id);
 
	
    $details = get_post_type( $id );

    if( is_tax() ){
        $object = get_queried_object();
        $id = $object->term_id;
        $details = $object->taxonomy;
        $downloadable_catalogue = get_field('downloadable_catalogue', $object->taxonomy . '_' . $id);
        $downloadable_spec = get_field('downloadable_davani_catalog', $object->taxonomy . '_' . $id);
		    $downloadable_catalogue_name_button = get_field('downloadable_catalogue_name_button', $object->taxonomy . '_' . $id); 
	$downloadable_davani_catalog_name_button = get_field('downloadable_davani_catalog_name_button', $object->taxonomy . '_' . $id);
    }

    $classes = 'downlaod-data';
    if( is_single( $id ) ){
        $classes .= ' request';
    }

	ob_start();
	?>
    <div class="button-download cc">
        <?php if( $downloadable_catalogue ): 
                $label = !empty($downloadable_catalogue_name_button)? $downloadable_catalogue_name_button : $downloadable_catalogue['title'];
                $file = !empty($downloadable_catalogue['url'])? $downloadable_catalogue['url'] : '';
	
            ?>
            <a 
                class="catalogue <?php echo $classes; ?>" href="#" 
                data-text="<?php echo $label; ?>"
                data-type="catalogue"
                data-details="<?php echo $details; ?>" 
                data-id="<?php echo $id; ?>" 
                data-file="<?php echo $file; ?>"
                data-form-id="4"
            >
                <span><?php echo $label; ?></span>
            </a>
        <?php endif; ?>
        <?php if( $downloadable_spec ): 
            $label = !empty($downloadable_davani_catalog_name_button)? $downloadable_davani_catalog_name_button : $downloadable_spec['title'];
            $file = !empty($downloadable_spec['url'])? $downloadable_spec['url'] : '';
            ?>
            <a 
                class="davani-catalog <?php echo $classes; ?>" href="#"
                data-text="<?php echo $label; ?>"
                data-type="spec"
                data-details="<?php echo $details; ?>" 
                data-id="<?php echo $id; ?>"
                data-file="<?php echo $file; ?>" 
                data-form-id="4"
            >
                <span><?php echo $label; ?></span>
            </a>
        <?php endif; ?>
    </div>
	<!-- Popup -->
    <div class="modal modal-popup">
        <div id="file-download-popup" class="file-download-popup">
            <h3 id="heading-popup" class="heading-popup">Download</h3>
            <div id="form-container" class="form-container">
                <?php echo do_shortcode('[gravityform id="4" title="false" ajax="true"]'); ?>
            </div>
            <button id="close-popup" class="close-popup">
                <span aria-hidden="true">×</span>
            </button>
        </div>

        <!-- Overlay -->
        <div id="popup-overlay" class="file-download-popup-overlay"></div>
    </div>

	<?php

	return ob_get_clean();
}
add_shortcode('file_download_popup', 'display_file_download_popup');


// Custom Form Download
add_filter( 'gform_confirmation', 'gform_confirmation_render_download_button', 10, 4 );
function gform_confirmation_render_download_button( $confirmation, $form, $entry, $ajax ) {

    if( $form['id'] == '4' ) {
        $file = rgar( $entry, '14' );
        $confirmation .= '<a href="'.$file.'" download class="cta-button download-button"><span>Download</span></a>';
    }

    return $confirmation;
}
