<?php
require_once("dbconnect_mysqli.php");
require_once("functions.php");


// $file  = fopen("./logs/".time().".txt", 'w');

// fwrite($file, print_r($_GET,true));
// fwrite($file, "\n-------------------\n");
// fwrite($file, print_r($_POST,true));
// fwrite($file, "\n-------------------\n");


if(isset($_GET['user'])){
  $user = $_GET['user'];
}elseif(isset($_POST['user'])){
  $user = $_POST['user'];
}

if (isset($_GET['campaign_id'])) {
  $campaign_id = $_GET['campaign_id'];
}elseif (isset($_POST['campaign_id'])) {
  $campaign_id = $_POST['campaign_id'];
}

if (isset($_GET['user_group'])) {
  $user_group = $_GET['user_group'];
}elseif (isset($_POST['user_group'])) {
  $user_group = $_POST['user_group'];
}

if (isset($_GET['event'])) {
  $event = $_GET['event'];
}elseif (isset($_POST['event'])) {
  $event = $_POST['event'];
}

if (isset($_GET['event_type'])) {
  $event_type = $_GET['event_type'];
}elseif (isset($_POST['event_type'])) {
  $event_type = $_POST['event_type'];
}

if (isset($_GET['status'])) {
  $status = $_GET['status'];
}elseif (isset($_POST['status'])) {
  $status = $_POST['status'];
}

$sub_status  = '';
if (isset($_GET['sub_status'])) {
  $sub_status = $_GET['sub_status'];
}elseif (isset($_POST['sub_status'])) {
  $sub_status = $_POST['sub_status'];
}

if (isset($_GET['message'])) {
  $message = $_GET['message'];
}elseif (isset($_POST['message'])) {
  $message = $_POST['message'];
}

if (isset($_GET['lead_id'])) {
  $lead_id = $_GET['lead_id'];
}elseif (isset($_POST['lead_id'])) {
  $lead_id = $_POST['lead_id'];
}

if (isset($_GET['agent_log_id'])) {
  $agent_log_id = $_GET['agent_log_id'];
}elseif (isset($_POST['agent_log_id'])) {
  $agent_log_id = $_POST['agent_log_id'];
}

if($user && $campaign_id && $user_group && $event && $event_type && $status){
  $current_time = date("Y-m-d H:i:s");

  $time_difference = 0;
   if($event_type != 'LOGIN'){
      //Updating previous event time duration
      $stmt = "SELECT * FROM telxio_agent_log_table WHERE user = '".$user."' AND user_group = '".$user_group."' AND campaign_id = '".$campaign_id."' ORDER BY time_stamp DESC, id DESC LIMIT 1;";  
      $rslt = mysql_to_mysqli($stmt, $link);
      
      $row = mysqli_fetch_assoc($rslt);
      $time_difference = strtotime($current_time) - strtotime($row['time_stamp']);
      $stmt = "UPDATE telxio_agent_log_table SET event_time = $time_difference WHERE id = ".$row['id'];
      $rslt = mysql_to_mysqli($stmt,$link);
    }

  //Create new event log
  $stmt = "INSERT INTO telxio_agent_log_table(user, campaign_id, user_group, sub_status, event_type, status) VALUES('".$user. "','" . $campaign_id . "','" . $user_group . "','" . $sub_status . "','" . $event_type . "','" . $status . "')";
  
  // fwrite($file, $stmt);
  // fwrite($file, "\n-------------------\n");

  $rslt=mysql_to_mysqli($stmt, $link);
  if($rslt){

    // fwrite($file, "success");
    echo "success";
  }else
  {
    // fwrite($file, "failed");
    echo "Failed";
  }
}else
{
  // fwrite($file, "failed2");
}
// fclose($file);