<?php
include_once('def.php');

class wrenchboard_class{

   // public  $REACT_APP_AUX_ENDPOINT = "https://apigate.lotus.g1.wrenchboard.com/svs/user";
   // public  $REACT_APP_USERS_ENDPOINT = "https://apigate.lotus.g1.wrenchboard.com/svs/user";

   // good ones 
    public  $REACT_APP_AUX_ENDPOINT   = "http://10.204.5.100:9083/en/wrench/api/v1"; //"https://apigate.lotus.g1.wrenchboard.com/svs/user";
    public  $REACT_APP_USERS_ENDPOINT = "http://10.204.5.100:9083/en/wrench/api/v1"; // "https://apigate.lotus.g1.wre


   //public  $REACT_APP_AUX_ENDPOINT   = "http://10.20.30.19:8000/en/wrench/api/v1"; //"https://apigate.lotus.g1.wrenchboard.com/svs/user";
  // public  $REACT_APP_USERS_ENDPOINT = "http://10.20.30.19:8000/en/wrench/api/v1"; // "https://apigate.lotus.g1.wre


   // public  $REACT_APP_AUX_ENDPOINT   = "http://10.204.5.100:9083/svs/user";
   // public  $REACT_APP_USERS_ENDPOINT = "http://10.204.5.100:9083/svs/user"; // "https://apigate.lotus.g1.wre


 //public  $REACT_APP_AUX_ENDPOINT   = "https://apigate.lotus.g1.wrenchboard.com/svs/user"; 
 //public  $REACT_APP_USERS_ENDPOINT = "https://apigate.lotus.g1.wrenchboard.com/svs/user";

    function __construct() {
        
      }

   /*   
   $endpoints = array(
    'apigate' => array('POST'),
    'generics' => array('POST'),
    'createuser' => array('POST'),
    'createmobileuser' => array('POST'),
    'completemobileuser' => array('POST'),
    'startresetpasword' => array('POST'),
    'userlogin' => array('POST'),
    'startjoblist' => array('POST'),
    'dashdata' => array('POST'),
    'getjobsdata' => array('POST'),
    'offerslist' => array('POST'),
    'activejoblist' => array('POST'),
    'loadprofile' => array('POST'),
    'account' => array('POST'),
    'message' => array('POST'),
    'pendingjob' => array('POST'),
    'paymenthx' => array('POST'),
    'getjob' => array('POST'),
    'mybanklist' => array('POST'),
    'sendmoney' => array('POST'),
    'sendinterest' => array('POST'),
    'sendmoneyfee' => array('POST'),
    'getpendingjobs' => array('POST'),
    'taskmessage' => array('POST'),
    'sendtaskmessage' => array('POST'),
    'getwallets' => array('POST'),
    'sitecontact' => array('POST'),
    'signupcountry' => array('POST'),
    'userscards' => array('POST'),
    'blogdata' =>  array('POST'),
    'blogitem' =>  array('POST'),
    'couponhx' =>  array('POST'),
    'couponpending' =>  array('POST'),
    'couponredeem' =>  array('POST'),
    'sendinterestmessage' =>  array('POST'),
    'replyinterestmessage' => array('POST')
);

*/

private function baseUrlPath($in)
{
$pathName = '';
    switch ($in['action']) {
        case WRENCHBOARD_ACCOUNT_LOGIN:
        $pathName = 'userlogin';
        break;
        case WRENCHBOARD_ACCOUNT_PENDING:
        $pathName = 'createuser';
        break;

        case WRENCHBOARD_CARDS_GETCARDLIST:
        $pathName = 'userscards';
        break;

        case WRENCHBOARD_CARDS_GETCARDLIST:
        $pathName = 'startjoblist';
        break;

        case WRENCHBOARD_MOBILE_ACTIVEJOB:
        $pathName = 'activejoblist';
        break;
        case WRENCHBOARD_ACCOUNT_WALLETS:
        $pathName ='getwallets';
        break;

        case WRENCHBOARD_START_JOBLIST:
        $pathName="startjoblist";
        break;

        case  WRENCHBOARD_MOBILE_PAYMENTHX:
        $pathName="paymenthx";
        break;

        case WRENCHBOARD_COUPON_MEMLIST:
            $pathName= 'couponhx';               
            break;

        case WRENCHBOARD_MOBILE_PURCHASEHX:
        $pathName= 'purchasehx';               
        break;

        case WRENCHBOARD_ACCOUNT_USER_RECIPEINT:
            $pathName= 'recipients';
            break;

        case WRENCHBOARD_SMONEY_PROCFEE:
            $pathName= 'sendmoneyfee';
            break;

        case WRENCHBOARD_MOBILE_UPDATEPROFILE:
            $pathName= 'updateprofile';
        break;
            
        case WRENCHBOARD_ACCOUNT_SENDREFER:
            $pathName= 'sendreferral';
        break;
        
        case WRENCHBOARD_ACCOUNT_REFFERHX:
            $pathName= 'refferhx';
            break;

        case WRENCHBOARD_ACCOUNT_COUNTRY_BANKS:
            $pathName= 'countrybanks';
            break;
    
        case WRENCHBOARD_ACCOUNT_PENDJOB:
            $pathName = 'getpendingjobs';
        break;
        case WRENCHBOARD_MOBILE_OFFERSLIST:
  		  $pathName =  'offerslist';
        break;

        case WRENCHBOARD_RESET_PASSWORD:
            $pathName =  'startresetpasword';
            break;

        case WRENCHBOARD_ACCOUNT_ACCOUNT_TYPES:
            $pathName =  'accounttypes';
            break;
            //loadprofile
        case WRENCHBOARD_MOBILE_LOADPROFILE:
            $pathName =  'loadprofile';
            break;

        case WRENCHBOARD_VERIFY_PENDING_LINK:
            $pathName =  'verifysignuplink';
            break;

        case WRENCHBOARD_JOB_LISTJOBS:
            $pathName =  'jobmanagerlist';
            break;
 
        case WRENCHBOARD_ACCOUNT_ADD_RECIPEINT:
             $pathName =  'addrecipient';
             break;
                
            

        case 1200021:
        $pathName="signupcountry";
        break;

    }
return $pathName;
}
      public function wrenchboard_api($in, &$out = array(),$pathWay='') {
        $ret = 0;
        
            $urlPath =  ($pathWay!='')? $pathWay :$this->baseUrlPath($in);
            $local_url = $this->REACT_APP_USERS_ENDPOINT; 
            $url = $local_url ."/".$urlPath; //"/generics";
            $data = $in;

            $content = json_encode($data);

            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type" => "application/json"));
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

            $json_response = curl_exec($curl);
            $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

            if ($status != 200) {
                echo ("Error: call to URL $url failed with status $status, response $json_response,  | curl_error " . curl_error($curl) . ", | curl_errno " . curl_errno($curl));
            }

            curl_close($curl);
            $response = json_decode($json_response, true);
           $this->showResult($url, $in, $response);
        $out = $response;
        return $response["internal_return"];
    }

    private function showResult($url, $in,$out){

        echo "<table>
        <tr><td colspan='3'><b>TARGET ENDPOINT[POST]</b>".$url."</td></tr>
            <tr>
                <td>".var_dump($in)."</td>
                <td style='background-color:yellow'>".var_dump($out)."</td>
                <td></td>
            </tr>
        </table>";
        //$response['result_list']
        if ( isset($out['result_list']) && is_array($out['result_list']) && count($out['result_list']) > 0 ){
            $arr =  $out['result_list'];
            $myHeaders =  array_keys($arr[0]);

var_dump( $myHeaders);
        $tableHead = '<tr>';
        foreach ($myHeaders as $hd){
            $tableHead .= "<th>".$hd."</th>";
        }
        $tableHead .= '</tr>';

        $tableBody = '';
        foreach ($arr as $item) {
            $tableBody .= '<tr>';
            $backColor ='white';
            foreach ($myHeaders as $hd){
                $backColor = ($backColor=='aliceblue')?'white': 'aliceblue';
                $tableBody .= "<td style='background-color:$backColor'>".$item[$hd]."</td>";
            }
            $tableBody .= '</tr>';
        }

        echo "<table>".$tableHead.$tableBody."</table>";
        }
    }

}

?>