<?php
if(isset($_SESSION['user_id']) AND (substr($_SERVER['PHP_SELF'], -10) != 'iesire.html')) {
    
    $qcont = "SELECT * FROM `utilizatori` WHERE `uid`='".$_SESSION['user_id']."' LIMIT 1";
    $rcont = $dbc->query($qcont);
    $rowct = $rcont->fetch_assoc(); 
    
    $qanunt = "SELECT * FROM `anunturi` WHERE `aid`='".$_POST['aid']."'";
    $ranunt = $dbc->query($qanunt);
    $rwa   = $ranunt->fetch_assoc();    
    
?>
<div class="w3-row-padding">
<div class="w3-quarter">
<?php include APP.'sidebar_usr.php'; ?>
</div>
<div class="w3-threequarter">
<div class="w3-row w3-indigo">
<h3 class="w3-container"><i class="fa fa-pencil-square-o"></i> Editare anunț</h3>
</div>

<?php
if(isset($err_edit_anunt)){echo '<div class="w3-panel w3-red w3-padding">'.$err_edit_anunt.'</div>';}
if(isset($incercdinnou)){echo '<div class="w3-panel w3-red w3-padding">'.$incercdinnou.'</div>';}
if(isset($notupload)){echo '<div class="w3-panel w3-red w3-padding">'.$notupload.'</div>';}
if(isset($editedanunt)){echo '<div class="w3-panel w3-green w3-padding">'.$editedanunt.'</div>';}


?>

<?php if(isset($stareEanunt) == 1){?>
<div class="w3-panel w3-padding w3-green">
<i class="fa fa-check"></i> Anunțul a fost actualizat</div>
<?php }elseif(isset($stareEanunt) == 2){ ?>
<div class="w3-panel w3-padding w3-red">
<i class="fa fa-remove"></i> Anunțul nu a fost actualizat</div>
<?php } ?>

<div class="w3-row w3-margin-top">

<div class="w3-rest w3-card w3-container w3-margin-bottom">   
   
   <form class="" action="" method="post" enctype="multipart/form-data">
   
   <div class="w3-row-padding w3-margin-bottom w3-margin-top">   
   <input class="w3-input w3-card w3-white" type="text" placeholder="Titlul anunțului" name="titlu" value="<?php if(isset($rwa['titlu'])){echo $rwa['titlu'];}elseif(isset($_POST['titlu'])){echo $_POST['titlu'];} ?>" required="" maxlength="60"/>
   <?php if(isset($err_titlu)){ ?>
   <div class="w3-panel w3-red w3-padding"><?=$err_titlu?></div>
   <?php } ?>
   </div>
   
   <div class="w3-row-padding w3-margin-bottom">   
   <textarea class="w3-input w3-card w3-white" placeholder="Conținutul anunțului" name="continut" required="" maxlength="5000" rows="5" style="white-space: pre-line;"><?php if(isset($rwa['descriere'])){echo $rwa['descriere'];}elseif(isset($_POST['continut'])){echo $_POST['continut'];} ?></textarea>
   <?php if(isset($err_continut)){ ?>
   <div class="w3-panel w3-red w3-padding"><?=$err_continut?></div>
   <?php } ?>
   </div>
   
   <div class="w3-row-padding w3-margin-bottom">   
   <input class="w3-input w3-card w3-white" type="text" placeholder="Etichete separate prin virgulă ( , )" name="eticheta" value="<?php if(isset($rwa['etichete'])){echo $rwa['etichete'];}elseif(isset($_POST['eticheta'])){echo $_POST['eticheta'];} ?>" required="" maxlength="60"/>
   <?php if(isset($err_eticheta)){ ?>
   <div class="w3-panel w3-red w3-padding"><?=$err_eticheta?></div>
   <?php } ?>
   </div>
   
   <div class="w3-row-padding w3-margin-bottom">     
      <div class="w3-rest">
      <?php $qfoto = "SELECT * FROM `imagini` WHERE `aid`='".$_POST['aid']."'";
    $rfoto = $dbc->query($qfoto);
    while($rwf   = $rfoto->fetch_assoc()){ ?>
    <img src="<?=PATH?>imagini/<?=$rwf['nume_fisier']?>" alt="" style="width:100px;height:100px;border:none;"/>
      <?php } ?>
      <p><input class="w3-input w3-card w3-white" type="file" name="poze[]"/></p>
      
      </div>      
   </div>   
   
   <div class="w3-row-padding w3-margin-bottom">
   <div class="w3-third w3-margin-bottom">
       <select class="w3-input w3-large w3-card w3-white srclocatie" style="padding-left:50px;" name="judetul" required="">       
        <?php
        $qjud = "SELECT * FROM judet";
        $rjud = $dbc->query($qjud);
        if(isset($rwa['judet'])){
            echo '<option value="'.$rwa['judet'].'" selected="">'.$rwa['judet'].'</option>';
        }else{
        echo '<option value="" selected="" disabled="">Selectați județul</option>';
        }
        while($rj = $rjud->fetch_assoc()){            
            echo '<option value="'.$rj['nume'].'">'.$rj['nume'].'</option>';
        }
        ?>        
       </select>
   </div>
   <div class="w3-third w3-margin-bottom">
       <select class="w3-input w3-large w3-card w3-white deliveryselect" style="padding-left:50px;" name="livrare" required="">
       <?php if(!isset($rwa['livrare'])){
        echo '<option value="" selected="" disabled="">Cum livrați?</option>
              <option value="posta">Prin poștă</option>
              <option value="curier">Prin curier</option>
              <option value="predare">Predare personală</option>
              <option value="online">Online (Produs digital)</option>';
        }else{
        if($rwa['livrare'] == 'posta'){
            $livrare = 'Prin poștă';
            echo '<option value="'.$rwa['livrare'].'" selected="" >'.$livrare.'</option>';
            echo '<option value="curier">Prin curier</option>
                  <option value="predare">Predare personală</option>
                  <option value="online">Online (Produs digital)</option>';
        }elseif($rwa['livrare'] == 'curier'){
            $livrare = 'Prin curier';
            echo '<option value="posta">Prin poștă</option>'; 
            echo '<option value="'.$rwa['livrare'].'" selected="" >'.$livrare.'</option>';
            echo '<option value="predare">Predare personală</option>
                  <option value="online">Online (Produs digital)</option>';
        }elseif($rwa['livrare'] == 'predare'){
            $livrare = 'Predare personală';
            echo '<option value="posta">Prin poștă</option>';           
            echo '<option value="curier">Prin curier</option>';
            echo '<option value="'.$rwa['livrare'].'" selected="" >'.$livrare.'</option>';
            echo '<option value="online">Online (Produs digital)</option>';
        }elseif($rwa['livrare'] == 'online'){
            $livrare = 'Online (Produs digital)';
            echo '<option value="posta">Prin poștă</option>';            
            echo '<option value="curier">Prin curier</option>
                  <option value="predare">Predare personală</option>';
                  echo '<option value="'.$rwa['livrare'].'" selected="" >'.$livrare.'</option>';
        }        
        
        } ?>        
       </select>
   </div>
   <div class="w3-third w3-margin-bottom">
       <input class="w3-input w3-large w3-card w3-white" name="pretul" type="number" value="<?php if(isset($rwa['pret'])){echo $rwa['pret'];}elseif(isset($_POST['pretul'])){echo $_POST['pretul'];} ?>" min="1" placeholder="Prețul.." required=""/> <div class="w3-right" style="position:relative;margin-top:-32px;margin-right:30px;" title="Pentru a modifica moneda, mergeti la editare profil"><?=$rowct['moneda']?><?php if(empty($_SESSION['user_id'])){echo 'RON';} ?></div>
   </div>
   </div>
   
   
   <div class="w3-center w3-margin-top w3-margin-bottom">
<input type="hidden" name="uip" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>"/>
<input type="hidden" name="aid" value="<?=$_POST['aid']?>"/>
<input type="hidden" name="editezanuntulmeu" value="true"/>
<button class="w3-button w3-blue w3-hover-black" type="submit"><i class="fa fa-save"></i>  Actualizați anunțul</button>

</form>

   </div>     
   
   </div>

</div>

</div></div>
<?php
 }else{
    $url = PATH.'index.html';
    ob_end_clean();
    header("Location: $url");
    exit();
 }
?>