<?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();}
?>

<!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>
<script type="text/javascript">       
 $(document).ready(function() { 

$("#addsavtrak").click(function(e) {
e.preventDefault();
var i = parseInt($('#irl').val());
var rl = parseInt($('#rl').val());
for(var j=i; j<rl; j++) {
	var savtrack = $.get("savenewtrak.php?im="+$('#imeino'+j).val()+"&d="+$('#idate'+j).val()+"&mo="+$('#mobno'+j).val()+"&it="+$('#itype'+j).val(), function(){});
}
alert("Trackers Saved!");
window.close();
});
 });
</script>   		
</head>
<body>		
    <p class="titletr">Modify Unsold Trackers Details <?php echo str_repeat('&nbsp;', 70); ?><a href="#"><img src="images/close.png" onclick="window.close();"> </img></a></p> 
<?php
if (isset($_POST['savtrakno'])) {         				//Save edited trak 
	$tno = htmlspecialchars($_POST['tno']);
	$imeino = htmlspecialchars($_POST['imeino']);
    $dd = htmlspecialchars($_POST['idate']);
    $mobno = htmlspecialchars($_POST['mobno']);
    $itype = htmlspecialchars($_POST['itype']);
    $idate = substr($dd,6,4)."-".substr($dd,3,2)."-".substr($dd,0,2);
	$ts = date('Y-m-d h:i:s');
	$us = $_SESSION['reusername'];
	$db=new MySQLDatabase();
	$db->open_connection();
    $query = "UPDATE devstock SET itype = '$itype', imeino = '$imeino', idate = '$idate', mobno = '$mobno', ts = '$ts', us = '$us' WHERE tno = '$tno'";
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
	if(!$result) { echo "ERROR in Updating Tracker Entry!"; }
        else { echo '<div id="dialog-message" title="Trackers Modification Sle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        Tracker Entry Updated!</p></div>';
				}		
	$db->close_connection();
}
elseif (isset($_GET['edtrakno'])) {         				//Edit trak Code

	$tno = htmlspecialchars($_GET['tno']);
	$imeino = htmlspecialchars($_GET['im']);
    $dd = htmlspecialchars($_GET['d']);
    $mobno = htmlspecialchars($_GET['mo']);
    $itype = htmlspecialchars($_GET['ty']);
	echo '<form id="traklist" method="post" onkeypress="return event.keyCode!=13" action="trackedit.php" > <center>'; 
	echo '<table id="tablelist" style="width:90%;margin-left:10px;background-color: #AAD4FF;">
    <thead>
    <tr> <th align="left" style="width:140px;">IMEI No. </th> <th style="width:80px;">Type</th> <th style="width:100px;">Date In</th> <th style="width:150px;">Mobile No.</th> </tr>
    </thead>';
		echo'<td><input type="text" id="imeino" name="imeino" value="'.$imeino.'" size="15"/></td>';
		echo '<td><input type="text" id="itype" name="itype" value="'.$itype.'" size="10"/></td>';
        echo '<td><input type="text" id="idate" name="idate" value="'.$dd.'" size="10"/></td>';
        echo '<td><input type="text" id="mobno" name="mobno" value="'.$mobno.'" size="15"/></td> <input type="hidden" id="tno" name="tno" value="'.$tno.'" /></tr>';
		echo '<tr id="trow"><br/></tr>';    
    echo '<tr align="center"><td colspan="5"><input type="submit" id="savtrakno" name="savtrakno"  class="text ui-widget-content ui-corner-all" value=" Save " style="width:80px;font-weight:bold;"/></td></tr>';
    echo '</table>';
    echo '</center></form>';
	echo '<script type="text/javascript">$("#idate").datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy" }); </script>';
}
elseif (isset($_GET['detrakno'])) {         				//Delete trak Code
	$tno = htmlspecialchars($_REQUEST['tno']);
	$db=new MySQLDatabase();
	$db->open_connection();
	$query = "DELETE FROM devstock WHERE tno = '$tno'";
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
	if(!$result) { echo "ERROR in Deleting Tracker Entry!"; }
        else { echo '<div id="dialog-message" title="Trackers Modification Status">
                        <p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        Tracker Entry Deleted!</p></div>';
                }
	$db->close_connection();
}

else {
	$db=new MySQLDatabase();
	$db->open_connection();
   echo '<form id="traklist" method="post" onkeypress="return event.keyCode!=13" action="#" > <center>'; 
   echo '<table cellspacing="10px" align="center" style="width:80%; margin-left:10px;" >'; 
   echo '<tr><td align="center"><input type="text" id="btnaddtrak" name="btnaddtrak" readonly="readonly" onclick="gettrakrow();" class="text ui-widget-content ui-corner-all" value="   Add New Tracker" style="width:160px;font-weight:bold;"/></td></tr>';	 
   echo '</table><br/>';
   
   echo '<table id="tablelist" style="width:90%;margin-left:10px;background-color: #AAD4FF;" class="display">
    <thead>
    <tr> <th align="left" style="width:140px;">IMEI No. </th> <th style="width:80px;">Type</th> <th style="width:100px;">Date In</th> <th style="width:150px;">Mobile No.</th> <th>Select</th> </tr>
    </thead>'; 

    $query = "SELECT * FROM devstock WHERE jcno=0;";
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
    $odd = true;    $i=1;   $lx='0';
    while ($row = mysqli_fetch_assoc($result)) 
        {
        echo ($odd == true) ? '<tr class = "odd_row">' : '<tr class = "alt">';
        $odd = !$odd; 	$dd = substr($row['idate'],8,2)."-".substr($row['idate'],5,2)."-".substr($row['idate'],0,4);
        echo'<td>';    
		echo '<input type="text" id="imeino'.$i.'" name="imeino'.$i.'" readonly="readonly" value="'.$row['imeino'].'" size="15"/>';
        echo '</td><td>';
		echo '<input type="text" id="itype'.$i.'" name="itype'.$i.'" readonly="readonly" value="'.$row['itype'].'" size="10"/>';
        echo '</td><td>';
        echo '<input type="text" id="idate'.$i.'" name="idate'.$i.'" readonly="readonly" value="'.$dd.'" size="10"/>';
        echo '</td><td>';
        echo '<input type="text" id="mobno'.$i.'" name="mobno'.$i.'" readonly="readonly" value="'.$row['mobno'].'" size="15"/>';
        echo '</td><td>';
        echo '<a href="trackedit.php?detrakno=true&tno='. $row['tno'].'"> DELETE</a> &nbsp; <a href="trackedit.php?edtrakno=true&tno='. $row['tno'].'&d='.$dd.'&im='.$row['imeino'].'&ty='.$row['itype'].'&mo='.$row['mobno'].'"> EDIT</a>';
        echo '</td></tr>';
		echo '<script type="text/javascript">$("#idate'.$i.'").datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy" }); </script>';
        $i++;
        }   
    echo '<input type="hidden" id="rl" name="rl" value="'.$i.'"/><input type="hidden" id="irl" name="irl" value="'.$i.'"/>';
    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 Trackers  " style="width:120px;font-weight:bold;"/></td></tr>';
    echo '</table>';
    echo '</center></form>';
	$db->close_connection();
}
?>

</body>

</html>
<script type="text/javascript"> 

	// dialog boxes
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
		$( "#dialog-message" ).dialog({
			modal: true,
			width: 400,
			buttons: {
				Ok: function() {
					window.close();
				}
			}
		});



function gettrakrow(){ 
var rl = $("#rl").val();    
 $('#trow').before('<tr><td><input type="text" id="imeino'+rl+'" name="imeino'+rl+'" value="" size="15"/></td>'
						+'<td><input type="text" id="itype'+rl+'" name="itype'+rl+'" value="" size="10"/></td>'
                        +'<td><input type="text" id="idate'+rl+'" name="idate'+rl+'" value="<?php echo date('d-m-Y'); ?>" size="10"/></td>'
                        +'<td><input type="text" id="mobno'+rl+'" name="mobno'+rl+'" value="" size="15"/></td>'
                        + '<tr>');   
 document.getElementById("rl").value = (parseInt(rl)+1);
$('#idate'+rl).datepicker({ changeMonth: true, changeYear: true, dateFormat: 'dd-mm-yy' }); 
}; 
</script>


