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

<?php

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

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

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "edit")) {
 /*
  $updateSQL = sprintf("UPDATE exit_records SET `date`=%s, student_adm_no=%s, `class`=%s, nurse=%s, complaint=%s, treatment=%s, duration=%s, bill=%s, report=%s WHERE s_no=%s",
                       GetSQLValueString($_POST['date'], "date"),
                       GetSQLValueString($_POST['adm_no'], "text"),
                       GetSQLValueString($_POST['class'], "text"),
                       GetSQLValueString($_POST['nurse'], "text"),
                       GetSQLValueString($_POST['complaint'], "text"),
                       GetSQLValueString($_POST['treatment'], "text"),
                       GetSQLValueString($_POST['duration'], "text"),
                       GetSQLValueString($_POST['bill'], "double"),
                       GetSQLValueString($_POST['report'], "text"),
                       GetSQLValueString($_POST['s_no'], "int"));
	*/
					   
//echo dateformat($_POST['start_date']);
//echo dateformat($_POST['end_date']);

//$start = strtotime(dateformat($_POST['tranx_date']));


  $updateSQL = sprintf("UPDATE accounts SET surname = %s, firstname=%s, middlename=%s, gender=%s, address=%s, opening_date=%s, balance=%s, phone=%s, location=%s WHERE account_no=%s",
                      // GetSQLValueString($_POST['date'], "date"),
                       GetSQLValueString($_POST['surname'], "text"),
                       GetSQLValueString($_POST['firstname'], "text"),
                       GetSQLValueString($_POST['middlename'], "text"),
                       GetSQLValueString($_POST['gender'], "text"),
                       GetSQLValueString($_POST['address'], "text"),
                       GetSQLValueString(dateformat($_POST['opening_date']), "date"),
                       GetSQLValueString($_POST['balance'], "int"),
                       GetSQLValueString($_POST['phone'], "text"),
					   GetSQLValueString($_POST['account_no'], "int"),
             GetSQLValueString($_POST['location'], "text"));

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

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


$colname_records = "-1";
if (isset($_GET['account_no'])) {
  $colname_records = $_GET['account_no'];
}

mysqli_select_db($conn, $database_conn);
$query_records = sprintf("SELECT * FROM accounts  WHERE account_no = %s", GetSQLValueString($colname_records, "text"));

$records = mysqli_query($conn, $query_records) or die(mysqli_error($conn));
$row_records = mysqli_fetch_assoc($records);
$totalRows_records = mysqli_num_rows($records);



?>
<html>
<head>
<title> Hylook Accounts - Edit 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">
<br />
<div style="font-family:Century Gothic; font-size:18px; font-weight:bold"> Editing Account for: <?php echo $row_records['surname']. " ". $row_records['firstname']. " ". $row_records['middlename'] . " - ".$row_records['account_no']; ?></div>


<form action="<?php echo $editFormAction; ?>" method="post" name="form1" class="form-group">
  
  <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="<?php echo $row_records['surname']; ?>" class="form-control" />
   </td>
   <td style="padding-right:10px;">
   <label>Firstname:</label> <br/>
  <input name="firstname" type="text"  value="<?php echo $row_records['firstname']; ?>" class="form-control" />
    </td>
    <td>
   <label>Middlename:</label> <br/>
  <input name="middlename" type="text"  value="<?php echo $row_records['middlename']; ?>" 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>
          <option value="Male" <?php if($row_records['status'] == "Male") { ?> selected="selected" <?php } ?> >Male</option>
		<option value="Female" <?php if($row_records['status'] == "Female") { ?> selected="selected" <?php } ?>>Female</option>						
  </select>
  
       <h5>Phone:</h5>
     <input type="text" name="phone" value="<?php echo $row_records['phone']; ?>" size="32"  class="form-Location" />


      <h5>Location:</h5>
<select name="location" class="form-control">
        <option selected="selected" value="" >Select State</option>
        <option value="Abia"  <?php if( strtolower($row_records['location']) == "abia" ) echo "selected=true"; ?>>Abia</option>
        <option value="Adamawa" <?php if( strtolower($row_records['location']) == "adamawa" ) echo "selected=true"; ?> >Adamawa</option>
        <option value="Akwa Ibom" <?php if( strtolower($row_records['location']) == "akwa ibom" ) echo "selected=true"; ?>>Akwa Ibom</option>
        <option value="Anambra" <?php if( strtolower($row_records['location']) == "anambra" ) echo "selected=true"; ?>>Anambra</option>
        <option value="Bauchi"  <?php if( strtolower($row_records['location']) == "bauchi" ) echo "selected=true"; ?>>Bauchi</option>
        <option value="Bayelsa" <?php if( strtolower($row_records['location']) == "bayelsa" ) echo "selected=true"; ?>>Bayelsa</option>
        <option value="Benue" <?php if( strtolower($row_records['location']) == "benue" ) echo "selected=true"; ?>>Benue</option>
        <option value="Borno" <?php if( strtolower($row_records['location']) == "borno" ) echo "selected=true"; ?>>Borno</option>
        <option value="Cross River" <?php if( strtolower($row_records['location']) == "cross river" ) echo "selected=true"; ?>>Cross River</option>
        <option value="Delta" <?php if( strtolower($row_records['location']) == "delta" ) echo "selected=true"; ?>>Delta</option>
        <option value="Ebonyi" <?php if( strtolower($row_records['location']) == "ebonyi" ) echo "selected=true"; ?>>Ebonyi</option>
        <option value="Edo" <?php if( strtolower($row_records['location']) == "edo" ) echo "selected=true"; ?>>Edo</option>
        <option value="Ekiti" <?php if( strtolower($row_records['location']) == "ekiti" ) echo "selected=true"; ?>>Ekiti</option>
        <option value="Enugu" <?php if( strtolower($row_records['location']) == "enugu" ) echo "selected=true"; ?>>Enugu</option>
        <option value="FCT" <?php if( strtolower($row_records['location']) == "fct" ) echo "selected=true"; ?>>FCT</option>
        <option value="Gombe" <?php if( strtolower($row_records['location']) == "gombe" ) echo "selected=true"; ?>>Gombe</option>
        <option value="Imo" <?php if( strtolower($row_records['location']) == "imo" ) echo "selected=true"; ?>>Imo</option>
        <option value="Jigawa" <?php if( strtolower($row_records['location']) == "jigawa" ) echo "selected=true"; ?>>Jigawa</option>
        <option value="Kaduna" <?php if( strtolower($row_records['location']) == "kaduna" ) echo "selected=true"; ?>>Kaduna</option>
        <option value="Kano" <?php if( strtolower($row_records['location']) == "kano" ) echo "selected=true"; ?>>Kano</option>
        <option value="Katsina" <?php if( strtolower($row_records['location']) == "katsina" ) echo "selected=true"; ?>>Katsina</option>
        <option value="Kebbi" <?php if( strtolower($row_records['location']) == "kebbi" ) echo "selected=true"; ?>>Kebbi</option>
        <option value="Kogi" <?php if( strtolower($row_records['location']) == "kogi" ) echo "selected=true"; ?>>Kogi</option>
        <option value="Kwara" <?php if( strtolower($row_records['location']) == "kwara" ) echo "selected=true"; ?>>Kwara</option>
        <option value="Lagos" <?php if( strtolower($row_records['location']) == "lagos" ) echo "selected=true"; ?>>Lagos</option>
        <option value="Nasarawa" <?php if( strtolower($row_records['location']) == "nasarawa" ) echo "selected=true"; ?>>Nasarawa</option>
        <option value="Niger" <?php if( strtolower($row_records['location']) == "niger" ) echo "selected=true"; ?>>Niger</option>
        <option value="Ogun" <?php if( strtolower($row_records['location']) == "ogun" ) echo "selected=true"; ?>>Ogun</option>
        <option value="Ondo" <?php if( strtolower($row_records['location']) == "ondo" ) echo "selected=true"; ?>>Ondo</option>
        <option value="Osun" <?php if( strtolower($row_records['location']) == "osun" ) echo "selected=true"; ?>>Osun</option>
        <option value="Oyo" <?php if( strtolower($row_records['location']) == "oyo" ) echo "selected=true"; ?>>Oyo</option>
        <option value="Plateau" <?php if( strtolower($row_records['location']) == "plateau" ) echo "selected=true"; ?>>Plateau</option>
        <option value="Rivers" <?php if( strtolower($row_records['location']) == "rivers" ) echo "selected=true"; ?>>Rivers</option>
        <option value="Sokoto" <?php if( strtolower($row_records['location']) == "sokoto" ) echo "selected=true"; ?>>Sokoto</option>
        <option value="Taraba" <?php if( strtolower($row_records['location']) == "taraba" ) echo "selected=true"; ?>>Taraba</option>
        <option value="Yobe" <?php if( strtolower($row_records['location']) == "yobe" ) echo "selected=true"; ?>>Yobe</option>
        <option value="Zamfara" <?php if( strtolower($row_records['location']) == "zamfara" ) echo "selected=true"; ?>>Zamfara</option>
            </select>

      <h5>Address:</h5>
      <textarea name="address"  type="textarea" class="form-control" rows="2"><?php echo $row_records['address']; ?></textarea> 
     
    <h5>Account no:</h5>
   <input type="text" name="account_no" value="<?php echo $row_records['account_no']; ?>" size="32" class="form-control" readonly="readonly" />
      
      <h5>Opening Balance: (NGN)</h5>
     <input type="number" name="balance" value="<?php echo $row_records['balance']; ?>" 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" value="<?php  echo dateformatReverse($row_records['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="Update Account" class="btn btn-primary" style="width:40%; height:auto;padding-left:10px;padding-left:10px;padding-top:8px;padding-bottom:11px; background-color:#f2f1f3; border-radius:5px; color:#333" /> &nbsp;&nbsp;&nbsp;&nbsp;
      <a href="accounts.php?account_no=<?php echo $row_records['account_no']; ?>" > <button type="button" class="btn btn-primary" style="width:40%; height:auto;padding-left:10px;padding-left:10px;padding-top:8px;padding-bottom:11px; background-color:#f2f1f3; border-radius:5px; color:#333" >CANCEL </button> </a>
    <?php //} ?>
    <input type="hidden" name="MM_insert" value="edit">
    <input type="hidden" name="MM_update" value="edit">
</form>
   
</div></div></div>
<br />
</body>
 
</html>
<?php
mysqli_free_result($records);
?>
