<?php

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

if (!file_exists('../../config.php')) die('ajax/[config.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");

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

$where = 't1.opid = '.$opcacheid;
if (isset($_SESSION["sortdepid"]) || isset($_SESSION["sortopid"]) || isset($_SESSION["sortstatus"])) {

  if (isset($_SESSION["sortstatus"]) && is_numeric($_SESSION["sortstatus"]) && isset($_SESSION["sortdepid"]) && is_numeric($_SESSION["sortdepid"]) && isset($_SESSION["sortopid"]) && is_numeric($_SESSION["sortopid"])) {
    $where .= ' AND t1.status = '.$_SESSION["sortstatus"].' AND t1.operatorid = '.$_SESSION["sortopid"].' AND t1.depid = '.$_SESSION["sortdepid"];
  } elseif (isset($_SESSION["sortstatus"]) && is_numeric($_SESSION["sortstatus"]) && isset($_SESSION["sortdepid"]) && is_numeric($_SESSION["sortdepid"]) && !isset($_SESSION["sortopid"])) {
    $where .= ' AND t1.status = '.$_SESSION["sortstatus"].' AND t1.depid = '.$_SESSION["sortdepid"];
  } elseif (isset($_SESSION["sortstatus"]) && is_numeric($_SESSION["sortstatus"]) && isset($_SESSION["sortopid"]) && is_numeric($_SESSION["sortopid"]) && !isset($_SESSION["sortdepid"])) {
    $where .= ' AND t1.status = '.$_SESSION["sortstatus"].' AND t1.operatorid = '.$_SESSION["sortopid"];
  } elseif (isset($_SESSION["sortdepid"]) && is_numeric($_SESSION["sortdepid"]) && isset($_SESSION["sortopid"]) && is_numeric($_SESSION["sortopid"]) && !isset($_SESSION["sortstatus"])) {
    $where .= ' AND t1.operatorid = '.$_SESSION["sortopid"].' AND t1.depid = '.$_SESSION["sortdepid"];
  } elseif (isset($_SESSION["sortdepid"]) && is_numeric($_SESSION["sortdepid"]) && !isset($_SESSION["sortopid"]) && !isset($_SESSION["sortstatus"])) {
    $where .= ' AND t1.depid = '.$_SESSION["sortdepid"];
  } elseif (isset($_SESSION["sortopid"]) && is_numeric($_SESSION["sortopid"]) && !isset($_SESSION["sortdepid"]) && !isset($_SESSION["sortstatus"])) {
    $where .= ' AND t1.operatorid = '.$_SESSION["sortopid"];
  } elseif (isset($_SESSION["sortstatus"]) && is_numeric($_SESSION["sortstatus"]) && !isset($_SESSION["sortopid"]) && !isset($_SESSION["sortdepid"])) {
    $where .= ' AND t1.status = '.$_SESSION["sortstatus"];
  }

}

// and then we filter the support departments
if (is_numeric($jakuser->getVar("support_dep")) && $jakuser->getVar("support_dep") != 0) {

  if (isset($_SESSION["sortstatus"]) && is_numeric($_SESSION["sortstatus"])) {
    $where .= 't1.status = '.$_SESSION["sortstatus"].' AND (t1.operatorid = '.$jakuser->getVar("id").' OR t1.depid = '.$jakuser->getVar("support_dep").')';
  } else {
    $where .= 't1.operatorid = '.$jakuser->getVar("id").' OR t1.depid = '.$jakuser->getVar("support_dep");
  }

} elseif (!is_numeric($jakuser->getVar("support_dep")) && $jakuser->getVar("support_dep") != 0) {

  if (isset($_SESSION["sortstatus"]) && is_numeric($_SESSION["sortstatus"])) {
    $where .= 't1.status = '.$_SESSION["sortstatus"].' AND (t1.operatorid = '.$jakuser->getVar("id").' OR t1.depid IN ('.$jakuser->getVar("support_dep").'))';
  } else {
    $where .= 't1.operatorid = '.$jakuser->getVar("id").' OR t1.depid IN ('.$jakuser->getVar("support_dep").')';
  }
} else {
  if (isset($_SESSION["sortstatus"]) && is_numeric($_SESSION["sortstatus"])) {
    $where .= 't1.status = '.$_SESSION["sortstatus"];
  }
}

// DB table to use
$table = JAKDB_PREFIX.'support_tickets AS t1';
$table2 = ' LEFT JOIN '.JAKDB_PREFIX.'support_departments AS t2 ON (t1.depid = t2.id)';
$table3 = '';

// Table's primary key
$primaryKey = 't1.id';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
  array( 'db' => 't1.id', 'dbjoin' => 'id', 'dt' => 0, 'formatter' => function( $d, $row ) {
      return '<input type="checkbox" name="jak_delete_tickets[]" class="highlight" value="'.$d.'">';
    } ),
  array( 'db' => 't1.id', 'dbjoin' => 'id', 'dt' => 1, 'formatter' => function( $d, $row ) {
      return '#'.$d;
    } ),
  array( 'db' => 't1.subject', 'dbjoin' => 'subject', 'dt' => 2, 'formatter' => function( $d, $row ) {
      return '<a class="btn btn-sm btn-primary" href="'.str_replace('ajax/', '', JAK_rewrite::jakParseurl('support', 'read', $row['id'])).'">'.$d.'</a>'.($row['mergeid'] ? ' <a class="btn btn-sm btn-default" href="'.str_replace('ajax/', '', JAK_rewrite::jakParseurl('support', 'read', $row['mergeid'])).'"><i class="fa fa-compress"></i></a>' : '');
    } ),
  array( 'db' => 't2.title', 'dbjoin' => 'title', 'dt' => 3 ),
  array( 'db' => 't1.name', 'dbjoin' => 'name', 'dt' => 4 ),
  array( 'db' => 't1.private', 'dbjoin' => 'private', 'dt' => 5, 'formatter' => function( $d, $row ) {
      return (isset($d) && $d != 0 ? '<i class="fa fa-check"></i>' : '<i class="fa fa-times"></i>');
    } ),
  array( 'db' => 't1.attachments', 'dbjoin' => 'attachments', 'dt' => 6, 'formatter' => function( $d, $row ) {
      return (isset($d) && $d != 0 ? '<i class="fa fa-check"></i>' : '<i class="fa fa-times"></i>');
    } ),
  array( 'db' => 't1.reminder', 'dbjoin' => 'reminder', 'dt' => 7, 'formatter' => function( $d, $row ) {
      return (isset($d) && $d == 3 ? '<a href="'.str_replace('ajax/', '', JAK_rewrite::jakParseurl('support', 'rating', $row['id'])).'" data-toggle="modal" data-target="#jakModal"><i class="fa fa-check"></i></a>' : '<i class="fa fa-times"></i>');
    } ),
  array( 'db' => 't1.status', 'dbjoin' => 'status', 'dt' => 8, 'formatter' => function( $d, $row ) {
      if (isset($_SESSION['jak_lcp_lang']) && file_exists(APP_PATH.JAK_OPERATOR_LOC.'/lang/'.$_SESSION['jak_lcp_lang'].'.php')) {
          include (APP_PATH.JAK_OPERATOR_LOC.'/lang/'.$_SESSION['jak_lcp_lang'].'.php');
      } else {
          include (APP_PATH.JAK_OPERATOR_LOC.'/lang/'.JAK_LANG.'.php');
      }

      // Now include the stuff
      global $HD_SUPPORT_STATUS;
      $support_status = '';
      if (isset($HD_SUPPORT_STATUS) && !empty($HD_SUPPORT_STATUS)) foreach ($HD_SUPPORT_STATUS as $v) {
        $support_status .= '<a href="'.str_replace('ajax/', '', JAK_rewrite::jakParseurl('support', 'status', $row['id'], $v['id'], $v['closed'])).'" class="dropdown-item">'.$v["title"].($d == $v['id'] ? '&nbsp;<i class="fa fa-check"></i>' : '').'</a>';
        }
      return '<div class="btn-group">
    <button id="ticket_status_change" type="button" class="btn btn-secondary btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <i class="fa fa-cog"></i>
  </button>
    <div class="dropdown-menu" aria-labelledby="ticket_status_change">
    '.$support_status.'
  </div>
</div>';
    } ),
  array( 'db' => 't1.updated', 'dbjoin' => 'updated', 'dt' => 9, 'formatter' => function( $d, $row ) {
      return JAK_base::jakTimesince($d, JAK_DATEFORMAT, JAK_TIMEFORMAT);
    } ),
  array( 'db' => 't1.status', 'dbjoin' => 'status', 'dt' => 'tdc' , 'formatter' => function( $d, $row ) {
      // Now include the stuff
      global $HD_SUPPORT_STATUS;
      if (isset($HD_SUPPORT_STATUS) && !empty($HD_SUPPORT_STATUS)) foreach ($HD_SUPPORT_STATUS as $v) {
        if ($v["id"] == $d) {
          return $v["class"];
        } }
    } ),
  array( 'db' => 't1.mergeid', 'dbjoin' => 'mergeid', 'dt' => 'mid' )
);

die(json_encode(SSP::join( $_GET, $table, $table2, $table3, $primaryKey, $columns, $where, $where )));
?>
