<?php  
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');

if(isset($_REQUEST['act']) && $_REQUEST['act']=='getcode' && $_REQUEST['ghl_clientId'] != '' && $_REQUEST['ghl_client_secret'] != '')
{
    $client_id = trim($_REQUEST['ghl_clientId']);
    $client_secret = trim($_REQUEST['ghl_client_secret']);
    $redirect_url = SITE_URL."x-admin/gohl.php";
    $url = "https://marketplace.leadconnectorhq.com/oauth/chooselocation?response_type=code&client_id=".$client_id."&redirect_uri=".$redirect_url."&scope=locations.write contacts.write contacts.readonly campaigns.readonly locations.readonly locations/customValues.readonly locations/customValues.write oauth.write oauth.readonly companies.readonly locations/customFields.readonly locations/customFields.write businesses.readonly";

    echo "<script type=\"text/javascript\">
            window.open('".$url."', '_blank')
        </script>";

    exit;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='GHLaddcodetoken' && $_REQUEST['ghl_code']!=""){
    $client_id = trim($_REQUEST['ghl_clientId']);
    
    if(isset($_REQUEST['ghl_code']) && $_REQUEST['ghl_code'] != ''){
        $curl = curl_init();
        $client_secret = trim($_REQUEST['ghl_client_secret']);
        $ghl_code =$_REQUEST['ghl_code'];
      

        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://services.leadconnectorhq.com/oauth/token',
        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 => 'client_id='.$client_id.'&client_secret='.$client_secret.'&grant_type=authorization_code&code='.$ghl_code,
        CURLOPT_HTTPHEADER => array(
            'Accept: application/json',
            'Content-Type: application/x-www-form-urlencoded'
        ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            echo "cURL Error #:" . $err;
        }else{
            $ghlresponse =  json_decode($response,1);
            if(isset($ghlresponse['error']) && $ghlresponse['error'] != ''){
                echo $ghlresponse['error'].' => '.$ghlresponse['error_description'];
                $responseerror = $ghlresponse['error'].' and '.$ghlresponse['error_description'];
                
            }else{
                echo 'success';
                $where = array("setting_name="=>'XCARE_DEFAULT');
                $data = array("where"=>$where);
                $xcaredefultDetail = $settingObj->fromFields($data);
                $xcaredefultDetail =json_decode($xcaredefultDetail->setting_value,1);
                if(isset($ghlresponse['access_token']) && $ghlresponse['access_token'] != ''){
                    $xcaredefultDetail['ghl_access_token']=$ghlresponse['access_token'];
                }
                if(isset($ghlresponse['refresh_token']) && $ghlresponse['refresh_token'] != ''){
                    $xcaredefultDetail['ghl_refersh_token']=$ghlresponse['refresh_token'];
                }
                if(isset($ghlresponse['companyId']) && $ghlresponse['companyId'] != ''){
                    $xcaredefultDetail['ghl_companyId']=$ghlresponse['companyId'];
                }
                if(isset($ghlresponse['locationId']) && $ghlresponse['locationId'] != ''){
                    $xcaredefultDetail['ghl_locationId']=$ghlresponse['locationId'];
                }
                if(isset($ghlresponse['userId']) && $ghlresponse['userId'] != ''){
                    $xcaredefultDetail['ghl_userId']=$ghlresponse['userId'];
                }
                $data=array(
                    'setting_value'=>json_encode($xcaredefultDetail),
                );
                $where_up = array('setting_name=' => 'XCARE_DEFAULT');
                $rs=$settingObj->updateFromFields($data,$where_up);
            }
        }
      exit;
    }
    exit;
}

$where = array("setting_name="=>'XCARE_DEFAULT');
$data = array("where"=>$where);
$xcaredefultDetail = $settingObj->fromFields($data);
$xcaredefultDetail =json_decode($xcaredefultDetail->setting_value,1);

// Include Template 
include_once('../libs/routs.php');