<?php if(isset($_SESSION['user_id']) AND (substr($_SERVER['PHP_SELF'], -10) != 'iesire.html')) {
$qnousr = "SELECT * FROM `utilizatori` WHERE `uid`={$_SESSION['user_id']} LIMIT 1";
$rnousr = $dbc->query($qnousr);
$rownousr = $rnousr->fetch_assoc();
 }
?>
<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">
   <div class="w3-rest w3-card w3-container w3-margin-bottom w3-mobile">
   <h3 class="w3-container w3-center w3-border-bottom w3-padding">Adăugare anunț</h3>


<?php // Limitare Anunturi
/*if(isset($_SESSION['user_id'])){
    
$qvna = 'SELECT COUNT(*) AS una FROM anunturi WHERE uid="'.$_SESSION['user_id'].'"';
$rvna = $dbc->query($qvna);
$rwvna = $rvna->fetch_assoc();
$ruc = $rwvna['una'];
$rut = $rownousr['limita'];
$rms = $rut - $ruc;
?>
<?php if($ruc == 0){?>
<div class="w3-panel w3-green w3-center"><h5>Aveți <?=$rms?> anunțuri disponibile.</h5></div>
<?php }elseif($ruc <= $rownousr['limita']){ ?>
<div class="w3-panel w3-green w3-center"><h5>Aveți un număr de <?=$ruc?> anunțuri, mai puteți adăuga <?=$rms?> anunț.</h5></div>
<?php }elseif($ruc == $rownousr['limita']){ ?>
<div class="w3-panel w3-red w3-center"><h5>Ați atins numărul maxim de anunțuri.</h5></div>
<?php } }*/ ?>

   
   <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($_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 w3-margin-top">   
   
<div class="w3-half">
<select class="w3-input w3-col w3-xlarge w3-border w3-white srccata" name="cat_id" style="padding-left:8%;" id="categorie" required="">
<?php if(empty($ra['categorie'])){?>
   <option value="" selected="" disabled="" class="w3-hide">Selectați o categorie</option>
<?php } ?>
   <?php
$qcat = "SELECT * FROM categorii ORDER BY titlu ASC";
$rcat = $dbc->query($qcat);
while($rc = $rcat->fetch_array()){
    
       echo '<option value="'.$rc['cat_id'].'" class="w3-light-gray w3-hover-indigo w3-large w3-card" style="text-transform:uppercase;">&#xf00b; '.$rc['titlu'].'</option>';  
      }
?>   
</select>
</div>

<div class="w3-half">
<select class="w3-input w3-col w3-xlarge w3-border w3-white srccata w3-hide" name="scat_id" style="padding-left:8%;" id="scat">
</select>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){

    $('#categorie').on("change",function () {
        var cat_id = $(this).find('option:selected').val();
        $.ajax({
            url: "ajax.php",
            type: "POST",
            //data: "cat_id = "+catId,
            data: {cat_id : cat_id},
            success: function (response) {
                console.log(response);
                $("#scat").html(response);
            },
        });
    }); 

});

</script>
   
   <?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($_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($_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 w3-card w3-light-gray">
         <p><input class="w3-input w3-button w3-light-gray" type="file" name="poze[]" multiple="" id="fileupload"/></p>
         <div id="dvPreview"></div>
      </div>
      
     <?php if(isset($filelarge)){echo $filelarge;} ?>
     <?php if(isset($fileuploaded)){echo $fileuploaded;} ?>
     
     <div class="validation w3-text-red" style="display:none;"> <strong>Sunt permise maxim 5 imagini! Alegeți din nou...</strong> </div>
     
     
     <script language="javascript" type="text/javascript">
window.onload = function () {
    var fileUpload = document.getElementById("fileupload");
    fileUpload.onchange = function () {
        if (typeof (FileReader) != "undefined") {
            var dvPreview = document.getElementById("dvPreview");
            dvPreview.innerHTML = "";
            var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png)$/;
            for (var i = 0; i < fileUpload.files.length; i++) {
                var file = fileUpload.files[i];
                if (regex.test(file.name.toLowerCase())) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var img = document.createElement("IMG");
                        img.height = "100";
                        img.width = "120";
                        img.style = "padding:4px;";
                        img.src = e.target.result;
                        dvPreview.appendChild(img);
                    }
                    reader.readAsDataURL(file);
                } else {
                    alert(file.name + " nu are o extensie permisă.");
                    dvPreview.innerHTML = "";
                    return false;
                }
            }
        } else {
            alert("This browser does not support HTML5 FileReader.");
        }
    }
 };
 
 $('#fileupload').change(function(){
   //get the input and the file list
   var input = document.getElementById('fileupload');
   if(input.files.length>5){
       $('.validation').css('display','block');
   }else{
       $('.validation').css('display','none');
   }
});
</script>
            
   </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);
        echo '<option value="" selected="" disabled="">Selectați județul</option>';
        //foreach ($dbc->query($qjud) as $rj) {
        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="">       
        <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>
       </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($_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"><?=$rownousr['moneda']?><?php if(empty($_SESSION['user_id'])){echo 'RON';} ?></div>
   </div>
   </div>
   
   
   <div class="w3-center w3-margin-top w3-margin-bottom">
<?php
if(isset($_SESSION['user_id']) AND (substr($_SERVER['PHP_SELF'], -10) != 'iesire.html')) {
?>
<input type="hidden" name="uip" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>"/>
<?php //if($ruc == $rownousr['limita']){}else{ ?>
<input type="hidden" name="anuntnou" value="true"/>
<button class="w3-button w3-blue w3-hover-black" type="submit">Adaugă anunțul</button>
<?php //}
if(isset($err_id)){echo '<div class="w3-panel w3-red w3-padding">'.$err_id.'</div>';}
if(isset($err_add_anunt)){echo '<div class="w3-panel w3-red w3-padding">'.$err_add_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($addedanunt)){echo '<div class="w3-panel w3-green w3-padding">'.$addedanunt.'</div>';}


?>
</form>
<?php }else{ ?>
<a onclick="document.getElementById('acces').style.display='block'" class="w3-button w3-green w3-hover-black" style="text-decoration:none;">Autentificare</a>
<?php } ?>
   </div>     
   
   </div>
 </div> 


</div>
</div>