<?php  include_once('../config/db.php'); include_once('../config/check.php');  

if (filter_has_var(INPUT_POST, "submit")) 
try{

    $fname =    strtoupper($_POST['fname']);
     $mname =    '.';
     $lname =    strtoupper($_POST['lname']);
    $username = strtoupper($_POST['myusername']);
    $email =    strtoupper($_POST['email']);
    // $phone =    strtoupper($_POST['phone']);
    $job =           strtoupper($_POST['job']);
    // $username =      strtoupper($_POST['username']);
    $role =      strtoupper($_POST['role']);
    $uPassword = strtoupper($_POST['userPassword']);
    $branch =    strtoupper($_POST['branch']);
    
    $uHPassword = password_hash($uPassword, PASSWORD_BCRYPT);
    // $uImage = $_FILES['userImage']['name'];
    // $target = "images/".basename($uImage); 
    $status = 1 ;

   $sql = 'INSERT INTO sys_user ( fname,mname,lname,email,job,username,role,password,branch ) 
   VALUES(:fname,:mname,:lname,:email,:job,:username,:role,:password,:branch  )';
    
   $conn->prepare($sql)->execute( ['fname'=>$fname,'mname'=>$mname,'lname'=>$lname, 'email' =>$email,'job'=>$job,
   'username' => $username,'role'=>$role,'password'=> $uHPassword ,'branch'=>$branch ] );
     
  header("location:user.php");
} catch(Exception $e) { echo 'Message: ' .$e->getMessage();}

                  
?>

<!DOCTYPE html>
<html>
<head>
<?php include_once('../inc/slink.php');   ?>

</head>
<body <?php include_once('../inc/snav.php');  ?>>
<div class="wrapper">

  <!-- Main Header -->
    <header class="main-header">

    <!-- Logo -->
    <?php include_once('../inc/sbrand.php');   ?>


    <!-- Header Navbar -->
       <?php include_once('../inc/snavbar.php');   ?>

   
  </header>
 
  <!-- Left side column. contains the logo and sidebar -->
  <aside class="main-sidebar">

    <!-- sidebar: style can be found in sidebar.less -->
     <?php include_once('../inc/smenu.php');   ?>
     
    <!-- /.sidebar -->
  </aside>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
        <section class="content-header">
          <h1 class="listheader" >   <i class="fa fa-user-plus"></i> Add User    </h1>
            <ol class="breadcrumb">
            <!-- <li><a href="#"><i class="fa fa-dashboard"></i> </a></li>
            <li><a href="#"> </a></li>
            <li class="active"> </li> -->
            <li><a href="users" class="btn btn-default btn-md"> <i class="fa fa-bars"></i>  Listed Users  </a></li>
            </ol>
        </section>

    <section class="content container-fluid">   
       
        <div class="box box-default box-xs">
            <div class="box-body">
                <div class="row">
                <form class="form-horizontal" action="" method="post"  enctype="multipart/form-data"  >                  
        
                  <div class=""> 
                        
                        <div class="col-md-3">
                            <label> Full name</label>
                            <input type="text" class="form-control" id="" name="fname" placeholder="" autocomplete="off" >
                        </div>
                        <div class="col-md-3">
                            <label> Last name</label>
                            <input type="text" class="form-control" id="" name="lname" placeholder="" autocomplete="off" >
                        </div>
                        <div class="col-md-3">
                          <label> Username </label>
                          <input type="text" class="form-control" name="myusername"  required="" autocomplete="">
                        </div>
                        
                        <div class="col-md-3">
                            <label>Email</label>
                            <input type="email" class="form-control" id="" name="email" placeholder="" autocomplete="off" required="" >
                        </div>
                        <!-- <div class="col-md-3">
                          <label> Phone </label>
                            <input type="number" class="form-control" id="" name="phone" placeholder="" required="" >
                        </div> -->
                        <div class="col-md-3">
                          <label > Position </label>
                          <input type="text" class="form-control" id="" name="job" required=""  autocomplete="off" >
                        </div>
                    
                        
                        <div class="col-md-3">
                          <label > Roles </label>
                              <select class=" js-example-basic-single  " name="role" required=""  id="userrole" style="width:100%" >
                              
                                          <?php
                                          $stmt = $conn->prepare("SELECT * FROM sys_roles");
                                          $stmt->execute();
                                        while($data=$stmt->fetch(PDO::FETCH_ASSOC))
                                              {?>
                                <option> </option>
                                <option value="<?php echo $data['id']; ?>"><?php echo $data['name']; ?></option>
                                <?php } ?>
                              </select>
                        </div>

                         <div class="col-md-3">
                          <label > Branch </label>
                              <select class=" js-example-basic-single  " name="branch" required=""  id="userrole" style="width:100%" >
                              
                                          <?php
                                          $stmt = $conn->prepare("SELECT * FROM branches");
                                          $stmt->execute();
                                        while($data=$stmt->fetch(PDO::FETCH_ASSOC))
                                              {?>
                                <option> </option>
                                <option value="<?php echo $data['id']; ?>"><?php echo $data['name']; ?></option>
                                <?php } ?>
                              </select>
                        </div>

                        <div class="col-md-3">
                          <label "> Enter New password </label>
                          <input type="password" class="form-control" required="" autocomplete="off" id=""  name="userPassword" >                    
                        </div>
                        
                        
                    
                  </div>  
                        
                    <div class="form-group">
                      <div class="col-sm-offset-4 col-sm-8">
                        <button type="submit" name="submit" class="btn btn-default col-sm-4 subbtn  ">  <i class="fa fa-plus "></i> Add  user </button>
                      </div>
                    </div>
                        
                </form> 
                </div>
            </div>
        </div>                         
    </section>
</div>



  <!-- Main Footer -->
  <footer class="main-footer">
    <?php  include_once('../inc/sfooter.php'); ?>
  </footer>
  

</div>

<!-- REQUIRED JS SCRIPTS -->
<?php include_once('../inc/slinkjs.php') ?>
 

</body>
</html>

