<?php 
session_start();
require_once 'includes/database.php';
include '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>
        <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>
            html, body {margin:0; padding:0; height:100%; width:100%; min-width:100%; min-height:100%; overflow:scroll;z-index: 100;}
        </style>
<script type="text/javascript">       
$(document).ready(function() { 
$("#btnaddjc").click(function(e) {
e.preventDefault();

if( $('#cname').val() === ''  ){ document.getElementById('valerr').value="Customer Name Cannot be Blank!";	}
else if( $('#jdate').val() === ''  ){ document.getElementById('valerr').value="Date Cannot be Blank!";	}
else if( $('#imeino').val() === ''  ){ document.getElementById('valerr').value="IMEI No. Cannot be Blank!";	}
else if( $('#chasis').val() === ''  ){ document.getElementById('valerr').value="Chasis Cannot be Blank!";	}
else if( $('#cname').val() !== '' && $('#jdate').val() !== '' && $('#chasis').val() !== '' && $('#imeino').val() !== '') {
		var savjcdet = $.post("savejc.php?e=0&cname="+$('#cname').val()+"&jdate="+$('#jdate').val()+"&vehmod="+$('#vehmod').val()+"&chasis="+$('#chasis').val()+"&regno="+$('#regno').val()+"&vcolor="+$('#vcolor').val()+"&testd="+$('#testd').val()+"&techn="+$('#techn').val()+"&imeino="+$('#imeino').val()+"&rendate="+$('#rendate').val()+"&rmks="+$('#rmks').val()+"&repl="+$('#repl').val()+"&cussign="+$('#cussign').val(), function(data){
                                if(data.trim()!=="") { alert('Job Card Saved with No. '+data); window.close(); } });
	 	}						
});

$("#btmoddjc").click(function(e) {
e.preventDefault();
if( $('#cname').val() === ''  ){ document.getElementById('valerr').value="Customer Name Cannot be Blank!";	}
else if( $('#jdate').val() === ''  ){ document.getElementById('valerr').value="Date Cannot be Blank!";	}
else if( $('#imeino').val() === ''  ){ document.getElementById('valerr').value="IMEI No. Cannot be Blank!";	}
else if( $('#chasis').val() === ''  ){ document.getElementById('valerr').value="Chasis Cannot be Blank!";	}
else if( $('#cname').val() !== '' && $('#jdate').val() !== '' && $('#chasis').val() !== '' && $('#imeino').val() !== '') {
		var savjcdet = $.post("savejc.php?e=1&cname="+$('#cname').val()+"&jdate="+$('#jdate').val()+"&vehmod="+$('#vehmod').val()+"&chasis="+$('#chasis').val()+"&regno="+$('#regno').val()+"&vcolor="+$('#vcolor').val()+"&testd="+$('#testd').val()+"&techn="+$('#techn').val()+"&jcno="+$('#jcno').val()+"&rendate="+$('#rendate').val()+"&rmks="+$('#rmks').val()+"&repl="+$('#repl').val()+"&cussign="+$('#cussign').val(), function(data){
                                if(data.trim()!=="") { window.close(); } });
	 	}
						

});

});
</script>
<?php 
if($_REQUEST['e'] === "0"){
	$cname = '';
	$jdate = date("d-m-Y");
	$vehmod = '';
	$regno = '';
	$chasis = '';
	$vcolor = '';
	$testd = '';
	$rendate = '';
	$imeino = '';
	$techn = '';
	$testd = '';
	$rmks = '';
        $cussign = '';
    $repl = 0;
	$jcno = 0;
}
else {
	$jcno = $_REQUEST['jc'];
	require_once 'includes/database.php';

	$db=new MySQLDatabase();
	$db->open_connection();
	$query = "SELECT cname, jdate, vehmod, regno, chasis, vcolor, testd, rendate, imeino, techn, rendate, cussign, rmks, repl FROM jcards WHERE jcno = '$jcno';";
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
		$cname = $row['cname'];
		$jdate = substr($row['jdate'],8,2)."-".substr($row['jdate'],5,2)."-".substr($row['jdate'] ,0,4);
		$vehmod = $row['vehmod'];
		$regno = $row['regno'];
		$chasis = $row['chasis'];
		$vcolor =$row['vcolor'];
		$testd = $row['testd'];
        $repl = $row['repl'];
		$rendate = substr($row['rendate'],8,2)."-".substr($row['rendate'],5,2)."-".substr($row['rendate'] ,0,4);
		$imeino = $row['imeino'];
		$techn = $row['techn'];
		$testd = $row['testd'];
		$rmks = $row['rmks'];
                $cussign = $row['cussign'];
	}
	else { echo '<script type="text/javascript"> window.close();</script>';}
	$db->close_connection();
}
?>   
</head><body>
<p class="titletr"> <?php echo ($_REQUEST['e'] === "0") ? 'New JobCard' : 'Edit JobCard No. '.$jcno ; echo str_repeat('&nbsp;', 4); ?> <input type="text" id="valerr" name="valerr" value="" size="30"> <?php echo str_repeat('&nbsp;', 6); ?> <a href="#"><img src="images/close.png" onclick="window.close();"> </a>  </p>       
<form id="jcform" method="post" onkeypress="return event.keyCode!=13" enctype="multipart/form-data" action="#" >
                    <center>
                    <table cellspacing="5px" id="regaddbox">
					<tr><td align="left"><label for="jdate"> Date &nbsp;</label></td>
                    <td><input type="text" id="jdate" name="jdate" value="<?php echo $jdate; ?>" size="20"/> </td>
                    </tr>
                    <tr><td align="left"><label for="cname">Customer Name &nbsp; </label></td>
                    <td><select id="cname" name="cname"><?php echo '<option value="'.$cname.'">'.$cname.'</option>'; echo selcus(); ?></select> <input type="hidden" id="jcno" name="jcno" value= "<?php echo $jcno; ?>"> </td>
                    </tr>
                    <tr><td align="left"><label for="vehmod"> Vehicle Model &nbsp;</label></td>
                    <td><input type="text" id="vehmod" name="vehmod" value="<?php echo $vehmod; ?>" size="40"/> </td>
                    </tr>
                    <tr><td align="left"><label for="chasis"> Chassis No. &nbsp;</label></td>
                    <td><input type="text" id="chasis" name="chasis" value="<?php echo $chasis; ?>" size="20"/> </td>
                    </tr>
                    <tr><td align="left"><label for="regno"> Reg. No. &nbsp;</label></td>
                    <td><input type="text" id="regno" name="regno" value="<?php echo $regno; ?>" size="10"/> </td>
                    </tr>
		    <tr><td align="left"><label for="vcolor"> Vehicle Colour &nbsp;</label></td>
                    <td><input type="text" id="vcolor" name="vcolor" value="<?php echo $vcolor; ?>" size="20"/> </td>
                    </tr>
		    <tr><td align="left"><label for="testd"> Tested ? &nbsp;</label></td>
                    <td><input type="checkbox" id="testd" name="testd" <?php if ($testd) { echo 'checked="checked" ';} ?> value="<?php echo $testd; ?>" /> </td>
                    </tr>
                    <tr><td align="left"><label for="repl"> Job Type  &nbsp;</label></td>
                    <td><select id="repl" name="repl"> <?php echo '<option value="'.$repl.'">'.($repl==="0" ? "FIXING" : "REPLACEMENT").'</option>'; echo '<option value="0">FIXING</option> <option value="1">REPLACEMENT</option>'; ?> </select> </td>
                    </tr>
		    <tr><td align="left"><label for="techn"> Technician &nbsp;</label></td>
                    <td><input type="text" id="techn" name="techn" required="requirred" value="<?php echo $techn; ?>" size="20"/> </td>
                    </tr>
					<tr><td align="left"><label for="imeino"> IMEI No. &nbsp;</label></td>
                    <td><?php if($_REQUEST['e'] === "0"){ echo '<select id="imeino" name="imeino" style="width:200px;">'; echo selimei(); echo '</select>'; }
								else { echo '<input type="text" id="imeino" disabled="disabled" name="imeino" size="20" value="'.$imeino.'"/>'; } ?> </td> 
                    </tr>
					<tr><td align="left"><label for="rendate"> Renewal Date &nbsp;</label></td>
                    <td><input type="text" id="rendate" name="rendate" value="<?php echo $rendate; ?>" size="20"> </td>
                    </tr>
		    <tr><td align="left"><label for="cussign"> Signed By &nbsp;</label></td>
                    <td><input type="text" id="cussign" name="cussign" value="<?php echo $cussign; ?>" size="20"> </td>
                    </tr>
			<tr><td align="left"><label for="rmks"> Remarks &nbsp;</label></td>
                    <td><input type="text" id="rmks" name="rmks" value="<?php echo $rmks; ?>" size="20"/> </td>
                    </tr>		
                    <tr><td colspan=2> </td></tr><br />
                    <tr><td colspan=2 align="center">
		<?php if($_REQUEST['e'] === "0"){  echo '<input type="button" class="text ui-widget-content ui-corner-all" style="width:160px;font-weight:bold;" id="btnaddjc" name="btnaddjc" value=" Add JobCard " />';}
			  else {echo '<input type="button" class="text ui-widget-content ui-corner-all" style="width:160px;font-weight:bold;" id="btmoddjc" name="btmoddjc" value=" Save Details " />';}	
		?>
                    </td></tr>
                    </table></center>
        </form>
</body>
</html>

<script type="text/javascript">     
$('#techn').autocomplete({ source: <?php echo alltechs(); ?>, minLength: 2  });                                                              
$('#jdate').datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"}); 
$('#rendate').datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"}); 


</script>



