<?php

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

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

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

// Database tables
$jaktable = 'support_departments';
$jaktable2 = 'ticketpriority';
$jaktable3 = 'ticketoptions';

// Reset some vars
$errormsg = $errors = false;
$DEP_CREDIT = 0;
$JAK_PRE_CONTENT = '';

if (isset($_SESSION['jak_lcp_lang']) && file_exists(APP_PATH.'lang/'.$BT_LANGUAGE.'.php')) {
  include (APP_PATH.'lang/'.$BT_LANGUAGE.'.php');
} else {
  include (APP_PATH.'lang/'.JAK_LANG.'.php');
}

if (isset($_GET['depid']) && !empty($_GET['depid']) && is_numeric($_GET['depid'])) {

  // Let us collect the department details.
  if (isset($HD_SUPPORT_DEPARTMENTS) && is_array($HD_SUPPORT_DEPARTMENTS)) foreach ($HD_SUPPORT_DEPARTMENTS as $v) {
    if ($v["id"] == $_GET['depid']) {
      $DEP_CREDIT = $v["credits"];
    }         
  }

  if (JAK_CLIENTID) {
        // We run on a credit based system?
    if (JAK_BILLING_MODE == 1) {
          // We need to get the credits
      if ($jakclient->getVar("credits") < $DEP_CREDIT) {
        $errormsg = sprintf($jkl["hd103"], $jakclient->getVar("credits"), JAK_rewrite::jakParseurl(JAK_CLIENT_URL));
      }

        // We run the membership based system
    } elseif (JAK_BILLING_MODE == 2 && strtotime($jakclient->getVar("paid_until")) < time()) {
      $errormsg = sprintf($jkl['hd104'], JAK_rewrite::jakParseurl(JAK_CLIENT_URL));
    }

    if ($errormsg) {
      die(json_encode(array("status" => 0, "errormsg" => $errormsg)));
    }
  }

    // Get all priorities  but only if there is any
    $TPRIORITY = "";
  $PRIORITY_ALL = $jakdb->select($jaktable2, ["id", "title"], ["AND" => ["opid" => $_SESSION['opid'], "depid" => [0, $_GET['depid']], "oponly" => 0], "ORDER" => ["dorder" => "ASC"]]);
  if (!empty($PRIORITY_ALL)) $TPRIORITY = $PRIORITY_ALL;
    // Get all options but only if there is any
    $TOPTIONS = "";
  $TOPTIONS_ALL = $jakdb->select($jaktable3, ["id", "title"], ["AND" => ["opid" => $_SESSION['opid'], "depid" => [0, $_GET['depid']], "oponly" => 0], "ORDER" => ["dorder" => "ASC"]]);
  if (!empty($TOPTIONS_ALL)) $TOPTIONS = $TOPTIONS_ALL;

    // Get the custom fields if any jak_get_custom_fields($location, $opid, $depid, $clientid, $ticketid, $contactid, $lang, $readonly, $admin, $table, $registerform, $errors = NULL)
    $custom_fields = jak_get_custom_fields(2, $_SESSION['opid'], $_GET["depid"], JAK_CLIENTID, 0, 0, $BT_LANGUAGE, false, false, false, false, $errors);

    // finally get the predefined message if any.
  $JAK_PRE_CONTENT = $jakdb->get($jaktable, "pre_content", ["AND" => ["opid" => $_SESSION['opid'], "id" => $_GET['depid']]]);

  die(json_encode(array("status" => 1, "ticketpriority" => $TPRIORITY, "ticketoptions" => $TOPTIONS, "customfields" => $custom_fields, "precontent" => $JAK_PRE_CONTENT)));

} else {
  die(json_encode(array("status" => 0, "errormsg" => $jkl['e25'])));
}
?>
