<?php
$judet   = $_POST['judet'];
$cautare = $_POST['caut'];
if(!empty($judet)){
$qsrc = "SELECT * FROM `anunturi` WHERE `judet`='".$judet."' 
                  AND (`verificat` IS NULL) 
                  AND (`aprobat`=1) 
                  AND (`titlu` LIKE '%".$cautare."%' 
                  OR `descriere` LIKE '%".$cautare."%' 
                  OR `etichete` LIKE '%".$cautare."%')";
                }else{
$qsrc = "SELECT * FROM `anunturi` WHERE `verificat` IS NULL 
                  AND (`aprobat`=1) 
                  AND (`titlu` LIKE '%".$cautare."%' 
                  OR `descriere` LIKE '%".$cautare."%' 
                  OR `etichete` LIKE '%".$cautare."%')";
                  }
$ressrc = $dbc->query($qsrc);
?>

<div class="w3-row-padding">
<div class="w3-quarter">
<?php include APP.'sidebar_gen.php'; ?>
</div>
<div class="w3-threequarter">
<?php include APP.'searchform.php'; ?>
<div class="w3-row w3-container w3-text-white w3-hide-small" style="background-color:#117594;">
<h5 style="padding:5px;">Rezultatul căutării pentru <strong><ins><?=$cautare?></ins></strong><?php if($judet == true){ ?> in județul <strong><ins><?=$judet?></ins></strong><?php } ?></h5>
</div>

<div class="w3-row w3-container w3-text-white w3-hide-medium w3-hide-large" style="background-color:#117594;">
<h5 style="padding:5px;">Caut <strong><ins><?=$cautare?></ins></strong><?php if($judet == true){ ?> in <strong><ins><?=$judet?></ins></strong><?php } ?></h5>
</div>

<div class="w3-row w3-margin-top">
<?php
function lungime_text($x, $length){
  if(strlen($x)<=$length)  {
    echo $x;
  }else{
    $y=substr($x,0,$length) . '...';
    echo $y;
  }
 }
while($rsrc = $ressrc->fetch_array()){
    
      $qsrcimg = "SELECT * FROM `imagini` WHERE `aid`=\"".$rsrc['aid']."\" LIMIT 1";
      $rsrcimg = $dbc->query($qsrcimg);
      $rsimg = $rsrcimg->fetch_assoc();
      
      $qgetuser = "SELECT * FROM `utilizatori` WHERE `uid`={$rsrc['uid']}";
      $rgetuser = $dbc->query($qgetuser);
      $rowgetusr = $rgetuser->fetch_assoc();

?>
<a href="<?=PATH.$rsrc['seo']?>.html">
<div class="w3-half w3-card w3-hover-light-gray w3-margin-bottom">
<div class="w3-col w3-hide-small" style="width:26%;">
<img src="<?=PATH?>imagini/<?=$rsimg['nume_fisier']?>" alt="<?=$rsrc['titlu']?>" title="<?=$rsrc['titlu']?>" loading="lazy" style="width:120px;max-width:100%;height:120px;"/>
</div>
<div class="w3-col w3-hide-medium w3-hide-large" style="width:35%;">
<img src="<?=PATH?>imagini/<?=$rsimg['nume_fisier']?>" alt="<?=$rsrc['titlu']?>" title="<?=$rsrc['titlu']?>" loading="lazy" style="width:70px;max-width:100%;height:70px;"/>
</div>
<div class="w3-col w3-hide-small" style="width:74%;">
<h5 class="w3-border-bottom">
<?php echo lungime_text($rsrc['titlu'], 40); ?></h5>
<div style="text-align: justify;padding-left:5px;padding-right:5px;">
<span><?php echo lungime_text($rsrc['descriere'], 90); ?></span></div>
<div class="w3-row w3-border-top w3-green">
<div class="w3-right" style="padding-right:5px;">
<i class="fa fa-money"></i> Preț: <?php if($rsrc['pret'] > 0){echo '<strong>'.$rsrc['pret'].'</strong> '.$rowgetusr['moneda'];}else{echo 'GRATUIT';} ?>
</div>
</div>

<div class="w3-row w3-border-top">
<div class="w3-left" style="padding-left:5px;">
<i class="fa fa-map-marker"></i> <?php echo $rsrc['judet']; ?>
</div>
<div class="w3-right" style="padding-right:5px;">
<i class="fa fa-clock-o"></i> <?php echo date('d F Y, H:i', strtotime($rsrc['data_publicarii'])); ?>
</div>
</div>

</div>

<div class="w3-col w3-hide-medium w3-hide-large" style="width:65%;">
<h6 class="w3-border-bottom">
<?php echo lungime_text($rsrc['titlu'], 22); ?>
</h6>
<span><?php echo lungime_text($rsrc['descriere'], 52); ?></span>
</div>

</div>
</a>
<?php }
if($ressrc->num_rows == 0){
    echo '<p class="w3-center">Căutarea dumneavoastră nu a întors niciun rezultat!</p>';
}
if(!empty($cautare)){
if($ressrc->num_rows >= 1){
    $qselsrc = "SELECT * FROM `cautari` WHERE `titlu`=\"$cautare\"";
    $rselsrc = $dbc->query($qselsrc);    
    
    if($rselsrc->num_rows >= 1){
    $qsavesrc = "UPDATE `cautari` SET `valoare`=`valoare` + 1, `ultima_data`=NOW() WHERE `titlu`=\"$cautare\"";
    $rsavesrc = $dbc->query($qsavesrc);
    }
    
    if($rselsrc->num_rows == 0){
    $qsavesrc = "INSERT INTO `cautari`(`srcid`, `titlu`, `valoare`, `prima_data`, `ultima_data`) VALUES ('0', '$cautare', '1', NOW(), NOW())";
    $rsavesrc = $dbc->query($qsavesrc);
    }
 }
 }
?></div>
</div></div>