<?php
$class1="";
$message="";
if (file_exists("embedded/functions/site_functions.php"))
	{
	include_once("embedded/functions/site_functions.php");
	}
else
	{
include_once("../../embedded/functions/site_functions.php");
	}
db_open();
$language=$_SESSION['language'];
if ($language==1)
	{
	$problem="Σφάλμα βάσης δεδομένων!";
	$head1="ενεργοποιήθηκε!";
	$head2=" πρέπει να απενεργοποιηθεί . <b>Καμία αλλαγή!</b>";
	$head3="Περίοδο διαγραφής σεναρίου:";
	$period="Περίοδος";
	$today="Σήμερα";
	$week="Eβδομάδα";
	$montt="Μήνας";
	$yaer="Χρόνός";
	$edit="Αλλαγή";
	$edit11="Διαγραφή";
	$clear="Άκυρο";
	$head4="Περίοδο απενεργοποιήσης σεναρίου:";
	$deleted1="διεγράφη!";
	$delteted2="διγράφησαν!";
	$dact1="απενεργοποιήθηκε";
	$dact2="απενεργοποιήθηκαν";
	$event="γεγονός";
	$events="γεγονότα";
	$head5="Ώρα:";
	$head6="Σενάριο";
	$head7="Ενέργειες:";
	$old="παλαιό";
	$now="τώρα";
	$next="ενεργό";
	$title1=" Πατήστε για απενεργοποίηση.";
	$title2=" Πατήστε για ενεργοποίηση.";
	$head8="ενεργοποιήθηκε";
	$head9="απενεργοποιήθηκε";
	$head10="Ενεργοποιήστε, απενεργοποιήστε, δημιουργήστε, διαγράψτε σενάρια!";
	$deltitle="Διαγράψτε την καταχώρηση.";
	$next_d="αδρανή";
	}
else
	{
	$problem="Database troubleshooting!";
	$head1="activated!";
	$head2=" should be de-activated! <b>No change performed!</b>";
	$head3="Select period for deleting scenario:";
	$period="Period";
	$today="This day";
	$week="Week";
	$montt="Month";
	$yaer="Year";
	$edit="Edit";
	$edit11="Delete";
	$clear="Clear";
	$head4="Select period for de-activating scenario: ";
	$deleted2=$deleted1="deleted!";
	$dact2=$dact1="de-activated!";
	$event="event";
	$events="events";
	$head5="Time:";
	$head6="Scenario:";
	$head7="Αctions:";
	$old="old";
	$now="now";
	$next="active";
	$title1=" Click for de-activation.";
	$title2=" Click for activation.";
	$head8="activated.";
	$head9="de-activated.";
	$head10="Check and edit scenario list!";
	$deltitle="Delete scheduling.";
	$next_d="idle";
	}	
if ((isset($_GET['q']) && !empty($_GET['q']) && is_numeric($_GET['q'])) && (isset($_GET['p']) && !empty($_GET['p']) && is_numeric($_GET['p'])) && (isset($_GET['e'])) && (isset($_GET['d'])) && (isset($_GET['f']) && is_numeric($_GET['f'])) && (isset($_GET['df']) && !empty($_GET['df']) && is_numeric($_GET['df'])))
{

$device_id=$_SESSION['device_id'];

$q= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['q']))));
$p= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['p']))));
$e=$ee= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['e']))));
$d=$dd= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['d']))));
$f= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['f']))));
$df= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['df']))));
$sday=$f[0].$f[1].$f[2].$f[3].'-'.$f[4].$f[5].'-'.$f[6].$f[7];
	if ($ee[0]==9)
		{
		$ee[0]=0;
		}
	if ($dd[0]==9)
		{
		$dd[0]=0;
		}
$start_time=$ee[0].$ee[1].":".$ee[2].$ee[3].":"."00";
$end_time=$dd[0].$dd[1].":".$dd[2].$dd[3].":"."00";
	
	$sql3 = "SELECT scenario_id FROM schedule_scenarios WHERE schedule_id='$p'";
	@ $resultaaa = mysql_query($sql3);
	$rowaa= mysql_fetch_array($resultaaa);
	$scenario_id=$rowaa[0];
	$sql2 = "SELECT scenario_name FROM scenarios WHERE scenario_id='$scenario_id'";
	@ $resultaa = mysql_query($sql2);
	$rowa= mysql_fetch_array($resultaa);
	$scenario_name=$rowa["scenario_name"];
	if ($q==1 ||$q==11 ||$q=12)
		{
		$sql1 = "SELECT * FROM schedule_scenarios WHERE device_id='$device_id' AND status='1' AND day='$sday' AND schedule_id!='$p' AND ((start_time<='$start_time' AND end_time>='$start_time') OR (end_time<='$end_time' AND start_time>='$start_time') OR (end_time>='$end_time' AND start_time>='$start_time' AND start_time<='$end_time')) ";
		$res=mysql_query($sql1);
		$resulta = mysql_numrows($res);
		$name="";
		if ($df==6 && $q==1)
			{
			if ($resulta==0)
				{
				$sql = "UPDATE schedule_scenarios SET status ='1' WHERE schedule_id='$p' AND status ='0' AND start_time='$start_time' AND end_time='$end_time'";
				@ $result = mysql_query($sql);
				if ($result!=0)
					{
					$class1="valid";
					$message=$scenario_name.' <b>'.$head1.'</b>';
					}
				else
					{
					$class1="invalid";
					$message="<b>$problem</b>";
					}
				}
			else
				{	
				$s="";
				while($row= mysql_fetch_array($res))
					{
					$s=$row["scenario_id"];
					$sql1 = "SELECT scenario_name FROM scenarios WHERE scenario_id='$s'";
					@ $resulta = mysql_query($sql1);
					$rowe= mysql_fetch_array($resulta);
					if($name=="")
							{
							$name=$rowe["scenario_name"];
							}
						else
							{
							$name=$name."/".$rowe["scenario_name"];
							}
					}
					$class1="invalid";
					$message=$name.$head2;
				}
			}
		else
			{
			if($df==8)
				{
				$message='<span class="invalid"><b>'.$head3 .'</b></span><select id="delete_frequency" onchange="enable_edit(this.value);" ><option value="">'.$period.'</option><option value="1">'.$today.'</option><option value="2">'.$week.'</option><option value="3">'.$montt.'</option><option value="4">'.$yaer.'</option></select>
				<input type="button" id="edit" disabled="true" value="'.$edit11.'" onclick="scenario_status_edit(11,'.$p.','.$e.','.$d.','.$f.',11);" /><input type="button" value="'.$clear.'" onclick="day_info('.$f[4].$f[5].','.$f[0].$f[1].$f[2].$f[3].','.$f[6].$f[7].');" /> ';
				}
			else if($df==9)
				{
				$message='<span class="invalid"><b>'.$head4.'</b></span><select id="disable_frequency"  onchange="enable_edit(this.value);" ><option value="">'.$period.'</option><option value="1">'.$today.'</option><option value="2">'.$week.'</option><option value="3">'.$montt.'</option><option value="4">'.$yaer.'</option></select>
				<input type="button" id="edit" value="'.$edit.'" disabled="true" onclick="scenario_status_edit(12,'.$p.','.$e.','.$d.','.$f.',12);" /><input type="button" value="'.$clear.'" onclick="day_info('.$f[4].$f[5].','.$f[0].$f[1].$f[2].$f[3].','.$f[6].$f[7].');" /> ';
				}
			else if($df<=4)
				{
				$sql1="SELECT * FROM schedule_scenarios WHERE device_id='$device_id' AND schedule_id='$p' ";
				@ $result=mysql_query($sql1);
				if ($result!=0)
					{
						$row=mysql_fetch_array($result);
						$scenario_id=$row['scenario_id'];
						if ($df==1)
						{
						$i=1;
						}
					else if($df==2)
						{
						$i=7;
						}
					else if ($df==3)
						{
						$i=days_in_month($f[4].$f[5],$f[0].$f[1].$f[2].$f[3]);
						}
					else
						{
						$i=0;
						for ($d=1;$d<=12;$d++)
							{
							$i=$i+days_in_month($d,$f[0].$f[1].$f[2].$f[3]);
							}
						}
					$da = mktime(0,0,0,$f[4].$f[5],$f[6].$f[7],$f[0].$f[1].$f[2].$f[3]);
					$table="";
					$counter="";
					for($d=0;$d<$i;$d++)
						{
						$dater=date("Y-m-d",strtotime("+ $d days",$da));
						if ($q==12)
							{
							$sql111="SELECT * FROM schedule_scenarios WHERE device_id='$device_id' AND scenario_id='$scenario_id' AND day='$dater' AND status='1' AND start_time='$start_time' AND end_time='$end_time'";
							}
						else if($q==11)
							{
							$sql111="SELECT * FROM schedule_scenarios WHERE device_id='$device_id' AND scenario_id='$scenario_id' AND day='$dater' AND start_time='$start_time' AND end_time='$end_time'";
							}
						@ $r111=mysql_query($sql111);
						$rr111=mysql_numrows($r111);
						if ($rr111==1)
							{
							$r101=mysql_fetch_array($r111);
							if ($table=="")
								{
								$table="'".$r101['schedule_id']."'";
								}
							else
								{
								$table=$table.",'".$r101['schedule_id']."'";
								}
							$counter++;
							}
						}
					if ($q==11)
						{
						$sql_1="DELETE FROM schedule_scenarios WHERE device_id='$device_id' AND schedule_id IN($table)";
						$action1="$deleted1";
						$action2="$deleted2";
						}
					else if ($q==12)
						{
						$sql_1="UPDATE schedule_scenarios SET status='0' WHERE device_id='$device_id' AND schedule_id IN($table)";
						$action1="$dact1";
						$action2="$dact2";
						}
					@ $final_res=mysql_query($sql_1);
					if ($final_res!=0)
						{
						if ($counter==1)
							{
							$message='<span class="invalid"><b>'.$counter.' '.$event.' '.$action1.'</b></span>';
							}
						else
							{
							$message='<span class="invalid"><b>'.$counter.' '.$events.' '.$action2.'</b></span>';
							}
						}
					else
						{
						$message='<span class="invalid"><b>Database Troubleshooting!</b></span>';
						}
					}
				else
					{
					$message="<span class='invalid'><b>".$problem."</b></span>";
					break;
					}
				
				}
			}
		}
		else
			{
			$class1="invalid";
			$message="<b>".$problem."</b>";
			}
	
}
if (!isset($title))
	{
	$y=$f[0].$f[1].$f[2].$f[3];
	$d=$f[6].$f[7];
	$m=$f[4].$f[5];
	$title=date("l $d, F Y",mktime(0,0,0,$m,$d,$y));
	}

echo<<<a
<div id="day_list">
<table id="scenario_list_day">
<tr class="p_i_title_d">
<td class="list_aa">A/A:</td>
<td class="list_time">$head5</td>
<td class="list_scenario">$head6</td>
<td class="list_actions">$head7</td>
</tr>
a;
if ($m<10 && strlen($m)==1)
	{
	$m="0".$m;
	}
if ($d<10 && strlen($d)==1)
	{
	$d="0".$d;
	}
$date=$y."-".$m."-".$d;
$date1=$y.$m.$d;
$today=date("Y-m-d");
$t=date("Ymd");
$a=explode("-",$today);
$c_y=$a[0];
$c_m=$a[1];
$c_d=$a[2];
$t_time=time();
$e="";
	if ($y<$c_y || ($m<$c_m && $y==$c_y) || ($d<$c_d && $m==$c_m && $y==$c_y))
		{
		$e='disabled="true"';
		}
	else
		{
		$e="";
		}

$sql1 = "SELECT * FROM schedule_scenarios WHERE day='$date' ORDER BY start_time,end_time";
@ $result = mysql_query($sql1);
$i=1;
while($row = mysql_fetch_array($result))
	{
	$scenario_id=$row["scenario_id"];
	$sql15="SELECT scenario_id FROM scenarios WHERE scenario_id='$scenario_id' LIMIT 1";
	@ $resul = mysql_fetch_array(mysql_query($sql15));
	if ($resul!=0)
		{
		$schedule_id=$row['schedule_id'];
		$start_time1=$start_time2=$row['start_time'];
		$end_time1 =$end_time2= $row['end_time'];
		if ($start_time2[0]==0)
				{
				$start_time2[0]="9";
				}
			if ($end_time2[0]==0)
				{
				$end_time2[0]="9";
				}
		$a=explode(":",$start_time2);
		$s_t=$a[0].$a[1].$a[2];
		$b=explode(":",$end_time2);
		$e_t=$b[0].$b[1].$b[2];
		$status=$row['status'];
		$scenario_id=$row['scenario_id'];
		$sql="SELECT scenario_name FROM scenarios WHERE scenario_id='$scenario_id'";
		@ $resulta = mysql_query($sql);
		$rowa= mysql_fetch_array($resulta);
		$scenario_name=$rowa[0];
		if (($y<$c_y || ($m<$c_m && $y==$c_y) || ($d<$c_d && $m==$c_m && $y==$c_y)) || ($y==$c_y && $m==$c_m && $d==$c_d && $t_time>strtotime($end_time1))) 
			{
			$c="";
			$astate="";
			$class="d_list_scenario";
			$comment="$old";
			$del='-';
			}
		else
			{
			$del='<input type="button" class="delete posit" name="delete" title="'.$deltitle.'" onclick="scenario_status_edit(1,'.$schedule_id.','.$s_t.','.$e_t.','.$date1.',8);" />';
			$class="d_list_scenario_hover";
			if ($status==1)
				{
				if ($y==$c_y && $m==$c_m && $d==$c_d && $t_time<=strtotime($end_time1) && $t_time>=strtotime($start_time1))
					{
					$comment="$now";
					}
				else
					{
					$comment="$next";
					}
				$c='onclick="scenario_status_edit(1,'.$schedule_id.','.$s_t.','.$e_t.','.$date1.',9);"';
				$astate=" $title1";
				}
			else
				{
				$c='onclick="scenario_status_edit(1,'.$schedule_id.','.$s_t.','.$e_t.','.$date1.',6);"';
				$astate="$title2";
				$comment="$next_d";
				}
			}
		if ($status==1)
			{
			$class=$class." day_active";
			$state="$head8";
			
			}
		else
			{
			$class=$class." day_deactivated";
			$state="$head9";
			}
		echo'<tr class="p_i_day_data"><td class="d_list_aa">'.$i.'</td><td class="d_list_time">'.$start_time1.'-'.$end_time1.'</td><td id="'.$i.'" class="'.$class.'" title="'.$scenario_name.' '.$state.''.$astate.'" '.$c.'>'.$scenario_name.' <span class="d_important">('.$comment.')</span></td><td class="d_list_actions">'.$del.'</td></tr>';
		$i++;
		}
	}
if ($message=="")
	{
	$class1="invalid";
	$message="<b>$head10</b>";
	}
db_close();
echo<<<a
</table>
</div>
<div id="day_notes"><span class="$class1">$message</span></div>
a;

?>