<?php 

require_once("db_conn_class.php");

function getCourseDate($courseName, $batchType = '') {
    $dbh = new db_conn();
    $dbh->set_conn();

    // Step 1: Get course info
    $sql_course = "SELECT c.course_id, c.course_name, c.website_fees_inr, c.website_fees_usd, 
                          c.website_fees_thb, c.website_fees_gbp, c.website_fees_euro 
                   FROM tbl_courses c
                   WHERE c.course_name = '".mysqli_real_escape_string($dbh->link, $courseName)."'
                     AND c.status = 'ACTIVE' AND c.deleted = 0";

    $result_course = mysqli_query($dbh->link, $sql_course);

    if ($result_course && mysqli_num_rows($result_course) > 0) {
        $course = mysqli_fetch_assoc($result_course);
        $courseId = $course['course_id'];

        // Step 2: Prepare condition for batch type
        $batchCondition = "";
        if ($batchType === 'saturday_class') {
            $batchCondition = "AND b.saturday_class = 1";
        } elseif ($batchType === 'bi_weekly') {
            $batchCondition = "AND b.bi_weekly = 1";
        }elseif ($batchType === 'Full Time') {
            $batchCondition .= " AND b.batch_type = 'Full Time'";
        } elseif ($batchType === 'Part Time') {
            $batchCondition .= " AND b.batch_type = 'Part Time'";
        }else
		{
			 $batchCondition .= " ";
		}
 $currentDate = date('Y-m-d');
        // Step 3: Get batch info
        $sql_batch = "SELECT b.batch_start_dt, b.batch_end_dt, b.batch_start_time, b.batch_end_time,
                             b.batch_trainer, b.saturday_class, b.bi_weekly, b.location, b.type, u.user_name 
                      FROM tbl_batch_calendar b
                      LEFT JOIN tbl_user u ON u.user_id = b.batch_trainer
                      WHERE b.batch_course_id = ".$courseId." 
                        ".$batchCondition."
                        AND b.deleted = 0 AND b.status = 'ACTIVE'
						AND b.batch_start_dt >= '".$currentDate."'
                      ORDER BY b.batch_end_dt 
                      LIMIT 1";

        $result_batch = mysqli_query($dbh->link, $sql_batch);

        if ($result_batch && mysqli_num_rows($result_batch) > 0) {
            $batch = mysqli_fetch_assoc($result_batch);

           
            $startDate   = $batch['batch_start_dt'];
            $endDate     = $batch['batch_end_dt'];

             if ($currentDate > $endDate || $currentDate > $startDate) {
                     $batchDate      = "Coming Soon";
                     $batchTime      = "Coming Soon";
                     $startbatchDate = "Coming Soon";
                     $endbatchDate   = "Coming Soon";
            } else {
                $batchDate      = date('jS F, Y', strtotime($endDate));
                $startbatchDate = date('jS F', strtotime($startDate));
                $endbatchDate   = date('jS F, Y', strtotime($endDate));
                $batchTime      = date('g:i a', strtotime($batch['batch_start_time'])) . ' - ' .
                                  date('g:i a', strtotime($batch['batch_end_time'])) . ' (IST)';
            }

            return array(
                'course_name'      => $course['course_name'],
                'fees_inr'         => number_format($course['website_fees_inr']),
                'fees_usd'         => number_format($course['website_fees_usd']),
                'fees_thp'         => number_format($course['website_fees_thb']),
                'fees_gbp'         => number_format($course['website_fees_gbp']),
                'fees_euro'        => number_format($course['website_fees_euro']),
                'batch_date'       => $batchDate,
                'batch_start_date' => $startbatchDate,
                'batch_end_date'   => $endbatchDate,
                'batch_time'       => $batchTime,
                'saturday_class'   => $batch['saturday_class'],
                'bi_weekly'        => $batch['bi_weekly'],
                'batch_trainer'    => $batch['user_name'],
				'batch_location'   => $batch['location']
            );
        } else {
            // No batch found, but course exists — return course fees only
            return array(
                'course_name'      => $course['course_name'],
                'fees_inr'         => number_format($course['website_fees_inr']),
                'fees_usd'         => number_format($course['website_fees_usd']),
                'fees_thp'         => number_format($course['website_fees_thb']),
                'fees_gbp'         => number_format($course['website_fees_gbp']),
                'fees_euro'        => number_format($course['website_fees_euro']),
                'batch_date'       => "Coming Soon",
                'batch_start_date' => "Coming Soon",
                'batch_end_date'   => "Coming Soon",
                'batch_time'       => "Coming Soon",
                'saturday_class'   => '-',
                'bi_weekly'        => '-',
                'batch_trainer'    => '-',
				'batch_location'   => '-'

            );
        }
    }

    // Fallback: Course not found at all
    return array(
        'course_name'      => $courseName,
        'fees_inr'         => '-',
        'fees_usd'         => '-',
        'fees_thp'         => '-',
        'fees_gbp'         => '-',
        'fees_euro'        => '-',
        'batch_date'       => "Course coming Soon",
        'batch_start_date' => "Course coming Soon",
        'batch_end_date'   => "Course coming Soon",
        'batch_time'       => "Course coming Soon",
        'saturday_class'   => '-',
        'bi_weekly'        => '-',
        'batch_trainer'    => '-',
		'batch_location'   => '-'

    );
}


?>

