<?php
// $pstyle='height:1.2em;';

// FOTOS
// 1 solo foto defa
// 2 fotos(S) seleccionar default
// 3 foto(S) y descri 
// 4 foto y descri


// STOCK
// 0 hide (default)
// 1 stock 
// 2 dispo /no dispo
// 3 dispo /no dispo (+msj)
// 4 rango

// carrito
// 0 solo catalogo
// 1 add item+cant->carrito (carr1 CAR1)
// 2 add item+cant->pedido (pc10)
// 3 add item+cant->pedido (pf10)

// LISTAS
// 0 defa
// 1 add item-> favorito (carr1 CAR2)

// $ptablai='';
$pcodigo=trim($pcodigo);
$pprecio=strtolower(substr($pprecio,-4));
if(empty($pstockv)){$pstockv=0;}
if(empty($pstock)){$pstock=0;}
// if(empty($pid)){$pid=microtime()*1000000;}
if(empty($pid)){$pid=microtime()*1000000;}
if(empty($pobj)){$pobj=3;}
if(empty($psize)){$psize=1;}
if(esMovil()){$psize=$psize*1.3;}
// if(empty($plabel)){$plabel=$pcaption;}
if(!empty($psize) and $psize!=1){$pstyle.='font-size:'.($psize).'em;';}
if(!empty($pcolor)){$pstyle.='background:'.$pcolor.';';}
if(empty($ptabla)){$ptabla='item';}
$ptablai='img'.substr($ptabla,0,1);

if($ptcells>=2){$pstyle.='width:100%;';}


// if(strlen($pcodigo)<6)
// {}


// if(!empty($ptabla))
{
   $sql_image ='select * from '.$ptabla.'0 ';
   $sql_image.='left join '.$ptablai.'0 on '.$ptabla.'_codigo='.$ptablai.'_'.$ptabla.' ';
   $sql_image.='left join lsub0 on '.$ptabla.'_lsub=lsub_codigo ';
   $sql_image.='left join mrca0 on '.$ptabla.'_mrca=mrca_codigo ';
   $sql_image.='left join tlla0 on '.$ptabla.'_tlla=tlla_codigo ';
   $sql_image.='left join clor0 on '.$ptabla.'_clor=clor_codigo ';
   $sql_image.='left join kldd0 on '.$ptabla.'_kldd=kldd_codigo ';
   $sql_image.='where length(trim('.$ptabla.'_codigo))>=6 ';
   $sql_image.='and '.$ptabla.'_status not in ("O","X") ';
   $sql_image.='and '.$ptablai.'_status not in ("O","X") ';
   $sql_image.='and '.$ptabla.'_codigo like "'.$pcodigo.'%" ';
   $sql_image.='order by '.$ptablai.'_defa desc,'.$ptablai.'_codigo ';
   $sql_image.='limit 10 ';
   $res_image=msql_query($sql_image,$link_data);
//       echo '<br><br>==>'.$sql_image;
//       echo '<br><br>==>'.msql_num_rows($res_image);
}

if(msql_num_rows($res_image)==0)
{
     $sql_image=str_replace('and '.$ptablai.'_status not in ("O","X") ','',$sql_image);
     $res_image=msql_query($sql_image,$link_data);
}

if(msql_num_rows($res_image)>0)
{
     
msql_data_seek($res_image,0);
$asoc=msql_fetch_assoc($res_image);
$img_rfer=trim($asoc[$ptabla.'_rfer']);
// $img_codigo=trim($asoc[$ptabla.'_rfer']);
$img_codigo=trim($asoc[$ptabla.'_mdlo']);
if(strlen($img_codigo)<6){$img_codigo=$asoc[$ptabla.'_mdlo'];}
if(strlen($img_codigo)<6){$img_codigo=$asoc[$ptabla.'_rfer'];}
if(strlen($img_codigo)<6){$img_codigo=$asoc[$ptabla.'_codigo'];}
if(strlen($img_codigo)<6){$img_codigo=$pcodigo;}

     
// if(empty($img_codigo)){$img_codigo=$pcodigo;}
// $img_codigo=$asoc[$ptabla.'_codigo'];
$img_descri=trim($asoc[$ptabla.'_descri']);
$mrca_descri=trim($asoc['mrca_descri']);
$tlla_descri=trim($asoc['tlla_descri']);
$kldd_descri=trim($asoc['kldd_descri']);
$item_composi=trim($asoc['item_composi']);
$item_umdd=trim($asoc['item_umdd']);
$item_duni=$asoc['item_duni'];
$clor_descri=trim($asoc['clor_descri']);
$clor_hexa=trim($asoc['clor_hexa']);
$item_dpre=trim($asoc['item_dpre']);
     
if($item_dpre<2 or mb_stristr($pprecio,'pvp')){$item_dpre=2;}     

if(!empty($pprecio))
{
     $pprecio=strtolower($pprecio);
     $xprecio=$asoc['item_'.$pprecio];
}
     
if($ptabla=='item')
{
     $sql_tllas ='select * ';
     $sql_tllas.='from item0 left join tlla0 on item_tlla=tlla_codigo ';
     $sql_tllas.='where item_status not in("O","X") ';
     $sql_tllas.='and length(trim(item_codigo))>=6 ';
     $sql_tllas.='and item_codigo like "'.$img_rfer.'%" ';
     $sql_tllas.='and tlla_codigo*1>0 ';
     $sql_tllas.='order by tlla_codigo ';
     $sql_tllas.='';
     $sql_tllas.='';
     $res_tllas=msql_query($sql_tllas,$link_data);     
//      echo '<br><br>'.$sql_tllas;
     $ctllas=msql_num_rows($res_tllas);
     
     $tlla_descri2='';
     while($asoc=msql_fetch_assoc($res_tllas))
     {$tlla_descri2.=$asoc['tlla_descri'].' ';}
}     
     
     
$domi = new DOMDocument();
     $domi->encoding = 'utf-8';
     $domi->xmlVersion = '1.0';
     $domi->formatOutput = true;
$elem=$domi;
     
if(!empty($pdom)){$domi=$pdom;}
if(!empty($pele)){$elem=$pele;}
     
if($ptcells>=2)
{
     $vrimage=$elem->appendChild($domi->createElement("td"));
          if(!empty($pid))
          {$vrimage->setAttribute("id",$pid);}
          $vrimage->setAttribute("valign","top");
}
else
{
     $vrimage=$elem->appendChild($domi->createElement("span",""));
          if(!empty($pid))
          {$vrimage->setAttribute("id",$pid);}
          $vrimage->setAttribute("img-span",'');
//           $vrimage->setAttribute("style",'position:relative;float:left;padding:0.3em;background:;');
}

$cfiles=0;
msql_data_seek($res_image,0);
while($asoc=msql_fetch_assoc($res_image))
{if(!empty($asoc[$ptablai.'_file'])){$cfiles++;}}
// echo '==>'.$cfiles;

     $table=$vrimage->appendChild($domi->createElement("table",""));
               $table->setAttribute("name",$pcodigo);
          $table->setAttribute("cellpadding",'0');
          $table->setAttribute("img-table",'');
          $table->setAttribute("class",'img-table');

     $trow=$table->appendChild($domi->createElement("tr",""));
 
     $tcell=$trow->appendChild($domi->createElement("td",""));
          $tcell->setAttribute("colspan",'99');


     // header
     msql_data_seek($res_image,0);
     $asoc=msql_fetch_assoc($res_image);
     $hr=$tcell->appendChild($domi->createElement("hr",""));
          $hr->setAttribute("size","5");
          $hr->setAttribute("noshade","");

     if($pobj!=2)
     {
          if($pobj!=4)
          {
               $span=$tcell->appendChild($domi->createElement("a",'CODIGO : '.$img_codigo));
                         $span->setAttribute("name",'codigo');
               $div=$tcell->appendChild($domi->createElement("div",' '.$mrca_descri));
                    $div->setAttribute("size80",'');
                    $div->setAttribute("bold",'');
          }
          
          $trow=$table->appendChild($domi->createElement("tr",""));
          if($cfiles>0)
          {$trow->setAttribute("style",'height:17em; ');}
          else
          {$trow->setAttribute("style",'height:5em;');}
//                $trow->setAttribute("style",'height:2em;');
          if($cfiles>1)
          {
               $tcell=$trow->appendChild($domi->createElement("td",""));
                    $tcell->setAttribute("width",'10%');
          }
     }

     // detail
     if($pobj==2 and $cfiles>1)
     {
          $trow=$table->appendChild($domi->createElement("tr",""));
          msql_data_seek($res_image,0);
          while($asoc=msql_fetch_assoc($res_image))
          {
               $tcell=$trow->appendChild($domi->createElement("td",""));
               $image=$tcell->appendChild($domi->createElement("img",""));
                    $image->setAttribute("id",'img_'.$asoc[$ptablai.'_codigo']);
                    $image->setAttribute("src",'../'.$ptablai.'/'.$asoc[$ptablai.'_file']);
                    $image->setAttribute("style",'max-width:'.intval(12*$psize).'em;position:relative;float:left;' );
          }
     }

     if($pobj>2 and $cfiles>1)
     {
          msql_data_seek($res_image,0);
          while($asoc=msql_fetch_assoc($res_image))
          {
               $div=$tcell->appendChild($domi->createElement("div",""));
               $image=$div->appendChild($domi->createElement("img",""));
                    $image->setAttribute("style",'max-width:'.intval(4*$psize).'em' );
                    $image->setAttribute("src",'../'.$ptablai.'/'.$asoc[$ptablai.'_file']);
                    $image->setAttribute("onmouseover","{document.getElementById('img_".$pcodigo."').src=this.src;}");
          }
     }

     // main
     if($pobj!=2 )
     {
          msql_data_seek($res_image,0);
          $asoc=msql_fetch_assoc($res_image);
          $img_file=$asoc[$ptablai.'_file'];
          if(empty($img_file))
          {
               $tcell=$trow->appendChild($domi->createElement("td","Sin Foto"));
                    $tcell->setAttribute("style",'min-width:'.intval(15*$psize).'em;vertical-align:middle;' );          }
          else
          {
               $tcell=$trow->appendChild($domi->createElement("td",""));
                    if($cfiles>1)
                    {$tcell->setAttribute("style",'min-width:'.intval(15*$psize).'em;' );}
                    else
                    {$tcell->setAttribute("style",'min-width:'.intval(21*$psize).'em;' );}
               $image=$tcell->appendChild($domi->createElement("img",""));   
                    $image->setAttribute("id",'img_'.$pcodigo);
                    $image->setAttribute("src",'../'.$ptablai.'/'.$img_file);
                    if($cfiles>1)
                    {$image->setAttribute("style",'max-width:'.intval(17*$psize).'em; max-height:'.intval(30*$psize).'em; ' );}
                    else
                    {$image->setAttribute("style",'max-width:'.intval(21*$psize).'em; max-height:'.intval(30*$psize).'em; ' );}
          }
     }

     // foot
     msql_data_seek($res_image,0);
     $asoc=msql_fetch_assoc($res_image);
     if($pobj<=3)
     {$trow=$table->appendChild($domi->createElement("tr",""));}
     
     
     $tcell=$trow->appendChild($domi->createElement("td",""));
          $tcell->setAttribute("colspan",'99');
          $tcell->setAttribute("style",'min-width:'.intval(21*$psize).'em;');
     if($pobj!=2)
     {
          if($pobj==4)
          {
          $span=$tcell->appendChild($domi->createElement("a",'CODIGO : '.$img_codigo));
                    $span->setAttribute("name",'codigo');
          
          $div=$tcell->appendChild($domi->createElement("div",' '.$mrca_descri));
                    $div->setAttribute("style",'min-height:'.intval(6*$psize).'em;');
               $div->setAttribute("size80",'');
               $div->setAttribute("bold",'');
          }
          
          $div=$tcell->appendChild($domi->createElement("div",''.$img_descri));
//                $div->setAttribute("id",$pid.'d');
          if($pobj==4)
          {
               $div->setAttribute("style",'min-height:'.intval(6*$psize).'em;');
          }
          else
          {
               if(strlen($img_descri)>40){$div->setAttribute("size80",'');}
               if(strlen($img_descri)>60){$div->setAttribute("size60",'');}
          }

          if(!empty($kldd_descri))
          {
               $div=$tcell->appendChild($domi->createElement("div",''.$kldd_descri.' /'.$item_composi));
                    $div->setAttribute("size80",'');
                    $div->setAttribute("italic",'');
          }
          
          
//           if(!empty($clor_descri))
          if(strlen($clor_descri)>=3)
          {
               $div=$tcell->appendChild($domi->createElement("div",'COLOR : '.$clor_descri));
                    $div->setAttribute("size80",'');
                    if(!empty($clor_hexa))
                    {
                         $div->setAttribute("bold",'');
//                          $div->setAttribute("style",'border:0px solid;background:'.$clor_hexa);
                         $div->setAttribute("style",'border:4px solid;border-color:'.$clor_hexa);
                    }                         
          }
          
          if(!empty($tlla_descri2))
          {
               $div=$tcell->appendChild($domi->createElement("div",'   '.$tlla_descri2));
                    $div->setAttribute("size80",'');
          }

          if(!empty($xprecio))
          {
               $div=$tcell->appendChild($domi->createElement("div",'PVP : $ '.number_format($xprecio,$item_dpre)));
                    $div->setAttribute("size80",'');
                    $div->setAttribute("bold",'');
          }
          if(!empty($pstockv))
          {
               $alertmsg='';
               $sstyle='';
               if($pstock>0)
               {
                    if($pstockv==1){$valstock='DISPONIBLE : '.number_format($pstock,$item_duni).' '.$item_umdd;}
                    if($pstockv>=2){$valstock='DISPONIBLE';}
                    if($pstockv==3)
                    {
                         $alertmsg='';
                         $alertmsg.='swal.fire({';
                         $alertmsg.='icon:"info",';
                         $alertmsg.='title:"'.$img_descri.'",';
                         $alertmsg.='text:"DISPONIBLE : '.number_format($pstock,$item_duni).' '.$item_umdd.'",';
                         $alertmsg.='confirmButtonColor:"'.$empr0['empr_color1'].'",';
                         $alertmsg.='timer:10000';
                         $alertmsg.='});';
                    }
               }
               else{$valstock='NO DISPONIBLE'; $sstyle='color:darkred;';}

               $div=$tcell->appendChild($domi->createElement("div",$valstock));
                    $div->setAttribute("size80",'');
                    $div->setAttribute("bold",'');
                    if(!empty($sstyle))
                    {$div->setAttribute("style",$sstyle);}
                    if(!empty($alertmsg))
                    {
                         $div->setAttribute("underline",'');
                         $div->setAttribute("unselect",'');
                         $div->setAttribute("onclick",$alertmsg);
                    }
          }
     }

     $trow=$table->appendChild($domi->createElement("tr",""));
     $tcell=$trow->appendChild($domi->createElement("td",""));
          $tcell->setAttribute("colspan",'99');
     $hr=$trow->appendChild($domi->createElement("hr",""));
          $hr->setAttribute("size","4");
          $hr->setAttribute("noshade","");

if(empty($pdom))
{
     if($pxml==1){$pxml=$domi->saveXML();}
     else{echo $domi->saveXML();}
}     
     
}

$pid='';
$pdom='';
$pele='';
$ptabla='';
$pcodigo='';
$pobj=0;
$pstock=0;
$plabel='';
$pvalue='';
$pclass='';
$ponclick='';
// $pxml=0;

$pstockv=0;
$pstock=0;
$pprecio=0;

$ptype='';
$pplace='';
$pstyle='';
$pwidth='';
$psize='';
$palign='';
$pcolor='';
?>