<?php
session_start();

class Go4itController extends Controller{
    public $layout='//layouts/go4it';

    public function actionIndex(){			
		  $this->render('index');
    }

    public function actionCotization(){
        $returnArr = array();
        $returnArr['check'] = 0;
        //------------ peticion de token ------------------
        //The url you wish to send the POST request to
        $urlToken = "https://go4it.mx/SSO/connect/token";
        $token = "";


        //The data you want to send via POST
        $fields = [
            'username'      => 'osanchez@centrogeo.edu.mx',
            'password'      => 'C3ntr0G30&_2019',
            'grant_type'    => 'password',
            'client_id'     => 'clienteGo4ITWebApp',
            'client_secret' => 'SDJ45478%&7SD@3440_96P?ASD'
        ];

        //url-ify the data for the POST
        $fields_string = http_build_query($fields);

        //open connection
        $curlToken = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($curlToken,CURLOPT_URL, $urlToken);
        curl_setopt($curlToken,CURLOPT_POST, true);
        curl_setopt($curlToken,CURLOPT_POSTFIELDS, $fields_string);

        //So that curl_exec returns the contents of the cURL; rather than echoing it
        curl_setopt($curlToken,CURLOPT_RETURNTRANSFER, true); 

        $response = curl_exec($curlToken);
        $err = curl_error($curlToken);

        curl_close($curlToken);

        if ($err) {
            //return "cURL Error : Error en obtencion de token";
            $returnArr['check'] = 2;
        } else {
            $json_response = json_decode($response, true);
            $token = $json_response['access_token'];
        } 
        //~ //--------------------- peticion de cotizacion ------------------------

        $urlCotizacion = "https://go4it.mx/brainbowAVM/api/AVM3";
        
        $ANIO_TERMINACION_OBRA = 2019 - $_POST['ANTIGUEDAD'];
        
		/*
        $data = array("CALLE" => "0", 
                    "NO_EXTERIOR" => "0", 
                    "NO_INTERIOR" => "0", 
                    "COLONIA" => "0", 
                    "CP" => "0", 
                    "MUNICIPIO" => "0", 
                    "ESTADO" => "0", 
                    "CAT_TIPO_INMUEBLE" => $_POST['CAT_TIPO_INMUEBLE'], 
                    "CVE_CLASE_INMUEBLE" => $_POST['CVE_CLASE_INMUEBLE'], 
                    "LATITUD" => $_POST['LATITUD'], 
                    "LONGITUD" => $_POST['LONGITUD'], 
                    "ANIO_TERMINACION_OBRA" => $ANIO_TERMINACION_OBRA, 
                    "SUPERFICIE_VENDIBLE" => $_POST['SUPERFICIE_VENDIBLE'], 
                    "SUPERFICIE_TERRENO" => $_POST['SUPERFICIE_TERRENO'], 
                    "NUM_ESTACIONAMIENTOS" => $_POST['NUM_ESTACIONAMIENTOS'], 
                    "NUM_NIVELES" => $_POST['NUM_NIVELES'], 
                    "NUM_RECAMARAS" => $_POST['NUM_RECAMARAS'], 
                    "NUM_BANOS" => $_POST['NUM_BANOS'], 
                    "NUM_MEDIO_BANOS" => $_POST['NUM_MEDIO_BANOS']
                    );   
        $data_string = json_encode($data);  
        */
           
        $data_string = "{\t\r\n\t\"CALLE\": \"0\",\r\n\t\"NO_EXTERIOR\": \"0\",\r\n\t\"NO_INTERIOR\": \"0\",\r\n\t\"COLONIA\": \"0\",\r\n\t\"CP\": \"0\",\r\n\t\"MUNICIPIO\": \"0\",\r\n\t\"ESTADO\": \"0\",\r\n\t\"CAT_TIPO_INMUEBLE\": {$_POST['CAT_TIPO_INMUEBLE']},\r\n\t\"CVE_CLASE_INMUEBLE\": {$_POST['CVE_CLASE_INMUEBLE']},\r\n\t\"LATITUD\": {$_POST['LATITUD']},\r\n\t\"LONGITUD\": {$_POST['LONGITUD']},\r\n\t\"ANIO_TERMINACION_OBRA\": {$ANIO_TERMINACION_OBRA},\r\n\t\"SUPERFICIE_VENDIBLE\": {$_POST['SUPERFICIE_VENDIBLE']},\r\n\t\"SUPERFICIE_TERRENO\": {$_POST['SUPERFICIE_TERRENO']},\r\n\t\"NUM_ESTACIONAMIENTOS\": {$_POST['NUM_ESTACIONAMIENTOS']},\r\n\t\"NUM_NIVELES\": {$_POST['NUM_NIVELES']},\r\n\t\"NUM_RECAMARAS\": {$_POST['NUM_RECAMARAS']},\r\n\t\"NUM_BANOS\": {$_POST['NUM_BANOS']},\r\n\t\"NUM_MEDIO_BANOS\": {$_POST['NUM_MEDIO_BANOS']}\r\n}\r\n";          

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $urlCotizacion);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_ENCODING, ""); 
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);  
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);   
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
                                                                           
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                    'Content-Type: application/json',    
                    'Authorization: Bearer ' . $token    
                    )                                                                        
        ); 

        $result = curl_exec($ch);
        $err = curl_error($ch);

        curl_close($ch);

        if ($err) {
            //return "cURL Error : Error en cotizacion";
            $returnArr['check'] = 3;
        } else {
            $returnArr = json_decode($result, true);
            $returnArr['check'] = 1;
            // echo "TOKEN: ";
            // echo $token;
        } 
        

		//~ $returnArr['check'] = 1;
		//~ $returnArr['folio'] = "3726312e-8ef4-45d8-9190-7580e996574b";
		//~ $returnArr['valuacionComercial'] = 8340000;
		//~ $returnArr['valorDestructible'] = 8340000;
		//~ $returnArr['valorRentaSugerida'] = 34750;
		//~ $returnArr['indiceConfianza'] = 49;
		//~ $returnArr['latitud'] = 19.4415;
		//~ $returnArr['longitud'] = -99.1825;
		//~ $returnArr['costoOperacion'] = 15;
		//~ $returnArr['saldoAnterior'] = 9670;
		//~ $returnArr['saldoActual'] = 9655;
		
		
		

		echo json_encode($returnArr);
		Yii::app()->end();
	}
}