<?php 
// session_start();
foreach($_GET as $campo => $valor){${$campo}=trim($valor);} 
foreach($_POST as $campo => $valor){${$campo}=trim($valor);} 

include_once('lib_main.php');
if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}

$usrt0=data_maes('usrt',$usrt);

echo '<br>*/*/*/*/*'.$usrt0['usrt_email'];
echo '<br>*/*/*/*/*'.$usrt0['usrt_pin'];
echo '<br>*/*/*/*/*'.$usrt0['usrt_clave'];

echo '<br>*/*/*/*/*'.$empr;
echo '<br>*/*/*/*/*'.$subdom;
echo '<br>*/*/*/*/*'.$usrt;
echo '<br>*/*/*/*/*'.$pin;

$sql_usrt='select * from ';


echo '<br>';
die('test');


// SELECT SQL
{
$sql_item ='select *,';
$sql_item.='count(item_tlla) as item_ctalla ';
$sql_item.='from item0 ';
$sql_item.='left join lsub0 on item_lsub=lsub_codigo ';
$sql_item.='left join mrca0 on item_mrca=mrca_codigo ';
$sql_item.='left join clor0 on item_clor=clor_codigo ';
$sql_item.='left join tlla0 on item_tlla=tlla_codigo ';
$sql_item.='left join kldd0 on item_kldd=kldd_codigo ';
$sql_item.='where item_status not in ("I","X") ';
$sql_item.='and item_descri!="" ';
$sql_item.='and item_codigo not like "99%" ';
$sql_item.='and item_'.$precio.'>0 ';
if(!empty($buscar))
{
     $buscar=alltrim($buscar);
     $buscararray=explode(' ',$buscar);
     for($idx=0; $idx<count($buscararray); $idx++)
     {
          $buscaidx=$buscararray[$idx];
          if(strtolower(substr($buscaidx,-2,2))=='es'){$buscaidx=substr($buscaidx,0,-2);}
          if(strtolower(substr($buscaidx,-1,1))=='s'){$buscaidx=substr($buscaidx,0,-1);}
          $sql_item.=' and (item_descri like "%'.$buscaidx.'%" or item_codigo like "%'.$buscaidx.'%" or item_codigov like "%'.$buscaidx.'%") ';
     }
}
if($lsub!=''){$sql_item.='and (item_lsub like "'.$lsub.'%") ';}
if($mrca!=''){$sql_item.='and (item_mrca = "'.$mrca.'") ';}
if($item!=''){$sql_item.='and (item_codigo like "'.$item.'%") ';}
if($mdlo!=''){$sql_item.='and (item_mdlo like "'.$mdlo.'%") ';}
if($clor!='')
{
     $clor_tipo=r_fiel('clor',$clor,'tipo');
     $sql_item.=' and (clor_tipo like "'.$clor_tipo.'%")';
}
if($solo_ctlg==1)
{$sql_item.=' and item_ctlg=1 ';}

if(mb_stristr('gnya.gmny',$cfg_empr))
{$sql_item.=' and item_codigo!=item_rfer ';}

if(mb_stristr('gnya.gmny',$cfg_empr) and !empty($categoria))
{
     $categoriax=$categoria;
     if(strtolower(substr($categoriax,-2,2))=='es'){$categoriax=substr($categoriax,0,-2);}
     if(strtolower(substr($categoriax,-1,1))=='s'){$categoriax=substr($categoriax,0,-1);}
     $sql_item.=' and item_descri like "%'.$categoriax.'%" ';
}

if($solo_foto==1)     
{$sql_item.='and item_codigo in(select imgi_item from imgi0 where imgi_status!="X") ';}
     
$xgroupby='group by item_codigo ';
// $xorderby='order by item_descri,item_codigo ';                                 
$xorderby='order by item_codigo, item_descri ';                                 
if(empty($buscar) and empty($lsub) and empty($mrca) and empty($clor))
{$xlimit=300;}

     
if(mb_stristr('gmny.gnya',$cfg_empr)){$xgroupby='group by item_mdlo,item_raiz ';}
if(mb_stristr('gmny.gnya',$cfg_empr)){$xorderby='order by item_idate, item_codigo ';}
     
// if(mb_stristr($orden,'Cod')){$xorderby='order by item_codigo ';}
if(mb_stristr($orden,'A-Z')){$xorderby='order by item_descri,item_codigo ';}
if(mb_stristr($orden,'Z-A')){$xorderby='order by item_descri desc,item_codigo ';}
if(mb_stristr($orden,'<')){$xorderby='order by item_puni,item_descri ';}
if(mb_stristr($orden,'>')){$xorderby='order by item_puni desc,item_descri ';}

     
     
     
$sql_item.=$xgroupby;                                 
$sql_item.=$xorderby;

// if(empty($lsub) and empty($mrca) and empty($clor)){$sql_item.='desc ';}

if(!empty($xlimit)){$sql_item.='limit '.$xlimit;}
     
$res_item=msql_query($sql_item,$link_data);
// echo $sql_item;

}


$ptitulo='FILTROS'; include('ele_title.php');


if(mb_stristr('gmny.gnya',$cfg_empr))
{
     $pdefa=$categoria; $plabel='Categorías'; $pid='categoria'; $ptodo=1;
     $ponchange='{buscar_item();}';
     $poptions='Blusas;Vestidos;Faldas;Enterizos;Shorts;Leggins;Camisas;Camisetas;Pantalones;Pijamas;Niñ,Niñ@s;';
     include('ele_select.php');
//      echo '<span style="float:left;width:2em;">.</span>';
}


$pid='lsub'; $pdefa=$lsub; $ptodo=1;
$ptabla='lsub';
$plabel='Linea';
// $ponchange='{buscar_item();}';
if(mb_stristr('gmny.gnya',$cfg_empr)){$phide=1;}
include('ele_select.php');
// echo '<span style="float:left;width:2em;">.</span>';


// $span=$dom->appendChild($dom->createElement("span","."));
//      $span->setAttribute("style","float:left;width:2em;");

$pid='mrca'; $pdefa=$mrca; $ptodo=1;
$ptabla='mrca';
$plabel='Marca';
$pwhere='(mrca_codigo in(select distinct item_mrca from inve1 left join inve2 on idnum1=idnum2 left join item0 on item=item_codigo))';
include('ele_select.php');


$pid='orden'; $pdefa=$orden;
$plabel='Ordenar X';
$poptions='Código;Nombre (A-Z);Nombre (Z-A);Precio <;Precio >';
$ponchange='{buscar_item();}';
// $palign='right';
include('ele_select.php');

// echo '<div></div>';

$msgres='';
$msgres.=msql_num_rows($res_item).' Resultado(s)...';

$simi_buscar=trim($buscar);
if(mb_stristr($simi_buscar,' '))
{$simi_buscar=substr($simi_buscar,0,strpos($simi_buscar,' '));}



if(msql_num_rows($res_item)<=1){$buscar='';}
else
{if(!empty($buscar)){$msgres.='para " '.$buscar.' "';}}

// if(is_numeric($buscar)){$buscar='';}

$pid='buscar'; 
$plabel='Texto';
$pvalue=$buscar; 
$pplace='buscar';
// $palign='right';
$ponenter='{buscar_item();}';
include('ele_input.php');

$pid='btn_busca'; 
$plabel='Buscar';
$pplace='buscar';
$palign='right';
$ponclick='{buscar_item();}';
include('ele_button.php');



// catalogo
{    
echo '<div style="float:left;white-space: nowrap;">';     
echo $msgres;
echo '</div>';     
     
include('ele_break.php');
// echo '<div style ="display:block;height:3em;"></div>';     

$ptitulo='CATALOGO';include('ele_title.php');
// include('ele_break.php');
     
     
msql_data_seek($res_item,0);     
while($aso_item=msql_fetch_assoc($res_item))
{
//      echo '<br>==>'.$aso_item['item_codigo'];
//      if(msql_num_rows($res_item)==1){$palign='center';}
     $pid=$aso_item['item_codigo'];
     $ptabla='item';
     $pprecio='pvp1';
//      $pcodigo=$aso_item['item_codigo'];
     $pcodigo=$aso_item['item_rfer'];
     if(empty($pcodigo)){$pcodigo=$aso_item['item_mdlo'];}
     if(empty($pcodigo)){$pcodigo=$aso_item['item_codigo'];}
     $pstockv=3;
     if(mb_stristr('gnya.gmny',$cfg_empr) and !esMovil()){$pobj=4;}
     if(mb_stristr('gnya.gmny',$cfg_empr)){$pstockv=0;}
     $pcarr=1;
//      $pobj=2;
//      $pimgdesc=1;
//      if(!empty($pstockv))
//      {
//           $stock=sal_item($aso_item['item_codigo']);
//           $pstock=$stock['uni_fin'];
//      }
     
     include('ele_image.php');
     $simi_codi=$pcodigo;
     $same_codi=trim($aso_item['item_codigo']);
     $same_desc=trim($aso_item['item_descri']);
     
}
     
if(msql_num_rows($res_item)==1)
{
     include('ele_break.php');

     
     
//      $simi_desc=trim(substr($simi_desc,0,10));
     $same_desc=trim($same_desc);
     $same_desc=str_replace('  ',' ',$same_desc);
     $simi_desc=$same_desc;
     
          
     if(mb_stristr('gmny.gnya',$cfg_empr)){$same_codi=substr($same_codi,0,10);}
     
     if(mb_stristr('gmny.gnya',$cfg_empr)){$simi_desc=substr($same_codi,0,8);}  

     $sql_item=str_replace('where ','where item_codigo not like "'.$same_codi.'%" and ',$sql_item);
     
     $sql_simi=$sql_item;
     $sql_simi=str_replace('%'.$simi_codi.'%','%'.$simi_desc.'%',$sql_simi);
     $res_simi=msql_query($sql_simi,$link_data);

     if(msql_num_rows($res_simi)==0)
     {
          if(mb_stristr($simi_desc,' '))
          {
               $words=explode(' ',$same_desc);
               for($idx=count($words)-1; $idx>=0; $idx-- )
               {
                    $simi_desc='';
                    for($idx2=0; $idx2<=$idx; $idx2++)
                    {$simi_desc.=$words[$idx2].' ';}
                    $simi_desc=trim($simi_desc);
                    
                    $sql_simi=$sql_item;
                    $sql_simi=str_replace('%'.$simi_buscar.'%','%'.$simi_desc.'%',$sql_simi);
                    $res_simi=msql_query($sql_simi,$link_data);
//                     if(msql_num_rows($res_simi)!=0){break;}
                    if(msql_num_rows($res_simi)>1){break;}
               }
          }
     }     
     
     
     
     if(msql_num_rows($res_simi)==0)
     {
          $sql_simi=$sql_item;
          $sql_simi=str_replace('%'.$simi_buscar.'%','%'.$simi_desc.'%',$sql_simi);
          $res_simi=msql_query($sql_simi,$link_data);
     }

     if(msql_num_rows($res_simi)==0)
     {
          $sql_simi=$sql_item;
          $sql_simi=str_replace('%'.$simi_buscar.'%','%'.$same_desc.'%',$sql_simi);
          $res_simi=msql_query($sql_simi,$link_data);
     }
     
     if(msql_num_rows($res_simi)==0)
     {
          if(mb_stristr($same_desc,' ')){$same_desc=substr($same_desc,0,strpos($same_desc,' '));}
          $sql_simi=$sql_item;
          $sql_simi=str_replace('%'.$simi_buscar.'%','%'.$same_desc.'%',$sql_simi);
          $res_simi=msql_query($sql_simi,$link_data);
     }

     
     if(msql_num_rows($res_simi)!=0)
     {
          $ptitulo='SIMILARES';include('ele_title.php');
//           echo msql_num_rows($res_simi).' Resultado(s)...';
     }
     
     
     while($aso_item=msql_fetch_assoc($res_simi))
     {
          $pid=$aso_item['item_codigo'];
          $pcodigo=$aso_item['item_rfer'];
          if(empty($pcodigo)){$pcodigo=$aso_item['item_mdlo'];}
          if(empty($pcodigo)){$pcodigo=$aso_item['item_codigo'];}
//           $pstockv=3;
//           if(mb_stristr('gnya.gmny',$cfg_empr) and !esMovil()){$pobj=4;}
//           if(mb_stristr('gnya.gmny',$cfg_empr)){$pstockv=0;}
//           $pcarr=1;
          include('ele_image.php');
     }

}
     
}

// echo '</div>';

?>


<script>
function buscar_item()
{
     get_para();
     var xhref='';
     xhref=xhref+'?scri=shop_item2';
     xbusca=document.getElementById("buscar").value; xhref=xhref+'&buscar='+xbusca;
     if(document.getElementById("categoria")){xcateg=document.getElementById("categoria").value;xhref=xhref+'&categoria='+xcateg;}
     if(document.getElementById("lsub")){xlsub=document.getElementById("lsub").value;xhref=xhref+'&lsub='+xlsub;}
     if(document.getElementById("mrca")){xmrca=document.getElementById("mrca").value;xhref=xhref+'&mrca='+xmrca;}
     if(document.getElementById("orden")){xorden=document.getElementById("orden").value;xhref=xhref+'&orden='+xorden;}
     location.href=xhref;          
}
</script>

<script>
function save_carr(item,cant)
{
     var data=new FormData();
     data.append('script','save_carr');
     data.append('item',item);
     data.append('cant',cant);
     exe_ajax(data);
}
</script>



<script>document.title="<?php echo ucfirst($cfg_descri).' - Catálogo';?>"</script>
