<?php session_start();
//Fetching Values from URL


require_once __DIR__."/allfunctions.php";
$con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());



if(isset($_SESSION["proditemarrayinventory"]))
{
    $itemarrayprod = $_SESSION["proditemarrayinventory"];
}
else
{
 $itemarrayprod = array();
}

if(isset($_SESSION["proditemqty"]))
{
  $itemarrayquantity = $_SESSION["proditemqty"];
}
else
{
  $itemarrayquantity = array();
}


$materialdata = "";
$ctr2 = count($itemarrayprod);
//echo $itemarray;



	if(empty($_SESSION["MEMBER_ID"]) && empty($_SESSION["TYPE"]) && empty($_SESSION["FIRST_NAME"]))
	{
	   $itemarray = array();
	   $memberid = "";
	  $type = "";
	  $fname = "";
	}
	else
	{
	  $memberid = $_SESSION['MEMBER_ID'];
	  $type = $_SESSION['TYPE'];
	  $fname = $_SESSION['FIRST_NAME'];
	}


$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$birthday = $_POST['birthday'];
//$address = $_POST['address'];
$address = mysqli_real_escape_string($con, $_POST['address']);
$country = mysqli_real_escape_string($con, $_POST['country']);
$region = mysqli_real_escape_string($con, $_POST['region']);
$dateToday = date('Y-m-d');
$status = "Added";
$city = mysqli_real_escape_string($con, $_POST['city']);
//$mobile = $_POST['mobile'];
$mobile = mysqli_real_escape_string($con, $_POST['mobile']);
$gender = $_POST['gender'];
$email = mysqli_real_escape_string($con, $_POST['email']);
$refID = $_POST['refID'];
$other = mysqli_real_escape_string($con, $_POST['other']);
$parent_name = getRecord('level','test','id ='.$refID);
//echo $parent_name;
//echo $refID;
//$ref_id = getRecord('id','test','name= "'.$refID.'"');
$parentlevel =  (int) $parent_name;
//echo "AND";
$level = $parentlevel + 1;
//echo $level;

$insert_sql = "INSERT INTO agent(id,firstname,middlename,lastname,gender,birthday,address,country,region,city,mobile,email,refID,remarks,datecreated,status)
VALUES(NULL,'".$firstname."','".$middlename."', '".$lastname."','".$gender."','".$birthday."','".$address."','".$country."','".$region."','".$city."','".$mobile."','".$email."','".$refID."','".$other."','".$dateToday."','".$status."')";










		if(mysqli_query($con, $insert_sql) or die("database error: ". mysqli_error($con)))
		{
			$fname = substr($firstname, 0,1);
			$midname = substr($middlename, 0,1);
			$lname = substr($lastname, 0,1);
			$bday = substr($birthday, 0,4);
      $getIDdata = getRecord('id','test', 'status=1 ORDER by id DESC LIMIT 1');
      $counter = (int)$getIDdata++;
			$username= strtoupper($fname)."".strtoupper($midname)."".strtoupper($lname)."".$counter;
			$usernameclean = mysqli_real_escape_string($con, $username);
			$tet = "admin";
			$numRows = getTblNumRows('users','USERNAME= "'.$usernameclean.'"');
			$pass =  generateCode2();
			$dept = "Agent";
			$usertype = "Agent";
			$useraccess = "Agent";
      $last_id = mysqli_insert_id($con);

				if($numRows != 1)
				{
					$insert_sql1 = "INSERT INTO users(id,USERNAME,PASSWORD,FIRST_NAME,LAST_NAME,DEPARTMENT,USER_TYPE,ACCESS_TYPE,GENDER,STATUS,DATECREATED,agentID)
					VALUES(NULL,'".$usernameclean."', '".$pass."','".$firstname."','".$lastname."','".$dept."','".$usertype."', '".$useraccess."', '".$gender."', 'Active', '".$dateToday."', '".$last_id."')";
					if(mysqli_query($con, $insert_sql1) or die("database error: ". mysqli_error($con)))
					{

						$insert_sql2 = "INSERT INTO test(id,name,parent_id,amount,totalamount,level,status)
						VALUES(NULL,'".$usernameclean."', '".$refID."','50','50','".$level."','1')";
						if(mysqli_query($con, $insert_sql2) or die("database error: ". mysqli_error($con)))
						{
								echo 1;
						}
						else
						{
							echo 0;
						}
					}
					else
					{
						echo 0;
					}
				}
				else {
					echo 0;
				}

		}
		else
		{
			echo 0;
		}

?>
