<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 6 May 1998 03:10:00 GMT");

/*===============================================*\
|| ############################################# ||
|| # JAKWEB.CH / Version 1.2                   # ||
|| # ----------------------------------------- # ||
|| # Copyright 2021 JAKWEB All Rights Reserved # ||
|| ############################################# ||
\*===============================================*/

if (!file_exists('../../config.php')) die('ajax/[usronline.php] config.php not exist');
require_once '../../config.php';

if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !isset($_SESSION['jak_lcp_idhash'])) die("Nothing to see here");

// Import the user or standard language file
if (isset($_SESSION['jak_lcp_lang']) && file_exists(APP_PATH.JAK_OPERATOR_LOC.'/lang/'.$_SESSION['jak_lcp_lang'].'.php')) {
    include_once(APP_PATH.JAK_OPERATOR_LOC.'/lang/'.$_SESSION['jak_lcp_lang'].'.php');
} else {
    include_once(APP_PATH.JAK_OPERATOR_LOC.'/lang/'.JAK_LANG.'.php');
}

// Get the current time
$currentime = time();

if (!is_numeric($_GET['oid'])) die("There is no such thing!");

switch ($_GET['case']) {
  case 'typing':

    if (isset($_GET['ticketid']) && isset($_GET['oid'])) {
      $jakdb->update("ticket_live_user", ["lastvisit" =>  time(), "istyping" => 1], ["AND" => ["opid" => $opcacheid, "ticketid" => $_GET['ticketid'], "userid" => $_GET['oid']]]);
    }
    die(json_encode(array('status' => 1)));

  break;
  case 'nottyping':

    if (isset($_GET['ticketid']) && isset($_GET['oid'])) {
      $jakdb->update("ticket_live_user", ["lastvisit" =>  time(), "istyping" => 0], ["AND" => ["opid" => $opcacheid, "ticketid" => $_GET['ticketid'], "userid" => $_GET['oid']]]);
    }
    die(json_encode(array('status' => 1)));

  break;
  case 'live':

    if (isset($_GET['ticketid']) && isset($_GET['oid'])) {

      // Update the operator that is currently in the ticket
      $jakdb->update("ticket_live_user", ["lastvisit" =>  time()], ["AND" => ["opid" => $opcacheid, "ticketid" => $_GET['ticketid'], "userid" => $_GET['oid']]]);

      // the last 5 minutes
      $checkLastmin = time() - 300;

      $result = $jakdb->select("ticket_live_user", ["userid", "clientid", "istyping", "lastvisit"], ["AND" => ["opid" => $opcacheid, "ticketid" => $_GET['ticketid'], "userid[!]" => $_GET['oid'], "lastvisit[>]" => $checkLastmin], "ORDER" => ["lastvisit" => "DESC"], "LIMIT" => 5]);

    }

    $opticketlive = "";
    if (isset($result) && !empty($result)) foreach ($result as $row) {
      # code...

      if ($row["userid"] != 0) {

        $user = $jakdb->get("user", ["username", "name", "picture"], ["id" => $row["userid"]]);

        $picture = $user["picture"];
        $name = $user["name"].' <small>('.$user["username"].')</small>';

      } else {

        $client = $jakdb->get("clients", ["email", "name", "picture"], ["id" => $row["clientid"]]);

        $picture = $client["picture"];
        $name = $client["name"].' <small>('.$client["email"].')</small>';

      }

      $opticketlive .= '<div class="media"><img src="'.str_replace(JAK_OPERATOR_LOC."/ajax/", "", BASE_URL.JAK_FILES_DIRECTORY.$picture).'" class="mr-3 rounded-circle'.(isset($row["istyping"]) && $row["istyping"] == 1 ? ' border border-danger' : '').'" width="53" alt="'.$name.'"><div class="media-body"><h5 class="mt-0 mb-0">'.$name.'</h5><p>'.(isset($row["istyping"]) && $row["istyping"] == 1 ? $jkl['g143'] : '').'</p></div></div><hr>';

    }

    die(json_encode(array("status" => 1, "onlineList" => $opticketlive)));
  
  break;
  default:

  die(json_encode(array("status" => 0)));

}
?>
