<?php
session_start();
require_once('../Server/connection.php');


$reset_type = $_POST['reset_type'];

if($reset_type=='Customer Reset'){
	
$toEmail = addslashes(strtolower($_POST['cust_email']));
$mailfrom = 'info@brightvision.co.tz';
$check = $conn->query("SELECT users.user_id,clients.client_fname FROM users inner join clients on users.staff_id=clients.client_id WHERE clients.client_email = '$toEmail' AND users.status=1");
$value = $check->fetch_assoc();
$user_id = $value['user_id'];
$firstname = $value['client_fname'];
$check2 = mysqli_num_rows($check);
if ($check2 == 0){
//header("Location:../resetPaswd.php?msg=alert");

$_SESSION['error_message'] = "email is not recognized by database or you have been blocked from using system";
                        header("Location: ../resetPaswd.php");
                             exit();



}else{

$token = md5($_POST['cust_email']).rand(10,9999);

$result22 = $conn->query("UPDATE users SET activation_code='$token' WHERE user_id = '$user_id' and status=1"); 

$link = "<a href='https://cms.brightvision.co.tz/resetPassword.php?key=".$user_id."&token=".$token."'>Click This link to reset your password</a>"; 

//$message = "Hello"."&nbsp&nbsp".$FirstName."&nbsp&nbsp".$LastName."&nbsp&nbsp"."You have been registered into edge engineering Employee Management System - EMS, You will login using your email"."&nbsp&nbsp".$Email."&nbsp&nbsp"."and your default password will be"."&nbsp&nbsp".$passwd."&nbsp&nbsp"."You can change your password after login"."&nbsp&nbsp"."Please Click the following link to login into the System"."&nbsp&nbsp".$link;

$message = $link;
$subject = "Password Reset Using your email";


if(@mail($toEmail,$subject,$message,"From:" . $mailfrom)){
	
$_SESSION['success_message'] = "We have sent password reset link into your email";
                        header("Location: ../resetPaswd.php");
                             exit();

//header("Location:../resetPaswd.php?msg=success"); 

} 
	
}	
	
	
	
	
}else{
	
$toEmail = addslashes(strtolower($_POST['staff_email']));
$mailfrom = 'info@brightvision.co.tz';
$check = $conn->query("SELECT users.user_id,staff.firstname FROM users inner join staff on users.staff_id=staff.staff_id WHERE staff.email = '$toEmail' AND users.status=1");
$value = $check->fetch_assoc();
$user_id = $value['user_id'];
$firstname = $value['firstname'];
$check2 = mysqli_num_rows($check);
if ($check2 == 0){
//header("Location:../resetPaswd.php?msg=alert");

$_SESSION['error_message'] = "email is not recognized by database or you have been blocked from using system";
                        header("Location: ../resetPaswd.php");
                             exit();



}else{

$token = md5($_POST['staff_email']).rand(10,9999);

$result22 = $conn->query("UPDATE users SET activation_code='$token' WHERE user_id = '$user_id' and status=1"); 

$link = "<a href='https://cms.brightvision.co.tz/resetPassword.php?key=".$user_id."&token=".$token."'>Click This link to reset your password</a>"; 

//$message = "Hello"."&nbsp&nbsp".$FirstName."&nbsp&nbsp".$LastName."&nbsp&nbsp"."You have been registered into edge engineering Employee Management System - EMS, You will login using your email"."&nbsp&nbsp".$Email."&nbsp&nbsp"."and your default password will be"."&nbsp&nbsp".$passwd."&nbsp&nbsp"."You can change your password after login"."&nbsp&nbsp"."Please Click the following link to login into the System"."&nbsp&nbsp".$link;

$message = $link;
$subject = "Password Reset Using your email";


if(@mail($toEmail,$subject,$message,"From:" . $mailfrom)){
	
$_SESSION['success_message'] = "We have sent password reset link into your email";
                        header("Location: ../resetPaswd.php");
                             exit();

//header("Location:../resetPaswd.php?msg=success"); 

} 
	
}
	
}






?>