<?php
	if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    if(!function_exists("ticketmachine_widget_event_list")) {
        function ticketmachine_widget_event_list ( $atts, $isWidget ) {
            global $ticketmachine_globals, $ticketmachine_api;
            $ticketmachine_output = "";
            unset($atts['page']);
            unset($atts['widget']);

            $params = $atts;

            if(empty($params['per_page'])) {
                $params["per_page"] = 30;
            }
            
            if(empty($params["pg"])) {
                $params["pg"] = 1;
            }
            
            if(empty($params['approved'])) {
                $params = ticketmachine_array_push_assoc($params, "approved", 1);
            }
            $response = ticketmachine_tmapi_events($params);
            $events = $response->result;
            $meta = $response->meta;

            if(!isset($atts['show_image'])){
                $atts['show_image'] = 1;
            }
            if(!isset($atts['show_description'])){
                $atts['show_description'] = 1;
            }
            if(!isset($atts['show_date'])){
                $atts['show_date'] = 1;
            }
            if(!isset($atts['pagination'])){
                $atts['pagination'] = 1;
            }

            if($isWidget == 1){
                $ticketmachine_output .= "<div class='row'><div class='row ticketmachine_widget_event_list'>";
            }

            if(empty($events)) {	
                $ticketmachine_output .= "<div class='col-12 text-center mt-1'>";
                    $ticketmachine_output .= ticketmachine_alert(esc_html__("No events could be found", "ticketmachine-event-manager"), "error");
                $ticketmachine_output .= "</div>";
                
            }else{
                    
                $ticketmachine_output .= '<div class="col-12">';
                    $ticketmachine_output .= '<ul class="list-unstyled mx-0">';

                    foreach($events as $event){
                        $event = (object)$event;

                        if(empty($atts['image_ratio'])) {
                            $image_ratio = $ticketmachine_globals->event_list_image_ratio;
                        }else{
                            $image_ratio = $atts['image_ratio'];
                        }

                        if(empty($image_ratio)) {
                            $image_ratio = "16:9";
                        }

                        $image_ratio = str_replace(" ", "", $image_ratio);
                        $image_ratio = str_replace("/", "-", $image_ratio);
                        $image_ratio = str_replace(":", "-", $image_ratio);

                        $ratio_parts = explode('-', $image_ratio);
                        $ratio_w = (int)$ratio_parts[0];
                        $ratio_h = isset($ratio_parts[1]) ? (int)$ratio_parts[1] : $ratio_w;
                        $width = 200;
                        $height = round(($width / $ratio_w) * $ratio_h);

                        $img_src = 'https://nu.ticketmachine.de/ajax/event/' . esc_attr($event->id) . '/image?width=' . $width . '&height=' . $height . '&gravity=cover%3Acentre';

                        $event->has_location = 0;
                        $event->has_location_link = 0;
                        if(!empty($event->ev_location_name) || !empty($event->event_location['city']) || !empty($event->event_location['street']) || !empty($event->event_location['zip']) || !empty($event->event_location['house_number'])){
                            $event->has_location = 1;
                            $event->has_location_link = 1;
                        }

                        if(empty($event->event_location['city']) || empty($event->event_location['street']) || empty($event->event_location['house_number'])){
                            $event->has_location_link = 0;
                        }
        
                        if(empty($event->state['sale_active'])){
                            $event->link = '/' . esc_html($ticketmachine_globals->event_slug) .'/?id=' . esc_html($event->id);
                        }else{
                            $event->link = esc_html($ticketmachine_globals->webshop_url) .'/events/unseated/select_unseated?event_id=' . esc_html($event->id);
                        }

                        $ticketmachine_output .= '<li class="media mx-0 mt-2 p-3">';

                        if(isset($atts['show_image']) && $atts['show_image'] > 0){
                            $ticketmachine_output .= '<div><a class="me-3 media-img ratio-' . $image_ratio . '" href="' . $event->link . '" style="background-image:url( ' . $img_src . ' )"></a></div>';
                        }
                                            
                            $ticketmachine_output .= '<div class="media-body">';

                            if(!empty($event->rules["badge"])) {
                                $ticketmachine_output .= '<div class="badge bg-danger m-0">'. esc_html($event->rules["badge"]) .'</div>';
                            }
                            
                            $ticketmachine_output .= '<div class="mb-1 h5"><a class="tm-list-title" href="' . $event->link . '">' . $event->ev_name . '</a></div>';

                            if(isset($atts['show_date']) && $atts['show_date'] > 0){
                                if(ticketmachine_i18n_date("H:i", $event->ev_date) == "00:00" && ticketmachine_i18n_date("H:i", $event->endtime) == "23:59") {
                                    $dateoutput = __("Entire Day", "ticketmachine-event-manager");
                                }else{
                                    $dateoutput = ticketmachine_i18n_date("H:i", $event->ev_date);
                                }

                                if(isset($event->endtime) && ticketmachine_i18n_date("d.m.Y", $event->ev_date) != ticketmachine_i18n_date("d.m.Y", $event->endtime) ) {
                                    $ticketmachine_output .= '
                                    <div class="card-meta-tag"><i class="far fa-calendar-alt tm-icon" aria-hidden="true"></i> &nbsp;'. ticketmachine_i18n_date("d.m.Y", $event->ev_date) .' - '. ticketmachine_i18n_date("d.m.Y", $event->endtime) .'</div> 
                                    <div class="card-meta-tag"><i class="far fa-clock tm-icon" aria-hidden="true"></i> &nbsp;'. $dateoutput .'</div>';
                                }else{
                                    $ticketmachine_output .= '
                                    <div class="card-meta-tag"><i class="far fa-calendar-alt tm-icon" aria-hidden="true"></i> &nbsp;'. ticketmachine_i18n_date("d.m.Y", $event->ev_date) .'</div> 
                                    <div class="card-meta-tag"><i class="far fa-clock tm-icon" aria-hidden="true"></i> &nbsp;'. $dateoutput .'</div>';
                                }

                            }

                            if(isset($event->has_location) && $event->has_location == 1){   
                                if(empty($event->ev_location_name)) {
                                    $event_location = $event->event_location["street"] . " " . $event->event_location["house_number"];
                                }else{
                                    $event_location = $event->ev_location_name;
                                }
                                $ticketmachine_output .= '<div class="card-meta-tag"><i class="fas fa-map-marker-alt tm-icon"></i> &nbsp; ';
                                    if(isset($event->has_location_link) && $event->has_location_link == 1){        
                                        $ticketmachine_output .= '<a aria-label="' . esc_attr__("Event Location", 'ticketmachine-event-manager') . ': ' . esc_html($event->ev_location_name) . '" href="' . esc_url($ticketmachine_globals->map_query_url . urlencode($event->ev_location_name . " " .$event->event_location["street"] . " " . $event->event_location["house_number"] . " " . $event->event_location["zip"] . " " . $event->event_location["city"] . " " . $event->event_location["country"] )) . '" target="_blank" title="' . esc_attr__("Event Location", 'ticketmachine-event-manager') . ': ' . esc_html($event_location) . '">' . esc_html($event_location) . '</a>';
                                    }else{
                                        $ticketmachine_output .= $event_location;
                                    }
                                $ticketmachine_output .= '</div>';
                            }
                            
                            if(isset($atts['show_description']) && $atts['show_description'] > 0){
                                if(empty($atts['description_length'])){
                                    $atts['description_length'] = 15;
                                }
                                if(isset($atts['description_length'])){
                                    $ticketmachine_output .= '<div class="mt-1">' . esc_html(wp_trim_words(wp_strip_all_tags($event->ev_description), $atts['description_length'], "...")) . '</div>';
                                }
                            }

                        $ticketmachine_output .= '</div>
                                    </li>';
                    }

                    if(isset($atts['show_more']) && $atts['show_more'] > 0){
                        $ticketmachine_output .= '<li class="media"><a href="/' . esc_html($ticketmachine_globals->events_slug) . '">' . esc_html__("Show all events", "ticketmachine-event-manager") . '</a></li>';
                    }

                    $ticketmachine_output .= '</ul>';
                    
					if(isset($atts['pagination']) && $atts['pagination'] > 0) {
                    	$ticketmachine_output .= ticketmachine_pagination($meta, $params);
					}

                $ticketmachine_output .= '</div>';

            }

            if($isWidget == 1){
                $ticketmachine_output .= "</div></div>";
            }

            return $ticketmachine_output;
        }
    }

?>