/*
Theme Name: Avada Child
Description: Child theme for Avada theme
Author: ThemeFusion
Author URI: https://theme-fusion.com
Template: Avada
Version: 1.0.0
Text Domain:  Avada
*/

<?php
/**
 * Shortcode: [tec_add_to_calendar label="Toevoegen aan kalender"]
 * Werkt op single event pagina's van The Events Calendar.
 */

add_shortcode('tec_add_to_calendar', function($atts) {
    if (!function_exists('tribe_get_gcal_link') || !function_exists('tribe_get_single_ical_link')) {
        return '';
    }

    if (!is_singular('tribe_events')) {
        return '';
    }

    $atts = shortcode_atts([
        'label' => 'Toevoegen aan kalender',
    ], $atts, 'tec_add_to_calendar');

    $event_id = get_the_ID();
    if (!$event_id) {
        return '';
    }

    $title = get_the_title($event_id);
    $description = wp_strip_all_tags(get_post_field('post_content', $event_id));
    $description = trim(preg_replace('/\s+/', ' ', $description));

    $venue = function_exists('tribe_get_venue') ? tribe_get_venue($event_id) : '';
    $location = $venue ? $venue : '';

    // ISO-8601 formaat voor Outlook-links
    $start = function_exists('tribe_get_start_date') ? tribe_get_start_date($event_id, false, 'c') : '';
    $end   = function_exists('tribe_get_end_date') ? tribe_get_end_date($event_id, false, 'c') : '';

    // Officiële TEC links
    $google_link = tribe_get_gcal_link($event_id);
    $ical_link   = tribe_get_single_ical_link($event_id);

    // Outlook 365 / Live
    $outlook_params = [
        'path'     => '/calendar/action/compose',
        'rrv'      => 'addevent',
        'subject'  => $title,
        'startdt'  => $start,
        'enddt'    => $end,
        'body'     => $description,
        'location' => $location,
    ];

    $outlook_365_link  = 'https://outlook.office.com/calendar/action/compose?' . http_build_query($outlook_params);
    $outlook_live_link = 'https://outlook.live.com/calendar/action/compose?' . http_build_query($outlook_params);

    ob_start();
    ?>
    <div class="my-tec-atc">
        <button class="my-tec-atc__toggle" type="button" aria-expanded="false">
            <?php echo esc_html($atts['label']); ?>
            <span class="my-tec-atc__caret">▾</span>
        </button>

        <div class="my-tec-atc__menu">
            <a href="<?php echo esc_url($google_link); ?>" target="_blank" rel="noopener">Google Calendar</a>
            <a href="<?php echo esc_url($ical_link); ?>">iCalendar</a>
            <a href="<?php echo esc_url($outlook_365_link); ?>" target="_blank" rel="noopener">Outlook 365</a>
            <a href="<?php echo esc_url($outlook_live_link); ?>" target="_blank" rel="noopener">Outlook Live</a>
        </div>
    </div>
    <?php
    return ob_get_clean();
});