<?php include 'inc_classes.php'; 
$action=$_POST['action'];
if($action=="day")
{
	$id=$_POST['id'];
}
else if($action=="topic")
{
	$topic_id=$_POST['topic_id'];
	$course_batch_id=$_POST['course_batch_id'];
	
	$sel_timet="Select topic_content,batch_timetable_map_id from batch_timetable where c_b_id='".$course_batch_id."' and topic_id='".$topic_id."'";
	$ptr_timet=mysql_query($sel_timet);
	$data_timet=mysql_fetch_array($ptr_timet);
	
	echo $data_timet['topic_content'];
	echo "##";
	?>
     <option value="">Select Day</option>
	 <?php
     $sel_timetable="Select batch_timetable_map_id,day,date,topic_id,topic_content from batch_timetable where c_b_id='".$course_batch_id."'";
     $ptr_timetable=mysql_query($sel_timetable);
     while($data_time=mysql_fetch_array($ptr_timetable))
     {
     	$sel='';
        if($data_timet['batch_timetable_map_id']==$data_time['batch_timetable_map_id'])
        {
            $sel='selected';
        }
        ?>
        <option value="<?php echo $data_time['batch_timetable_map_id']; ?>" <?php echo $sel; ?>>Day <?php echo $data_time['day']; ?></option>
        <?php
     }
}
else if($action=="change_day")
{
	$c_b_id=$_POST['course_batch_id'];
	$change_day=$_POST['day'];
	$hollidate=$_POST['date'];
	
	$sel_cb="select cm_id,course_id from batch_timetable where c_b_id='".$c_b_id."' ";
	$ptr_cb=mysql_query($sel_cb);
	$data_cb=mysql_fetch_array($ptr_cb);
	$course_id=$data_cb['course_id'];
	
	$select_holidays="select holiday_date from holiday_mapping where 1 and cm_id='".$data_cb['cm_id']."'";
	$ptr_hol=mysql_query($select_holidays);
	while($day_holi=mysql_fetch_array($ptr_hol))
	{
		$skipdates[]=$day_holi['holiday_date'];
	}
	
	$select_batch_holiday="select holiday_date from batch_holiday where 1 and c_b_id='".$c_b_id."'";
	$ptr_batch_holiday=mysql_query($select_batch_holiday);
	while($batch_holiday=mysql_fetch_array($ptr_batch_holiday))
	{
		$skipbatchholiday[]=$batch_holiday['holiday_date'];
	}
	
	$sel_data="select * from batch_timetable where c_b_id='".$c_b_id."' and course_id='".$course_id."' and date >='".$hollidate."' group by day order by day asc";
	$ptr_timet_data=mysql_query($sel_data);
	$total_days=mysql_num_rows($ptr_timet_data);
	
	$holiStartDate=$hollidate;
	$holiTimestamp=strtotime($holiStartDate);
	
	$startDate=$hollidate;
	$timestamp=strtotime($startDate);
	$days=$total_days;
	while($data_times=mysql_fetch_array($ptr_timet_data))
	{
		$day=$data_times['day'];
		$c_b_id=$c_b_id;
		
		$data_record_timetable['c_b_id']=$c_b_id;
		
		$sel_cb="select batch_type from course_batch_mapping where c_b_id='".$c_b_id."'";
		$ptr_cb=mysql_query($sel_cb);
		$data_cb=mysql_fetch_array($ptr_cb);
		
		if($data_cb['batch_type']=='weekdays')
		{
			$skipdays = array("Sunday");
		}
		else
		{
			$skipdays = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday");
		}
		
		$skipholidays = array(date("Y-m-d", $holiTimestamp));
		
		$i = 1;
		$dates=array();
		
		//echo "<br/>dates-".date("Y-m-d", $holiTimestamp).'--'.date("Y-m-d", $timestamp);
		//$timestamp = strtotime("+ 0 day", $timestamp);
		//echo "<br/>-->".date("l", $timestamp)."--".$timestamp;
		$totals=1;
		for($i=1;$i<=$totals;$i++)
		{
			if((in_array(date("l", $timestamp), $skipdays)) || (in_array(date("Y-m-d", $timestamp), $skipdates)) || in_array(date("Y-m-d", $timestamp),$skipbatchholiday) )
			{	
				
				$timestamp=strtotime("+ 1 day", $timestamp);
				//$dates[$i]=date("d/m/Y",strtotime("+ 1 day", $timestamp));
				//$timestamp = strtotime("+ 1 day", $timestamp);
				$totals++;
			}
			else
			{
				// We need to add add_holiday table and add dates of holiday and take in timestamp and check in this condition so it will skip in when we change topic
				if((in_array(date("l", $timestamp), $skipdays)) || (in_array(date("Y-m-d", $timestamp), $skipdates)) || in_array(date("Y-m-d", $timestamp),$skipbatchholiday)  )
				{	
					
					$timestamp=strtotime("+ 1 day", $timestamp);
					//$dates[$i]=date("d/m/Y",strtotime("+ 1 day", $timestamp));
					//$timestamp = strtotime("+ 1 day", $timestamp);
					$totals++;
				}
				else if(in_array(date("Y-m-d", $timestamp),$skipholidays))
				{
					//echo "<br/>Day ".$data_record_timetable['date']=date("Y-m-d", $timestamp);
					$update="update batch_timetable set date='".date("Y-m-d", $timestamp)."' where day='".$change_day."' and c_b_id='".$c_b_id."'";
					$up_query=mysql_query($update);
					//$where_record="batch_timetable_map_id='".$data_date['batch_timetable_map_id']."'";
					//$db->query_update("batch_timetable", $data_record_timetable,$where_record);
					$timestamp=strtotime("+ 1 day", $timestamp);
					//$totals++;
				}
				if((in_array(date("l", $timestamp), $skipdays)) || (in_array(date("Y-m-d", $timestamp), $skipdates)) || in_array(date("Y-m-d", $timestamp),$skipbatchholiday)  )
				{
					$timestamp=strtotime("+ 1 day", $timestamp);
					//$dates[$i]=date("d/m/Y",strtotime("+ 1 day", $timestamp));
					//$timestamp = strtotime("+ 1 day", $timestamp);
					$totals++;
				}
				else if($data_times['day']!=$change_day )
				{
					$data_record_timetable['date']=date("Y-m-d", $timestamp);
					//echo "<br/>".date("Y-m-d", $timestamp);
					
					$update_batch="update batch_timetable set date='".date("Y-m-d", $timestamp)."' where day='".$data_times['day']."' and c_b_id='".$c_b_id."'";
					$ptr_update=mysql_query($update_batch);
					
					if($i==$totals)
					{
						$update_last_day="update course_batch_mapping set end_date='".date("Y-m-d", $timestamp)."' where c_b_id='".$c_b_id."'";
						$ptr_update_last_day=mysql_query($update_last_day);
					}
					$timestamp=strtotime("+ 1 day", $timestamp);
				}
			}
		}
											
	}
	$topic_content='';
	$sel_top="Select topic_id,date from batch_timetable where c_b_id='".$c_b_id."' and day='".$change_day."'";
	$ptr_top=mysql_query($sel_top);
	while($data_top=mysql_fetch_array($ptr_top))
	{
		$sel_topic="select topic_name from topic where topic_id='".$data_top['topic_id']."'";
		$ptr_topic=mysql_query($sel_topic);
		$data_topic=mysql_fetch_array($ptr_topic);
		 
		$topic_content .=$data_topic['topic_name']."<br/>";
	}
	echo $topic_content;
}