<?php include("session_controller.php"); ?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  //$theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($theValue) : mysqli_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
//format date function
function dateformatReverse($datetime){
$received_timestamp = "";
 if($datetime != ""){
 $full_date = explode(" ", $datetime);
 $date = explode("-", $full_date[0]);
 $d = $date[2];
 $mo = $date[1];
 $y = $date[0];
 $time = explode(":",$full_date[1]);
 $h = $time[0];
 $min = $time[1];
 $s = $time[2];
 
 $received_timestamp = $d."/".$mo."/".$y." ".$h.":".$min.":".$s;
 }
 return $received_timestamp;
 }
 
function dateformat($datetime){
$received_timestamp = "";
 if($datetime != ""){
 $full_date = explode(" ", $datetime);
 $date = explode("/", $full_date[0]);
 $d = $date[0];
 $mo = $date[1];
 $y = $date[2];
 $time = explode(":",$full_date[1]);
 $h = $time[0];
 $min = $time[1];
 $s = $time[2];
 
 $received_timestamp = $y."-".$mo."-".$d." ".$h.":".$min.":".$s;
 }
 return $received_timestamp;
 }
 
 $colname_Recordset1 = "-1";

 

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
 

mysqli_select_db($conn, $database_conn);    
		 
 $updateSQL = sprintf("INSERT INTO assets (name, cost, quantity, description, date_created) values( %s, %s, %s, %s, %s)",
   GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['cost'], "int"),
					   GetSQLValueString($_POST['quantity'], "int"),
					  GetSQLValueString($_POST['description'], "text"),
                       GetSQLValueString(dateformat($_POST['date_created']), "text"));

  $Result1 = mysqli_query($conn, $updateSQL) or die(mysqli_error($conn));
}
        
  
if($Result1){

  $updateGoTo = "assets.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
  
}

?>


<html>
<head>
<title> Add Tranx</title>
<link href="style.css" rel="stylesheet" type="text/css" />

</head>
<body>
<?php include("links.php");?>
<div class="container">
<div class="row">
<div class="col-sm-6">

<div style="font-family:Century Gothic; font-size:18px; font-weight:bold">Create New Asset</div>



<form method="post" name="form1" action="<?php echo $editFormAction; ?>" class="form-group">
  <div class="table-responsive">	
	<br/>
 <div style="font-family:Century Gothic; font-size:15px;"> Name of Asset:</div>
  <input type="textfield" name="name" value="" class="form-control">
   
   <br/>
   <div style="font-family:Century Gothic; font-size:15px;">Quantity:</div>
     <input type="textfield" name="quantity" value="" class="form-control">
     <!-- <input type="text" name="sex" value="" size="32" /> -->
     <!-- <select name="tranx_type" class="form-control">
							  <option selected="selected" value="">Select</option>
							  <option value="credit" >Credit</option>
							  <option value="debit" >Debit</option>
						  
      </select>
    -->
      <br/>
      <div style="font-family:Century Gothic; font-size:15px;">Estimated Cost:</div>
   <input type="number" name="cost" value="" class="form-control"> 
    
  <br/>
 <div style="font-family:Century Gothic; font-size:15px;">Asset Description:</div>
  <input type="text" name="description" value="" class="form-control">
  <br/>
  
   <div style="font-family:Century Gothic; font-size:15px;"> Date Acquired: &nbsp;&nbsp; (DD/MM/YYYY HH:mm:ss) </label></div>
    <input type='text' class="form-control" id='datetimepicker1' name="date_created" />
        <script type="text/javascript">
            $(function () {
                $('#datetimepicker1').datetimepicker({
					format: 'DD/MM/YYYY HH:mm:ss'
				});
            });
        </script>	
        
        <br/><br/>				 
	<input type="submit" value="SAVE" class="btn btn-primary" style="width:40%; height:auto;padding-left:10px;padding-left:10px; background-color:#f2f1f3; border-radius:5px; color:#333"/> &nbsp; &nbsp; &nbsp; &nbsp; <a href="assets.php" class="btn btn-primary" style="width:40%; height:auto;padding-left:10px;padding-left:10px; background-color:#f2f1f3; border-radius:5px; color:#333">CANCEL </a></td>
   
  </div>

  <input type="hidden" name="MM_update" value="form1">


</form>

  </form></div></div></div>

<br />
 </body>
 
 </html>

