<?php
session_start();
require_once 'includes/database.php';
include 'includes/funcs.php';
$db=new MySQLDatabase();
$db->open_connection();
$user = $_SESSION['reusername'];
$cname = strtoupper($_REQUEST['cname']);
$vehmod = strtoupper($_REQUEST['vehmod']);
$chasis = strtoupper($_REQUEST['chasis']);
$vcolor = strtoupper($_REQUEST['vcolor']);
$regno = strtoupper($_REQUEST['regno']);
$techn = strtoupper($_REQUEST['techn']);
$rmks = strtoupper($_REQUEST['rmks']);
$imeino = $_REQUEST['imeino'];
$testd = $_REQUEST['testd'];
$repl = $_REQUEST['repl'];
$cussign = $_REQUEST['cussign'];
$rendate = substr($_REQUEST['rendate'],6,4)."-".substr($_REQUEST['rendate'],3,2)."-".substr($_REQUEST['rendate'] ,0,2);
$jdate = substr($_REQUEST['jdate'],6,4)."-".substr($_REQUEST['jdate'],3,2)."-".substr($_REQUEST['jdate'] ,0,2);

if($_REQUEST['e'] === '0'){													// add new	
	$jcno = getnewno('jcno');														
	$query = "INSERT INTO jcards (jcno, cname, jdate, vehmod, chasis, vcolor, regno, techn, imeino, testd, repl, cussign, rendate, rmks, us, ts) VALUES ('".$jcno."', '".$cname."', '".$jdate."', '".$vehmod."' , '".$chasis."', '".$vcolor."', '".$regno."', '".$techn."', '".$imeino."', '".$testd."', ".$repl.", '".$cussign."', '".$rendate."', '".$rmks."', '".$user."', '".date("Y-m-d H:i:s")."')";
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
	$query = "UPDATE devstock SET jcno = '$jcno' WHERE imeino = '$imeino' ORDER BY tno DESC LIMIT 1";
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
	$db->close_connection();
	echo $jcno;  
}

else {																		// mod entry
	$jcno = $_REQUEST['jcno'];
	$query = "UPDATE jcards SET cname = '$cname', jdate ='$jdate', vehmod = '$vehmod', chasis = '$chasis', vcolor='$vcolor', regno='$regno', techn='$techn', testd='$testd', repl='$repl', cussign = '$cussign', rendate='$rendate', rmks='$rmks', us = '$user', ts ='".date("Y-m-d H:i:s")."' WHERE jcno = '$jcno';"; 
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
	$db->close_connection();
	echo "OK"; 
}

?>