<?php
require_once("../../dbconnect_mysqli.php");
require_once("../../functions.php");
include("./zoho_functions.php");

if (isset($_GET["user"])) {
	$user = $_GET["user"];
} elseif (isset($_POST["user"])) {
	$user = $_POST["user"];
}
if (isset($_GET["dispo_choice"])) {
	$dispo_choice = $_GET["dispo_choice"];
} elseif (isset($_POST["dispo_choice"])) {
	$dispo_choice = $_POST["dispo_choice"];
}
if (isset($_GET["CallBackDatETimE"])) {
	$CallBackDatETimE = $_GET["CallBackDatETimE"];
} elseif (isset($_POST["CallBackDatETimE"])) {
	$CallBackDatETimE = $_POST["CallBackDatETimE"];
}
if (isset($_GET["phone_number"])) {
	$phone_number = $_GET["phone_number"];
} elseif (isset($_POST["phone_number"])) {
	$phone_number = $_POST["phone_number"];
}
if (isset($_GET["phone_code"])) {
	$phone_code = $_GET["phone_code"];
} elseif (isset($_POST["phone_code"])) {
	$phone_code = $_POST["phone_code"];
}
if (isset($_GET["uniqueid"])) {
	$uniqueid = $_GET["uniqueid"];
} elseif (isset($_POST["uniqueid"])) {
	$uniqueid = $_POST["uniqueid"];
}
if (isset($_GET["CallBackLeadStatus"])) {
	$CallBackLeadStatus = $_GET["CallBackLeadStatus"];
} elseif (isset($_POST["CallBackLeadStatus"])) {
	$CallBackLeadStatus = $_POST["CallBackLeadStatus"];
}
if (isset($_GET["comments"])) {
	$comments = $_GET["comments"];
} elseif (isset($_POST["comments"])) {
	$comments = $_POST["comments"];
}
if (isset($_GET["recording_id"])) {
	$recording_id = $_GET["recording_id"];
} elseif (isset($_POST["recording_id"])) {
	$recording_id = $_POST["recording_id"];
}
if (isset($_GET["recording_filename"])) {
	$recording_filename = $_GET["recording_filename"];
} elseif (isset($_POST["recording_filename"])) {
	$recording_filename = $_POST["recording_filename"];
}
if (isset($_GET["recording_location"])) {
	$recording_location = $_GET["recording_location"];
} elseif (isset($_POST["recording_location"])) {
	$recording_location = $_POST["recording_location"];
}
if (isset($_GET["whatID"])) {
	$whatID = $_GET["whatID"];
} elseif (isset($_POST["whatID"])) {
	$whatID = $_POST["whatID"];
}
if (isset($_GET["whoID"])) {
	$whoID = $_GET["whoID"];
} elseif (isset($_POST["whoID"])) {
	$whoID = $_POST["whoID"];
}
if (isset($_GET["customer_sec"])) {
	$customer_sec = $_GET["customer_sec"];
} elseif (isset($_POST["customer_sec"])) {
	$customer_sec = $_POST["customer_sec"];
}
if (isset($_GET["api_action"])) {
	$action = $_GET["api_action"];
} elseif (isset($_POST["api_action"])) {
	$action = $_POST["api_action"];
}
if (isset($_GET["callBackID"])) {
	$callBackID = $_GET["callBackID"];
} elseif (isset($_POST["callBackID"])) {
	$callBackID = $_POST["callBackID"];
}
//#######################################   Only For Click To Call   ###################################################
if (isset($_GET["value"])) {
	$value = $_GET["value"];
} elseif (isset($_POST["value"])) {
	$value = $_POST["value"];
}
if (isset($_GET["agent_user"])) {
	$agent_user = $_GET["agent_user"];
} elseif (isset($_POST["agent_user"])) {
	$agent_user = $_POST["agent_user"];
}
########################################################################################################################
//    ################################   Functions    ##################################

if (isset($action) && $action == 'phoneCheckLeadOrContact') {
	$data = searchForLeadOrContact($phone_number);
	echo json_encode($data);
} elseif (isset($action) && $action == 'recording_check') {
	$recording_check_data = array();
	$stmt = "SELECT * FROM recording_log WHERE recording_id = '" . $recording_id . "' AND filename = '" . $recording_filename . "' LIMIT 1 ";
	$rslt = mysql_to_mysqli($stmt, $link);
	$recording_check_ct = mysqli_num_rows($rslt);
	if ($recording_check_ct > 0) {
		$recording_check_data['flag'] = TRUE;
	} else {
		$recording_check_data['flag'] = FALSE;
	}
	echo json_encode($recording_check_data);
} elseif (isset($action) && $action == 'leadCallLogCreate') {
	$CallBackDatETimE = date_format(date_create($CallBackDatETimE), DATE_ATOM);
	$current_time = date_format(date_create(date('Y-m-d H:m:s', time())), DATE_ATOM);
	$result_data = array();
	$checkingInboundOrOutbound = checkingInboundOrOutbound($uniqueid, $link);
	if ($callBackID) {
		$checking_zohoCallLog_id = checking_zohoCallLog_id($callBackID, $link);
	} else {
		$checking_zohoCallLog_id = NULL;
	}

	$stmt = "SELECT status_name FROM vicidial_statuses WHERE status ='" . $dispo_choice . "' LIMIT 1;";
	$rslt = mysql_to_mysqli($stmt, $link);
	$status_name_check_data = mysqli_fetch_assoc($rslt);
	$Disposition_Name_dialer = $status_name_check_data['status_name'];

	$data = CallLog_create_forLead($recording_location, $dispo_choice, $Disposition_Name_dialer, $comments, $whatID, $current_time, $customer_sec, $checkingInboundOrOutbound, $CallBackDatETimE, $CallBackLeadStatus, $checking_zohoCallLog_id);

	if ($data) {
		$result_data['flag'] = TRUE;
		$result_data['callLog_id'] = $data;
	} else {
		$result_data['flag'] = FALSE;
	}
	echo json_encode($result_data);
} elseif (isset($action) && $action == 'contactCallLogCreate') {
	$CallBackDatETimE = date_format(date_create($CallBackDatETimE), DATE_ATOM);
	$current_time = date_format(date_create(date('Y-m-d H:m:s', time())), DATE_ATOM);
	$result_data = array();
	$checkingInboundOrOutbound = checkingInboundOrOutbound($uniqueid, $link);

	$stmt = "SELECT status_name FROM vicidial_statuses WHERE status ='" . $dispo_choice . "' LIMIT 1;";
	$rslt = mysql_to_mysqli($stmt, $link);
	$status_name_check_data = mysqli_fetch_assoc($rslt);
	$Disposition_Name_dialer = $status_name_check_data['status_name'];

	$data = CallLog_create_forContact($recording_location, $dispo_choice, $Disposition_Name_dialer, $comments, $whoID, $current_time, $customer_sec, $checkingInboundOrOutbound, $CallBackDatETimE, $CallBackLeadStatus);
	if ($data) {
		$result_data['flag'] = TRUE;
		$result_data['callLog_id'] = $data;
	} else {
		$result_data['flag'] = FALSE;
	}
	echo json_encode($result_data);
} elseif (isset($action) && $action == 'ClickToCall') {
	$post_data = [
		"value" 			=> $value,
		"phone_code" 	=> "1",
		"search" 			=> "YES",
		"preview"			=> "NO",
		"focus" 			=> "YES",
		"user" 				=> "6666",
		"pass" 				=> "eurus2020",
		"agent_user" 	=> $agent_user,
		"source" 			=> "ZOHO",
		"function"		=> "external_dial"
	];

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "https://etidialer206.telxio.com.sg/agent/api.php");
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
	$response = curl_exec($ch);
	$response = json_decode($response);
	curl_close($ch);
} elseif (isset($action) && $action == 'updateCallBackToCompleted') {
	$current_time = date_format(date_create(date('Y-m-d H:m:s', time())), DATE_ATOM);

	$stmt = "SELECT vc.zoho_callLog_id, vl.phone_number FROM vicidial_callbacks vc LEFT JOIN vicidial_list vl ON vc.lead_id = vl.lead_id WHERE callback_id = '" . $callBackID . "' ";
	$rslt = mysql_to_mysqli($stmt, $link);
	$zohoCallLog_arr = mysqli_fetch_assoc($rslt);
	$zohoCallLog_ID = $zohoCallLog_arr['zoho_callLog_id'];
	$phone_number = $zohoCallLog_arr['phone_number'];

	if (isset($zohoCallLog_arr) && !empty($zohoCallLog_arr)) {
		$leadOrContactID_data = searchForLeadOrContact($phone_number);
		if ($leadOrContactID_data['found']) {
			if ($leadOrContactID_data['lead']) {
				$id = $leadOrContactID_data['lead'];
				$module = 'Leads';
			} elseif ($leadOrContactID_data['contact']) {
				$id = $leadOrContactID_data['contact'];
				$module = 'Contacts';
			}
		}

		$result = updateCallBackToCompleted($id, $zohoCallLog_ID, $module, $current_time);

		if ($result) {
			$stmt = "UPDATE vicidial_callbacks SET zoho_callLog_id = '" . $result . "' WHERE callback_id = $callBackID;";
			$rslt = mysql_to_mysqli($stmt, $link);
			if ($rslt) {
				$result_data['flag'] = TRUE;
			} else {
				$result_data['flag'] = FALSE;
			}
		}
		echo json_encode($result_data);
	}
}
// #########################################    Custom Functions   ###############################################################

function checkingInboundOrOutbound($uniqueid = NULL, $link = NULL)
{
	if ($uniqueid) {
		$result = '';
		$stmt1 = "SELECT direction FROM (select uniqueid, comments, 'OUTBOUND' as direction from vicidial_log where uniqueid = '" . $uniqueid . "' UNION select uniqueid, comments, 'INBOUND' as direction from vicidial_closer_log where uniqueid = '" . $uniqueid . "') a;";
		$rslt1 = mysql_to_mysqli($stmt1, $link);
		$result_data_array = mysqli_fetch_assoc($rslt1);
		$result_data = $result_data_array['direction'];
		return $result_data;
	}
}

function checking_zohoCallLog_id($callBackID = NULL, $link = NULL)
{
	if ($callBackID) {
		$stmt = "SELECT zoho_callLog_id FROM vicidial_callbacks WHERE callback_id = '" . $callBackID . "';";
		$rslt = mysql_to_mysqli($stmt, $link);
		$result_data_array = mysqli_fetch_assoc($rslt);
		if ($result_data_array['zoho_callLog_id']) {
			$result_data = $result_data_array['zoho_callLog_id'];
			return $result_data;
		} else {
			return FALSE;
		}
	} else {
		return FALSE;
	}
}
