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

if (isset($_GET['action']) && !empty($_GET['action'])) {
  $action = $_GET['action'];
} elseif (isset($_POST['action']) && !empty($_POST['action'])) {
  $action = $_POST['action'];
}

if (isset($_GET['agent_user']) && !empty($_GET['agent_user'])) {
  $agent_user = $_GET['agent_user'];
} elseif (isset($_POST['agent_user']) && !empty($_POST['agent_user'])) {
  $agent_user = $_POST['agent_user'];
}


$response = array("message" => "", "flag" => false);
$host_url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'];
$admin_user   = "49645333";
$admin_password = "ExYdeLLr630253";

if ('hold_active_call' == $action) {
  if ($agent_user) {
    //Getting selected ingroups for agent
    $stmt = "SELECT closer_campaigns FROM vicidial_live_agents WHERE user='$agent_user'";
    $rslt = mysql_to_mysqli($stmt, $link);
    $row = mysqli_fetch_row($rslt);
    $closer_groups_pre = preg_replace('/-$/', '', $row[0]);

    if (preg_match('/AGENTDIRECTHOLDQ /', $closer_groups_pre)) {
      //Remove hold ingroup from agent
      $closer_groups_pre = preg_replace('/AGENTDIRECTHOLDQ /', '', $closer_groups_pre);
      $curl = curl_init();
      curl_setopt_array($curl, array(
        CURLOPT_URL => $host_url . "/agent/api.php",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => array('source' => 'realtime', 'function' => 'change_ingroups', 'user' => $admin_user, 'pass' => $admin_password, 'agent_user' => $agent_user, 'value' => 'CHANGE', 'ingroup_choices' => $closer_groups_pre, 'set_as_default' => 'NO', 'blended' => 'YES')
      ));
      $response_ingroup_changes = curl_exec($curl);
      curl_close($curl);
    }

    #####  Perform dial with customer action   #####
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => $host_url . "/agent/api.php",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_SSL_VERIFYHOST => 0,
      CURLOPT_SSL_VERIFYPEER => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => array('source' => 'test', 'user' => $admin_user, 'pass' => $admin_password, 'agent_user' => $agent_user, 'function' => 'transfer_conference', 'value' => 'DIAL_WITH_CUSTOMER', 'ingroup_choices' => 'AGENTDIRECTHOLDQ', 'consultative' => 'YES', 'phone_number' => $agent_user, 'cid_choice' => 'AGENT_PHONE')
    ));
    $response_dial_with_customer = curl_exec($curl);
    curl_close($curl);

    if (preg_match("/SUCCESS/", $response_dial_with_customer)) {
      #######   2 Second sleep required for performing action on agent page.   #######
      sleep(2);
      #####  Perform leave3way action   #####
      $curl = curl_init();
      curl_setopt_array($curl, array(
        CURLOPT_URL => $host_url . "/agent/api.php",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => array('source' => 'test', 'user' => $admin_user, 'pass' => $admin_password, 'agent_user' => $agent_user, 'function' => 'transfer_conference', 'value' => 'LEAVE_3WAY_CALL')
      ));
      $response_leave_call = curl_exec($curl);
      curl_close($curl);

      if (preg_match("/SUCCESS/", $response_leave_call)) {
        sleep(2);
        #####  Perform dispose action and submit   #####
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => $host_url . "/agent/api.php",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_SSL_VERIFYHOST => 0,
          CURLOPT_SSL_VERIFYPEER => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS => array('source' => 'test', 'user' => $admin_user, 'pass' => $admin_password, 'agent_user' => $agent_user, 'function' => 'external_status', 'value' => 'HOLDCU')
        ));
        $response_disposition = curl_exec($curl);
        curl_close($curl);
        if (preg_match("/SUCCESS/", $response_disposition)) {
          echo json_encode(array("message" => "Hold Active call request success", "flag" => true));
        } else {
          echo json_encode(array("message" => "Disposition submit request failed." . $response_disposition, "flag" => false));
        }
      } else {
        echo json_encode(array("message" => "Leave 3 Way call request failed." . $response_leave_call, "flag" => false));
      }
    } else {
      echo json_encode(array("message" => "Dial with customer request failed." . $response_dial_with_customer, "flag" => false));
    }
  } else {
    echo json_encode(array("message" => "Partial parameters received", "flag" => false));
  }
} elseif ('remove_ingroup' == $action) {
} elseif ('add_ingroup' == $action) {
  $stmt = "SELECT closer_campaigns FROM vicidial_live_agents WHERE user='$agent_user'";
  $rslt = mysql_to_mysqli($stmt, $link);
  $row = mysqli_fetch_row($rslt);
  $closer_groups_pre = preg_replace('/-$/', '', $row[0]);
  if (!preg_match('/AGENTDIRECTHOLDQ /', $closer_groups_pre)) {
    $closer_groups_pre = preg_replace('/-/', '', $closer_groups_pre);
    $closer_groups_pre .= "AGENTDIRECTHOLDQ -";

    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => $host_url . "/agent/api.php",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_SSL_VERIFYHOST => 0,
      CURLOPT_SSL_VERIFYPEER => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => array('source' => 'realtime', 'function' => 'change_ingroups', 'user' => $admin_user, 'pass' => $admin_password, 'agent_user' => $agent_user, 'value' => 'CHANGE', 'ingroup_choices' => $closer_groups_pre, 'set_as_default' => 'NO', 'blended' => 'YES')
    ));
    $response_dial_with_customer = curl_exec($curl);
    curl_close($curl);

    if (preg_match("/SUCCESS/", $response_dial_with_customer)) {
      echo json_encode(array("message" => "Ingroups successfully changed", "flag" => true));
    } else {
      echo json_encode(array("message" => "Ingroups change request failed.", "flag" => false));
    }
  } else {
    echo json_encode(array("message" => "AGENTDIRECTHOLDQ ingroup is not added.", "flag" => false));
  }
} else {
  echo json_encode(array("message" => "Invalid request made", "flag" => false));
}
