<?php
 	session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);	

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

	
	
	$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));
	
	
	include("connect.php");

	//$umid=$_SESSION['umid'];
	$contactname=str_replace ("'","\'",$_POST['contactname']);
	$contactname=str_replace ('"','\"',$contactname);
	
	$cmiddlename=str_replace ("'","\'",$_POST['cmiddlename']);
	$cmiddlename=str_replace ('"','\"',$cmiddlename);
	
	$csurname=str_replace ("'","\'",$_POST['csurname']);
	$csurname=str_replace ('"','\"',$csurname);
	
		$contactnumber=str_replace ("'","\'",$_POST['contactnumber']);
	$contactnumber=str_replace ('"','\"',$contactnumber);
	$mobilecode=$_POST['mobilecode'];
	$contactnumber=$mobilecode."".$contactnumber;
	$cmobilenumber=str_replace ("'","\'",$_POST['cmobilenumber']);
	$cmobilenumber=str_replace ('"','\"',$cmobilenumber);
	$homemobilecode=$_POST['homemobilecode'];
	$cmobilenumber=$homemobilecode."".$cmobilenumber;
	$contactemail=str_replace ("'","\'",$_POST['contactemail']);
	$contactemail=str_replace ('"','\"',$contactemail);
	$caltenateemail=str_replace ("'","\'",$_POST['caltenateemail']);
	$caltenateemail=str_replace ('"','\"',$caltenateemail);
	
	$cremarks=str_replace ("'","\'",$_POST['cremarks']);
	$cremarks=str_replace ('"','\"',$cremarks);
	
	$caddress1=str_replace ("'","\'",$_POST['caddress1']);
	$caddress1=str_replace ('"','\"',$caddress1);
	
	$caddress2=str_replace ("'","\'",$_POST['caddress2']);
	$caddress2=str_replace ('"','\"',$caddress2);
	
	$ccountry=str_replace ("'","\'",$_POST['ccountry']);
	$ccountry=str_replace ('"','\"',$ccountry);
	$cpostalcode=str_replace ("'","\'",$_POST['cpostalcode']);
	$cpostalcode=str_replace ('"','\"',$cpostalcode);
	
	$cmsgappname=str_replace ("'","\'",$_POST['cmsgappname']);
	$cmsgappname=str_replace ('"','\"',$cmsgappname);
	
	$cmsgappid=$_POST['cmsgappid'];
	
	
	$cvideoappname=str_replace ("'","\'",$_POST['cvideoappname']);
	$cvideoappname=str_replace ('"','\"',$cvideoappname);
	
	$cvideoappid=$_POST['cvideoappid'];
	
	
	
	$cstate=str_replace ("'","\'",$_POST['cstate']);
	$cstate=str_replace ('"','\"',$cstate);
	
	$ccity=$_POST['ccity'];
	
	
	
	
	
	
	$utype='referral';
	$ustatus="Approved";

	
	
	
	
	
	$umobile=$_POST['umobile'];
	$upass=$_POST['upass'];
	$ubusinessname=$_POST['ubusinessname'];
	
	
	
	$ubank=$_POST['ubank'];
	$ubranch=$_POST['ubranch'];
	$customermid=$_POST['customermid'];
	$uaccountnumber=$_POST['uaccountnumber'];
	$uifsccode=$_POST['uifsccode'];
	$umicrcode=$_POST['umicrcode'];
	$uaccountdetails=$_POST['uaccountdetails'];
	$ugst=$_POST['ugst'];
	$rpcmic=$_POST['rpcmic'];
	
	
	
	
	
	
	
	$couid=0;
	if($utype=="agent")
	{
		$couid=0;
		/* $couid=$_POST['couid']; */
	}
	//$rolemid=$_POST['rolemid'];
	//$dashboardurl=$_POST['dashboardurl'];
	//$udate1=implode('-', array_reverse(explode('-', $_POST['udate1'])));
	//$udate2=implode('-', array_reverse(explode('-', $_POST['udate2'])));
	
	$uimage ='';
	if(empty($_FILES['uimage']['name']) == false  )
	{
		$uimage = date("YmdHis").$_FILES['uimage']['name'];
		$path = "docs/";
		move_uploaded_file( $_FILES['uimage']['tmp_name'], $path ."/". $uimage );
	
	}
	
	
	 $eve = "select * from res_user_master where umobile='$umobile'";
	
	$re = mysqli_query($conn, $eve);
	if(mysqli_num_rows($re) > 0)
	{
		$_SESSION['msg'] = "User Already Inserted.";
		header('Location: agent.php');
	}
	else
	{	
		
			 $eve1 = "INSERT INTO res_user_master (utype,ustatus,ucode,uwebsite,umobile,upass,ubank,ubranch,customermid,uaccountnumber,uifsccode,umicrcode,uaccountdetails,ugst,uimage,rpcmic) VALUES('$utype','$ustatus','$ucode','$uwebsite','$umobile','$upass','$ubank','$ubranch','$customermid','$uaccountnumber','$uifsccode','$umicrcode','$uaccountdetails','$ugst','$uimage','$rpcmic')";
			
			
			
			
			if ($conn->query($eve1) === TRUE)

	{	
			$last_id = $conn->insert_id;
			$_SESSION['msg'] = "Record saved successfully";
			
			 $eve2 = "INSERT INTO res_contact_master (contactname, contactemail,contactnumber,cmobilenumber,contactdepartmnet,cposition,cremarks,caltenateemail,caddress1,caddress2,ccountry,cstate,ccity,cpostalcode,cmiddlename,csurname,cmsgappname,cmsgappid,cvideoappname,cvideoappid,ccompanyname,clinktype,clinkid,csubcategory) VALUES('$contactname','$contactemail','$contactnumber','$cmobilenumber','Agent','Owner','$cremarks','$caltenateemail','$caddress1','$caddress2','$ccountry','$cstate','$ccity','$cpostalcode','$cmiddlename','$csurname','$cmsgappname','$cmsgappid','$cvideoappname','$cvideoappid','$ubusinessname','Referral','$last_id','Direct')";
		
		$re2 = mysqli_query($conn, $eve2);
			
			
			
			
		}else{
			$_SESSION['msg1'] = "Error";
		}
		
		
		
		
		
	
	header('Location: referral.php');
	}
?>