<?php
if(empty($znombre1)){return;}

if($zsala!=0){$zsala.=' '.num2let($zsala,'N');}
if(empty($zsala)){$zsala='PRINCIPAL';}


$trty0=data_maes('trty',$ztrty);
// $docu0=data_docu('RTRT',$idnum);
$domd0 = new DOMDocument();
     $domd0->encoding = 'utf-8';
     $domd0->xmlVersion = '1.0';
     $domd0->formatOutput = true;
$elem0=$domd0;



if(getPlatform()=='Android'){$android=1;}
if(getPlatform()=='Linux'){$linux=1;}

if(!empty($zdom)){$domd0=$zdom;}
if(!empty($zele)){$elem0=$zele;}


// LINK WSAPP ASIGNACION
$linka='';
$linka.=$_SERVER['HTTP_HOST'];
$linka.='/?scri=jwib_asig4';
$linka.='&cong='.$cong_codigo;
$linka.='&idnum='.$zidnum;
$linka.='&prnt=1';

{$linka.='&info='.$info;}


// link JWOL
if($info=='MMAE')
{
     $zdiscurso=str_replace('<a','<b',$zdiscurso);
     $zdiscurso=str_replace('</a','</b',$zdiscurso);
     $zdiscurso=str_replace('<p','<x',$zdiscurso);
     $zdiscurso=str_replace('</p','</x',$zdiscurso);
     
     $xdiscurso='';
     $xdiscurso=$domd0->createDocumentFragment();
          $xdiscurso->appendXML($zdiscurso);               
     
     $domj = new DOMDocument();
     $domj->loadHTML($zdiscurso, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
          $bdiscurso=$domj->getElementsByTagName('b')->item(0);          
     $zintervencion=$bdiscurso->textContent;          
     $atributos = [];
     foreach ($bdiscurso->attributes as $attr)
     {$atributos[$attr->nodeName] = $attr->nodeValue;}
     $linkb=$atributos['href'];
     $rspac=strrpos($linkb,' ');
     if($rspac>0)
     $linkb=substr($linkb,0,$rspac);
     $linkb=str_replace(' ','%20',$linkb);

     
     if(empty($zdiscurso)){return;}
}


if($info=='MMAE')
{
     $linkr='';
//      $href.='https://';
     $linkr.=$_SERVER['HTTP_HOST'];
     $linkr.="/_doc/".$zdocu.'_'.str_pad($zidnum,9,'0',STR_PAD_LEFT).".ics";
//           $linkr=$href;     
}


if($info=='RTRT')
{
     $linkr='';
//      $href.='https://';
     $linkr.=$_SERVER['HTTP_HOST'];
     $linkr.='/?scri=regi_jwrv3';
     $linkr.='&cong='.$cong_codigo;
     $linkr.='&trty='.$ztrty;
     $linkr.='&rtrt='.$zidnum;
     $linkr.='&prnt=1';
//           $linkr=$href;     
     
     $rtrt0=data_docu('rtrt',$idnum);
}




     ///// LINKS
     {
     $linka=str_replace('http://','',$linka);
     $linka=str_replace('https://','',$linka);

     $linkb=str_replace('http://','',$linkb);
     $linkb=str_replace('https://','',$linkb);

     $linkr=str_replace('http://','',$linkr);
     $linkr=str_replace('https://','',$linkr);

     $linkr=str_replace('http://','',$linkr);
     $linkr=str_replace('https://','',$linkr);

//      if(empty($idnum))
     if($info=='MMAE')
     {
          $eol="\r\n";
          $texto='';
          $texto.='*== MEJORES MAESTROS ==*'.$eol;
          $texto.='😀 Nombre       : '.$znombre1.$eol;
          if(!empty($znombre2))
          {$texto.='🙂 Ayudante     : '.$znombre2.$eol;}
          $texto.='📆 Fecha        : '.$zfecha .$eol;
          $texto.='📃 Intervención : '.$zintervencion.$eol;
          $texto.='🪑 Sala             : '.$zsala.$eol;
          $texto.=$eol;
          $texto.='📃 Asignación : '.$linka.$eol;
          $texto.=$eol;
          if(!empty($linkb))
          {
          $texto.='📚 JW.ORG : '.$linkb.$eol;
          $texto.=$eol;
          }
          $texto.='⏰ Recordar : '.$linkr.$eol;
          $texto=urlencode($texto);
          
     }
          

     if($info=='RTRT')
     {
          $eol="\r\n";
          $texto='';
          $texto.='*== ASIGNA.TERRITORIO ==*'.$eol;
          $texto.=$eol;
          $texto.='🗺 Territorio       : '.$trty0['trty_descri'].$eol;
          if(!empty($trty0['trty_descri']))
          {$texto.='📍 Referencia     : '.$trty0['trty_referencia'].$eol;}
          $texto.='😀 Asignado       : '.$znombre1 .$eol;
          $texto.='📆 Fecha        : '.$zfecha .$eol;
          $texto.='📆 Vence        : '.$zvence.$eol;
          $texto.=$eol;
          $texto.='📄 Link : '.$linka.$eol;
          $texto=urlencode($texto);
     }
          
          
     }









     $div=$elem0->appendChild($domd0->createElement("div",''));
          $xstyle0='';
          if($info=='MMAE')
          {
          $xstyle0.='width:9cm;';
          $xstyle0.='height:13cm; ';
          }
          if($info=='RTRT')
          {
          $xstyle0.='width:99%;';
//           $xstyle0.='width:9cm;';
//           $xstyle0.='height:26cm; ';
          }
          $xstyle0.='float:left;';
          $xstyle0.='margin-right:0.1cm;';
          $xstyle0.='margin-bottom:0.1cm;';
          $xstyle0.='border:double 4px;';
          $xstyle0.='font-family:arial;';
          $xstyle0.='page-break-inside: avoid;';
          $xstyle0.='line-height: normal;';
          $xstyle0.='vertical-align:top !important;';
          $div->setAttribute('style',$xstyle0);

//// TABLE

     $table=$div->appendChild($domd0->createElement("table",''));
//           $table->setAttribute('border','');
          $table->setAttribute('width','99%');
//           $table->setAttribute('style','border-collapse:collapse;cellspacing:0;cellpadding:0;');


     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td",""));
          if($info=='MMAE')
          {
          $div=$tcol->appendChild($domd0->createElement("div","ASIGNACION PARA LA REUNION"));
          $div=$tcol->appendChild($domd0->createElement("div","VIDA Y MINISTERIO CRISTIANOS"));
          }
          if($info=='RTRT')
          {
          $div=$tcol->appendChild($domd0->createElement("div","ASIGNACION DE TERRITORIO"));
          $div=$tcol->appendChild($domd0->createElement("div",'GRUPO '.$trty0['trty_grupo']));
          }
     
          $tcol->setAttribute('bold','');
          $tcol->setAttribute('align-c','');
          $tcol->setAttribute('colspan','10');
          $tcol->setAttribute('style','padding:15px;');

     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td",""));
          $tcol->setAttribute('style','width:2.3cm;');
     
     
     
     if($info=='MMAE')
     {
     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td","Nombre :"));
          $tcol->setAttribute('style','height:2.0em; padding:0 0 0 10px;');
          $tcol->setAttribute('bold','');
     $tcol=$trow->appendChild($domd0->createElement("td",$znombre1));
          $tcol->setAttribute('bold','');

     $trow=$table->appendChild($domd0->createElement("tr",''));
          if(empty($znombre2)){$trow->setAttribute('hide','');}
     $tcol=$trow->appendChild($domd0->createElement("td","Ayudante :"));
          $tcol->setAttribute('style','height:2.0em; padding:0 0 0 10px;');
          $tcol->setAttribute('bold','');
//           if(empty($znombre2)){$tcol->setAttribute('hide','');}
     $tcol=$trow->appendChild($domd0->createElement("td",$znombre2));

     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td","Fecha :"));
          $tcol->setAttribute('style','height:2.0em; padding:0 0 0 10px;');
          $tcol->setAttribute('bold','');
     $tcol=$trow->appendChild($domd0->createElement("td",$zfecha));

     $trow=$table->appendChild($domd0->createElement("tr",''));
          $trow->setAttribute('size80','');
     $tcol=$trow->appendChild($domd0->createElement("td","Intervención :"));
          $tcol->setAttribute('bold','');
          $tcol->setAttribute('style','vertical-align:top; height:2.0em; padding:0 0 0 5px;');
     $tcol=$trow->appendChild($domd0->createElement("td",''));
          $tcol->appendChild($xdiscurso);
          $tcol->setAttribute('style','text-align:;');
          

//                $href=$linkb;
               $href='https://'.$linkb;
               $ref=$tcol->appendChild($domd0->createElement("a","JW.ORG"));
                    $ref->setAttribute('noprint','');
                    $ref->setAttribute('size120','');
                    $ref->setAttribute('float-r','');
                    $ref->setAttribute('href',$href);
                    $ref->setAttribute('target','_blank');

     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td","Sala :"));
          $tcol->setAttribute('style','height:2.0em; padding:0 0 0 10px;');
          $tcol->setAttribute('bold','');
     $tcol=$trow->appendChild($domd0->createElement("td",$zsala));
          $tcol->setAttribute('style','height:2.0em;');

     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td",""));
          $tcol->setAttribute('colspan','10');
          $tcol->setAttribute('size80','');
          $tcol->setAttribute('style','text-align:justify; line-height: normal; padding:15px;');

          $span=$tcol->appendChild($domd0->createElement("span","Nota al estudiante:"));
               $span->setAttribute('bold','');
          $span=$tcol->appendChild($domd0->createElement("span","En la Guía de actividades encontrará la información que necesita para su intervención."));
          $span=$tcol->appendChild($domd0->createElement("div","Prepare con la ayuda del libro Benefíciese el aspecto de la oratoria que se le indica en esta hoja."));
//           $span=$tcol->appendChild($domd0->createElement("div",""));
          $span=$tcol->appendChild($domd0->createElement("div","No olvide llevar su libro a la reunión Vida y Ministerio."));
               $span->setAttribute('bold','');

          
     $href='';
     $href.='https://';
     $href.=$_SERVER['HTTP_HOST'];
     $href.='/?scri=jwib_asig2';
     $href.='&info=MMAE';
     $href.='&cong='.$cong_codigo;

     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td",""));
          $tcol->setAttribute('colspan','10');
          $tcol->setAttribute('size80','');
          $tcol->setAttribute('style','text-align:right; line-height: normal; padding:15px;');
     $ahref=$trow->appendChild($domd0->createElement("a","Anuncios"));
          $ahref->setAttribute('noprint',' ');
          $ahref->setAttribute('href',$href);
          $ahref->setAttribute('target','_blank');

     $href='';
     $href.='jwib.vcrxol.com/?'.$cong_codigo;
     $ahref=$trow->appendChild($domd0->createElement("span",$href));
          $ahref->setAttribute('noscreen',' ');
          
     }
          
     if($info=='RTRT')
     {
     $trty0=data_maes('trty',$ztrty);

     $bcstyle='border:6px solid; border-color:'.$trty0['trty_color'].';';
     $trow=$table->appendChild($domd0->createElement("tr",''));
          $trow->setAttribute('style',$bcstyle);
          
     $tcol=$trow->appendChild($domd0->createElement("td","Territorio :"));
          $tcol->setAttribute('style','height:2.0em; padding:0 0 0 10px;');
          $tcol->setAttribute('bold','');
     $tcol=$trow->appendChild($domd0->createElement("td",$trty0['trty_descri']));
          $tcol->setAttribute('bold','');

     $trow=$table->appendChild($domd0->createElement("tr",''));
//           if(empty($znombre2)){$tcol->setAttribute('hide','');}
     $tcol=$trow->appendChild($domd0->createElement("td","Referen. :"));
          $tcol->setAttribute('style','height:2.0em; padding:0 0 0 10px;');
          $tcol->setAttribute('bold','');
     $tcol=$trow->appendChild($domd0->createElement("td",$trty0['trty_referencia']));

     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td","Asignado :"));
          $tcol->setAttribute('style','height:2.0em; padding:0 0 0 10px;');
          $tcol->setAttribute('bold','');
     $tcol=$trow->appendChild($domd0->createElement("td",$znombre1));
          $tcol->setAttribute('bold','');

     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td","Fecha :"));
          $tcol->setAttribute('style','height:2.0em; padding:0 0 0 10px;');
          $tcol->setAttribute('bold','');
     $tcol=$trow->appendChild($domd0->createElement("td",$zfecha));

     $trow=$table->appendChild($domd0->createElement("tr",''));
//           $trow->setAttribute('size80','');
     $tcol=$trow->appendChild($domd0->createElement("td","Vence :"));
          $tcol->setAttribute('style','height:2.0em; padding:0 0 0 10px;');
          $tcol->setAttribute('bold','');
     $tcol=$trow->appendChild($domd0->createElement("td",$zvence));
//           $tcol->appendChild($xdiscurso);
          $tcol->setAttribute('style','text-align:;');
          

          
          
     // MAPA IMAGEN
     $sql_imgt='select * from imgt0 where ifnull(imgt_status,"")not in ("I","X") and imgt_trty="'.$trty0['trty_codigo'].'" order by imgt_defa desc, imgt_codigo ';
     $res_imgt=msql_query($sql_imgt,$link_data);
     $aso_imgt=msql_fetch_assoc($res_imgt);

     $imgt_file=$aso_imgt['imgt_file'];
     if(!empty($imgt_file))
     {
     $imgt_file='//'.$_SERVER['HTTP_HOST'].'/imgt/'.$aso_imgt['imgt_file'];

     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td",""));
//           $tcol->setAttribute('style','border:5px solid '.$trty0['trty_color']);
          $tcol->setAttribute('colspan','10');
          $tcol->setAttribute('align-c','');
     $img=$tcol->appendChild($domd0->createElement("img",""));
          $img->setAttribute('src',$imgt_file);
//           $img->setAttribute('align-c','');
          $img->setAttribute('width','99%');
          $img->setAttribute('ondblclick','{_clickimg("'.$ztrty.'","trty");}');
          $img->setAttribute('style','border:6px solid '.$trty0['trty_color']);

     }

          
     $trow=$table->appendChild($domd0->createElement("tr",''));
     $tcol=$trow->appendChild($domd0->createElement("td",""));
          $tcol->setAttribute('colspan','10');
          $tcol->setAttribute('size80','');
          $tcol->setAttribute('style','text-align:right; line-height: normal; padding:15px;');

     $href='';
     $href.='jwib.vcrxol.com/?'.$cong_codigo;
     $ahref=$trow->appendChild($domd0->createElement("span",$href));
          $ahref->setAttribute('noscreen',' ');

      
     }

     // LINKS wsapp
     {
//      $thead=$table->appendChild($domd0->createElement("thead",''));
     $trow=$table->appendChild($domd0->createElement("tr",''));
          $trow->setAttribute('noprint',' ');

     $tcol=$trow->appendChild($domd0->createElement("td",""));
          $tcol->setAttribute('colspan','10');
          $tcol->setAttribute('size120','');
          $tcol->setAttribute('style','text-align:center; line-height: normal; padding:15px;');

          $href='';
          $href.='https://api.whatsapp.com/send/';
          $href.='?text='.$texto;

          $ref=$tcol->appendChild($domd0->createElement("a","📱 WhatsApp"));
               $ref->setAttribute('float-l','');
               $ref->setAttribute('href',$href);
               $ref->setAttribute('target','_blank');

          $href='';
          $href.='https://';
          $href.=$linkr;

//           $linkr_descri='Recordar';
          if($info=='MMAE'){$linkr_descri='⏰ Recordar';}
          if($info=='RTRT'){$linkr_descri='🚪 Nueva';}

          $ref=$tcol->appendChild($domd0->createElement("a",$linkr_descri));
               $ref->setAttribute('float-r','');
               $ref->setAttribute('href',$href);
               $ref->setAttribute('target','_blank');

     }


     
     if($info=='RTRT')
     {
          $fecha0=date('Y-m-01',strtotime('-3 month'));
          
          $zdocu='';
          $zid='nomb_info';
          $ztabla='nomb';
          $zcolu='nomb_nombre';
          $zwhere='(';
          $zwhere.='nomb_cong="'.$cong0['cong_codigo'].'"  ';
          $zwhere.='and nomb_lused>="'.$fecha0.'" ';
          $zwhere.='and nomb_nombre not like "%.%" ';
          $zwhere.=')';
          include('ele_datalist.php');

          $div=$elem0->appendChild($domd0->createElement("div",''));
          $table=$div->appendChild($domd0->createElement("table",''));
               $table->setAttribute('width','100%');
          {
               $trow=$table->appendChild($domd0->createElement("tr",''));
               
               $tcol=$trow->appendChild($domd0->createElement("td"," "));
               if(empty($publicador)){$publicador=$_COOKIE['publicador'];}
               $zdom=$domd0;
               $zele=$tcol;
               $zid='publicador';
               $zvalue=$_COOKIE[$zid];
               $zlabel='Publicador';     
               $zlist='nomb_info';
               $zopen=1; 
               $zwidth='auto';
               include('ele_input.php');

               $tcol=$trow->appendChild($domd0->createElement("td"," "));
               $zdom=$domd0;
               $zele=$tcol;
               $zalign='right';
               $zid='campana';
               $zvalue=$_COOKIE[$zid];
               $zlabel='Campaña';
               $zoptions='';
               if(in_array(date('n'),[3,4]))
               {$zoptions.='🍷Conmemoración;';}
               if(in_array(date('n'),[6,7,8]))
               {$zoptions.='🛐 Asamblea;';}
               $zoptions.='🔭 Atalaya;';
               $zoptions.='‼️ Despertad;';
               $zoptions.='📚 Folleto;';
               $zexact=1;
               include('ele_select.php');


               $zobj=1;
               $zid='docu';
               $zvalue='RVST';
               $zlabel='documento';     
               $zhide=1;
               include('ele_input.php');

               $zobj=1;
               $zid='cong';
               $zvalue=$cong_codigo;
               $zlabel='congregacion';     
               $zhide=1;
               include('ele_input.php');

               $uniclock=uni_clock(date('H:i'));     
               $zid='hora';
               $zlabel=$uniclock.'Horario';
               $zoptions='';
               $zoptions.='1,🌞Mañana;';     
               $zoptions.='2,🌇Tarde;';     
               $zoptions.='3,🌙Noche';     
               $zvalue=$hora;
               if(empty($zvalue))
               {
                    $zvalue='1';
                    if(date('H')>12){$zvalue='2';}     
                    if(date('H')>18){$zvalue='3';}     
               }     
               $zexact=1;
               $zhide=1;
               include('ele_select.php');


          }     
          
          $div=$elem0->appendChild($domd0->createElement("div",''));
          $table=$div->appendChild($domd0->createElement("table",''));
               $table->setAttribute('width','100%');
          {
               $zfecha0dh=date('Y-m-01',strtotime('-30 days'));     
               $zfecha0nt=date('Y-m-01',strtotime('-365 days'));     
               // NO TOCAR
               {
                    $sql_ntok ='select *, max(fecha) as u_fecha  ';     
                    $sql_ntok.='from jwrv1 ';     
                    $sql_ntok=calc_fields('jwrv',$sql_ntok);
                    $sql_ntok.='where ifnull(status,"")not in("O","X") ';     
                    $sql_ntok.='and cong="'.$cong_codigo.'" ';     
                    $sql_ntok.='and trty="'.$ztrty.'" ';     
                    $sql_ntok.='and fecha>="'.$zfecha0.'" ';     
                    $sql_ntok.='and visita = "NT" ';     
                    $sql_ntok.='group by bloq,ndptoA ';     
                    $sql_ntok.='order by bloq,ndptoA ';     
                    $res_ntok=msql_query($sql_ntok,$link_data);

                    if(msql_num_rows($res_ntok)!=0)
                    {
                         $trow=$table->appendChild($domd0->createElement("tr",''));
                              $trow->setAttribute('style','border:4px solid red;');
                         $tcol=$trow->appendChild($domd0->createElement("td"," "));
                         $tcol=$trow->appendChild($domd0->createElement("td"," "));
                              $tcol->setAttribute('style','height:2.0em; padding:0 0 0 10px;');
                              $tcol->setAttribute('bold','');
                              $tcol->setAttribute('valign-t','');
                              $tcol->setAttribute('colspan','10');
                                        $div=$tcol->appendChild($domd0->createElement("div","NO TOCAR :"));
//                          $tcol=$trow->appendChild($domd0->createElement("td",''));
//                               $tcol->setAttribute('valign-t','');
//                               $tcol->setAttribute('style','text-align:;');
                              $lbloq='';
                              while($aso_ntok=msql_fetch_assoc($res_ntok))
                              {
                                   if($aso_ntok['bloq']!=$lbloq)
                                   {
                                        $div=$tcol->appendChild($domd0->createElement("div",""));
//                                         $span=$div->appendChild($domd0->createElement("span",$aso_ntok['bloq'].' =>'));
                                        $span=$div->appendChild($domd0->createElement("span",$aso_ntok['bloq'].' ➡️ '));
                                        
                                             $span->setAttribute('bold','');
                                        $lbloq=$aso_ntok['bloq'];
                                   }
                                        $span=$div->appendChild($domd0->createElement("span",' /'.$aso_ntok['ndpto'].''));
                              }

                    }
                    
               }


               $zfecha2=date('Y-m-d',strtotime($rtrt0['fecha'].' +'.($rtrt0['plazo']-1).' days'));     
               $zfecha1=date('Y-m-01',strtotime($zfecha2.' -60 days'));     

               $tjwrv1='T'.date('His').'_'.rand();
               
               $sql_ndpto='';
               $sql_ndpto.='create temporary table '.$tjwrv1.' as ';
               $sql_ndpto.='select * from jwrv1 ';
               $sql_ndpto=calc_fields('jwrv',$sql_ndpto);
               $sql_ndpto.='where ifnull(status,"")not in ("O","X") ';
               $sql_ndpto.='and cong="'.$cong_codigo.'" ';
               $sql_ndpto.='and trty="'.$ztrty.'" ';     
//                $sql_ndpto.='and visita!="DH" ';     
               $sql_ndpto.='group by bloq,ndptoA ';
               $sql_ndpto.='order by bloq,ndptoA ';
               $res_ndpto=msql_query($sql_ndpto,$link_data);
               
               $sql_bloq='select distinct cong,trty,bloq from '.$tjwrv1.' order by bloq ';
               $res_bloq=msql_query($sql_bloq,$link_data);
               while($aso_bloq=msql_fetch_assoc($res_bloq))
               {
                    $newndptos=gen_ndpto($aso_bloq['cong'],$aso_bloq['trty'],$aso_bloq['bloq']);  
                    $xcontaid=0;
                    foreach($newndptos as $newndptox)
                    {
                         $xcontaid++;
                         $zfecha3=$zfecha2;
                         if($zfecha3>date('Y-m-d')){$zfecha3=date('Y-m-d');}
                         
                         $sql_inse='insert into '.$tjwrv1.'(idnum1,fecha,cong,trty,bloq,ndpto,ndptoA,visita) ';
//                          $sql_inse.='value ("'.$xcontaid.'",now(),"'.$aso_bloq['cong'].'","'.$aso_bloq['trty'].'","'.$aso_bloq['bloq'].'","'.$newndptox.'","'.$newndptox.'","PV") ';
                         $sql_inse.='value ("'.$xcontaid.'","'.$zfecha3.'","'.$aso_bloq['cong'].'","'.$aso_bloq['trty'].'","'.$aso_bloq['bloq'].'","'.$newndptox.'","'.$newndptox.'","PV") ';
                         $res_inse=msql_query($sql_inse,$link_data);
                    }
               }
                    
               
               $sql_ndpto='select * from '.$tjwrv1.' ';
               $sql_ndpto.='group by bloq,ndpto ';
               $sql_ndpto.='order by bloq,(ndpto*1) ';
               $res_ndpto=msql_query($sql_ndpto,$link_data);
               
               $lbloq='';
               $lndpto='';
               $nndpto=0;
//                $newndpto=[];
//                $lnewndpto=[];
               $xconta=0;
               
//                $aso_ndpto=msql_fetch_assoc($res_ndpto);
//                $newndpto=gen_ndpto($aso_ndpto['cong'],$aso_ndpto['trty'],$aso_ndpto['bloq']);
               
               if(msql_num_rows($res_ndpto)!=0)
               {
//                     $newndpto=gen_ndpto($aso_ndpto['cong'],$aso_ndpto['trty'],$aso_ndpto['bloq']);
                    msql_data_seek($res_ndpto);
                    while($aso_ndpto=msql_fetch_assoc($res_ndpto))
                    {
                         if($lbloq!=$aso_ndpto['bloq'] )
                         {
                              $trow=$table->appendChild($domd0->createElement("tr",''));
                                   $trow->setAttribute('id','rowid_'.$aso_ndpto['bloq']);
                              $tcol=$trow->appendChild($domd0->createElement("td",''));
                              $tcol=$trow->appendChild($domd0->createElement("td",''));
                                   $tcol->setAttribute('colspan','10');
                                   $tcol->setAttribute('bold','');
                                   $tcol->setAttribute('size140','');
                                   $trow->setAttribute('style','height:2.5em; border:white 6px; background-color:black; color:white; ');

                              $span=$tcol->appendChild($domd0->createElement("span",'🏘️ Mz. Bloq. : '.$aso_ndpto['bloq']));
//                               $spandptos=$tcol->appendChild($domd0->createElement("span",''));
//                                    $spandptos->appendChild($domd0->createTextNode("Tu valor aquí"));
//                                    $spandptos->setAttribute('float-r','');
                              $spandptos=$tcol->appendChild($domd0->createElement("span",''));
                                   $spandptos->setAttribute('float-r','');
                                   $spandptos->setAttribute('onclick','{cambia_display("bloq_'.$aso_ndpto['bloq'].'")}{this.scrollIntoView({behavior: "auto", block: "start"})}');
                              $xconta=0;
                         }

                              
                         // 2 ultimas visitas
                         {

                              $sql_uvst ='select * from jwrv1 ';
                              $sql_uvst=calc_fields('jwrv',$sql_uvst);
                              $sql_uvst.='where ifnull(status,"")not in ("O","X") ';
                              $sql_uvst.='and fecha<="'.date('Y-m-d',strtotime($rtrt0['fecha'].' +'.($rtrt0['plazo']-1).' days')).'" ';
                              $sql_uvst.='and cong="'.$cong_codigo.'" ';
                              $sql_uvst.='and trty="'.$ztrty.'" ';     
                              $sql_uvst.='and bloq="'.$aso_ndpto['bloq'].'" ';     
                              $sql_uvst.='and ndpto="'.$aso_ndpto['ndpto'].'"';
                              $sql_uvst.='group by idnum1 ';
                              $sql_uvst.='order by fecha desc, idnum1 desc ';
                              $sql_uvst.='limit 2 ';
                              $res_uvst=msql_query($sql_uvst,$link_data);

                              
                              if(msql_num_rows($res_uvst)!=0)
                              {
                                   $xconta++;
                                   $spandptos->nodeValue = '('.$xconta.')👁️';
                              }
                              
                              if(msql_num_rows($res_uvst)==0)
                              {
                                   $sql_uvst=str_replace('jwrv1',$tjwrv1,$sql_uvst);
                                   $res_uvst=msql_query($sql_uvst,$link_data);
                              }
                              
                              $lndpto='';
                              while($aso_uvst=msql_fetch_assoc($res_uvst))
                              {
                                   $rowid='rowid_'.$aso_uvst['bloq'].'_'.$aso_uvst['ndpto'];
                                   $trow=$table->appendChild($domd0->createElement("tr",''));
                                        $trow->setAttribute('id',$rowid);
                                        $trow->setAttribute('class','bloq_'.$aso_uvst['bloq']);
                                        $trow->setAttribute('size140',' ');

                                        $rstyle='';
                                        $rstyle.='height:2.5em; ';
                                        if(!mb_stristr2($idrow,$aso_uvst['bloq']))
                                        {$rstyle.='display:none; ';}
                                        $rstyle.='border-top:dotted 1px; ';
                                        if($aso_uvst['ndpto']!=$lndpto){$rstyle.='border-top:solid 1px; ';}
                                        $trow->setAttribute('style',$rstyle);

                                   $tcol=$trow->appendChild($domd0->createElement("td",''));
                                        if($rowid==$idrow){$tcol->setAttribute('id','scrollhere');}
                                        $tcol->setAttribute('align-c','');

                                   if($aso_uvst['ndpto']!=$lndpto)
                                   {$span=$tcol->appendChild($domd0->createElement("span",$aso_uvst['ndptoA'].' '));}

                                   $visita=$aso_uvst['visita_icon'];
                                   $tcol=$trow->appendChild($domd0->createElement("td",''));
                                   $span=$tcol->appendChild($domd0->createElement("span",' '.ucwords2($aso_uvst['dia_d']).' '));
                                        $span->setAttribute('size80','');
                                        $span->setAttribute('bold','');
                                   if(mb_stristr('SD',substr($aso_uvst['dia_d'],0,1)))
                                   {$span->setAttribute('style','color:red;');}

                                   if(!mb_stristr('SD',substr($aso_uvst['dia_d'],0,1)))
                                   {$span->setAttribute('style','color:blue;');}

                                   
//                                    echo '===='.ucfirst($aso_uvst['mes_d']);
                                   $span=$tcol->appendChild($domd0->createElement("span",' '.$aso_uvst['dia'].'/'.ucfirst(strtolower($aso_uvst['mes_d'])).' '));
                                        $span->setAttribute('size80','');
                                   $span=$tcol->appendChild($domd0->createElement("span",' '.$aso_uvst['hora_icon'].' '));
                                   
                                   $span=$tcol->appendChild($domd0->createElement("span",' '.$aso_uvst['visita_icon'].''));
                                        $span->setAttribute('float-r','');

                                   
                                   if(!empty($aso_uvst['campana']) and $aso_uvst['visita']=='OK')
                                   {
                                        $span=$tcol->appendChild($domd0->createElement("div",' '.$aso_uvst['campana'].' '));
                                        $span->setAttribute('size80','');
                                   }
                                   if(!empty($aso_uvst['observacion']))
                                   {
                                        $span=$tcol->appendChild($domd0->createElement("div",' '.$aso_uvst['observacion'].' '));
                                        $span->setAttribute('size80','');
                                   }
                                   
                                   $tcol=$trow->appendChild($domd0->createElement("td",''));

                                   
                                   if($aso_uvst['ndpto']!=$lndpto)
                                   {
                                        $linkr2='https://';
                                        $linkr2.=$linkr;
                                        $linkr2.='&bloq='.$aso_uvst['bloq'];
                                        $linkr2.='&ndpto='.$aso_uvst['ndpto'];
                                        $linkr2.='&lock=1';
//                                         $linkr2.='&prnt=1';

                                        $span=$tcol->appendChild($domd0->createElement("a",'✏️ '));
                                             $span->setAttribute('title','Editar');
                                             $span->setAttribute('float-r','');
                                             $span->setAttribute('style','width:2em; text-align:right;');
                                             $span->setAttribute('onclick','{window.open("'.$linkr2.'","_blank");}');

                                        $xnotocar=0;
                                        if($aso_uvst['visita']=='NT' and $aso_uvst['fecha']!=date('Y-m-d') and $aso_uvst['fecha']>=$zfecha0nt){$xnotocar=1;}
//                                              if($aso_uvst['visita']=='DH' and $aso_uvst['fecha']!=date('Y-m-d') and $aso_uvst['fecha']>=$zfecha0dh){$xnotocar=1;}
                                        if($aso_uvst['visita']=='DH'){$xnotocar=1;}

                                        if($xnotocar==0)
                                        {
                                             $span=$tcol->appendChild($domd0->createElement("span",' '));
                                                  $span->setAttribute('float-r','');
               //                                    $span->setAttribute('style','max-width:4em;');
                                             $zdom=$domd0;
                                             $zele=$span;

                                             if($android==1 or $linux==1){$zwidth='7em';}
                                             $zobj=1;
                                             $zid='visita'.$aso_uvst['idnum1'];
                                             $zplace='.';
                                             $zvalue='PV';

                                             if($aso_uvst['fecha']==date('Y-m-d'))
                                             {$zvalue=$aso_uvst['visita'];}
                                             $zoptions='';
                                             $zoptions.='PV,⏳ Pendiente;';     
                                             $zoptions.='NC,✖️ Nadie en Casa;';     
                                             $zoptions.='OC,🤐 Ocupado;';     
                                             $zoptions.='OK,🆗 Visita OK;';     
                                             $zoptions.='NT,😡 No Tocar;';     
                                             $zoptions.='DH,🕸️ Deshabitada;';     

                                             $zonchange='{save_jwrv("'.date('Y-m-d').'","'.$aso_uvst['trty'].'","'.$aso_uvst['bloq'].'","'.$aso_uvst['ndpto'].'",this.value,"'.$rowid.'");}';
//                                              if($aso_uvst['fecha']==date('Y-m-d')){$zhide=1;}
//                                              $zexact=1;
                                             if($zfecha2<date('Y-m-d')){$zhide=1;}
                                             include('ele_select.php');
                                        }
                                   }


                                   if($aso_uvst['visita']=='OK'){$trow->setAttribute('bgc-lblue','');}
                                   if($aso_uvst['visita']=='OC'){$trow->setAttribute('bgc-ulyellow','');}
                                   if($aso_uvst['visita']=='NT'){$trow->setAttribute('bgc-ulred','');}
                                   if($aso_uvst['visita']=='DH'){$trow->setAttribute('bgc-ulgreen','');}

                                   if($aso_uvst['ndpto']!=$lndpto){$lndpto=$aso_uvst['ndpto'];}
//                                    if($lnndpto!='')

                              }
                         }
                         $lbloq=$aso_ndpto['bloq'];
                    }
               }

               
               $zdom=$domd0;
               $zele=$domd0;
               $zobj=1;     
               include('ele_break.php');

          }     
          
     }


     if(empty($zdom))
     {
          if($zxml==1){$zxml=$domd0->saveXML();}
          else{echo $domd0->saveXML();}
     }     



include ('ele_reset.php');

?>



