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

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

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

  $insertSQL = sprintf("INSERT INTO accounts (surname, firstname, middlename, gender, account_no, no_of_tranx, `address`, opening_date, balance,phone,location) VALUES ( %s, %s, %s, %s, %s, %s, %s, %s,%s, %s, %s)",
                      
                       GetSQLValueString($_POST['surname'], "text"),
                       GetSQLValueString($_POST['firstname'], "text"),
                       GetSQLValueString($_POST['middlename'], "text"),
                       GetSQLValueString($_POST['gender'], "text"),
                       GetSQLValueString($_POST['account_no'], "int"),
                       GetSQLValueString(0, "int"),
                       GetSQLValueString($_POST['address'], "text"),
					   GetSQLValueString(dateformat($_POST['opening_date']), "text"),
					    GetSQLValueString($_POST['balance'], "int"),
						 GetSQLValueString($_POST['phone'], "text"),
             GetSQLValueString($_POST['location'], "text"));

  mysqli_select_db($conn, $database_conn);
  $Result1 = mysqli_query($conn, $insertSQL) or die(mysqli_error($conn));

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

//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;
 }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add New Account</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">


<form action="<?php echo $editFormAction; ?>" method="post" name="form1" class="form-group">
   
  
  <h3> Add New Account</h3>
  <br/>
    <div class="table-responsive">
    <table width="100%" class="table-borderless">  
    <th>
    <td style="padding-right:10px;"><label>Surname:</label><br/>
  <input name="surname" type="text"  value="" class="form-control" />
   </td>
   <td style="padding-right:10px;">
   <label>Firstname:</label> <br/>
  <input name="firstname" type="text"  value="" class="form-control" />
    </td>
    <td>
   <label>Middlename:</label> <br/>
  <input name="middlename" type="text"  value="" class="form-control" />
  </td></th></table>
  </div>
 
 
      <h5>Gender:</h5>
     
     <!-- <input type="text" name="sex" value="" size="32" /> -->
      <select name="gender" class="form-control">
							  <option selected="selected" value="">Select</option>
							  <option value="Male"  >Male</option>
							  <option value="Female" >Female</option>
						  
  </select>
       <h5>Phone:</h5>
     <input type="text" name="phone" value="" size="32"  class="form-control" />

  
      <h5>Location:</h5>
       <select name="location"  class="form-control" >
                <option selected="selected" value="" >Select City</option>
                                <option value="Abia" >Abia</option>
                                <option value="Adamawa" >Adamawa</option>
                                <option value="Akwa Ibom" >Akwa Ibom</option>
                                <option value="Anambra" >Anambra</option>
                                <option value="Bauchi"  >Bauchi</option>
                                <option value="Bayelsa" >Bayelsa</option>
                                <option value="Benue" >Benue</option>
                                <option value="Borno" >Borno</option>
                                <option value="Cross River">Cross River</option>
                                <option value="Delta" >Delta</option>
                                <option value="Ebonyi" >Ebonyi</option>
                                <option value="Edo" >Edo</option>
                                <option value="Ekiti">Ekiti</option>
                                <option value="Enugu" >Enugu</option>
                                <option value="FCT" >FCT</option>
                                <option value="Gombe">Gombe</option>
                                <option value="Imo" >Imo</option>
                                <option value="Jigawa" >Jigawa</option>
                                <option value="Kaduna" >Kaduna</option>
                                <option value="Kano" >Kano</option>
                                <option value="Katsina" >Katsina</option>
                                <option value="Kebbi">Kebbi</option>
                                <option value="Kogi" >Kogi</option>
                                <option value="Kwara" >Kwara</option>
                                <option value="Lagos" >Lagos</option>
                                <option value="Nasarawa">Nasarawa</option>
                                <option value="Niger" >Niger</option>
                                <option value="Ogun" >Ogun</option>
                                <option value="Ondo" >Ondo</option>
                                <option value="Osun" >Osun</option>
                                <option value="Oyo">Oyo</option>
                                <option value="Plateau">Plateau</option>
                                <option value="Rivers">Rivers</option>
                                <option value="Sokoto">Sokoto</option>
                                <option value="Taraba">Taraba</option>
                                <option value="Yobe">Yobe</option>
                                <option value="Zamfara">Zamfara</option>              
                            </select>

      <h5>Address:</h5>
      <textarea name="address"  type="textarea" class="form-control" rows="2"></textarea> 
     
    <h5>Account no:</h5>
   <input type="text" name="account_no" value="<?php echo date('ymdgi',time()); ?>" size="32" class="form-control" readonly="readonly" />
      
      <h5>Opening Balance: (NGN)</h5>
     <input type="number" name="balance" value="" size="32"  class="form-control" />
      
		  <h5><label>Opening Date: &nbsp;&nbsp; (DD/MM/YYYY HH:mm:ss) </label></h5>
    <input type='text' class="form-control" id='datetimepicker1' name="opening_date" />
        <script type="text/javascript">
            $(function () {
                $('#datetimepicker1').datetimepicker({
					format: 'DD/MM/YYYY HH:mm:ss'
				});
            });
        </script>					 
							
  <br />
 <br/>
       <?php //if(isset($_SESSION['MM_UserGroup']) && ($_SESSION['MM_UserGroup'] == "admin")){ ?>
      <input type="submit" value="Add Account" class="btn btn-primary" />
    <?php //} ?>
    <input type="hidden" name="MM_insert" value="form1" />
</form>

<p>&nbsp;</p>
</div></div></div>
</body>
</html>