<?php
if (file_exists("functions/site_functions.php"))
	{
	include_once("functions/site_functions.php");
	}
else
	{
include_once("../../functions/site_functions.php");
	}
echo<<<a
<div id="marginn">
<div id="data_message">
</div>
<select id="filter" onchange="filter(this.value);" >
<option value="">Filter by:</option>
<option value="1">User</option>
<option value="2">Date</option>
<option value="3">Problem</option>
</select>
<div id='filter_result'>
</div>
a;
$o=0;
db_open();
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==2)
	{
	$sql="SELECT * FROM troubleshoots WHERE user_id='$b' order by troubleshoot_id DESC"; 
	}
else 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 dater BETWEEN '$date1' AND '$date2' order by user_id,troubleshoot_id DESC"; 
		}
	else
		{
		die();
		}
	
	}
else if ($a==4)
		{
		if ($b==1)
			{
			$sql="SELECT * FROM troubleshoots WHERE problem='1' OR problem='2' OR problem='3' OR problem='4' order by user_id,troubleshoot_id DESC";
			}
		else if ($b==2)
			{
			$sql="SELECT * FROM troubleshoots WHERE problem='11' OR problem='12' OR problem='13' order by user_id,troubleshoot_id DESC";
			}
		else if ($b==4)
			{
			$sql="SELECT * FROM troubleshoots WHERE problem='7' OR problem='8' order by user_id,troubleshoot_id DESC";
			}
		else if ($b==3)
			{
			$sql="SELECT * FROM troubleshoots WHERE 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 order by troubleshoot_id DESC"; 
$c=1;
}
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">User_id</td>
<td class="td_title_troubleshoot_small">Date</td>
<td class="td_title_troubleshoot_small">Time</td>
<td class="td_title_troubleshoot_big">Description</td>
</tr>
a;
@ $result=mysql_query($sql);
$res=mysql_numrows($result);
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="Temperature sensor is not working";
	}
else if($subject==2)
	{
	$subject="Humidity sensor is not working";
	}
else if($subject==3)
	{
	$subject="Soil Moisture sensor is not working";
	}
else if($subject==4)
	{
	$subject="Indoor Lighting sensor is not working";
	}
else if($subject==11)
	{
	$subject="Temperature sensor data problem";
	}
else if($subject==12)
	{
	$subject="Humidity sensor data problem";
	}
else if($subject==13)
	{
	$subject="Indoor Lighting sensor data problem";
	}
else if($subject==8)
	{
	$subject="Overview mail-message failed";
	}
else if($subject==7)
	{
	$subject="Troubleshoot mail-message failed";
	}
else
	{
	$l=strlen($subject);
	$x=$l-1;
	$duration="";
	for ($i=0;$i<$x;$i++)
		{
		$duration=$duration.$subject[$i];
		}
	if ($duration==1)
		{
		$s='';
		}
	else
		{
		$s='s';
		}
	if ($subject[$x]==0)
		{
		$subject="Connection lost for $duration day$s" ;
		}
	else
		{
		$subject="Connection lost for $duration minute$s" ;
		}
	}
echo<<<b
<tr class="tr_troubleshoot_hover">
<td class="td_data_troubleshoots">$o</td>
<td class="td_data_troubleshoots" title="user_id">$id</td>
<td class="td_data_troubleshoots center title="date">$date</td>
<td class="td_data_troubleshoots center" title="time">$timer</td>
<td class="td_data_troubleshoots center" title="type of problem">$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>
<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">-</td>
<td class="td_data_troubleshoots">Troubleshoots deleted successfully!</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>";
?>