<?php
        $uName     = $_POST['uName'];
		$mobile    = $_POST['mobile'];
		$email     = $_POST['email'];
		$state     = $_POST['state'];
		$district  = $_POST['district'];
		$pin       = $_POST['pin'];
		$address   = $_POST['address'];
        
        if($userType=="ADMIN")
        {   
            if($newUserType=="API" || $newUserType=="MASTER")
            {
                $Parent   = $userid;
                $G_Parent = "";
            }
            elseif($newUserType=="RETAILER" || $newUserType=="DISTRIBUTER")
            {
                $Parent     = $_POST['Parent'];
                $dataParent = getUser($Parent);
                $G_Parent   = $dataParent['Parent'];
            }
            elseif($newUserType=="ADMIN")
            {
                $Parent   = "";
                $G_Parent = "";
            }
		    
        }
        else
        {
            $Parent=$userid;
			$G_Parent=$dataUser['Parent'];
        }
        
        $quGetDupId="SELECT * FROM user WHERE Mobile='$mobile'";
		$reGetDupId=mysqli_query($con,$quGetDupId);
		$DupId=mysqli_num_rows($reGetDupId);
		
        if(empty($uName))
        {
            echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>Please Insert User's Name !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
        }
        elseif($DupId>0)
        {
            echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>This Mobile Number Already Exist !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
        }
        elseif(empty($mobile))
        {
            echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>Please Insert User's Mobile Number !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
        }
        elseif($state=="--Select State--")
        {
            echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>Select State Please !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
        }
        elseif($state=="--Select District--")
        {
            echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>Select District Please !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
        }
        else
        {   
            if($newUserType=="ADMIN")
            {   
		        $maxusrkey="AD";
            }
            elseif($newUserType=="API")
            {
               $maxusrkey="AU"; 
            }
            elseif($newUserType=="MASTER")
            {
               $maxusrkey="MU"; 
            }
            elseif($newUserType=="DISTRIBUTER")
            {
               $maxusrkey="DU"; 
            }
            elseif($newUserType=="RETAILER")
            {
               $maxusrkey="RU"; 
            }
            
            $querygetmaxid="SELECT Userid FROM user ORDER BY Id DESC";
		    $resultgetmaxid=mysqli_query($con,$querygetmaxid);
		    $row=mysqli_fetch_array($resultgetmaxid);
		    $maxid=$row['Userid'];
		    $maxusrid=substr($maxid,2);
		    $newMaxid=$maxusrid+1;
		    $newUsrid=$maxusrkey.$newMaxid;
			$pwd=rand(100000,999999);
			
			
			//Insert into User profile
		 	$query = "INSERT INTO user(Date,Userid,Password,Name,Mobile,Mail,State,City,Address,Pincode,Type,Parent,G_Parent)
			values('$datetime','$newUsrid','$pwd','$uName','$mobile','$email','$state','$district','$address','$pin','$newUserType','$Parent','$G_Parent')";
			if(mysqli_query($con,$query))
			{   
			    if($newUserType=="API" || $newUserType=="ADMIN")
			    {
			        $quOp="SELECT * FROM operator WHERE Status='ACTIVE'";
			    }
			    else
			    {
			        $quOp="SELECT * FROM operator WHERE Status='ACTIVE' AND Type='ALL'";
			    }
				$reOp=mysqli_query($con,$quOp);
				while($dataOp=mysqli_fetch_array($reOp))
				{
					$OpName      = $dataOp['Name'];
					$OpCode      = $dataOp['Id'];
					$ServiceType = $dataOp['ServiceType'];
					
					$quCommSlab="INSERT INTO comm_slab (Userid,OpName,OpCode) VALUES ('$newUsrid','$OpName','$OpCode')";
					mysqli_query($con,$quCommSlab);
					
					if($newUserType=="MASTER" || $newUserType=="API")
					{
					    if($ServiceType=="DTH")
	    				{
		    			    $useApiName = "RechBS";
			    		    $ApiOpCode  = $dataOp['RechBS'];
				    	}
					    else
	    				{
		    			    $useApiName = "M_ROBO";
			    		    $ApiOpCode  = $dataOp['M_ROBO'];
				    	}
					    
					    $quSetApi="INSERT INTO set_api (Userid,OpCode,UseApi,ApiOpCode) VALUES ('$newUsrid','$OpCode','$useApiName','$ApiOpCode')";
					    mysqli_query($con,$quSetApi);
					}
				}
				    if(!empty($email))
				    {
		    		    $toMail      = $email;
		    		    $toName      = $uName;
		    		    $subjectMail ="Welcome To LLP Recharge";
		    		    $msgMail     = "Dear $toName , Your LLP User Account Registration Is Successfull. Your Userid is $newUsrid & Password is $pwd , Login Your LLP Account on https://www.llprecharge.in/login.php";
			    	    require'../LinkFile/mail.php';
				    }
				    
				    echo "<div class='msg-div'>
				        <form method='post'>
					        <h3>Registration Successfully !</h3><br>
					        <h3>Userid : ".$newUsrid."</h3><br>
					        <h3>Password : ".$pwd."</h3><br>
					        <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
					        <button type='submit' class='btnPopup'>OK</button><br><br>
				        </form>
		            </div>";
			}
			else
			{
			    echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>Something Error !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
			}
		}
?>