
<?php

require_once('dbconnection.php');//DB Connection

session_start();
if(!$_SESSION['emp_email'] || $_SESSION['emp_type'] !== 'admin')  
{  
header("Location: login.php");//redirect to login page to secure the welcome page without login access.  
}  


if($_SERVER['REQUEST_METHOD'] == "POST")
{
 // Username and Password sent from Form
 $emp_n = $_POST['e_name'];
 $username = $_POST['user'];
 $password = $_POST['pass'];
 $password = md5($password); //Password Encrypted
 $e_phone = $_POST['e_phone'];
 $e_type = $_POST['e_type'];
 $sql = "INSERT INTO employee(emp_name,emp_email,emp_password,emp_type) values('$emp_n','$username','$password','$e_type')";
 $result = mysqli_query($con, $sql);
 header("location: login.php");

}
?>
<!DOCTYPE html>
<html lang="en" class="h-100">

<head>
    <meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="keywords" content="admin, dashboard">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	
	<!-- PAGE TITLE HERE -->
	<title>راجستر کاربر</title>
	
	<!-- FAVICONS ICON -->
	<link rel="shortcut icon" type="image/png" href="images/favicon.png">
    <link href="css/style.css" rel="stylesheet">
    <link rel="stylesheet" href="dist/sweetalert.css"> 
    <!--<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> -->
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
  

</head>

<body class="vh-100">
    <div class="authincation h-100">
        <div class="container h-100">
            <div class="row justify-content-center h-100 align-items-center">
                <div class="col-md-6">
                    <div class="authincation-content">
                        <div class="row no-gutters">
                            <div class="col-xl-12">
                                <div class="auth-form">
									<div class="text-center mb-3" >
                                    <img src="images/logo1.png" alt="" height="150" >
									</div>
                                    
                                    <h4 class="text-center mb-4">راجستر کاربر</h4>
                                    <form action="<?php $_SERVER['PHP_SELF'];?>" method="post" name="reg_form" dir="rtl">
                                        <div class="mb-3">
                                            <label class="mb-1" for="emp_name"><strong>اسم کاربر</strong></label>
                                            <input type="text" name="e_name" id="e_name" class="form-control" placeholder="نام"><span id="lblError" style="color: red"></span>
                                        </div>
                                        
                                       


                                        <div class="mb-3">
                                            <label class="mb-1" for=""><strong>نوعیت کاربر</strong></label>
                                            <select name="e_type" id="e_type" class="form-control">
                                                <option value="">انتخاب</option>
                                                <option value="admin">ریس</option>
                                                <option value="staff">کارمند</option>
                                            </select>
                                        </div>

                                        <div class="mb-3">
                                            <label class="mb-1" for="email_add"><strong>ایمیل آدرس</strong></label>
                                            <input type="email" name="user" id="user" class="form-control"  placeholder="" onkeyup="ValidateEmail();"><span id="lblErroremail" style="color:red"></span>
                                        </div>
                                        <div class="mb-3">
                                            <label class="mb-1"><strong>رمز</strong></label>
                                            <input type="password" name="pass" id="pass" class="form-control" value="" data-validation-length="min:3,max:35">
                                        </div>
                                        <div class="text-center mt-4">
                                            <button type="submit" name="submit" id="submit" class="btn btn-primary btn-block">ثبت سیستم</button>
                                        </div>
                                    </form>
                                  
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<!--**********************************
	Scripts
***********************************-->
<!-- Required vendors -->
<script src="vendor/global/global.min.js"></script>
<script src="js/custom.min.js"></script>
<script src="js/dlabnav-init.js"></script>
<script src="js/styleSwitcher.js"></script>
<script src="dist/sweetalert.min.js"></script>
	<script src="vendor/jquery-nice-select/js/jquery.nice-select.min.js"></script>
   
    <script type="text/javascript">
   /*
   function Validatephone() {
        var phone = document.getElementById("e_phone").value;
        var lblErrorcontact = document.getElementById("lblErrorcontact");
        lblErrorcontact.innerHTML = "";
        var expr = /^((\+*)93|0093)?0?7[0-9]{8}$/;
        if (!expr.test(phone)) {
            lblErrorcontact.innerHTML = "Warning ..!    "+"Your Number is incorrect";
        }
    }
    */

    function ValidateEmail() {
        var email = document.getElementById("user").value;
        var lblErroremail = document.getElementById("lblErroremail");
        lblErroremail.innerHTML = "";
        var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        if (!expr.test(email)) {
            lblErroremail.innerHTML = "Oops...!    "+"Invalid email address.";
        }
    }

    $("#submit").click(function(){
        var email = document.getElementById("user").value;
        var lblErroremail = document.getElementById("lblErroremail");
        lblErroremail.innerHTML = "";
        var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;


            var emp =$("#e_name").val();
            var user_email = $("#user").val();
            var password = $("#pass").val();

      if(emp == null || emp == "" ){
      sweetAlert("warning","اسم کاربر لازمی میباشد","warning");  
      return false;  
      }
      else if(user_email == null || user_email == ""){
        sweetAlert("Warning","ایمیل آدرس لازمی میباشد","warning");  
        return false;  
      }
      else if(!expr.test(email)){
        sweetAlert("Warning","ایمیل آدرس درست نیست","warning");  
        return false;  
      }

      else if(password == null || password == ""){
        sweetAlert("Warning","رمز سیستم لازمی میباشد","warning");  
        return false;  
      }
      else
      swal("معلومات شما موفقانه ثبت سیستم شد","","success")
                window.setTimeout(function(){ 
                    form.submit();              ;
            } ,1500);
        
        });
</script>
</body>
</html>
