<?php
include_once('../helpers/init.php');
if(isset($_POST['changePassword'])){
	$old_password = $_POST['old_password'];
	$new_password = $_POST['new_password'];
	$cnf_password = $_POST['cnf_password'];
	$username = $_SESSION['username'];
	if(login($username,$old_password)){
		if($new_password != $cnf_password){
			header('Location:../settings/settings.php?result=failer&message=Password do not match');	
		}else{
			if(changePassword($username,$new_password)){
				header('Location:../settings/settings.php?result=success&message=Password has been successfully Changed!');
			}else{
				header('Location:../settings/settings.php?result=failer&message=Unable to change the password');
			}
			
		}
		
	}else{
		header('Location:../settings/settings.php?result=failer&message=Old Password Incorrect');	
	}
}

if(isset($_GET['action']) && $_GET['action'] =="deleteEnquery"){
	$id=$_GET['id'];
	$sql = "delete from enquiries where id=$id";
	if(query($sql)){
		header('Location:../enquiries/manage-enquiries.php?result=success&message=Enquiry Deleted Successfully!');	
	}else{
		$error = error();
		header('Location:../enquiries/manage-enquiries.php?result=failer&message='.$error);	
	}
}


if(isset($_GET['action']) && $_GET['action'] =="approveEnquiry"){
	$id=$_GET['id'];
	$pid=$_GET['pid'];
	$qt=$_GET['qt'];
	$sql = "update enquiries set status='approved' where id=$id";

	$product =getWhere("products","id",$pid);
	$product_qty=0;
	if(num_rows($product)==0){
		$error = "Cannot Approve this enquiry becuase the product does not exist.";
		header('Location:../enquiries/manage-enquiries.php?result=failer&message='.$error);	
	}else{
	
		
	foreach($product as $row){
		$product_qty=$row['qty'];
	}
	$stock = $product_qty-$qt;
	if($stock<=10){
		$error = "Cannot Approve this enquiry becuase of the limited stock. The current stock value for that product is ".$product_qty.". The minimum Stock Value Should be 10.";
		header('Location:../enquiries/manage-enquiries.php?result=failer&message='.$error);	
	}else{
		$update_products = "update products set qty=$stock where id=$pid";
	
	if(query($sql) && query($update_products)){
		header('Location:../enquiries/manage-enquiries.php?result=success&message=Enquiry Approved Successfully!');	
	}else{
		$error = error();
		header('Location:../enquiries/manage-enquiries.php?result=failer&message='.$error);	
	}
	}
	
		
	}
	
}

if(isset($_GET['action']) && $_GET['action'] =="disapproveEnquiry"){
	$id=$_GET['id'];
	$sql = "update enquiries set status='not approved' where id=$id";
	if(query($sql)){
		header('Location:../enquiries/manage-enquiries.php?result=success&message=Enquiry Disapproved Successfully!');	
	}else{
		$error = error();
		header('Location:../enquiries/manage-enquiries.php?result=failer&message='.$error);	
	}
}


/*remove subscribers*/
if(isset($_GET['action']) && $_GET['action'] =="deleteSubscriber"){
	$id=$_GET['id'];
	$sql = "delete from subscriptions where id=$id";
	if(query($sql)){
		header('Location:../subscription/manage-subscribers.php?result=success&message=Subscriber Deleted Successfully!');	
	}else{
		$error = error();
		header('Location:../subscription/manage-subscribers.php?result=failer&message='.$error);	
	}
}
?>