<?php
declare(strict_types=0);
foreach($_GET  as $key => $value){${$key}=trim($value);$_PARAMS[$key]=${$key};}  
foreach($_POST as $key => $value){${$key}=trim($value);$_PARAMS[$key]=${$key};}

include_once('lib_main.php');
if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}
$user_codigo=$_SESSION['user_codigo'];
$user0=data_maes('user',$user_codigo);
$docu0=data_maes('docu',$docu);
$tran0=data_docu($docu,$idnum);

$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");

$tabla='fdoc';
$tabla0=$tabla.'0';

if(empty($maxsize)){$maxsize=2;}
$maxbytes=$maxsize*1024*1024;


if(empty($docu))
     {
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","Documento NO VALIDO")); 
     }
elseif(empty($idnum))
     {    
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","ID NO VALIDO")); 
     }
elseif(empty($_FILES['xfile']['name']))
     {    
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","Archivo NO VALIDO")); 
     }
elseif($_FILES['xfile']['size']>$maxbytes)
     {    
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","Archivo Mayor al limite permitido : ".$maxsize."MB")); 
     }
else
     {
     
          if(empty($iddet)){$iddet='';}
     
     
          $filetype=$_FILES['xfile']['type'];
          $filetype=substr($filetype,strpos($filetype,'/')+1);
     
          $xpath='../'.$empr.'/'.$tabla.'/';
          $xcad=date("is").rand(0,9);
//           $xname2=$codigo.'_'.$xcad.'.png';
          $xname2=$docu.'_'.$idnum.'_'.$iddet.'_'.$xcad.'.'.$filetype;
          $move_ok=move_uploaded_file($_FILES['xfile']['tmp_name'],$xpath.$xname2);
          
          $sql_imgi ='insert into ';
//           $sql_imgi.=''.$tabla0.'('.$tabla.'_descri,'.$tabla.'_status,'.$tabla.'_item,'.$tabla.'_file,'.$tabla.'_iuser,'.$tabla.'_idate)';
//           $sql_imgi.='value("'.$_FILES['xfile']['name'].'","-","'.$codigo.'","'.$xname2.'","'.$user_codigo.'",now())';
          $sql_imgi.=''.$tabla0.'('.$tabla.'_descri,'.$tabla.'_status,'.$tabla.'_docu,'.$tabla.'_idnum,'.$tabla.'_iddet,'.$tabla.'_file,'.$tabla.'_iuser,'.$tabla.'_idate)';
          $sql_imgi.='value("'.$_FILES['xfile']['name'].'","-","'.$docu.'","'.$idnum.'","'.$iddet.'","'.$xname2.'","'.$user_codigo.'",now())';
          $res_imgi=msql_query($sql_imgi,$link_data);

          $msgswal->appendChild($dom->createElement("icon","success")); 
          $msgswal->appendChild($dom->createElement("title","Archivo Cargado...".$msg_ok)); 
//           $msgswal->appendChild($dom->createElement("text",$docu.'-'.$idnum.'-'.$iddet.' = '.($_FILES['xfile']['name']))); 
          $msgswal->appendChild($dom->createElement("text",$docu.'-'.$idnum.'-'.$iddet.' = '.($_FILES['xfile']['name']))); 
     
          $msgswal->appendChild($dom->createElement("isConfirmed",'{location.reload();}'));
     
          if($_FILES['xfile']['type']!='image/jpg')
          {}
     
     }
echo $dom->saveXML();


   
?>