<?php 

require_once("db_conn_class.php");

function format_inr($amount) {
    $amount = round($amount, 2);
    $amount_parts = explode('.', number_format($amount, 2, '.', ''));
    $number = $amount_parts[0];
    //$decimal = isset($amount_parts[1]) ? $amount_parts[1] : '00';

    $last3 = substr($number, -3);
    $rest = substr($number, 0, -3);
    if ($rest != '') {
        $rest = preg_replace("/\B(?=(\d{2})+(?!\d))/", ",", $rest);
        $number = $rest . "," . $last3;
    }

    return  $number ;
}



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";
		//echo $sql_batch;
		
		
		//Step 4: Get alumni no
	/*	$sql_alumni = "select alumni_no from tbl_comp_info where comp_id = 1";
		$result_alumni = mysqli_query($dbh->link3, $sql_alumni);
		 if ($result_alumni && mysqli_num_rows($result_batch) > 0) {
            $alumni = mysqli_fetch_assoc($result_alumni);
			$alumni_no = $alumni['alumni_no'];
			
		 }*/
		
		
        $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'];
			
//echo  $endDate ;
             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'         => format_inr($course['website_fees_inr']),
                'fees_usd'         => number_format($course['website_fees_usd']),
                'fees_thp'         => format_inr($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'         => format_inr($course['website_fees_inr']),
                'fees_usd'         => number_format($course['website_fees_usd']),
                'fees_thp'         => format_inr($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'   => '-'

    );
	//exit();
}

function getAlumniNo() {
    $dbh = new db_conn();
    $dbh->set_conn();

    $sql = "SELECT alumni_no FROM tbl_comp_info WHERE comp_id = 1";
    $result = mysqli_query($dbh->link, $sql);

    if ($result && mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        return number_format($row['alumni_no']);
    } else {
        return '70,000';
    }
}


?>

