<?php 
session_start();
require_once 'includes/database.php';
require_once 'includes/funcs.php'; 
if (!isset($_SESSION['reusername'])) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
else {$user = $_SESSION['reusername'];}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Modify Trackers List</title>
        <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.10.4.custom.js"></script>
        <link rel="stylesheet" href="css/allcss.css" type="text/css" />
        <link rel="stylesheet" href="css/jquery-ui-1.10.4.custom.css" type="text/css" />
        <style type="text/css">
            html, body {margin:0; padding:0; height:100%; width:100%; min-width:100%; min-height:100%;}
			.hsetr {
            text-align: left;
            width:100%;
            padding-left:10px; padding-right:5px; 
            background: lightblue;
            box-shadow: 0px 0px 5px rgba(0,0,0,0.5); 
            font-family: Arial, Helvetica, sans-serif;
            font-weight:bold;
            font-size:22px;
            color:darkbrown;
            }
        </style>
</head>
<body>		
    <p class="titletr">Modify Tracker Owner Details <?php echo str_repeat('&nbsp;', 70); ?><a href="#"><img src="images/close.png" onclick="window.close();"> </img></a></p> 
<?php
if (isset($_POST['addsavtrak'])) {         //Save tracker table

		$db=new MySQLDatabase();
		$db->open_connection();
        $imeino = htmlspecialchars($_POST['imeino']);
        $owner = htmlspecialchars($_POST['owner']);
        $owntel = htmlspecialchars($_POST['owntel']);
        $email = htmlspecialchars($_POST['email']);
		$stat = htmlspecialchars($_POST['stat']);
		$sdate = htmlspecialchars($_POST['sdate']);
		$rmks = htmlspecialchars($_POST['rmks']);
        $ts = date('Y-m-d h:i:s');
		$us = $_SESSION['reusername'];
		$std = substr($_REQUEST['sdate'],6,4)."-".substr($_REQUEST['sdate'],3,2)."-".substr($_REQUEST['sdate'],0,2);
        $query = "UPDATE devstock SET owner = '$owner', owntel = '$owntel', sdate = '$std', email = '$email', stat = '$stat', rmks = '$rmks', ts = '$ts', us = '$user' WHERE imeino ='$imeino';";
        $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
        if(!$result) { echo "ERROR in Saving Tracker Details!"; }
        else { echo '<div id="dialog-message" title="Tracker Modification Status">
                        <p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        Tracker Details Updated!</p></div>';
                }
		$db->close_connection();		
}

else {
	
   echo '<form id="traklist" method="post" onkeypress="return event.keyCode!=13" action="trackerem.php" > <center>';
   $i = $_GET['i'];		$r = $_GET['r'];
   echo '<table id="tablelist" style="width:90%;margin-left:10px;margin-right:10px;background-color: #AAD4FF;padding-left:10px;" class="display">';
 
	$db=new MySQLDatabase();
	$db->open_connection();
	if($i !== ''){ $query = "SELECT devstock.imeino, devstock.mobno, devstock.owner, devstock.owntel, devstock.email, devstock.stat, devstock.sdate, devstock.rmks, jcards.jdate, jcards.cname, jcards.regno FROM devstock, jcards WHERE jcards.jcno = devstock.jcno AND jcards.imeino = '$i';";  }
	else {  $query = "SELECT devstock.imeino, devstock.mobno, devstock.owner, devstock.owntel, devstock.email, devstock.stat, devstock.sdate, devstock.rmks, jcards.jdate, jcards.cname, jcards.regno FROM devstock, jcards WHERE jcards.jcno = devstock.jcno AND jcards.regno = '$r';"; }
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
    $odd = true;    $i=1;   $lx='0';
    if ($row = mysqli_fetch_assoc($result)) 
        {
        $dd = substr($row['jdate'],8,2)."-".substr($row['jdate'],5,2)."-".substr($row['jdate'],0,4); 
		$std = substr($row['sdate'],8,2)."-".substr($row['sdate'],5,2)."-".substr($row['sdate'],0,4);
		echo '<tr><td><label>IMEI No.</label></td> <td><input type="text" id="imeino" readonly="readonly" name="imeino" value="'.$row['imeino'].'" size="15"/></td></tr>';
		echo '<tr><td><label>Reg. No.</label></td> <td><input type="text" id="regno" disabled="disabled" name="regno" value="'.$row['regno'].'" size="8"/></td></tr>';
        echo '<tr><td><label>Installed Date</label></td> <td><input type="text" id="jdate" disabled="disabled" name="jdate" value="'.$dd.'" size="10"/></td></tr>';
        echo '<tr><td><label>Mobile No. No.</label></td> <td><input type="text" id="mobno" disabled="disabled" name="mobno" value="'.$row['mobno'].'" size="15"/></td></tr>';
        echo '<tr><td><label>Dealer</label></td> <td><input type="text" id="cname" disabled="disabled" name="cname" value="'.$row['cname'].'" size="30"/></td></tr>';
		echo '<tr><td><label>Owner name</label></td> <td><input type="text" id="owner" name="owner" value="'.$row['owner'].'" size="30"/></td></tr>';
        echo '<tr><td><label>Owner Mobile No.</label></td> <td><input type="text" id="owntel" name="owntel" value="'.$row['owntel'].'" size="20"/></td></tr>';
        echo '<tr><td><label>Email</label></td> <td><input type="text" id="email" name="email" value="'.$row['email'].'" size="30"/></td></tr>';
		echo '<tr><td><label>Status</label></td> <td><select id="stat" name="stat"><option  value="'.$row['stat'].'">'.$row['stat'].'</option> <option value="SHARED">SHARED</option> <option value="RELEASED">RELEASED</option><select></td></tr>';
		echo '<tr><td><label>Status Date</label></td> <td><input type="text" id="sdate" name="sdate" value="'.$std.'" size="8"/></td></tr>';
		echo '<tr><td><label>Remarks</label></td> <td><textarea id="rmks" name="rmks" row=2 col=40>'.$row['rmks'].'</textarea></td></tr>';
		echo '<tr id="trow"><br/></tr>';    
		echo '<tr align="center"><td colspan="5"><input type="submit" id="addsavtrak" name="addsavtrak"  class="text ui-widget-content ui-corner-all" value=" Save Details  " style="width:120px;font-weight:bold;"/></td></tr>';
        }  
	else { echo '<div id="dialog-message" title="Tracker Modification Error">
                        <p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        Tracker Not Found!</p></div>';
                }
    
    echo '</table>';
    echo '</center></form>';
	$db->close_connection();
}
?>

</body>

</html>
<script type="text/javascript"> 
$('#sdate').datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"}); 
	// dialog boxes
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
		$( "#dialog-message" ).dialog({
			modal: true,
			width: 400,
			buttons: {
				Ok: function() {
					window.close();
				}
			}
		});



</script>


