<?php
/**
 * Created by PhpStorm.
 * User: Joseg
 * Date: 22/5/2018
 * Time: 2:52 AM
 */
include "../uniqueNameGenerator/generateRamdonToken.php";
include '../comun/validarSesion.php';
include "../phpqrcode/qrlib.php";
if (isset($_REQUEST['token'])) {
    $Valido = new validator();
    $Tokeng = new generateRamdonToken();
    $tokenEntrada = $Tokeng->id();
    if ($Valido->checkToken($_REQUEST['token'])){
        if (isset($_REQUEST['horaEstimadaVisita']) && isset($_REQUEST['nombre'])) {
                $creadoPor = $Valido->retornarIdToken($_REQUEST['token']);
                $visita = R::dispense('visita');
                $visita->nombre = $_REQUEST['nombre'];
                $visita->fechaRegistro = date('Y-m-d H:i:s');
                $visita->horaEstimadaVisita = $_REQUEST['horaEstimadaVisita'];
                $visita->registradoPor = $creadoPor;
                $visita->codigoEntrada = $tokenEntrada;
                $visita->escaneadoPor = "";
                $visita->fechaEscaneado = "";
                $visita->estado = 1;
                QRcode::png($tokenEntrada, "../uploads/".$tokenEntrada.".png");
                $dire = "http://www.telepila.com.ve/Api_MiResidenciAPP/uploads/".$tokenEntrada.".png";
                $id = R::store($visita);
                include '../comun/mensajeOk.php';
                $data = array("message" => "Visita Guardada","codigo" => $tokenEntrada,"url" => $dire);
                echo json_encode($data);
        }else{
            include '../comun/mensajeFaltanCampos.php';
        }
    }else{
        include '../comun/mensajeDatosInvalidos.php';
    }
}else{
    include '../comun/mensajeSesionInvalida.php';
}