<?php

require_once("config/configDB.php"); 

$username = stripslashes($_POST['username']);
$pwd      = $username."@123";

$ename=$_POST['employeename'];

		$autoinc1=mysqli_query($conn,"ALTER TABLE user_login CHANGE id id INT(11) NOT NULL AUTO_INCREMENT  ");
	
	$autoinc2=mysqli_query($conn,"ALTER TABLE user_permission CHANGE id id INT(11) NOT NULL AUTO_INCREMENT  ");
	

$count=0;
$check_query	= mysqli_query($conn,"SELECT count(username) as counts FROM user_login WHERE username = '$username'  "); // Check the database


while($row = mysqli_fetch_array($check_query)){
$count=$row['counts'];
}
if($count!=0){
// Already Exsist


		echo "<script> alert ('Data Inserted Success.');</script>";
		?>
<script language="javascript">window.location.href = "usersMasterDashboard.php";</script>
<?php
		

}		
else
{


$sql      = "INSERT INTO user_login (ename,username,password,loginstatus,flag,user_permisson) VALUES ('$ename','$username','$pwd','2' ,'1','USER')";
mysqli_query($conn,$sql);

$userID = mysqli_insert_id($conn);

$sql_product = "SELECT * FROM user_permission_master where activestatus=1";
$result = $conn->query($sql_product);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {

        
        $filenames  = $row['filenames'];
        $dataadd    = $row['dataadd'];
        $dataedit   = $row['dataedit'];
        $datadelete = $row['datadelete'];
        $datashow   = $row['datashow'];
        $dataprint  = $row['dataprint'];
        $dataexcelformat    = $row['dataexcelformat'];        
        $activestatus       = $row['activestatus'];
		


        $sql_mapping  = "INSERT INTO user_permission (userID, filenames, dataadd, dataedit, datadelete, datashow, dataprint, dataexcelformat, activestatus) VALUES ('$userID', '$filenames', '$dataadd', '$dataedit', '$datadelete', '$datashow', '$dataprint', '$dataexcelformat', '$activestatus')";
        
        mysqli_query($conn,$sql_mapping);        
    }
} 
$conn->close();
}
header("Location:usersMasterDashboard.php");

?>