<?php
/*
    osConcert Seat Booking Software
    https://www.osconcert.com
    Copyright (c) 2024 osConcert

    Released under the GNU General Public License 
*/    
// Check to ensure this file is included in osConcert!
defined('_FEXEC') or die();

// Get the search term from the query string
$search_term = tep_db_input(trim($_GET['search_term']));

// Search the categories_description table
$search_result_query = tep_db_query("SELECT *
                                    FROM " . TABLE_CATEGORIES . " c
                                    JOIN " . TABLE_CATEGORIES_DESCRIPTION . " cd ON c.categories_id = cd.categories_id
                                    WHERE c.categories_status > 0 
                                    AND cd.categories_name LIKE '%" . tep_db_input($search_term) . "%'
                                    AND c.categories_id = cd.categories_id
                                    AND cd.language_id = '" . (int)$FSESSION->languages_id . "'");

// Display the results in the same Bootstrap 5.3 template format
$search_results_content = '';
$num_search_result = tep_db_num_rows($search_result_query);

if ($num_search_result > 0) {
    while ($search_result = tep_db_fetch_array($search_result_query)) {
        $categories_name = $search_result['categories_name'];
        $categories_description = $search_result['categories_description'];
        $cPath_new = tep_get_path($search_result['categories_id']);
        $cPath_end = $search_result['categories_id'];
        $categories_heading_title = $search_result["categories_heading_title"];
        $heading_venue = $search_result['concert_venue'];
        $categories_time = $search_result['concert_time'];
        $categories_GA = $search_result['categories_GA'];
        $categories_quantity_remaining = $search_result['categories_quantity_remaining'];
        $categories_date = $search_result['concert_date'];
        $cat_image = $search_result['categories_image']; // Replace with the actual logic for fetching images
        //require(DIR_WS_FUNCTIONS.'/date_formats.php');

        if (!strtotime($categories_date)) {
            // it's not in date format
            $heading_date = $categories_date; // gives concert_date
        }

        $search_results_content .= '
        <div class="col-6 col-md-3">
            <div class="featured-item">
                <a href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $search_result['categories_id']) . '">' . 
                tep_image(DIR_WS_IMAGES . $cat_image, $search_result['categories_name'], '', '') . 
                '<div class="featured-overlay">
                    <div class="featured-info">
                        <h3>' . $search_result['categories_name'] . '</h3>
                        <h5>' . $heading_date . '</h5>
                        <h5>' . $categories_time . '</h5>
                    </div>
                </div>
                </a>
            </div>
            <h6 class="fc_date">' . $search_result['categories_name'] . '</h6>
            <h6 class="fc_date">' . $heading_date . '</h6>
            <h6 class="fc_date">' . $categories_time . '</h6>
        </div>';
    }
} else {
    $search_results_content = "<h4 class=\"text-center\"><p>No events found matching your search.</p></h4>";
}
?>

<form action="search_results.php" method="GET">
    <div class="mb-3">
        <input type="text" name="search_term" class="form-control" placeholder="Search for a show name" required>
    </div>
    <button type="submit" class="btn btn-primary">Search</button>
</form>

<section id="featured">
    <div class="container featured" data-aos="fade-up">
        <div class="row gy-4">
            <?php echo $search_results_content; ?>
        </div>
    </div>
</section>
