<?php

	include('config.php');
	session_start();

	$userid = intval($_SESSION['user_login']);
	
	
		$stmt="SELECT * FROM users WHERE userid = :userid";
		$stmt=$pdo->prepare($stmt);
		$stmt->bindParam("userid", $userid, PDO::PARAM_STR);
		$stmt->execute();
		$userVal=$stmt->fetch();
		
		





		
		

			//UPADATE Notification as read - for admin command from dashboard.php, for users command from account.php - the form from header.php
			
			if(isset($_POST["senderID"])){
				
			$senderID = intval($_POST["senderID"]);
			
			$stmt = $pdo->prepare("UPDATE contact SET status='read' WHERE userid=:userid");
			$stmt->bindParam(":userid",$senderID,PDO::PARAM_STR);
			$stmt->execute();

			}

	









		//CHAT MESSAGES
		
		if(isset($_POST["messages"]) && isset($_POST["receiver"])){

		$message = htmlspecialchars($_POST["messages"]);
		$receiver = intval($_POST["receiver"]);
		$status = "unread";
		$received = date("Y-m-d H:i:s");
		
		//optional inputs for charges
		if(!empty($_POST["serviceTitle"])){ $serviceTitle = htmlspecialchars($_POST["serviceTitle"]);}else{$serviceTitle ="";}
		
		if(!empty($_POST["amount"])){$amount = htmlspecialchars($_POST["amount"]);}else{$amount ="";}

		$query=$pdo->prepare("INSERT INTO contact (message, userid, receiver, status, serviceTitle, amount,received) 
								VALUES (:message, :userid, :receiver, :status, :serviceTitle, :amount, :received)");
		$query->bindParam(":message", $message, PDO::PARAM_STR);
		$query->bindParam(":userid", $userid, PDO::PARAM_STR);
		$query->bindParam(":receiver", $receiver, PDO::PARAM_STR);
		$query->bindParam(":status", $status, PDO::PARAM_STR);
		$query->bindParam(":serviceTitle", $serviceTitle, PDO::PARAM_STR);
		$query->bindParam(":amount", $amount, PDO::PARAM_STR);
		$query->bindParam(":received", $received, PDO::PARAM_STR);
		$query->execute();
		
		if($query){
				
				if(!empty($amount)){
				//Service charges is set
				echo '<div class="service">'.$serviceTitle." | $".$amount.'</div><br><p>';
				
				echo "<div class='receiver service-body' style='margin-bottom:6px;'>".$message." &nbsp; 
						<i style='font-size:12px; float:right;margin-top:6px;' class='fa fa-check'></i></div>";
				
				echo "<script>$('.charge').fadeOut(function(){ $('#copy-con').removeClass('display-none'); }) </script>";
				}
				else{

				echo "<div class='receiver' style='margin-bottom:6px;'>".$message." &nbsp; 
						<i style='font-size:12px; float:right;margin-top:6px;' class='fa fa-check'></i></div>";
				}
		
				echo '<div class="time2" style="margin-bottom:30px;">Now</div>';
				echo "<script>$('#messages').val(''); </script>";
		}
		}
 
	

	
	
	
	
	
//CHAT IMAGES

if(isset($_FILES["image"]["name"])){


 //$output = '';
 sleep(1);
 for($count=0; $count<count($_FILES["image"]["name"]); $count++)
 {
  $image = $_FILES["image"]["name"][$count];
  $tmp_name = $_FILES["image"]['tmp_name'][$count];
  $file_array = explode(".", $image);
  $file_extension = end($file_array);
 
   $image = $file_array[0] . '-'. rand() . '.' . $file_extension;
  
  $location = 'img/chats/' . $image;
  if(move_uploaded_file($tmp_name, $location))
  {
   
		
		$receiver = intval($_GET["receiver"]);
		$status = "unread";
		$received = date("Y-m-d H:i:s");

   $stmt=$pdo->prepare("INSERT INTO contact (image, userid, receiver, status, received) VALUES (:image, :userid, :receiver, :status, :received)");
   $stmt->bindParam(":image", $image, PDO::PARAM_STR);
   $stmt->bindParam(":userid", $userid, PDO::PARAM_STR);
   $stmt->bindParam(":receiver", $receiver, PDO::PARAM_STR);
   $stmt->bindParam(":status", $status, PDO::PARAM_STR);
   $stmt->bindParam(":received", $received, PDO::PARAM_STR);
   $stmt->execute();
   if($stmt){
	 
		echo '<br><a href="../img/chats/'.$image.'"><img src="../img/chats/'.$image.'" alt="Image" id="import"
		style="width:200px;border-radius:5px"></a><br>';


  }
   
  }
 }
}













