<?php

include_once 'casafari.class.php';

//$client_url = 'https://crmapi.proppydev.com';
//$client_secret = 'ZXh0ZXJuYWxkZXY6Rml0aDVmQmRmZ3NkeEhWTUF4aXpkNW9yNjhn';
$client_url = 'https://crmapi.casafaricrm.com';
$client_secret = 'dGVuIEhvb3BlbiAtIENyZWF0aW9uIE1lZGlhOnRlbkhvb3BlbjIwMjQq';
$setFalse = false;
$lang = $_GET['lang'] ?: null;

if($_GET['name'] && ($_GET['phone'] || $_GET['email']) && $_GET['message']) {

    $casafari_api = new CasafariAPI($client_url, $client_secret);

    $reference = $_GET['reference'] ?: null;
    $property_id = $_GET['property_id'] ?: null;
    $name = $_GET['name'] ?: null;
    $phone = $_GET['phone'] ?: null;
    $email = $_GET['email'] ?: null;
    $message = $_GET['message'] ?: null;
    $subject = $_GET['subject'] ?: null;
    $allowpp = $_GET['allowpp'] ?: null;
   // $lang = $_GET['lang'] ?: null;

    //Prepare Title and Message
    $title = $subject ? $subject : $name . ($reference ? ' - ' . $reference : '');
    $msg = "Website: www.tenhoopenrealty.com" . "\r\n";
    $msg .= "Name: " . $name . "\r\n";
    $msg .= "Email: " . $email . "\r\n";
    $msg .= "Phone: " . $phone . "\r\n";
    $msg .= "Message: " . "\r\n";
    $msg .= $message;

    $params = [
        'Settings' => [
            'LeadTitle' => $title,
            'AssignToPropertyId' => $property_id ? (int)$property_id : null,
            'AssignToCustomerOriginId' => 1073,
            'AssignToAgentCurrentlyAssignedToProperty' => true,
            'AssignToAgentCurrentlyAssignedToCustomer' => true,
            'IncludeMailing' => $allowpp ? true : false,
            'IncludeOptIn' => $allowpp ? true : false
        ],
        'Contact' => [
            'Name' => $name,
            'Email' => $email,
            'Phone' => $phone,
            'Message' => $msg,
            'Address' => '',
            'Location' => '',
            'Culture' => 'pt',
            'CountryISO' => 'PT',
            'Type' => 'Buyer',
        ],
        'CorrelationId' => 'string',
        'EchoToken' => 'string',
        'PrimaryLangId' => 0,
        'RetransmissionIndicator' => true,
        'TimeStamp' => 'string',
        'Version' => 0.1
    ];

    $lead = $casafari_api->postLead($params);

} else {

    $lead = new stdClass();
    $lead->success = false;
    $lead->error = 'Invalid request';

}

//header('Content-type: application/json; charset=utf-8');
//echo json_encode([$params, json_decode($json)]);
//echo json_encode($lead);

// Modified by Mohsin
// Adding check for Property ID Also
// $redirect = $reference && $property_id ? 'https://new.tenhoopenrealty.com/property-details?reference=' . $reference . '&property_id=' . $property_id : 'https://new.tenhoopenrealty.com/contact-us';

$redirect = $lang ? 'https://new.tenhoopenrealty.com/' . $lang : 'https://new.tenhoopenrealty.com/thank-you';
header('Location: ' . $redirect);
die();
