<?php
session_start();
include_once('lib_main.php');
foreach($_GET as $campo => $valor){${$campo}=trim($valor);} 
foreach($_POST as $campo => $valor){${$campo}=trim($valor);} 
if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}

// $user_codigo=$_SESSION['user_codigo'];

$dom = new DOMDocument();
     $dom->encoding = 'utf-8';
     $dom->xmlVersion = '1.0';
     $dom->formatOutput = true;

$response=$dom->appendChild($dom->createElement("response"));
// $msgsys=$response->appendChild($dom->createElement("msgsys",$sql_query)); 
$msgswal=$response->appendChild($dom->createElement("msgswal")); 
     $msgswal->setAttribute("id","msgswal");

if(empty($user_codigo))
{
     
$usrt=trim($usrt);

// $link_data=msql_connect('','','',$empr);
$sql_query ='select * from usrt0 ';
$sql_query.='where usrt_codigo="'.$usrt.'" ';
// $sql_query.='and usrt_pin="'.$pin.'" ';
$sql_query.='order by usrt_codigo ';
$sql_query.='limit 1';
$res_query=msql_query($sql_query,$link_data);
     
$usrt_codigo=msql_result($res_query,0,'usrt_codigo');
$usrt_email=msql_result($res_query,0,'usrt_email');
$usrt_wsapp=msql_result($res_query,0,'usrt_wsapp');
$usrt_pin=msql_result($res_query,0,'usrt_pin');

// $user_codigo=msql_result($res_query,0,'user_codigo');
// $user_token=md5($user_codigo.date('His'));
// $user0=data_maes('user',$user_codigo);
// $_SESSION['user_codigo']='';
// $_SESSION['user_token']='';


if(empty($empr))
     {
//           $msgalert=$response->appendChild($dom->createElement("msgalert",'Empresa NO EXISTE')); 
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","Empresa NO EXISTE")); 
     }
elseif(empty($usrt_codigo))
     {
//           $msgalert=$response->appendChild($dom->createElement("msgalert",'Usuario : '.$user.' NO EXISTE')); 
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text",'Usuario NO VALIDO')); 
     }
elseif($usrt_pin!=$pin)
     {
//           $msgalert=$response->appendChild($dom->createElement("msgalert",'Clave INCORRECTA'));
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text",'PIN NO VALIDO')); 
     }
else
     {
//           $msgalert=$response->appendChild($dom->createElement("msgalert",'Acceso OK'));     
          $msgswal->appendChild($dom->createElement("icon","success")); 
          $msgswal->appendChild($dom->createElement("title","Confirmación OK")); 
          $msgswal->appendChild($dom->createElement("text",'Bienvenid@ '.$usrt0['user_descri'])); 
          $msgswal->appendChild($dom->createElement("isConfirmed",'window.location.href="?"')); 
          $result=$response->appendChild($dom->createElement("result",""));
               $result->appendChild($dom->createElement("user_token",$user_token));
     

          $sql_inse='insert into user0(user_descri,user_email,user_wsapp,user_idate,user_status)';
          $sql_inse.='value("'.$usrt0['usrt_descri'].'","'.$usrt0['usrt_email'].'","'.$usrt0['usrt_wsapp'].'",now(),"-")';
          $res_inse=msql_query($sql_inse,$link_data);
     
          $user_codigo=msql_insert_id($link_data);
          $user0=data_maes('user',$user_codigo);
          foreach($user0 as $key=>$value){$_SESSION[$key]=$value;}
          $empr0=data_maes('empr',$empr);
          foreach($empr0 as $key=>$value){$_SESSION[$key]=$value;}
          $_SESSION['user_token']=$user_token;
     }
}
else
{
          $msgswal->appendChild($dom->createElement("icon","warning")); 
          $msgswal->appendChild($dom->createElement("title","Salir")); 
          $msgswal->appendChild($dom->createElement("showCancel","Salir")); 
          $msgswal->appendChild($dom->createElement("confirmButtonText","Cerrar Sesión")); 
          $msgswal->appendChild($dom->createElement("isConfirmed",'window.location.href="?logout"')); 
}


echo $dom->saveXML();

?>