<?php
 	session_start(); 

	if(!isset($_SESSION['latcsuperloginsesion']))
	{
		header('Location: index.php');
		return;
	}

	
	if($_SERVER['REQUEST_METHOD'] === 'POST')
	{
		if(!isset($_POST['_token']) || ($_POST['_token']!==$_SESSION['_token'] ) )
		{
			header('Location: index.php');
			return;
		}
	}
	else
	{
		header('Location: index.php');
		return;
	}
	$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));
	
	
	include("connect.php");
	$mumid=$_SESSION['latcmumid'];
	$ufullname=$_POST['ufullname'];
	$ustatus="Active";
	$umobile="";
	$userpass=$_POST['userpass'];
	$uemail=$_POST['uemail'];
	$username=$_POST['username'];
	$ugroup="";

														
		$mumid=$_POST['mumid'];
	
	
	
	$eve = "select * from res_user_master where username='$username'";
	$re = mysqli_query($conn, $eve);
	if(mysqli_num_rows($re) > 0)
	{
		$_SESSION['msg1'] = "Try Diffrent Mobile Number";
		header('Location: user.php');
	}
	else
	{	
		$totalusers=0;
		$eve11 = "select * from res_mainuser_master where mumid=$mumid";
		$re11 = mysqli_query($conn, $eve11);
		if(mysqli_num_rows($re11)>0)
		{
			while($rt11 = mysqli_fetch_assoc($re11))
			{
			 	$totalusers=$rt11['totalusers'];
			}	
		}
			$eve12 = "select * from res_user_master where mumid=$mumid";
			$re12 = mysqli_query($conn, $eve12);
			if(mysqli_num_rows($re12)<$totalusers)
			{
				
				$eve1 = "INSERT INTO res_user_master (ufullname,username,userpass,ustatus,umobile,uemail,ugroup,mumid) VALUES('$ufullname','$username','$userpass','$ustatus','$umobile','$uemail','$ugroup','$mumid')";
				$re1 = mysqli_query($conn, $eve1);
				$_SESSION['msg'] = "user successfully added.";
			}
		
		
	}
	header('Location: user.php');
?>