<?php

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

/*===============================================*\
|| ############################################# ||
|| # JAKWEB.CH / Version 1.0.4                 # ||
|| # ----------------------------------------- # ||
|| # Copyright 2019 JAKWEB All Rights Reserved # ||
|| ############################################# ||
\*===============================================*/

if (!file_exists('config.php')) die('rest_api config.php not exist');
require_once 'config.php';

$userid = $loginhash = "";
if (isset($_REQUEST['userid']) && !empty($_REQUEST['userid']) && is_numeric($_REQUEST['userid'])) $userid = $_REQUEST['userid'];
if (isset($_REQUEST['loginhash']) && !empty($_REQUEST['loginhash'])) $loginhash = $_REQUEST['loginhash'];

if (!empty($userid) && !empty($loginhash)) {

  // Let's check if we are logged in
  $usr = $jakuserlogin->jakCheckrestlogged($userid, $loginhash);

  if ($usr) {

    // Select the fields
    $jakuser = new JAK_user($usr);
    // Only the SuperAdmin in the config file see everything
    if ($jakuser->jakSuperadminaccess($userid)) {
      define('JAK_SUPERADMINACCESS', true);
    } else {
      define('JAK_SUPERADMINACCESS', false);
    }

    // Check if a sibling has logged in
      if ($jakuser->getVar("opid") != 0) {
          $opcacheid = $jakuser->getVar("opid");
      } else {
          $opcacheid = $jakuser->getVar("id");
      }

      // Cache stuff
    if (file_exists(APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php')) include_once APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';

      // timezone from server
    date_default_timezone_set(JAK_TIMEZONESERVER);
    $jakdb->query('SET time_zone = "'.date("P").'"');

      // Check if a sibling has logged in
      if ($jakuser->getVar("opid") != 0) $opcacheid = $jakuser->getVar("opid");

    // Reset
    $totalAll = $commCtotal = $statsCtotal = $visitCtotal = $totalAllOT = $totalAllWT = 0;

    if (jak_get_access("statistic_all", $jakuser->getVar("permissions"), JAK_SUPERADMINACCESS)) {

      // Get all users from this operator
      $sessids = $jakdb->select("sessions", "id", ["OR" => ["opid" => $opcacheid, "operatorid" => $userid]]);

      // Get the stats
      $commCtotal = $jakdb->count("transcript", ["convid" => $sessids]);
      $statsCtotal = $jakdb->count("user_stats", ["userid" => $userid]);
      $visitCtotal = $jakdb->count("buttonstats", ["opid" => $opcacheid]);

    } else {

      // Get all convid into an array
      $sessids = $jakdb->select("sessions", "id", ["AND" => ["opid" => $opcacheid, "operatorid" => $userid]]);
      // Get all messages from the convids
      $commCtotal = $jakdb->count("transcript", ["convid" => $sessids]);
      $statsCtotal = $jakdb->count("user_stats", ["userid" => $userid]);
      $visitCtotal = $jakdb->count("buttonstats", ["AND" => ["opid" => $opcacheid, "depid" => [$jakuser->getVar("departments")]]]);

    }

      // Get the totals
    $totalAll = $jakdb->count("support_tickets", ["opid" => $opcacheid]);

      // Open Tickets
    $totalAllOT = $jakdb->count("support_tickets", ["AND" => ["status" => 1, "opid" => $opcacheid]]);
      // Awaiting Reply Tickets
    $totalAllWT = $jakdb->count("support_tickets", ["AND" => ["status" => 2, "opid" => $opcacheid]]);


    die(json_encode(array('status' => true, 'totaltickets' => $totalAll, 'totalmsg' => $commCtotal, 'totalfeedback' => $statsCtotal, 'totalvisitor' => $visitCtotal, 'totalopen' => $totalAllOT, 'totalwait' => $totalAllWT)));
  } else {
    die(json_encode(array('status' => false, 'errorcode' => 1)));
  }
}

die(json_encode(array('status' => false, 'errorcode' => 1)));
?>
