<?php
header("Access-Control-Allow-Origin: *");
$setting = array('brandUrl' => 'https://co.erticonzo.com/api/signup/procform', 'ai' => '2958032', 'ci' => '1', 'gi' => '86', 'fallback' => 'true', 'api_password' => 'g+>j!r=us4~*3QGaVX$yJ5', 'api_username' => 'erticonzo');
$result_data = sendLeads1($_POST);
$response = array();
$integrationResult = explode("@@", $result_data);

if ($integrationResult['0'] == 'ok') {
  $loginurl = loginUrl1($integrationResult['1']);
  $response['status'] = "success";
  $response['success'] = "success";
  $response['url'] = $integrationResult['1'];
  $response['data'] = $integrationResult['2'];
  $response['broker_lead_id'] = $integrationResult['3'];
} else {
  $response['status'] = "failed";
  $response['data'] = $result_data;
}
echo json_encode($response);

function sendLeads1($leads)
{

  global $setting;

  $apiData = array(
    'email' => trim($leads['email']),
    'firstname' => trim($leads['fname']),
    'lastname' => trim($leads['lname']),
    'phone' => (int) filter_var(trim($leads['phoneCode'] . $leads['phone']), FILTER_SANITIZE_NUMBER_INT),
    'userip' => trim($leads['ip']),
    'password' => trim($leads['password']),
    'so' => "FinInvestV1",
    'fallback' => $setting['fallback'],
    'gi' => trim($setting['gi']),
    'ci' => trim($setting['ci']),
    'ai' => trim($setting['ai'])
  );

  if ((isset($leads['fbclid'])) && ($leads['fbclid'] != '')) {
    $apiData['MPC_1'] = $leads['fbclid'];
  }

  $apiData['MPC_2'] = '5IT_FinInvestV1';

  if ((isset($leads['ad_name'])) && ($leads['ad_name'] != '')) {
    $apiData['MPC_3'] = $leads['ad_name'];
  }
  if ((isset($leads['site_source_name'])) && ($leads['site_source_name'] != '')) {
    $apiData['MPC_4'] = $leads['site_source_name'];
  }
  if ((isset($leads['placement'])) && ($leads['placement'] != '')) {
    $apiData['MPC_5'] = $leads['placement'];
  }
  if ((isset($leads['ad_id'])) && ($leads['ad_id'] != '')) {
    $apiData['MPC_6'] = $leads['ad_id'];
  }
  if ((isset($leads['ctry'])) && ($leads['ctry'] != '')) {
    $apiData['MPC_7'] = $leads['ctry'];
  }
  if ((isset($leads['city'])) && ($leads['city'] != '')) {
    $apiData['MPC_8'] = $leads['city'];
  }
  if ((isset($leads['adset_id'])) && ($leads['adset_id'] != '')) {
    $apiData['MPC_9'] = $leads['adset_id'];
  }
  if ((isset($leads['userid'])) && ($leads['userid'] != '')) {
    $apiData['MPC_10'] = $leads['userid'];
  }
  $apiData['MPC_11'] = 'NA';

  if ((isset($leads['campaign_id'])) && ($leads['campaign_id'] != '')) {
    $apiData['MPC_12'] = $leads['campaign_id'];
  }
  $curl_url = trim($setting['brandUrl']);

  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => $curl_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($apiData),
    CURLOPT_HTTPHEADER => array(
      "Cache-Control: no-cache",
      "Content-Type: application/json",
      "x-api-key:2643889w34df345676ssdas323tgc738",
      "x-trackbox-password: " . $setting['api_password'],
      "x-trackbox-username: " . $setting['api_username']
    ),
  ));

  $output1 = curl_exec($curl);
  $data1 = json_decode($output1, TRUE);

  $returnResult = false;
  if ($data1['status']) {
    $returnResult = "ok@@" . $data1['data'] . "@@" . $output1 . "@@" . $data1['addonData']['data']['customerId'];
  } else {
    $returnResult = $output1;
  }
  return $returnResult;
}

function loginUrl1($url)
{
  return $url;
}
