<?php 

// Include main file
include_once('../libs/main.php');

// Check user Login
include_once('checkLogin.php');

// Logical section of page
$userObj =  new  user();

$successMsg = '';

if(isset($_REQUEST['successMsg'])){
	$successMsg = $_REQUEST['successMsg'];
}

if(isset($_REQUEST['failedMsg'])){
	$failedMsg = $_REQUEST['failedMsg'];
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_email'){
	if($_REQUEST['email']==''){
		echo 'Please enter email address';
		exit;
	}else if(validateEmail($_REQUEST['email'])){
		
		if(isset($_REQUEST['user_id']) && $userObj->checkEmailExist($_REQUEST['email'],$_REQUEST['user_id'])){
			echo "Email already exist";
			exit;
		}else if($userObj->checkEmailExist($_REQUEST['email'])){
			echo "Email already exist";
			exit;
		}
	}else{
		echo 'Please enter valid email address';
		exit;
	}
	
	return true;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_username'){
	if($_REQUEST['username']==''){
		echo 'Please enter Username';
		exit;
	}else if(validateUsername($_REQUEST['username'])){
		
		if(isset($_REQUEST['user_id']) && $userObj->checkUsernameExist($_REQUEST['username'],$_REQUEST['user_id'])){
			echo "Username already exist";
			exit;
		}else if($userObj->checkUsernameExist($_REQUEST['username'])){
			echo "Username already exist";
			exit;
		}
	}else{
		echo 'Please enter valid Username';
		exit;
	}
	
	return true;
}



if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['user_id'])){
	$userDetail = $userObj->fromId($_REQUEST['user_id']);
}


if(isset($_POST['submit'])){
	
	if(isset($_FILES['user_profile_image']) && $_FILES['user_profile_image']['name']!=''){
		$image=$_FILES['user_profile_image']['name'];
		$image=time().'_'.$image;
		$extension = strtolower(getExtension($image));
		
		if($extension!='jpg' and $extension!='gif' and $extension!='jpeg' and $extension!='png'){
			header('location:addpaid_user.php?$failedMsg=Invalid image extension.');
			exit;
		}
		
		move_uploaded_file($_FILES['user_profile_image']['tmp_name'], DIR_UPLOAD_MEMBER.$image);		
		$userData->user_profile_image	= $image;	
	}else{
		$userData->user_profile_image	= $_POST['uploaded_file'];	
	}
	
	$userData->user_profile_created_by 		= $_POST['user_profile_created_by'];
	$userData->user_username 		= $_POST['user_username'];
	$userData->user_native_place 		= $_POST['user_native_place'];
	$userData->user_name 		= $_POST['user_name'];
	$userData->about_me 		= $_POST['about_me'];
	$userData->user_cast 		= $_POST['user_cast'];
	$userData->user_sub_cast 		= $_POST['user_sub_cast'];
	$userData->user_gotra 		= $_POST['user_gotra'];
	$userData->outercast_interest 		= $_POST['outercast_interest'];
	$userData->interested_cast 		= $_POST['interested_cast'];
	$userData->father_name 		= $_POST['father_name'];
	$userData->father_occupation 		= $_POST['father_occupation'];
	$userData->mother_name 		= $_POST['mother_name'];
	$userData->mother_occupation 		= $_POST['mother_occupation'];
	$userData->screen_name 		= $_POST['screen_name'];
	$userData->marriage_time 		= $_POST['marriage_time'];
	$userData->gender 		= $_POST['gender'];
	$userData->date_of_birth 		= date('Y-m-d',strtotime($_POST['date_of_birth']));
	$userData->marital_status 		= $_POST['marital_status'];
	$userData->have_children 		= $_POST['have_children'];
	$userData->chiledren_live_with 		= $_POST['chiledren_live_with'];
	$userData->num_of_son 		= $_POST['num_of_son'];
	$userData->num_of_doughter 		= $_POST['num_of_doughter'];
	$userData->religion 		= $_POST['religion'];
	$userData->community 		= $_POST['community'];
	$userData->mother_toungue 		= $_POST['mother_toungue'];
	$userData->lifestyle 		= $_POST['lifestyle'];
	$userData->last_degree 		= $_POST['last_degree'];
	$userData->degree_detail 		= $_POST['degree_detail'];
	$userData->proffession 		= $_POST['proffession'];
	$userData->annual_incom 		= $_POST['annual_incom'];
	$userData->family_status 		= $_POST['family_status'];
	$userData->country 		= $_POST['country'];
	$userData->state 		= $_POST['state'];
	$userData->city 		= $_POST['city'];
	$userData->post_code 		= $_POST['post_code'];
	$userData->address 		= $_POST['address'];
	$userData->nationality 		= $_POST['nationality'];
	$userData->mobile_no 		= $_POST['mobile_no'];
	$userData->phone_no 		= $_POST['phone_no'];
	$userData->suitable_time 		= $_POST['suitable_time'];
	$userData->email_address 		= $_POST['email_address'];
	$userData->person_contact 		= $_POST['person_contact'];
	$userData->height 		= $_POST['height'];
	$userData->weight 		= $_POST['weight'];
	$userData->spects 		= $_POST['spects'];
	$userData->star 		= $_POST['star'];
	$userData->mangal 		= $_POST['mangal'];
	$userData->horpscope_belive 		= $_POST['horpscope_belive'];
	$userData->blood_group 		= $_POST['blood_group'];
	$userData->complexion 		= $_POST['complexion'];
	$userData->eye_color 		= $_POST['eye_color'];
	$userData->hair_color 		= $_POST['hair_color'];
	$userData->eating_habit 		= $_POST['eating_habit'];
	$userData->drinking_habit 		= $_POST['drinking_habit'];
	$userData->smoking_habit 		= $_POST['smoking_habit'];
	$userData->special_case 		= $_POST['special_case'];
	$userData->father_alive 		= $_POST['father_alive'];
	$userData->mother_alive 		= $_POST['mother_alive'];
	$userData->total_brothers 		= $_POST['total_brothers'];
	$userData->total_sisters 		= $_POST['total_sisters'];
	$userData->married_brother 		= $_POST['married_brother'];
	$userData->married_sister 		= $_POST['married_sister'];
	$userData->user_status  		= $_POST['user_status'];
	$userData->age_in_year = daysDifference(date('Y-m-d'), $userData->date_of_birth);	
	$userData->user_created_date= date('Y-m-d H:i:s');	
	$userData->user_last_login_date = date('Y-m-d');	
	
	$userData->payment_status = 'Free';
	// edit user details 
	if(isset($_POST['user_id'])){

		$userData->user_id = $_POST['user_id'];
		$pageD = $userObj->fromId($userData->page_id);
		if(isset($_FILES['user_profile_image']) && $_FILES['user_profile_image']['name']!=''){
			if($pageD->user_profile_image!='' && file_exists(DIR_UPLOAD_MEMBER.$pageD->user_profile_image)){
				unlink(DIR_UPLOAD_MEMBER.$pageD->user_profile_image);
			}
		}	
		$rs = $userObj->updateRecords($userData);
		if($rs){
			$successMsg ="Member Details Updated successfully";			
			header('Location:userlist.php?successMsg='.$successMsg);
			exit;
		}
		
	// add user details 	
	}else{
	
		$userData->user_password 	= md5($_POST['user_password']);	
		$rs = $userObj->addRecords($userData);
		if($rs){
			$uniqId = 'VK'.$rs;
			$userObj->addUniqId($uniqId, $rs);
			$successMsg ="Member Details Added successfully";			
			header('Location:userlist.php?successMsg='.$successMsg);
			exit;
		}
		
	}
}

$allCountries = $settingObj->getAllCountry();
$allSubCast = $settingObj->getAllSubCast();
$allLanguages = $settingObj->getAllLanguages();
$allDegree = $settingObj->getAllDegree();

// Include Template 
include_once('../libs/routs.php')

?>
