<?php 
include_once "../includes/connectConfig.php";


if($_POST)
{		
		$fname = test_input($_POST['usr_fname']);
		$lname = test_input($_POST['usr_lname']);
		$ugender = test_input($_POST['usr_gender']);
		$country = test_input($_POST['usr_country']);
		$city = test_input($_POST['usr_city']);
		$phone = only_num(test_input($_POST['usr_phone']));
		$email = test_input($_POST['usr_email']);
		$user_pkgs = (isset($_POST['pkgselected']) && !empty($_POST['pkgselected'])) ? ucfirst(test_input($_POST['pkgselected'])) : NULL;
		$user_password = test_input($_POST['passwordset']);
		$Rfg = date('ymdis');
		$password_hd = $st_hasher->HashPassword($user_password);
	    //$nw_ss = date('Y');
	try	
	{
		
		//CHECK PHONE
		$chk_uspm = $db_con->prepare("SELECT * FROM  tks_users WHERE phone=:pphone  AND delete_status = 0 ");
		$chk_uspm->execute(array(":pphone"=>$phone));
		$count_uspm = $chk_uspm->rowCount();
		if($count_uspm == 0)
		{
			//CHECK EMAIL
			$chk_email = $db_con->prepare("SELECT * FROM  tks_users WHERE email=:email AND delete_status = 0 ");
			$chk_email->execute(array(":email"=>$email));
			$count_mail = $chk_email->rowCount();
			
			if($count_mail == 0)
			{
				$scls = gntRdmStgs();
				$ac_inf = 2 ;
				
				$ses_tkn = gntRdmStgs()."".uniqid();
				
				//INSERT INTO USERS TABLE
				$ins_usr = $db_con->prepare("INSERT INTO  tks_users (firstname, lastname, gender, country, city, email, phone, password, user_type, package_sel, ps_url)
																
															 VALUES (:fname, :lname, :ugen, :country, :city, :email, :pphone, :pass_hash, :usrtyp, :uspkp, :acurl)");
				
				$ins_usr->bindParam(':fname',$fname);
				$ins_usr->bindParam(':lname',$lname);
				$ins_usr->bindParam(':ugen',$ugender);
				$ins_usr->bindParam(':country',$country);
				$ins_usr->bindParam(':city',$city);
				$ins_usr->bindParam(':email',$email);
				$ins_usr->bindParam(':pphone',$phone);
				$ins_usr->bindParam(':pass_hash',$password_hd);
				$ins_usr->bindParam(':usrtyp',$ac_inf);
				$ins_usr->bindParam(':uspkp',$user_pkgs);
				$ins_usr->bindParam(':acurl',$Rfg);
				$ins_usr->execute();
					
				$last_id = $db_con->lastInsertId();
				
				
				//PAGE REPORT LOG
				$rmk = 'Registered Account';
				pages($last_id,$rmk);
				
				include_once "../includes/register-sendin-mail.php";
				
				$_SESSION['puc_stz_dflt'] = $ses_tkn;
				
				$_SESSION['puc_stz_tcktszusr'] = $last_id;
				$_SESSION['puc_stz_tcktsztyp'] = ($ac_inf == 3 || $ac_inf == 4) ? "Vendor_sstyp" :  gt_typ_nm($ac_inf)."_sstyp";
				
				$_SESSION['puc_stz_vnd'] = gt_typ_nm($ac_inf)."_typacc";
					
				
				echo 'account/index';
				exit();
			}
			else
			{
				echo "2"; // mail checked
				exit();
			}
		}
		else
		{
			echo "7"; // phone exist
			exit();
		}

	}
	catch(PDOException $e){
		echo $e->getMessage();
	}
}
?>