<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");

date_default_timezone_set('UTC');

include("../data/config/config.php");
$PDO = PDO_CONNECT();

$html = '<terminate><i>User ' . $_POST['username'] . ' has left the chat session.</i></terminate>';

$command = "SELECT * FROM `chats` WHERE request_id = :request_id AND content LIKE '%has left the chat session%'";
$result = $PDO->prepare($command);
$result->bindParam(':request_id', $_POST['request_id']);
$result->execute();

if($result->rowCount() == 0) {

    $command = "INSERT IGNORE INTO chats VALUES('', '', '', '', :request_id, '$html' , '', UTC_TIMESTAMP() )";
    $result = $PDO->prepare($command);
    $result->bindParam(':request_id', $_POST['request_id']);
    $result->execute();

}

$command = "UPDATE chat_requests SET end_date = UTC_TIMESTAMP(), u_status = '0', a_status = '0', in_chat = '0' WHERE request_id = :request_id";

$result = $PDO->prepare($command);
$result->bindParam(':request_id', $_POST['request_id']);
$result->execute();


// Change visitor status back to 1 to ensure invite is revoked in case it was initiated
$command = "SELECT * FROM chat_requests WHERE request_id = :request_id";
$result = $PDO->prepare($command);
$result->bindParam(':request_id', $_POST['request_id']);
$result->execute();

while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
    $data = $row;
}
	
$command = "UPDATE visitors SET status = '1' WHERE cookie_id = :cookie_id AND ip_address = :ip_address LIMIT 1";
$result = $PDO->prepare($command);
$result->bindParam(':cookie_id', $data['cookie_id']);
$result->bindParam(':ip_address', $data['ip_address']);
$result->execute();


echo 'chat_terminated';

die();