<?php

/*===============================================*\
|| ############################################# ||
|| # JAKWEB.CH / Version 2.1.4                 # ||
|| # ----------------------------------------- # ||
|| # 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.');

// Check if the user has access to this file
if (!jak_get_access("leads", $jakuser->getVar("permissions"), JAK_MAIN_OP)) jak_redirect(BASE_URL);

// All the tables we need for this plugin
$errors = array();
$jaktable = 'sessions';
$jaktable1 = 'transcript';
$jaktable2 = 'clientcontact';
$jaktable3 = 'checkstatus';
$jaktable4 = 'transfer';
$jaktable5 = 'chat_ai_translations';

switch ($page1) {
    case 'readleads':
    
      $rowi = $jakdb->get($jaktable, ["name", "operatorname", "email", "phone", "initiated", "ended"], ["AND" => ["id" => $page2, "opid" => $opcacheid]]);

      if ($rowi) {
    
      // Let's go on with the script
      if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['email_conv'])) {
          $jkp = $_POST;
          
          // Errors in Array
          $errors = array();
          
          if ($jkp['email'] == '' || !filter_var($jkp['email'], FILTER_VALIDATE_EMAIL)) {
              $errors['email'] = $jkl['e3'];
          }
          
          if (count($errors) > 0) {
          
          /* Outputtng the error messages */
            if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
            
              header('Cache-Control: no-cache');
              die(json_encode(array('status' => 0, 'errors' => $errors, 'html' => $jkl["g116"])));
              
            } else {
              $errors = $errors;
            }
            
          } else {
          
            $result = $jakdb->select($jaktable1, "*", ["convid" => $page2, "ORDER" => ["id" => "ASC"]]);
            
            $subject = $jkl["g57"].' '.$jkp['cagent'].' '.$jkl["g58"].' '.$jkp['cuser'];
            
            $mailchat = '<div style="margin:10px 0px 0px 0px;padding:10px;border:1px solid #A8B9CB;font-family: Verdana, sans-serif;font-size: 13px;
            font-weight: 500;letter-spacing: normal;line-height: 1.5em;"><p>'.$subject.'</p><p>'.$jkl["u"].': '.$rowi['name'].' / '.$jkl["u1"].': '.$rowi['email'].' / '.$jkl["u14"].': '.$rowi['phone'].'</p><ul style="list-style:none;">';
            
            foreach ($result as $row) {
             // collect each record into $_data
             if ($row['class'] == "notice") {
               $mailchat .= '<li style="background-color:#d0e5f9;padding:5px;"><span style="font-size:10px;">'.$row['name'].' '.$jkl['g66'].':</span><br />'.$row['message'].'</li>';
             } else if ($row['class'] == "admin") {
               $mailchat .= '<li style="background-color:#effcff;padding:5px;"><span style="font-size:10px;">'.$row['time'].' - '.$row['name']." ".$jkl['g66'].':</span><br />'.$row['message'].'</li>';
             } else {
               $mailchat .= '<li style="background-color:#f4fdf1;padding:5px;"><span style="font-size:10px;">'.$row['name'].' '.$jkl['g66'].':</span><br />'.$row['message'].'</li>';
             }
            }
                
            $mailchat .= '</ul></div>';

            // Ok, we send the email // email address, cc email address, reply to, subject, message, attachment
            if (jak_send_email($jkp['email'], "", "", $subject, $mailchat, "")) {
            
            // Ajax Request
            if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
            
              header('Cache-Control: no-cache');
              die(json_encode(array('status' => 1, 'html' => $jkl["g14"])));
              
            } else {
            
                  jak_redirect($_SERVER['HTTP_REFERER']);
              
              }
          } 
        }
      }
      
        $CONVERSATION_LS = $jakdb->select($jaktable1, "*", ["convid" => $page2, "ORDER" => ["id" => "ASC"]]);
      
        // Call the template
        $template = 'readleads.php';

    } else {

        $_SESSION["errormsg"] = $jkl['i3'];
        jak_redirect(JAK_rewrite::jakParseurl('leads'));
    }
        
    break;
    case 'clientcontact':
    
      // Let's go on with the script
      if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['send_email'])) {
          $jkp = $_POST;
          
          // Errors in Array
          $errors = array();

          if ($jkp['email'] == '' || !filter_var($jkp['email'], FILTER_VALIDATE_EMAIL)) {
              $errors['email'] = $jkl['e3'];
          }
          
          if (empty($jkp['subject']) || strlen(trim($jkp['subject'])) <= 2) {
              $errors['subject'] = $jkl['e17'];
          }
          
          if (empty($jkp['message']) || strlen(trim($jkp['message'])) <= 2) {
              $errors['message'] = $jkl['e1'];
          }
          
          if (count($errors) > 0) {
          
          /* Outputtng the error messages */
            if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
            
              header('Cache-Control: no-cache');
              die('{"status":0, "errors":'.json_encode($errors).'}');
              
            } else {
              $errors = $errors;
            }
            
          } else {

            // Ok, we send the email // email address, cc email address, reply to, subject, message, attachment
            if (jak_send_email($jkp['email'], "", "", trim($jkp['subject']), trim(nl2br($jkp['message'])), "")) {
          
              // Insert the stuff into the database
              $jakdb->insert($jaktable2, ["opid" => $opcacheid,
               "sessionid" => $page2,
               "operatorid" => JAK_USERID,
               "operatorname" => $jakuser->getVar("username"),
               "subject" => trim($jkp['subject']),
               "message" => trim($jkp['message']),
               "sent" => $jakdb->raw("NOW()")]);
              
              // Ajax Request
              if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
              
                header('Cache-Control: no-cache');
                die(json_encode(array('status' => 1, 'html' => $jkl["g14"])));
                
              } else {
              
                    jak_redirect($_SERVER['HTTP_REFERER']);
                
                }
            } 
        }
      }
      
      // Ouput all messages, well with paginate of course
      $MESSAGES_ALL = $jakdb->select($jaktable2, ["id", "operatorname", "subject", "message", "sent"], ["AND" => ["sessionid" => $page2, "opid" => $opcacheid]]);
          
      $rowi = $jakdb->get($jaktable, ["name", "email"], ["AND" => ["id" => $page2, "opid" => $opcacheid]]);
      
      // Call the template
      $template = 'clientcontact.php';
        
    break;
    case 'truncate':
    
      if (!JAK_SUPERADMINACCESS) jak_redirect(BASE_URL);
      
      $jakdb->query('TRUNCATE '.JAKDB_PREFIX.$jaktable1);
      $jakdb->query('TRUNCATE '.JAKDB_PREFIX.$jaktable3);
      $jakdb->query('TRUNCATE '.JAKDB_PREFIX.$jaktable4);
      $jakdb->query('TRUNCATE '.JAKDB_PREFIX.$jaktable5);
      $result = $jakdb->query('TRUNCATE '.JAKDB_PREFIX.$jaktable);
        
      if (!$result) {
        $_SESSION["infomsg"] = $jkl['i'];
        jak_redirect(JAK_rewrite::jakParseurl('leads'));
      } else {
          $_SESSION["successmsg"] = $jkl['g14'];
        jak_redirect(JAK_rewrite::jakParseurl('leads'));
      }
       
  break;
  default:

    // Let's go on with the script
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $jkp = $_POST;
        
        if (isset($jkp['action']) && $jkp['action'] == "delete") {

        if (!JAK_USERID || !JAK_SUPERADMINACCESS) jak_redirect(BASE_URL);
        
          if (isset($jkp['jak_delete_leads'])) {
            
            $lockuser = $jkp['jak_delete_leads'];
      
              for ($i = 0; $i < count($lockuser); $i++) {
                  $locked = $lockuser[$i];

                  $convid = $jakdb->get($jaktable, "id", ["AND" => ["id" => $locked, "opid" => $opcacheid]]);
                  $jakdb->delete($jaktable1, ["convid" => $convid]);
                  $jakdb->delete($jaktable3, ["convid" => $convid]);
                  $jakdb->delete($jaktable4, ["convid" => $convid]);
                  $result = $jakdb->delete($jaktable, ["id" => $locked]);

                  // Write the log file each time someone tries to login before
                  JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 23, $locked, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
                
              }
              
              $_SESSION["successmsg"] = $jkl['g14'];
              jak_redirect($_SESSION['LCRedirect']);
          }
      
          $_SESSION["errormsg"] = $jkl['i3'];
          jak_redirect($_SESSION['LCRedirect']);
        
        }     
    }
    
    // Leads
    $total_support = $totalAll = $total_ended = $total_initated = $totalAllc = $bounce_percentage = 0;

    // Get the totals
    if (jak_get_access("leads_all", $jakuser->getVar("permissions"), JAK_MAIN_OP)) {
      $totalAll = $jakdb->count($jaktable, ["opid" => $opcacheid]);
    } else {
      $totalAll = $jakdb->count($jaktable, ["AND" => ["operatorid" => JAK_USERID, "opid" => $opcacheid]]);
    }

    // contact available
    if (jak_get_access("leads_all", $jakuser->getVar("permissions"), JAK_MAIN_OP)) {
      $totalAllc = $jakdb->count($jaktable, ["AND" => ["fcontact" => 1, "opid" => $opcacheid]]);
    } else {
      $totalAllc = $jakdb->count($jaktable, ["AND" => ["operatorid" => JAK_USERID, "fcontact" => 1, "opid" => $opcacheid]]);
    }
    
    // Get percentage
    $bounce_percentage = 0;
    if ($totalAllc && $totalAll) $bounce_percentage = round($totalAllc / $totalAll * 100, 2, PHP_ROUND_HALF_UP);

    if (jak_get_access("leads_all", $jakuser->getVar("permissions"), JAK_MAIN_OP)) {
      $total_ended = $jakdb->sum($jaktable, "ended", ["AND" => ["opid" => $opcacheid, "ended[!]" => 0, "initiated[!]" => 0]]);
      $total_initated = $jakdb->sum($jaktable, "initiated", ["AND" => ["opid" => $opcacheid, "ended[!]" => 0, "initiated[!]" => 0]]);
    } else {
      $total_ended = $jakdb->sum($jaktable, "ended", ["AND" => ["opid" => $opcacheid, "operatorid" => JAK_USERID, "ended[!]" => 0, "initiated[!]" => 0]]);
      $total_initated = $jakdb->sum($jaktable, "initiated", ["AND" => ["opid" => $opcacheid, "operatorid" => JAK_USERID, "ended[!]" => 0, "initiated[!]" => 0]]);
    }

    // Get the total support
    $total_support = (int)$total_ended - (int)$total_initated;
    
    // Title and Description
    $SECTION_TITLE = $jkl["m1"];
    $SECTION_DESC = "";
    
    // Include the javascript file for results
    $js_file_footer = 'js_leads.php';
    
    // Call the template
    $template = 'leads.php';
}
?>
