<?php require_once('Connections/conn.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";
if (isset($_GET['account_no'])) {
  $colname_Recordset1 = $_GET['account_no'];
}
 

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

$table = "";

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

if( isset($_POST['account_type']) && ($_POST['account_type'] != "")){
	$table = $_POST['account_type']; 
 $updateSQL = sprintf("INSERT INTO $table (credit, debit, date_entered) values( %s, %s, %s)",
					
                       GetSQLValueString($_POST['credit'], "int"),
					    GetSQLValueString($_POST['debit'], "int"),
                       GetSQLValueString(dateformat($_POST['date_entered']), "text"));
					 
  $Result1 = mysqli_query($conn, $updateSQL) or die(mysqli_error($conn));
           }


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

mysqli_select_db($conn, $database_conn);
$query_Recordset1 = "SELECT * FROM accounts ORDER BY surname ASC";
$Recordset1 = mysqli_query($conn, $query_Recordset1) or die(mysqli_error($conn));
$totalRows_Recordset1 = mysqli_num_rows($Recordset1);
$row_Recordset1 = mysqli_fetch_assoc($Recordset1);

?>


<html>
<head>
<title> Add Bank Accounts</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:24px; font-weight:bold"> Balance Sheet Entry</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;">Account Type:</div>
     
     <!-- <input type="text" name="sex" value="" size="32" /> -->
      <select name="account_type" class="form-control">
					
                                <option value="balance" >Balance Sheet</option>
                              
      </select>
	<br/>
    
 <div style="font-family:Century Gothic; font-size:15px;"> Credit:</div>
  <input type="number" name="credit" value="" class="form-control">
   
   	<br/>
 <div style="font-family:Century Gothic; font-size:15px;"> Debit:</div>
  <input type="number" name="debit" value="" class="form-control">
   
   
 <br/>
 <!--  <div style="font-family:Century Gothic; font-size:15px;"> Account No:</div>
  <input type="text" name="account_no" value="" class="form-control" readonly ="true"> -->
   
  
   <div style="font-family:Century Gothic; font-size:15px;"> Transaction Date: &nbsp;&nbsp; (DD/MM/YYYY HH:mm:ss) </label></div>
    <input type='text' class="form-control" id='datetimepicker1' name="date_entered" />
        <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="accounts.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">
<?php if (isset($_GET['account_no'])) {  ?>
 <input type="hidden" name="account_no" value="<?php echo $_GET['account_no']; ?>"> 
<?php } ?>  

</form>

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

<br />
 </body>
 
 </html>
<?php mysqli_free_result($Recordset1); ?>
