<?php 

// Check to ensure this file is included in osConcert!
defined('_FEXEC') or die();
		
// -------------------------------------------------------------------------
// Determine the Correct Category ID ($cat) for Nested Categories
// -------------------------------------------------------------------------
if ($plan_id==4) {
	if ($parent_id == 0) 
	{
		$cat = $cPath;
	} else {
		$cat = $cPath;
	}
}elseif ($plan_id==3) {
	if ($parent_id == 0) 
	{
		$cat = $cPath;
	} else {
		$cat = $parent_id;
	}
}elseif ($plan_id==9) {
	if ($parent_id == 0) 
	{
		$cat = $cPath;
	} else {
		$cat = $parent_id;
	}
}

	$sql_order_by = FEATURED_CATEGORIES_ORDERBY;

$is_box_office = (isset($_SESSION['BoxOffice']) && $_SESSION['BoxOffice'] == 999) || 
                 (isset($_SESSION['customer_country_id']) && $_SESSION['customer_country_id'] == 999);

if ($is_box_office) {
    // Include all categories regardless of status
    $nested_categories_query = tep_db_query(
        "SELECT * 
         FROM " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd 
         WHERE c.parent_id = '" . (int)$cat . "' 
           AND c.categories_id = cd.categories_id 
           AND cd.language_id = '" . (int)$FSESSION->languages_id . "' 
         ORDER BY c." . $sql_order_by . " ASC"
    );
} else {
    // Exclude disabled categories
    $nested_categories_query = tep_db_query(
        "SELECT * 
         FROM " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd 
         WHERE c.categories_status > '0' 
           AND c.parent_id = '" . (int)$cat . "' 
           AND c.categories_id = cd.categories_id 
           AND cd.language_id = '" . (int)$FSESSION->languages_id . "' 
         ORDER BY c." . $sql_order_by . " ASC"
    );
}

$num_categories = tep_db_num_rows($nested_categories_query);

if ($num_categories > 0) {

		while ($nested_categories = tep_db_fetch_array($nested_categories_query)) 
		{
			//print_r($nested_categories);
			$categories_venue_id = $nested_categories['venue_id'];
			$categories_status = $nested_categories['categories_status'];
			$heading_name = $nested_categories['categories_name'];	
			$categories_description = $nested_categories['categories_description'];	
			//$categories_description = ltrim(substr($categories_description, 0, 75) . '...'); //Trims and Limits the desc
        	$heading_title = $nested_categories['categories_heading_title'];
			$concert_unix = $nested_categories['concert_date_unix'];
			$heading_venue = $nested_categories['concert_venue'];
			$concert_date = $nested_categories['concert_date'];
			$concert_time = $nested_categories['concert_time'];
			$cPath_new = tep_get_path($nested_categories['categories_id']);
			$cPath_end = $nested_categories['categories_id'];
			$columns = 'col-lg-6 col-md-6 col-sm-6';
			// Format the date and time if they are valid
			if (!empty($concert_date) && strtotime($concert_date)) {
				$formatted_date = format_heading_date($concert_date);
			}
			if (!empty($concert_time) && strtotime($concert_time)) {
				$formatted_time = format_heading_time($concert_time);
			}
				
        // Determine the category image; use a default if not set
        if (empty($nested_categories['categories_image']) || $nested_categories['categories_image'] == 'NULL') {
            $cat_image = "theatre.png";
        } else {
            $cat_image = $nested_categories['categories_image'];
        }

        // Set button style based on configuration
        $button = (USE_CIRCLE_BUTTONS == 'yes') ? "btn btn-category-circle" : "btn btn-primary";
		
		 if ($categories_venue_id == 'SOLD') 
			{
			$select=SOLD_OUT;
			$url = '<button class="btn btn-primary pull-right disabled">'.SOLD_OUT.'</button>';
			
			}else{
			$select=TEXT_SELECT_TICKETS;
			$url = '<a class="pull-right" href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">
			' . tep_template_image_button('', $select) . '</a>';							
			}

        // ---------------------------------------------------------------------
        // Render Nested Category Item
        // Two display modes: Button-style or Detailed Menu
        // ---------------------------------------------------------------------
        if (NESTED_CATEGORY_BUTTONS == 'yes') {
            // Button style layout
            $categories_content .= '
                <div class="col-lg-3 col-md-4 col-sm-6" style="padding:10px;text-align:center;">
                    <div class="portfolio-item">
                        <div class="portfolio-overlay">
                            <div class="portfolio-info">
                                <a class="' . $button . '" href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">
                                    ' . $heading_name . '
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            ';
        } 
		
		else {


					$categories_content .= '
					<div id="sub_listing" class="col-lg-12 col-md-12">
					<div class="row p-3 effects">
					<div class="col-md-2">
					' . tep_image(DIR_WS_IMAGES . 'small/' . $cat_image, '', '', '') . '
					</div>
					<div class="col-md-4">
				
					<h4>'.TEXT_DATE.' '.$formatted_date.'</h4>
					<h4>'.TEXT_TIME.' '.$formatted_time.'</h4>

					</div>
					<div class="col-md-4">
					<h4>'.$heading_name.'</h4>
					<h4>'.$heading_venue.'</h4>

					</div>
					<div class="col-md-2">'.$url.'</div>
					</div>
					</div>
					';
		
			}
		}
	}

// -------------------------------------------------------------------------

//alt:  
//<h4>'.TEXT_DATE.' '.$heading_date.'</h4>
//<h4>'.TEXT_TIME.' '.$heading_time.'</h4>


// Optionally disable the overlay style if configured
// -------------------------------------------------------------------------
if (DISABLE_OVERLAY == 'yes') {
    echo '<style>
            #featured .featured-overlay {
                opacity: 0;
            }
          </style>';
}

// -------------------------------------------------------------------------
// Render the Nested Categories Section if enabled
// -------------------------------------------------------------------------
	//if (SHOW_NESTED_CATEGORIES == 'true' || ($parent_id == 0 && SHOW_NESTED_CATEGORIES == 'false')) {
	?>
	<section id="featured" data-aos="fade-up">
	<div class="container">
	<div class="row no-gutters">
	<?php 
	//we show the menu
	echo $categories_content; 
	?>
	</div>
	</div>
	</section>
	<?php 
	//}
	
// -------------------------------------------------------------------------
// Include Featured Categories Module if enabled
// -------------------------------------------------------------------------
if (SHOW_FEATURED_CATEGORIES == 'true') {
    echo '<br><br>';
	require_once(DIR_WS_MODULES . FILENAME_FEATURED_CATEGORIES);
}
?>
<br>
