<?php
declare(strict_types=0);
// 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;}

$ipip=$_SERVER['REMOTE_ADDR'];

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

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

// $msgsys=$response->appendChild($dom->createElement("consolelog",'*/*/*/*/*/')); 
if(empty($user_codigo))
{
     
$user=trim($user);

// $link_data=msql_connect('','','',$empr);
$sql_query ='select * from user0 ';
$sql_query.='where user_status not in("O","X") ';
$sql_query.='and user_email="'.$user.'" ';
$sql_query.='order by user_codigo desc ';
$sql_query.='limit 1';
$res_query=msql_query($sql_query,$link_data);

$msgsys=$response->appendChild($dom->createElement("consolelog",$sql_query)); 
     
     
$user_codigo=msql_result($res_query,0,'user_codigo');
$user_email=msql_result($res_query,0,'user_email');
$user_cedula=msql_result($res_query,0,'user_cedula');
$user_wsapp=msql_result($res_query,0,'user_wsapp');
$user_pin=msql_result($res_query,0,'user_pin');

$user0=data_maes('user',$user_codigo);
     
     
// $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($user_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($user_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","Reseteo OK")); 
          $msgswal->appendChild($dom->createElement("text",'Bienvenid@ '.$user0['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_tipo,user_descri,user_email,user_wsapp,user_clave,user_idate,user_status)';
//           $sql_inse.='value("invi","'.$user0['user_descri'].'","'.$user0['user_email'].'","'.$user0['user_wsapp'].'","'.$user0['user_clave'].'",now(),"-")';
//           $res_inse=msql_query($sql_inse,$link_data);

          $sql_updt='update user0 set user_clave=user_clave2, user_pin=null where user_codigo='.$user0['user_codigo'].' limit 1';
          $res_updt=msql_query($sql_updt,$link_data);

          $msgalert=$response->appendChild($dom->createElement("consolelog",$sql_inse));     
          $msgalert=$response->appendChild($dom->createElement("consolelog",msql_error($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();

?>