<?php 	
session_start();
require_once '../../Server/connection.php';

if($_POST){	

  $staff_id = $_POST['staff_id'];
  //$roleId = $_POST['role_id'];
  
    $query153 = "select * from staff where staff_id='$staff_id'";
    $result153 = $conn->query($query153);
    $row153 = $result153->fetch_array();
    $FirstName = $row153['firstname'];
	$LastName = $row153['lastname'];
	$deptId = $row153['dept_id'];
	$Email = $row153['email'];
	
	
	/*
	$query1532 = "select * from users";
    $result1532 = $conn->query($query1532);
	$num1532 = mysqli_num_rows($result1532);
	
	if($num1532==0){
	$UserId = $staff_id;	
	}else{
	$query154 = "select max(user_id) from users";
    $result154 = $conn->query($query154);
    $row154 = $result154->fetch_array();
    $UserId = $row154['max(user_id)'] + 1;
         }
		 */
  
  $passwd = addslashes(strtoupper($LastName.'123'));
  $password = md5(addslashes(strtoupper($LastName.'123')));
  
  $link = "<a href='https://ems.edgeec.co.tz/'>CMS</a>"; 
  
  $fromEmail = 'info@brightvision.co.tz';
  $toEmail = $Email;
  $space = " ";
  
   $message = "Hello".$space.$FirstName.$space.$LastName.$space."You have been registered into Bright Vision Customer Management System - CMS, You will login using your email".$space.$Email.$space."and your default password will be".$space.$passwd.$space."You can change your password after login".$space."Please Click the following link to login into the System".$space.$link;
   $subject = "User Account Creation";
   
   $query150 = "select * from users where staff_id='$staff_id'";
   $result150 = $conn->query($query150);
   $num150 = mysqli_num_rows($result150);
   
  
   if($num150==0){
	   
	   
    $query158 = "select max(user_id) from users";
    $result158 = $conn->query($query158);
    $row158 = $result158->fetch_array();
    $UserId8 = $row158['max(user_id)'] + 1;
	
	
   $staffSql = "INSERT INTO users (user_id,staff_id,password,status,role_id) 
				VALUES ('$UserId8','$staff_id','$password','1','1')";
				
   $result23 = $conn->query($staffSql);
   
   
   foreach($_POST["permission_id"] as $permissionId){  
$sql8q = "SELECT * FROM permissions WHERE permission_id = '$permissionId'"  or  die("There was an error");
$result8q = $conn->query($sql8q);
while($row8q = mysqli_fetch_array($result8q)){
$role_id = $row8q['role_id'];
$perm_cate_id = $row8q['perm_cate_id'];
           }
$query82 = "INSERT INTO assigned_permissions (perm_cate_id,permission_id,role_id,user_id) VALUES ('$perm_cate_id','$permissionId','$role_id','$UserId8')";
$result82 = $conn->query($query82);		  
             }
			 
			 
              if($result82){
					
			if(@mail($toEmail,$subject,$message,"From:" . $fromEmail)){
					
			$_SESSION['success_message'] = "User Has Been Given Access and Permissions Successfully";
                        header("Location: ../give_access.php");
                             exit();
							 
			    }
					
				}
  
   
     }else{
		 
		 
		 $sql8q1 = "SELECT * FROM users WHERE staff_id = '$staff_id'"  or  die("There was an error");
$result8q1 = $conn->query($sql8q1);
while($row8q1 = mysqli_fetch_array($result8q1)){
$user_id20 = $row8q1['user_id'];
           }
		 
		
     foreach($_POST["permission_id"] as $permissionId){
	 
	   
	   $sql8q = "SELECT * FROM permissions WHERE permission_id = '$permissionId'"  or  die("There was an error");
$result8q = $conn->query($sql8q);
while($row8q = mysqli_fetch_array($result8q)){
$role_id = $row8q['role_id'];
$perm_cate_id = $row8q['perm_cate_id'];
           }

 

$query82 = "INSERT INTO assigned_permissions (perm_cate_id,permission_id,role_id,user_id) VALUES ('$perm_cate_id','$permissionId','$role_id','$user_id20')";
$result82 = $conn->query($query82);		   
	   
	   
             }

		
				
				if($result82){
					
			if(@mail($toEmail,$subject,$message,"From:" . $fromEmail)){
					
			$_SESSION['success_message'] = "User Has Been Given Access and Permissions Successfully";
                        header("Location: ../give_access.php");
                             exit();
							 
			    }
					
				}
  
		 
	 }
   

 
} 