<?php
if (file_exists("embedded/functions/site_functions.php"))
	{
	include_once("embedded/functions/site_functions.php");
	}
else
	{
include_once("../../embedded/functions/site_functions.php");
	}
$language=$_SESSION['language'];
$device_id=$_SESSION['device_id'];
if ($language==1)
	{
	$head1="Ημερομηνία";
	$head2="Ώρα";
	$head3="Περιγραφή";
	$head4="Εμφάνιση κατά:";
	$head5="Ημερομηνία";
	$head6="Πρόβλημα";
	$head7="Αισθητήρας θερμοκρασίας εκτός λειτουργίας";
	$head8="Αισθητήρας υγρασίας εκτός λειτουργίας";
	$head9="Αισθητήρας υγρασίας εδάφους εκτός λειτουργίας";
	$head10="Αισθητήρας φωτισμού εκτός λειτουργίας";
	$head11="Πρόβλημα δεδομένων αισθητήρα θερμοκρασίας";
	$head12="Πρόβλημα δεδομένων αισθητήρα υγρασίας";
	$head13="Πρόβλημα δεδομένων αισθητήρα φωτισμού";
	$head14="Μη επιτυχής αποστολή email διαγραμμάτων";
	$head15="Μη επιτυχής αποστολή email προβλήματος";
	$head16="ημέρα";
	$head17="ημέρες";
	$head18="Απώλεια επικοινωνιάς για ";
	$head19="λεπτό";
	$head20="λεπτά";
	$title1="Ημερομηνία";
	$title2="Ώρα";
	$title3="Τύπος προβλήματος";
	$head21="Επιτυχής διαγραφή σφαλμάτων!";
	$total="Συνολικά";
	$clear="Διαγραφή";
	}
else
	{
	$head1="Date";
	$head2="Time";
	$head3="Description";
	$head4="Filter by";
	$head5="Date";
	$head6="Problem";
	$head7="Temperature sensor is not working";
	$head8="Humidity sensor is not working";
	$head9="Soil Moisture sensor is not working";
	$head10="Indoor Lighting sensor is not working";
	$head11="Temperature sensor data problem";
	$head12="Humidity sensor data problem";
	$head13="Indoor lighting sensor data problem";
	$head14="Overview mail-message failed";
	$head15="Overview mail-message failed";
	$head16="day";
	$head17="days";
	$head18="Connection lost for ";
	$head19="minute";
	$head20="minutes";
	$title1="Date";
	$title2="Time";
	$title3="Type of problem";
	$head21="Troubleshoots deleted successfully!";
	$total="Total";
	$clear="Clear";
	
	}
echo<<<a
<div id ="marginn">
<select id="filter" onchange="filter(this.value);" >
<option value="">$head4</option>
<option value="2">$head5</option>
<option value="3">$head6</option>
</select>
<div id='filter_result'>
</div>
a;

$o=0;
db_open();
$sql="SELECT user_id FROM users WHERE device_id='$device_id'";
$row=mysql_fetch_array(mysql_query($sql));
$user_id=$row[0];
if (isset($_GET['a']) && !empty($_GET['a']) && isset($_GET['b']) && !empty($_GET['b']) && is_numeric($_GET['b']) && is_numeric($_GET['a']) && isset($_GET['c']) && !empty($_GET['c']) && is_numeric($_GET['c']))
{
$a= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['a']))));
$b= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['b']))));
$c= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['c']))));
echo'<div id="clear"><input type="button" value="'.$clear.'" onclick="troubleshoots('.$a.','.$b.',2);" /></div>';
if ($a==3)
	{
	$year=$b[0].$b[1].$b[2].$b[3];
	$month=$b[4].$b[5];
	if ($year >=2012 && $year<=2100 && $month>=0 && $month<=12)
		{
		$date1=$year."-".$month."-01";
		$t=date("t",strtotime($date1));
		$date2=$year."-".$month.'-'.$t;
		$sql="SELECT * FROM troubleshoots WHERE user_id='$user_id' AND dater BETWEEN '$date1' AND '$date2' order by troubleshoot_id,dater DESC"; 
		}
	else
		{
		die();
		}
	
	}
else if ($a==4)
		{
		if ($b==1)
			{
			$sql="SELECT * FROM troubleshoots WHERE user_id='$user_id' AND problem='1' OR problem='2' OR problem='3' OR problem='4' order by troubleshoot_id DESC";
			}
		else if ($b==2)
			{
			$sql="SELECT * FROM troubleshoots WHERE user_id='$user_id' AND problem='11' OR problem='12' OR problem='13' order by user_id,troubleshoot_id DESC";
			}
		else if ($b==4)
			{
			$sql="SELECT * FROM troubleshoots WHERE user_id='$user_id' AND problem='7' OR problem='8' order by user_id,troubleshoot_id DESC";
			}
		else if ($b==3)
			{
			$sql="SELECT * FROM troubleshoots WHERE user_id='$user_id' AND problem NOT IN('1','2','3','4','12','11','13','7','8') order by user_id,troubleshoot_id DESC";
			}
		else
			{
			die();
			}
		}
}		
else
{ 
$sql="SELECT * FROM troubleshoots WHERE user_id='$user_id' order by troubleshoot_id DESC"; 
$c=1;
}

@ $result=mysql_query($sql);
$res=mysql_numrows($result);
echo<<<a
<div id="troubleshoot_field">
<table id="p_i" class="troubleshoots">
<tr>
<td class="td_title_troubleshoot_aa">A/A</td>
<td class="td_title_troubleshoot_small">$head1</td>
<td class="td_title_troubleshoot_small">$head2</td>
<td class="td_title_troubleshoot_big">$head3</td>
</tr>
a;
if($c==1)
{
if ($res!=0)
{
while ($row=mysql_fetch_array($result))
{
$o++;
$id=$row['user_id'];
$date=$row['dater'];
$timer=$row['timer'];
$subject=$row['problem'];
if ($subject==1)
	{
	$subject="$head7";
	}
else if($subject==2)
	{
	$subject="$head8";
	}
else if($subject==3)
	{
	$subject="$head9";
	}
else if($subject==4)
	{
	$subject="$head10";
	}
else if($subject==11)
	{
	$subject="$head11";
	}
else if($subject==12)
	{
	$subject="$head12";
	}
else if($subject==13)
	{
	$subject="$head13";
	}
else if($subject==8)
	{
	$subject="$head14";
	}
else if($subject==7)
	{
	$subject="$head15";
	}
else
	{
	$l=strlen($subject);
	$x=$l-1;
	$duration="";
	for ($i=0;$i<$x;$i++)
		{
		$duration=$duration.$subject[$i];
		}
	if ($subject[$x]==0)
		{
		if ($duration==1)
			{
			$s="$head16";
			}
		else
			{
			$s="$head17";
			}
		$subject="$head18".$duration." ".$s;
		}
	else
		{
		if ($duration==1)
			{
			$s="$head19";
			}
		else
			{
			$s="$head20";
			}
		$subject="$head18".$duration." ".$s;
		}
		}
echo<<<b
<tr class="tr_troubleshoot_hover">
<td class="td_data_troubleshoots">$o</td>
<td class="td_data_troubleshoots" title="$title1">$date</td>
<td class="td_data_troubleshoots" title="$title2">$timer</td>
<td class="td_data_troubleshoots" title="$title3">$subject</td>
</tr>
b;
	}
}
else
{
echo<<<b
<tr class="tr_troubleshoot_hover">
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">-</td>
</tr>
b;
}
}
else if ($c==2)
{
if ($res!=0)
{
$expression="";
while ($row=mysql_fetch_array($result))
	{
	$message_id=$row['troubleshoot_id'];
	if ($expression=="")
		{
		$expression="'$message_id'";
		}
	else
		{
		$expression=$expression.",'$message_id'";
		}
	}
if ($expression!="")
	{
	$sql="DELETE FROM troubleshoots WHERE troubleshoot_id IN($expression)";
	@ $result=mysql_query($sql);
	if ($result!=0)
		{
		
echo<<<b
<tr class="tr_troubleshoot_hover">
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">$head21</td>
</tr>
b;
		}
	}
}	
}
db_close();
echo"</table>";
echo"</div>";
if ($o!=0)
	{
	echo '<div id="total">'.$total.': '.$o.'</div>';
	}
else
	{
	echo '<div id="total">'.$total.': - </div>';
	}
echo "</div>";	
	

?>