<?php
session_start();
ob_start();
 include "../config/config.php"; 
 include "../config/function.php"; 
 
// print_r($_REQUEST);
 $private_key=get_privatekey($_SESSION['admin_name']);
 $request_from_server=$_SERVER['HTTP_REFERER'];
$this_server=$_SERVER['HTTP_HOST'];
if($request_from_server!=''){
	check_access_url($request_from_server,$this_server);
}

// echo $_SESSION['login_status'];echo "<br>";
// echo $_SESSION['private_key'];echo "<br>";
// echo $_SESSION['admin_name'];echo "<br>";

if($_SESSION['login_status']!='YES' || $_SESSION['private_key']!=$private_key || $_SESSION['private_key']=='' || $_SESSION['admin_name']=='')
{
    echo "Blank";
	header("Location:../AppLogin");
	exit();
}
else
{
    echo $member_name=strtoupper(text_input($_REQUEST['member_name']));
    echo $contact_no=text_input($_REQUEST['contact']);
    $position=$_REQUEST['position'];
    echo $sponcer_id=strtoupper(text_input($_REQUEST['sponsor_id']));
    echo $sponcer_name=strtoupper(text_input($_REQUEST['sponsor_name']));
    echo $promoter_id=strtoupper(text_input($_REQUEST['promoter_id']));
    $promoter_name=strtoupper(text_input($_REQUEST['promoter_name']));
    $pan_no=strtoupper(text_input($_REQUEST['pan_no']));
    $aadhar_no=strtoupper(text_input($_REQUEST['aadhar_no']));
    

$username="RGC".substr(str_shuffle("0123456789"),0,7);
$password=substr(str_shuffle("0123456789"),0,4);

$qryU="Select * from tbl_memberreg where member_user_id='$username'";
$resultU=mysqli_query($connection,$qryU);
if(mysqli_num_rows($resultU)>0)
{
    $username="RGC".substr(str_shuffle("0123456789"),0,7);
}
// $qry="Select * from tbl_memberreg where pan_no='$pan_no'";
// $result1=mysqli_query($connection,$qry);
// if(mysqli_num_rows($result1)>2)
// {
//     $msg="PAN No Allready Exsit";
//     header("Location:registration.php?msg=$msg"); 
// }
// $qry="Select * from tbl_memberreg where aadhar_number='$aadhar_no'";
// $result1=mysqli_query($connection,$qry);
// if(mysqli_num_rows($result1)>2)
// {
//     $msg="Aadhar No Allready Exsit";
//     header("Location:registration.php?msg=$msg"); 
// }

$chk_sp=mysqli_query($connection,"Select * from  tbl_memberreg where member_user_id='$sponcer_id'");
if(mysqli_num_rows($chk_sp)==0)
{
   	$msg="Sponsor Not Exist........";
	header("Location:../AssociateRegistration?msg=$msg"); 
}
$chk_pr=mysqli_query($connection,"Select * from  tbl_memberreg where promoter_id='$promoter_id' and position='$position'");
if(mysqli_num_rows($chk_pr)>0)
{
   	$msg="Promoter Not Exist........";
	header("Location:../AssociateRegistration?msg=$msg"); 
}

	if(strlen($member_name)>2 && strlen($contact_no)==10 && strlen($sponcer_id)==10 && strlen($sponcer_name)>2 && strlen($promoter_id)==10)
	{
	    $reg_date=date("Y-m-d H:i:s");
    	echo $qry="Insert Into tbl_memberreg(member_user_id,member_name,password,contact,email,registration_date,sponcer_id,sponcer_name,
    	promoter_id,promoter_name,status,position,father_name,pan_no,aadhar_number)
    	values('$username','$member_name','$password','$contact_no','',
    	'$reg_date','$sponcer_id','$sponcer_name','$promoter_id','$promoter_name',1,'$position','','$pan_no','$aadhar_no')";
    	$result=mysqli_query($connection,$qry) or die(mysqli_error($connection));
    	if($result)
    	{
    	    
    	    $member_id=get_member_id($username);
    	   // total_reg_member_binary($member_id);
     		$message="Dear ".$member_name." Thank You for Registration at https://www.ramlandgreencity.com. Your  Sign In Details User ID: ".$username." Password: ".$password;
    	echo $resaa=fast2sms($contact_no,$message);
    		header("Location:../MemberReceipt?Asgct=$username");
    		
    	}
    	else
    	{
    		$msg="Something wrong........";
header("Location:../AssociateRegistration?msg=$msg"); 
    	}
	}
	else
	{
		$msg="Something wrong........";
		header("Location:../AssociateRegistration?msg=$msg"); 
	}
	

}

function fast2sms($contact_no,$message)
{
   $encoded_message = urlencode($message);
    echo $url="https://www.fast2sms.com/dev/bulkV2?authorization=GVtPODBTqspRUE8IHzM9aFjd4Jio0ZflywQXhY7bAncW6mS13NwYdx6R9eCusnhOroINQTz2jl5LJA4i&route=q&message=$encoded_message&flash=0&numbers=$contact_no";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        
        if (curl_errno($ch)) {
            echo 'Curl error: ' . curl_error($ch);
        } else {
            echo 'Operation completed without any errors';
            print_r($response);
        }
        
        curl_close($ch);
    
}
?>