<?php
	/*
		- Exent HUB
	*/
	try {
		//Variáveis
		$api_username = USER_EXENTHUB;  
		$api_password = PASS_EXENTHUB;  
		$exhub_campaing = "";
		$exhub_is_mail_sent = 0; // 0 para enviar e-mail através do Exent Hub / 1 para não enviar

		//Váriaveis ExentHub
		$exhub_name = $nome; //Required
		$exhub_email = $email;
		$exhub_ddd = $ddd;
		$exhub_phone = $telefone;

		//Info Origin
		$exhub_info_origin = array();

			//Trata informações padrão
			isset($_POST['utm_source']) ? $exhub_info_origin['utm_source'] = $_POST['utm_source'] : '';
			isset($_POST['utm_medium']) ? $exhub_info_origin['utm_medium'] = $_POST['utm_medium'] : '';
			isset($_POST['utm_campaign']) ? $exhub_info_origin['utm_campaign'] = $_POST['utm_campaign'] : '';
			isset($_POST['utm_id']) ? $exhub_info_origin['utm_id'] = $_POST['utm_id'] : '';
			isset($_POST['utm_term']) ? $exhub_info_origin['utm_term'] = $_POST['utm_term'] : '';
			isset($_POST['utm_content']) ? $exhub_info_origin['utm_content'] = $_POST['utm_content'] : '';

			//Trata informações campanha Google
			isset($_POST['campaignsource']) ? $exhub_info_origin['campaignsource'] = $_POST['campaignsource'] : '';
			isset($_POST['campaignmedium']) ? $exhub_info_origin['campaignmedium'] = $_POST['campaignmedium'] : '';
			isset($_POST['campaignid']) ? $exhub_info_origin['campaignid'] = $_POST['campaignid'] : '';
			isset($_POST['adgroupid']) ? $exhub_info_origin['adgroupid'] = $_POST['adgroupid'] : '';
			isset($_POST['keyword']) ? $exhub_info_origin['keyword'] = $_POST['keyword'] : '';
			isset($_POST['creative']) ? $exhub_info_origin['creative'] = $_POST['creative'] : '';
			isset($_POST['adposition']) ? $exhub_info_origin['adposition'] = $_POST['adposition'] : '';
			isset($_POST['matchtype']) ? $exhub_info_origin['matchtype'] = $_POST['matchtype'] : '';
			isset($_POST['network']) ? $exhub_info_origin['network'] = $_POST['network'] : '';
			isset($_POST['device']) ? $exhub_info_origin['device'] = $_POST['device'] : '';

		$exhub_info_origin = json_encode($exhub_info_origin);

		$exhub_info = array();
		$exhub_info['custom_fields'] = $custom_fields;
		$exhub_info = json_encode($exhub_info);

		$token_curl = curl_init();

		curl_setopt_array($token_curl, array(
			CURLOPT_URL => EXENTHUB_URL . 'auth/login',
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'POST',
			CURLOPT_POSTFIELDS => array(
				'api_username' => $api_username,
				'api_password' => $api_password),
		));

		$response_token_curl = json_decode(curl_exec($token_curl), true);

		//Adiciona LEAD se credênciais válidas
		if(!isset($response_token_curl['error'])) {

			$exhub_access_token = $response_token_curl['access_token'];
			$exhub_project_id = $response_token_curl['project_id'];

			//CURL - ADD LEAD
			$add_lead_curl = curl_init();

			curl_setopt_array($add_lead_curl, array(
				CURLOPT_URL => EXENTHUB_URL . 'add_lead',
				CURLOPT_RETURNTRANSFER => true,
				CURLOPT_ENCODING => '',
				CURLOPT_MAXREDIRS => 10,
				CURLOPT_TIMEOUT => 0,
				CURLOPT_FOLLOWLOCATION => true,
				CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				CURLOPT_CUSTOMREQUEST => 'POST',
				CURLOPT_POSTFIELDS => array(
						'name' => $exhub_name,
						'email' => $exhub_email,
						'ddd' => $exhub_ddd,
						'phone' => $exhub_phone,
						'content' => $exhub_content,
						'media' => $exhub_media,
						'campaing' => $exhub_campaing,
						'is_mail_sent' => $exhub_is_mail_sent,
						'project' => $exhub_project_id,
						'info' => $exhub_info,
						'info_origin' => $exhub_info_origin,
						'origin' => $origem
					),
				CURLOPT_HTTPHEADER => array(
					"Authorization: Bearer {$exhub_access_token}"
				),
			));

			$response = curl_exec($add_lead_curl);
			$exhub_response = json_decode($response, true);

			if (isset($exhub_response['info'])) {
				$exhub_response_info = $exhub_response['info'];
				$exhub_info_lead_id = $exhub_response_info['id'];
			}

			curl_close($add_lead_curl);

		}

		curl_close($token_curl);

	} catch (Exception $e) {
		echo 'Falha ao cadastrar no Exent Hub: ',  $e->getMessage(), "\n";
	}

	/*
		- FIM | Exent HUB
	*/

    return isset($exhub_response) ? $exhub_response : ['error' => 'No response from ExentHub API'];


?>