<?php
/**
 * Theme File Download Widget
 * @package Pubzi
 * @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('pubzi_file_download_widget', array(
        'title' => esc_html__('Pubzi: File Download', 'pubzi-core'),
        'classname' => 'pubzi-widget-file-download',
        'description' => esc_html__('Display File Download widget', 'pubzi-core'),
        'fields' => array(
            array(
                'id' => 'title',
                'type' => 'text',
                'title' => esc_html__('Title', 'Pubzi-core'),
                'default' => esc_html__('Download', 'pubzi-core')
            ),

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

                ),
            ),
        )
    ));


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

            echo $args['before_widget'];

            $title = $instance['title'] ?? '';
            $file_download = is_array($instance['pubzi-file-download-repeater']) && !empty($instance['pubzi-file-download-repeater']) ? $instance['pubzi-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['pubzi-file-download']['url'].'">
                                    ' . $file['pubzi-file-download-text'] . '
                                    <i class="fa fa-angle-double-right"></i>
                                </a>
                            </li>';
                        };
                    ?>
                </ul>
            </div>
            <?php

            echo $args['after_widget'];

        }
    }

}

?>