<?php
/**
 * Theme File Download Widget
 * @package Geroz
 * @since 1.0.0
 */

if (!defined('ABSPATH')) {
    exit(); //exit if access directly
}
// Control core classes for avoid errors
if (class_exists('CSF')) {


    // Create a About Widget
    CSF::createWidget('geroz_file_download_widget', array(
        'title' => esc_html__('Geroz: File Download', 'geroz-core'),
        'classname' => 'geroz-widget-file-download',
        'description' => esc_html__('Display File Download widget', 'geroz-core'),
        'fields' => array(
            array(
                'id' => 'title',
                'type' => 'text',
                'title' => esc_html__('Title', 'Geroz-core'),
                'default' => esc_html__('Download', 'geroz-core')
            ),

            array(
                'id' => 'geroz-file-download-repeater',
                'type' => 'repeater',
                'title' => esc_html__('File Download', 'geroz-core'),
                'fields' => array(
                    array(
                        'id' => 'geroz-file-download',
                        'type' => 'media',
                        'title' => esc_html__('File', 'geroz-core'),
                    ),
                    array(
                        'id' => 'geroz-file-download-text',
                        'type' => 'text',
                        'title' => esc_html__('File Text', 'geroz-core'),
                        'default' => esc_html__('Company Profile', 'geroz-core')
                    ),

                ),
            ),
        )
    ));


    if (!function_exists('geroz_file_download_widget')) {
        function geroz_file_download_widget($args, $instance)
        {

            echo $args['before_widget'];

            $title = $instance['title'] ?? '';
            $file_download = is_array($instance['geroz-file-download-repeater']) && !empty($instance['geroz-file-download-repeater']) ? $instance['geroz-file-download-repeater'] : [];


            ?>
            <div class="widget_download">
                <h5 class="widget-headline style-01"><?php echo esc_html($title); ?></h5>               
                <ul>
                    <?php
                        foreach ($file_download as $file) {
                            echo '<li class="mb-0 mt-0">
                                <a download href="'.$file['geroz-file-download']['url'].'">
                                    ' . $file['geroz-file-download-text'] . '
                                    <i class="fa fa-angle-double-right"></i>
                                </a>
                            </li>';
                        };
                    ?>
                </ul>
            </div>
            <?php

            echo $args['after_widget'];

        }
    }

}

?>