<?
session_start();
include('includes/config.php');
include('defs.php');

if(strlen($_SESSION['aid'])==0)
  { header('location:index.php');
}

$usr=$_SESSION['uname'];



 ?>


 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>GLAKB  | Company Invoice</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../plugins/fontawesome-free/css/all.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../dist/css/adminlte.min.css">
  <!--Function Email Availabilty---->

</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
  <!-- Navbar -->
<?php include_once("includes/navbar.php");?>
  <!-- /.navbar -->

  <!-- Main Sidebar Container -->
 <?php include_once("includes/sidebar.php");?>

 
  <div class="content-wrapper">

<?

$result12 = mysqli_query($link, "select Name, Role from user_details where Name='$usr' ");
$row12=  mysqli_fetch_object($result12);
$role = $row12->Role;
if($role=='Operator')
{
echo"<center>";
echo"<br><br><br><br><br>";
echo"<img src='users.png' width='160' height='160'><br>";
echo "<font size=6 color=red><b>ACCESS DENIED!</b><br>";
echo "<font size=4 color=darkblue><b>You Do Not Have Permission to View This Page.</b><br>";
goto a;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
?>
  <html>
<head>
<style>
#customers {
  font-family: Times-Roman;
  border-collapse: collapse;
  width: 98%;
}

#customers td, #customers th {
  border: 1px solid #4444;
  padding: 4px;
}

#customers tr:nth-child(even){background-color:gainsboro ;}


#customers th {
  padding-top: 5px;
  padding-bottom: 5px;
  text-align: center;
  background-color:dodgerblue;
  color: white;
}
</style>   

<script language="javascript" type="text/javascript">

function removeSpaces(string) {
 return string.split(' ').join('');
}
function validateForm()
  {




if((document.ind.name.value) == '')
    
  {
      alert("Please Enter User Name First!");
document.ind.name.focus();

      return false;
    }
    
if((document.ind.psw.value) == '')
    
  {
      alert("Please Enter Password!");
document.ind.psw.focus();

      return false;
    }
    
if((document.ind.cpsw.value) == '')
    
  {
      alert("Please Enter Confirm Password!");
document.ind.cpsw.focus();

      return false;
    }





if((document.ind.psw.value) != (document.ind.cpsw.value))
    
  {
      alert("Passowrd and Confirm Password do not Match!");
document.ind.cpsw.focus();

      return false;
    }


    return true;
  }

 function show_cbo1()
                {
                        var val = document.getElementById('strzone').value;
                        location.href = "add_user.php?Circ="+val;
                }

</script>

<?
echo "<form name='ind' action='' onSubmit='return validateForm()' method='post'>";
?>


<link rel="stylesheet" href="css/style.css">

              
             


<table align="center" id="customers">
       
<tr>
    <th colspan="4"><font size="4">SOFTWARE OPERATOR CREATION FORM</th>

  </tr>                                   

<tr>





<?
TextField("<b><font size=3 color=black>User Name:</font></b>", "name", "", 40, 100, 0, "onChange=\"this.value=removeSpaces(this.value);\" autofocus" );


?>

<td><b><font size=3 color="black">User Type:</font></b></td>
<td>
<select class="form-control" name=typee>
<option value='Administrator'>Administrator</option>
<option value='Operator'>Operator</option>

</select>

<tr>
<td width="200"><b><font size=3 color="black">Designation (If Any):</b></td>
<td><input type="text" name="desig" size="40" onkeydown="return tab(event)" required /></td>
<tr>   


<td width="200"><b><font size=3 color="black">Contact #:</b></td>
<td><input type="text" name="cont" size="20" onkeydown="return tab(event)" required /></td>
<tr>   

<td width="200"><b><font size=3 color="black">Password:</b></td>
<td><input type="text" name="psw" size="20" onkeydown="return tab(event)" required /></td>
<tr>   

<td width="200"><b><font size=3 color="black">Confirm Password:</b></td>
<td><input type="text" name="cpsw" size="20" onkeydown="return tab(event)" required /></td>
<tr>   

                            
                             

<td colspan="3" align="center">                             
<input type="submit" name="request" value="Create User" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">

    

    
   </table>                                  
                              
                            
                          
                         
 
               
 

     <?               
  if(isset($_POST['request'])) {
$opass = mysqli_real_escape_string($link, $_POST['psw']);
$cpass = mysqli_real_escape_string($link, $_POST['cpsw']);
$name = mysqli_real_escape_string($link, $_POST['name']);
$cont = mysqli_real_escape_string($link, $_POST['cont']);
$desig = mysqli_real_escape_string($link, $_POST['desig']);
$typee=$_POST['typee'];

$result = "select Name from user_details where Name  = '$name'";
$check_result = mysqli_query($link, $result);
if (mysqli_num_rows($check_result) > 0) {
echo "<script language='javascript'>";
echo "alert('Error: User-Name Already Exists!')";
echo "</script>";
goto a;
}

else
{


$sql = "INSERT INTO user_details (Name, pass, Role, Active, Contact, Designation)
                    VALUES ('$name', '$opass','$typee','1', '$cont', '$desig')";

            if (mysqli_query($link, $sql)) {
              $last_id = mysqli_insert_id($link);
                echo ' <div class="col-xl-4">';
              echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
                        <i class="fas fa-check-circle mr-1"></i> User Details added successfully. 
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>';
            } 


if($typee=='Operator')
{



mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Add-Mheads','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Add-Cust','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Add-Prod','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Add-Head','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Edit-Mhead','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Edit-Head','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Add-Raw','0')");

mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Raw-Pur','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Raw-Out','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Raw-Stock','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Raw-Dead','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Inv-Pur','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Inv-Sales','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','P-List','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Cash','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Cash-Rec','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','JV','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','JV-Search','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Rep-Mhead','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','JV-Date','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Rep-Head','0')");
mysqli_query($link, "insert into user_priv (Name,Form,Role) values ('$name','Rep-Cust','0')");

mysqli_close($link);
}
}
}

               
?>
<?php 
a:
include"footer.php"; ?>
</div>
<!-- ./wrapper -->

<!-- jQuery -->
<script src="../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- bs-custom-file-input -->
<script src="../plugins/bs-custom-file-input/bs-custom-file-input.min.js"></script>
<!-- AdminLTE App -->
<script src="../dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../dist/js/demo.js"></script>
<!-- Page specific script -->
<script>
$(function () {
  bsCustomFileInput.init();
});
</script>
</body>
</html>
<?php  ?>