<?php  
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$settingObj =  new  setting();
/*$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();
$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'xcare_default_setting.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}*/



if(isset($_REQUEST['event_name']) && $_REQUEST['event_name']=='xcare_default' && isset($_REQUEST['event_name'])){

    $where = array("setting_name="=>'xcare_default');
    $data = array("where"=>$where);
    $xcaredefultDetail = $settingObj->fromFields($data);
    //print_r($xcaredefultDetail->setting_value);exit;
    
}

//if(isset($_POST['submit'])){
if (isset($_REQUEST['act']) && $_REQUEST['act']=='setting'){
        
    $field_settingvalues1 = $_POST['field_setting1'];
    $field_settingvalues2 = $_POST['field_setting2'];
    if(!empty($field_settingvalues1) && !empty($field_settingvalues2))
    {

        $xcaresetting_details = array();
        foreach ($field_settingvalues1 as $skey => $settingkey) 
        {        
            if(isset($settingkey) && $settingkey != '' && $field_settingvalues2[$skey] != '')
            {
                $xcaresetting_details[$settingkey] = $field_settingvalues2[$skey];
            }
        }
        $xcaresetting_details['smsprovider']=$_POST['communicationprovider'];
        $all_setting = json_encode($xcaresetting_details);
        $data=array(
            'setting_value'=>trim($all_setting),
        );
        $where_up = array('setting_name=' => 'XCARE_DEFAULT');
        
        $rs=$settingObj->updateFromFields($data,$where_up);
        if ($rs){
            //header('Location:setting.php');
            exit;
        }
    }    
}


if(isset($_REQUEST['act']) && $_REQUEST['act']=='getcode' && $_REQUEST['goto_clientId'] != '' && $_REQUEST['goto_client_secret'] != '')
{
    $client_id = trim($_REQUEST['goto_clientId']);
    $redirect_url = SITE_URL."x-admin/gotocode.php";
    $url = "https://authentication.logmeininc.com/oauth/authorize?client_id=".$client_id."&response_type=code&redirect_uri=".$redirect_url."";

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

    exit;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='load' && $_REQUEST['goto_clientId'] != '')
{
    $clinicObj = new clinic();
    $clinic_detail = $clinicObj->fromId($_REQUEST['clinic_id']);
    
    $client_id = trim($_REQUEST['goto_clientId']);
    
    if(isset($_REQUEST['goto_code']) && $_REQUEST['goto_code'] != '')
    {
      $curl = curl_init();

      
      $client_secret = trim($_REQUEST['goto_client_secret']);
      $allclient_data = $client_id.":".$client_secret;
      $authorizationdata = base64_encode($allclient_data);
      //$rdirect_url = urlencode('https://google.com');
      $redirect_url = urlencode(SITE_URL."x-admin/gotocode.php");
      $data = 'redirect_uri='.$redirect_url.'&grant_type=authorization_code&code='.$_REQUEST['goto_code'];
      $authorized_val = $authorizationdata;
      //$authorized_header = "authorization: Basic ".GOTO_HEADER_TOKEN;
      $authorized_header = "authorization: Basic ".$authorized_val;

      curl_setopt_array($curl, array(
        CURLOPT_URL => "https://authentication.logmeininc.com/oauth/token",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_HTTPHEADER => array(
          $authorized_header,
          "cache-control: no-cache",
          "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
      {
         $gotoresponse =  json_decode($response,1);
         //print_r($gotoresponse);exit;
         if(isset($gotoresponse['error']) && $gotoresponse['error'] != '')
         {
            echo $gotoresponse['error'].' and '.$gotoresponse['error_description'];
            $responseerror = $gotoresponse['error'].' and '.$gotoresponse['error_description'];
            /*$admin_detail .= " <b>ClinicId</b>: ".$_REQUEST['clinic_id']."<br/><b>Gotosetting</b>: ".$responseerror;

            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$admin_detail);*/
         }
         else{
            
            echo 'success';
            
            $where = array("setting_name="=>'xcare_default');
            $data = array("where"=>$where);
            $xcaredefultDetail = $settingObj->fromFields($data);
            $clinicsettingData = json_decode($xcaredefultDetail->setting_value,1);

            if(isset($gotoresponse['access_token']) && $gotoresponse['access_token'] != '')
            {
                $clinicsettingData['gotoaccess_token']=$gotoresponse['access_token'];
                
            }
            if(isset($gotoresponse['refresh_token']) && $gotoresponse['refresh_token'] != '')
            {
                $clinicsettingData['gotorefersh_token']=$gotoresponse['refresh_token'];
                
            }
            if(isset($authorized_val) && $authorized_val != '')
            {
                $clinicsettingData['gotoheaderauthorized']=$authorized_val;
                
            }          
            
            $settingdata['setting_value'] = json_encode($clinicsettingData);

            
            $where_up = array('setting_name=' => 'XCARE_DEFAULT');
            
            $rs=$settingObj->updateFromFields($data,$where_up);
            /*$clinicseting = $clinicObj->updateRecords($settingdata,$_REQUEST['clinic_id']);*/ 
            /*$admin_detail .= " <b>ClinicId</b> ".$_REQUEST['clinic_id']."<br/><b>Gotosetting</b>".$response;
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$admin_detail);*/
         }
      }
      exit;
    }
    exit;
}


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