<?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");
include("mylibrary.php");
	//$umid=$_SESSION['umid'];
	$contactname=checkvalidstring ($_POST['contactname']);
	
	
	$cmiddlename=checkvalidstring ($_POST['cmiddlename']);
	
	
	$csurname=checkvalidstring ($_POST['csurname']);
	
	
		$contactnumber=checkvalidstring ($_POST['contactnumber']);
	
	$mobilecode=$_POST['mobilecode'];
	$contactnumber=$mobilecode."".$contactnumber;
	$cmobilenumber=checkvalidstring ($_POST['cmobilenumber']);
	
	$homemobilecode=$_POST['homemobilecode'];
	$cmobilenumber=$homemobilecode."".$cmobilenumber;
	$contactemail=checkvalidstring ($_POST['contactemail']);
	
	$caltenateemail=checkvalidstring ($_POST['caltenateemail']);
	
	
	$cremarks=checkvalidstring ($_POST['cremarks']);
	
	
	$caddress1=checkvalidstring ($_POST['caddress1']);
	
	
	$caddress2=checkvalidstring ($_POST['caddress2']);
	
	
	$ccountry=checkvalidstring ($_POST['ccountry']);
	
	$cpostalcode=checkvalidstring ($_POST['cpostalcode']);
	
	
	$cmsgappname=checkvalidstring ($_POST['cmsgappname']);
	
	
	$cmsgappid=$_POST['cmsgappid'];
	
	
	$cvideoappname=checkvalidstring ($_POST['cvideoappname']);
	
	
	$cvideoappid=$_POST['cvideoappid'];
	
	
	
	$cstate=checkvalidstring ($_POST['cstate']);
	
	
	$ccity=$_POST['ccity'];
	
	
	
	
	
	
	$utype='agent';
	$ustatus="Approved";

	$ucode=$_POST['ucode'];
	
	
	
	$uwebsite=checkvalidstring($_POST['uwebsite']);
	$umobile=checkvalidstring($_POST['umobile']);
	$upass=checkvalidstring($_POST['upass']);
	$ubusinessname=checkvalidstring($_POST['ubusinessname']);
	
	$ubussinessyear=checkvalidstring($_POST['ubussinessyear']);
	$ubussinessregistrationnum=checkvalidstring($_POST['ubussinessregistrationnum']);
	$upannumber=checkvalidstring($_POST['upannumber']);
	$uagreementnumber=checkvalidstring($_POST['uagreementnumber']);
	
	
	$ubank=checkvalidstring($_POST['ubank']);
	$ubranch=checkvalidstring($_POST['ubranch']);
	$customermid=checkvalidstring($_POST['customermid']);
	$uaccountnumber=checkvalidstring($_POST['uaccountnumber']);
	$uifsccode=checkvalidstring($_POST['uifsccode']);
	$umicrcode=checkvalidstring($_POST['umicrcode']);
	$uaccountdetails=checkvalidstring($_POST['uaccountdetails']);
	$ugst=checkvalidstring($_POST['ugst']);
	
	
	
	$uoqneraadharnumber=checkvalidstring($_POST['uoqneraadharnumber']);
	
	
	
	$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,ubusinessname,ubussinessyear,ubussinessregistrationnum,upannumber,uagreementnumber,ubank,ubranch,customermid,uaccountnumber,uifsccode,umicrcode,uaccountdetails,ugst,uoqneraadharnumber,uimage) VALUES('$utype','$ustatus','$ucode','$uwebsite','$umobile','$upass','$ubusinessname','$ubussinessyear','$ubussinessregistrationnum','$upannumber','$uagreementnumber','$ubank','$ubranch','$customermid','$uaccountnumber','$uifsccode','$umicrcode','$uaccountdetails','$ugst','$uoqneraadharnumber','$uimage')";
			
			
			
			
			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','Agent','$last_id','Direct')";
		
		$re2 = mysqli_query($conn, $eve2);
			
			
			
			
		}else{
			$_SESSION['msg1'] = "Error";
		}
		
		
		
		
		
	
	header('Location: agent.php');
	}
?>