<?php

/*===============================================*\
|| ############################################# ||
|| # JAKWEB.CH / Version 2.1.2                 # ||
|| # ----------------------------------------- # ||
|| # Copyright 2023 JAKWEB All Rights Reserved # ||
|| ############################################# ||
\*===============================================*/

// Check if the file is accessed only via index.php if not stop the script from running
if (!defined('JAK_ADMIN_PREVENT_ACCESS')) die('You cannot access this file directly.');

// Reset
$opmain = '';
$count = 0;

use JAKWEB\JAKsql;

// Now if we have multi site we have fully automated process
if (!empty(JAKDB_MAIN_NAME) && JAK_MAIN_LOC && JAK_MAIN_OP) {
  
  // Database connection to the main site
  $jakdb1 = new JAKsql([
    // required
    'database_type' => JAKDB_MAIN_DBTYPE,
    'database_name' => JAKDB_MAIN_NAME,
    'server' => JAKDB_MAIN_HOST,
    'username' => JAKDB_MAIN_USER,
    'password' => JAKDB_MAIN_PASS,
    'charset' => 'utf8',
    'port' => JAKDB_MAIN_PORT,
    'prefix' => JAKDB_MAIN_PREFIX,
               
    // [optional] driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
    'option' => [PDO::ATTR_CASE => PDO::CASE_NATURAL]
    ]);

  // We get the user data from the main table
  $opmain = $jakdb1->get("users", ["id", "signup", "trial", "paidtill", "active"], ["AND" => ["opid" => JAK_USERID, "locationid" => JAK_MAIN_LOC]]);

  // Now we get the subscriptions
  $subscriptions = $jakdb1->select("subscriptions", ["[>]packages" => ["packageid" => "id"]], ["subscriptions.id", "subscriptions.packageid", "subscriptions.amount", "subscriptions.currency", "subscriptions.paidfor", "subscriptions.paidhow", "subscriptions.subscribed", "subscriptions.paidwhen", "subscriptions.paidtill", "subscriptions.active", "subscriptions.success", "packages.title"], ["AND" => ["subscriptions.locationid" => JAK_MAIN_LOC, "subscriptions.userid" => JAK_USERID, "subscriptions.active" => 1], "ORDER" => ["subscriptions.paidwhen" => "DESC"]]);

  // Check if we have some new and unread tickets.
  $count = 0;
  $count = $jakdb1->count("support_tickets", ["AND" => ["userid" => $opmain["id"], "readtime" => 0]]);

  // We get the settings for the payment
    $sett = array();
    $settings = $jakdb1->select("settings", ["varname", "used_value"]);
    foreach ($settings as $v) {
        $sett[$v["varname"]] = $v["used_value"]; 
    }

}

// Statistics
$stataccess = false;
$sessCtotal = $commCtotal = $statsCtotal = $visitCtotal = 0;
if (jak_get_access("statistic", $jakuser->getVar("permissions"), JAK_MAIN_OP)) {

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

    // Get the stats
    $sessCtotal = $jakdb->count("sessions", ["opid" => $opcacheid]);
    $sessids = $jakdb->select("sessions", "id", ["opid" => $opcacheid]);
    if (!empty($sessids)) $commCtotal = $jakdb->count("transcript", ["convid" => $sessids]);
    $userids = $jakdb->select("user", "id", ["OR" => ["id" => $opcacheid, "opid" => $opcacheid]]);
    $statsCtotal = $jakdb->count("user_stats", ["userid" => $userids]);
    $visitCtotal = $jakdb->count("buttonstats", ["opid" => $opcacheid]);
    

  } else {

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

  }

  $stataccess = true;
}

$statsupport = false;
$totalAll = $totalAllOT = $totalAllWT = $totalAllCT = 0;
if (jak_get_access("support", $jakuser->getVar("permissions"), JAK_MAIN_OP)) {

  // We need the support status to figure out what we have to show
  $op_support_status = $jakdb->select("support_status", ["id", "title", "class", "dorder"], ["AND" => ["opid" => $opcacheid, "oponly" => 0], "ORDER" => ["dorder" => "ASC"]]);

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

    // We calculate the statistics for the dashboard
    if (isset($op_support_status) && !empty($op_support_status)) foreach ($op_support_status as $k => $v) {
      $totalTtemp = 0;
      $totalTtemp = $jakdb->count("support_tickets", ["AND" => ["opid" => $opcacheid, "status" => $v["id"]]]);
      
      // Get the stuff into a new array
      $totalTStat[] = array("title" => $v["title"], "class" => $v["class"], "total" => $totalTtemp);
    }

    $statsupport = true;

}

// Get the comments to approve
$approve_comments = array();
if (jak_get_access("blog", $jakuser->getVar("permissions"), JAK_MAIN_OP)) {
  $approve_comments = $jakdb->select("blogcomments", ["[>]blog" => ["blogid" => "id"]], ["blogcomments.id", "blogcomments.blogid", "blog.title", "blogcomments.message", "blogcomments.time"], ["AND" => ["blogcomments.opid" => $opcacheid, "blogcomments.approve" => 0], "ORDER" => ["blogcomments.time" => "DESC"]]);
}

// Get the public operator chat, check if we have access
if ($jakosub['groupchats']) {
  $gcarray = array();
  $JAK_PUBLICCHAT = $jakdb->select("groupchat", ["id", "title", "opids", "lang"], ["AND" => ["opid" => $opcacheid, "active" => 1]]);
  if (isset($JAK_PUBLICCHAT) && !empty($JAK_PUBLICCHAT)) foreach ($JAK_PUBLICCHAT as $gc) {
    // Let's check if we have access
    if ($gc["opids"] == 0 || in_array(JAK_USERID, explode(",", $gc["opids"]))) {
      $gcarray[] = $gc;
    }
  }
}

// Title and Description
$SECTION_TITLE = $jkl['m'];
$SECTION_DESC = "";

// Include the calendar
$css_file_header = BASE_URL.'css/fullcalendar.css';

// Include the javascript file for results
$js_file_footer = 'js_dashboard.php';
// Call the template
$template = 'dashboard.php';

?>
