

<?php 
  session_start();
  include"inc/config.php";
   $errors = array(); // initialize variables
  $Role= ""; $Username = "";  $id = ""; $Phoneno="";    $Branch= "";
  $Id = 0; 
  $update = false;

  if (isset($_GET['edit'])) { 
    $id = $_GET['edit'];
  $record = mysqli_query($db, "SELECT * FROM users WHERE id= $id ");


    if (count($record) == 1 ) {
    $n = mysqli_fetch_array($record);
    $id = $n['Id'];  $Username = $n['Username'];  $Branch = $n['Branch']; 
    $Role = $n['Role']; $Phone1 = $n['Phone1']; $Phone2 = $n['Phone2'];  $Email = $n['Email'];  
    $Idno = $n['Idno'];  $Fname = $n['Fname'];  $Lname = $n['Lname']; 
    }

  }

if (isset($_POST['update'])) {
$id = $_POST['id']; $username = $_POST['username']; $branch= $_POST['branch'];
 $role= $_POST['role']; $fname= $_POST['fname']; $lname= $_POST['lname'];
$phone1= $_POST['phone1']; $phone2= $_POST['phone2']; $email= $_POST['email']; $idno= $_POST['idno'];

    $sql_a = "SELECT * FROM users WHERE Phone1='$phone1'";
    $sql_b = "SELECT * FROM users  WHERE Idno='$idno'";
    $sql_c = "SELECT * FROM users  WHERE Username='$username'";
  
    $res_a = mysqli_query($db, $sql_a);
    $res_b = mysqli_query($db, $sql_b);
    $res_c = mysqli_query($db, $sql_c);


    if (mysqli_num_rows($res_a) > 1) {
      array_push($errors, "Sorry! There exist a staff with that phone No - 1");
    }else if(mysqli_num_rows($res_b) > 1){
      array_push($errors, "Sorry! There exist a staff member with that Idno"); 
    }else if(mysqli_num_rows($res_c) > 1){
      array_push($errors, "Sorry! There exist a staff member with that Username");
    }

    else {


$sql = "UPDATE users SET Branch= '$branch', Role='$role', Fname='$fname', Lname='$lname',   Username='$username', 
       Phone1='$phone1', Phone2='$phone2', Email='$email', Idno='$idno'   WHERE id =$id";

if ($db->query($sql) === TRUE) {
 echo "<script type= 'text/javascript'>alert(' $fname - record  updated succesfully.');</script>";
 echo "<script type='text/javascript'>location.href = 'index.php?link=manage-staffs';</script>";
}
else{echo "<script type= 'text/javascript'>alert('Error!!! while updating.');</script>";}

}
}

?>


<!DOCTYPE>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
 body { font-family: helvetica, sans-serif; }
* {  box-sizing: border-box;}
body {  margin: 0;}

.top-aa{ padding:15px; text-align: left; width: 100%; font-size: x-small; color: #848482; margin-bottom: 1%;}
 
label{color:#3D2B1F; margin-left:1%;  }

input[type=text],select, input[type=password]{  width: 100%;  padding: 10px;  border: 1px solid #ccc;  border-radius: 4px; color:#848482;box-sizing: border-box;  margin-top: 10px;  margin-bottom: 5px;  resize: vertical; background: white;font-weight: bold; font-size: x-small;}

input[type=submit] {  background:#007AA5;  color: white; font-weight: bold;  padding: 12px 15px; float: right; border: none;  border-radius: 4px;  cursor: pointer;  margin-top: 4%;}
  
input[type=submit]:hover { opacity: .8;  padding: 13px 20px;}


.error { width: 100%;  padding: 8px;    color: white; margin-bottom: 2% ;background:#C95A49;  border-radius: 5px;  font-size: small;}

label a {color:red; font-weight: bold;}
.c1{width:33.33%; padding: 10px; float:left;}
.c2{width:33.33%; padding: 10px; float:left;}

.container { width:98%; margin: auto; padding: 15px; background:white; float: right;   margin-top: 2%;  }



.wrapper{ background:whitesmoke;  width:100%; border: 0px solid #a1a1a1;  margin:auto; resize: none;     overflow:hidden; }

.column-a {  float: left;  width:18%;  padding: 0px; height: 90%; background:white ;border-top: 2px solid whitesmoke;  overflow-x: auto; scrollbar-width: thin; }

.column-b { float: left; width: 82%;  padding: 15px; overflow-x: auto; scrollbar-width: thin; height: 90%; }


/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

@media screen and (max-width:600px) {
  .column-a { width: 100%; height: auto; }
  .column-b {  width: 100%; height:auto; }
.c1, .c2, .c3 { float: left; width: 100%; }
   }

</style>
</head>
<body>


<div class="row">

<div class="wrapper">

  <nav> <?php include "header.php"; ?></nav>


  <div class="column-a"><?php include "menu.php"; ?>  </div>
  
  <div class="column-b">

    <div class="top-aa">
<a href="index.php" style="color: black; text-decoration: none; font-weight: bold;">Home </a>
<a href="index.php?link=003managestaffs" style="color: black; text-decoration: none; ">// Manage Staff Accounts </a>
<a>// Update Record</a>
</div>

<form  method="post" >
<nav  class="container">
<input type="hidden" name="id" value="<?php echo $id; ?>">

<?php include('errors.php') ?>

<div class="c1">
<label>Station Name <a> * </a> </label>
      <select name="branch" value="<?php echo $Branch; ?>" >
        <option><?php echo $Branch; ?></option>
        <option>OL-JORO-OROK</option>
        <option>KANGUI</option>
        <option>NAKURU</option>

      </select>
</div>

<div class="c2">
<label>Staff Role <a> * </a> </label>
      <select name="role"  >
        <option><?php echo $Role; ?></option>
        <option>Admin</option>
        <option>Staff</option>
      </select>
</div>

<div class="c1">
<label>First Name <a> * </a> </label>
<input type="text"  name="fname" value="<?php echo $Fname; ?>"  placeholder="first name" >
</div>

<div class="c1">
<label>Last Name <a> * </a> </label>
<input type="text"  name="lname" value="<?php echo $Lname; ?>"  placeholder="last name" >
</div>


<div class="c1">
<label>Username <a> * </a> </label>
<input type="text"  name="username" value="<?php echo $Username; ?>"  placeholder="Username" >
</div>


<div class="c1">
<label>Phone - 1 <a> * </a> </label>
<input type="text"  name="phone1" value="<?php echo $Phone1; ?>"  placeholder="Phone- 1" >
</div>

<div class="c1">
<label>Phone - 2 </label>
<input type="text"  name="phone2" value="<?php echo $Phone2; ?>"  placeholder="Phone-2" >
</div>


<div class="c1">
<label>Email <a> * </a>  </label>
<input type="text"  name="email" value="<?php echo $Email; ?>"  placeholder="Email" >
</div>


<div class="c1">
<label>Id No <a> * </a> </label>
<input type="text"  name="idno" value="<?php echo $Idno; ?>"  placeholder="Idno" >
</div>


<input type="submit" name="update" value="Submit"><br><br><br>
</nav>

</nav>

  </form>


  </div>

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

</div>

</div>
</body>
</html>
