<?php 	
require_once '../../Server/connection.php';

$valid['success'] = array('success' => false, 'messages' => array(), 'client_id' => '');

if($_POST){	

  $FirstName = addslashes($_POST['FirstName']);
  $MiddleName = addslashes($_POST['MiddleName']);
  $LastName = addslashes($_POST['LastName']);
  $Email = addslashes(strtolower($_POST['Email']));
  $PhoneNumber = addslashes($_POST['PhoneNumber']);
  $client_type = $_POST['client_type'];
  $client_source = $_POST['client_source'];
  $address = addslashes($_POST['address']);
  $client_tin = $_POST['client_tin'];
  
  
  $fromEmail = 'info@kreen.co.tz';
  $toEmail = $Email;
  $space = " ";
  
  $link = "<a href='https://cfms.kreen.co.tz/'>CFMS</a>"; 
  
  if($client_type=='Individual'){
  $passwd = addslashes(strtoupper($LastName.'123'));
  $password = md5(addslashes(strtoupper($LastName.'123')));
  $message = "Hello Customer".$space.$FirstName.$space.$LastName.$space."You have been registered into Kreen Logistic Clearing and Fowarding Management System - CFMS, You will login using your email".$space.$Email.$space."and your default password will be".$space.$passwd.$space."You can change your password after login".$space."Please Click the following link to login into the System".$space.$link;
   $subject = "User Account Creation";
         }else{
  $passwd = addslashes(strtoupper($FirstName.'123'));
  $password = md5(addslashes(strtoupper($FirstName.'123')));
  $message = "Hello Customer".$space.$LastName.$space."You have been registered into Kreen Logistic Clearing and Fowarding Management System - CFMS, You will login using your email".$space.$Email.$space."and your default password will be".$space.$passwd.$space."You can change your password after login".$space."Please Click the following link to login into the System".$space.$link;
   $subject = "User Account Creation";  
		 }
  
 
  $n = substr($PhoneNumber,1);
  $n1 = '255';
  $phone_number = $n1.$n;
  $role_id = 2;
  
  $sql = "SELECT * FROM clients WHERE clients.client_phone = '$phone_number' and clients.client_email='$Email'";
  $result = $conn->query($sql); 
  $count= mysqli_num_rows($result);
		
	if($count==0){
  
   
   $sql = "INSERT INTO clients (client_fname,client_mname,client_lname,client_tin,client_phone,client_email,client_address,client_type,client_source) VALUES ('$FirstName','$MiddleName','$LastName','$client_tin','$phone_number','$Email','$address','$client_type','$client_source')";
	
	
	$client_id;
	$clientStatus = false;
	if($conn->query($sql) === true) {
		$client_id = $conn->insert_id;
		$valid['client_id'] = $client_id;	

		$clientStatus = true;
	}
  
 
				
	$sql1 = "INSERT INTO users (staff_id,password,status,role_id) 
			VALUES ('$client_id','$password',1,2)";
				
				
	$result = $conn->query($sql1);
	
		
				if($result){
					
				if(@mail($toEmail,$subject,$message,"From:" . $fromEmail)){	
					
					
					$valid['success'] = true;
					$valid['messages'] = "Client is Successfully Registered";	
					
					$conn->close();

	               echo json_encode($valid);
					
				}	
					
				
					
				}
							
}else{
	
	
$valid['success'] = true;
					$valid['messages'] = "Client is Already Registered";	
					
					$conn->close();

	               echo json_encode($valid);
	
	
}
	
	
 
} // /if $_POST