<?php include 'inc/header.php';?>
<?php include 'inc/sidebar.php';?>
<?php include 'inc/navbar.php';?>
<?php
if ($_POST) { 
  $parent = $_POST['parent'];
  $product_name = $_POST['product_name'];
  $product_price = $_POST['product_price'];
  $product_description = $_POST['product_description'];
  $page_title = $_POST['page_title'];
  $page_description = $_POST['page_description'];
  $page_keywords = $_POST['page_keywords'];
  $foto = $_POST['foto'];

  $eksecenek = $_POST['eksecenek'];
  $eksecenekfiyat = $_POST['eksecenekfiyat'];
  $alerji = $_POST['alerji'];
  $secenek = $_POST['secenek'];
  $secenekfiyat = $_POST['secenekfiyat'];




  if ($parent <> "" && $product_name <> "" && $product_description <> "")  

    if ($_FILES['foto']['tmp_name'] != "") {
      $boyut = $_FILES['foto']['size'];
      $tip = mime_content_type($_FILES['foto']['tmp_name']);
      if($tip!="image/jpeg" && $tip!="image/jpg" && $tip!="image/png" && $tip!="application/pdf"){
        echo 'Dosya Jpeg veya Png yada Gif , PDF formatında olmalı';
      }elseif ($boyut > (1024 * 1024 * 30)) {
        echo 'Dosya 30MB den büyük olamaz.';
      } else {
        $tip = $_FILES['foto']['type'];
        $isim = $_FILES['foto']['name'];
        $uzanti = explode('.', $isim);
        $uzanti = $uzanti[count($uzanti) - 1];
        $random = rand();
        $dosya = $_FILES['foto']['tmp_name'];
        $foto_url = $random . "." . $uzanti;
        copy($dosya, '../img/' . $foto_url);
        echo '';
       $seo_name=seo($product_name);

        $satir = [
          'foto' => $foto_url,
          'parent' => $parent,
          'product_name' => $product_name,
          'product_price' => $product_price,
          'product_description' => $product_description,
          'page_title' => $page_title,
          'page_description' => $page_description,
          'page_keywords' => $page_keywords,
          'product_optionek' =>json_encode($eksecenek),
          'product_optionek_price' =>json_encode($eksecenekfiyat),
          'product_allergy' =>json_encode($alerji),
          'product_portion' =>json_encode($secenek),
          'product_portion_price' =>json_encode($secenekfiyat),
          'seo_name' => $seo_name, 

        ];
        $sql = "INSERT INTO product SET foto=:foto, parent=:parent, product_name=:product_name, product_price=:product_price, product_description=:product_description, product_optionek=:product_optionek, product_optionek_price=:product_optionek_price,product_allergy=:product_allergy,product_portion=:product_portion,product_portion_price=:product_portion_price, page_title=:page_title, page_description=:page_description, page_keywords=:page_keywords, seo_name=:seo_name;";
        /** @var PDO $dbh */
        $durum = ($dbh->prepare($sql))->execute($satir);



        if ($durum) {
          $sonId = $dbh->lastInsertId();
          foreach ($_POST["en"] as $key => $value) {
           dilguncelle("en","product",$sonId,$key,$value);
         } 
         echo "<script>
         const Toast = Swal.mixin({
          toast: true,
          position: 'top-end',
          showConfirmButton: false,
          timer: 6000,
          timerProgressBar: true,
          didOpen: (toast) => {
            toast.addEventListener('mouseenter', Swal.stopTimer)
            toast.addEventListener('mouseleave', Swal.resumeTimer)
          }
          })

          Toast.fire({
            icon: 'success',
            title: 'Başarı ile kayıt edildi.'
            })
            </script>";
          }
        }
      }
    }      
    ?>
    <!-- Page Content-->

    <div class="modal fade bs-example-modal-md in" tabindex="-1" role="dialog" id="alerjikeklemodal" aria-hidden="true" >
      <div class="modal-dialog modal-md">
        <div class="modal-content">

          <div class="modal-header">
            <h6 class="modal-title m-0" id="myLargeModalLabel">Alerjik Ekle</h6>
            <button type="button" class="close " data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true"><i class="la la-times"></i></span>
            </button>
          </div>


          <div class="modal-body">
            <div class="container-fluid">
              <div class="row">

                <div class="form-group col-md-12 ">
                  <label for="message-text" class="col-form-label">Alerjik Adı:</label>

                </div>
                <div class="form-group col-md-8 col-sm-6 col-xs-12">
                  <input class="form-control" type="text" id="alerjisecenek">
                </div>

                <div class="form-group col-md-4 col-sm-6 col-xs-12">


                  <button type="button" class="btn btn-primary" id="seceneksec" onclick="alerjikekle()" style="width: 100%;">Ekle</button>
                </div>
              </div>
              <span id="alerjikeklemodalhata" style="color: red"></span>
            </div>
          </div>
        </div>
      </div>
    </div>



    <div class="modal fade bs-example-modal-md in" tabindex="-1" role="dialog" id="eksecenekekle" aria-hidden="true" >
      <div class="modal-dialog modal-md">
        <div class="modal-content">

          <div class="modal-header">
            <h6 class="modal-title m-0" id="myLargeModalLabel">Seçenek Ekle</h6>
            <button type="button" class="close " data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true"><i class="la la-times"></i></span>
            </button>
          </div>
          <div class="modal-body">
            <div class="container-fluid">
              <div class="row">

                <div class="form-group col-md-12 ">
                  <label for="message-text" class="col-form-label">Seçenek Adı:</label>

                </div>
                <div class="form-group col-md-8 col-sm-6 col-xs-12">
                  <input class="form-control" type="text" id="eksecenekek">
                </div>

                <div class="form-group col-md-4 col-sm-6 col-xs-12">


                  <button type="button" class="btn btn-primary" id="urunusec" onclick="eksecenekekle()" style="width: 100%;">Ekle</button>
                </div>
              </div>
              <span id="ekshata" style="color: red"></span>
            </div>
          </div>
        </div>
      </div>
    </div>



    <div class="modal fade bs-example-modal-md in" tabindex="-1" role="dialog" id="porsiyonmodal" aria-hidden="true" >
      <div class="modal-dialog modal-md">
        <div class="modal-content">
          <div class="modal-header">
            <h6 class="modal-title m-0" id="myLargeModalLabel">Porsiyon Ekle</h6>
            <button type="button" class="close " data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true"><i class="la la-times"></i></span>
            </button>
          </div>
          <div class="modal-body">
            <div class="container-fluid">
              <div class="row">

                <div class="form-group col-md-12 ">
                  <label for="message-text" class="col-form-label">porsiyon Adı:</label>

                </div>
                <div class="form-group col-md-8 col-sm-6 col-xs-12">
                  <input class="form-control" type="text" id="porsekleval">
                </div>

                <div class="form-group col-md-4 col-sm-6 col-xs-12">


                  <button type="button" class="btn btn-primary" id="seceneksec" onclick="porsiyonekle()" style="width: 100%;">Ekle</button>
                </div>
              </div>
              <span id="shata" style="color: red"></span>
            </div>
          </div>
        </div>
      </div>
    </div>









    <div class="page-content">
      <div class="container-fluid">
        <!-- Page-Title -->

        <form method="post" action="" enctype="multipart/form-data">

          <div class="row">
            <div class="col-sm-12">
              <div class="page-title-box">
                <div class="row">
                  <div class="col">
                    <h4 class="page-title">Ürün Ekle</h4>
                    <ol class="breadcrumb">
                      <li class="breadcrumb-item"><a href="index.php">Anasayfa</a></li>
                      <li class="breadcrumb-item"><a href="urun-liste.php">Ürün Listesi</a></li>
                      <li class="breadcrumb-item active">Ürün Ekle</li>
                    </ol>
                  </div><!--end col-->
                  <div class="col-auto align-self-center">
                    <button class="btn btn-success" type="submit">
                      <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                      KAYDET
                    </button> 
                  </div><!--end col-->  
                </div><!--end row-->                                                              
              </div><!--end page-title-box-->
            </div><!--end col-->
          </div><!--end row-->
          <!-- end page title end breadcrumb -->


          <div class="row">
            <div class="col-lg-12">
              <div class="card">
                <div class="card-header">
                  <h4 class="card-title">Ürün Ekle</h4>
                  <p class="text-muted mb-0">Değerli kullanıcı, bu alandan yeni bir proje ekleyebilirsiniz. Zorunlu olarak doldurmanız gereken alanlar * olarak belirtilmiştir. Ürün ebatlarımız 741 x 451 px</p> 
                </div><!--end card-header-->
                <div class="card-body">

                 <div class="row">

                  <div class="col-xl-4">  
                    <div class="form-group">
                      <label for="exampleInputEmail1">Ürün Kategorisini Seçin *</label>
                      <select class="form-control" name="parent" required="">

                        <?php
                        $kategoric = $dbh->query("select * from product_categories WHERE parent = ''  ", PDO::FETCH_ASSOC);
                        if ($kategoric->rowCount()) {
                         foreach ($kategoric as $kategori) {

                           $optgroup="optgroup";
                           $alt = $dbh->query("select * from product_categories WHERE parent = '" . $kategori['id'] . "'   ", PDO::FETCH_ASSOC);


                           if ($alt->rowCount()) {
                             echo '<optgroup   label="' . $kategori['categories_name'] . '" >';
                             foreach ($alt as $alt) {
                              echo '<option '.($alt['id']==$sonuc["parent"]?"selected":"").' value="' . $alt['id'] . '">' . $alt['categories_name'] . '</option>';
                            }
                            echo '</optgroup>';
                          }else{
                           echo '<option '.($kategori['id']==$sonuc["parent"]?"selected":"").' value="' . $kategori['id'] . '">' . $kategori['categories_name'] . '</option>';
                         }


                       }
                     }
                     ?>
                   </select>
                 </div>
               </div>

               <div class="col-xl-4">  
                <div class="form-group">
                  <label for="exampleInputEmail1">Ürün Başlık *</label>
                  <input type="text" class="form-control" maxlength="999" id="thresholdconfig" aria-describedby="emailHelp" placeholder="Lütfen bir başlık giriniz" name="product_name" required=""> 
                </div>
              </div>

              <div class="col-xl-4">  
                <div class="form-group">
                  <label for="exampleInputEmail1">Ürün Fiyatı </label>
                  <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Lütfen bir fiyat girin" name="product_price"> 
                </div>
              </div>




              <div class="col-xl-12">  
                <div class="form-group">
                  <label for="exampleInputEmail1">Ürün Açıklaması *</label>
                  <textarea name="product_description" id="metin" class="ckeditor" cols="30" rows="10" required=""></textarea>
                </div>
              </div>


              <div class="col-md-12">
                <br><br>
                <label class="form-control-label active">Seçilebilir Seçenekler</label>
                <div class="row" id="liste"> 
                  <?php 
                  $eksecenek = $dbh->prepare("SELECT * FROM selectable where status=1  order by option asc");
                  $eksecenek-> execute(array());
                  $eksecenek= $eksecenek->fetchAll(PDO::FETCH_OBJ);

                  foreach ($eksecenek as $secenek) {  ?>
                    <div class="col-md-2 col-sm-4 col-xs-6" id="secfff<?=$secenek->id?>" style="    padding-left: 38px;">
                      <input type="checkbox" class="custom-control-input" onchange="secf('<?=$secenek->id?>')"  name="eksecenek[]" id="customCheck<?=$secenek->id?>" value="<?=$secenek->id?>">
                      <label style="float: left ; margin-right: 5px;" class="custom-control-label" for="customCheck<?=$secenek->id?>"><?=$secenek->option?></label>

                    </div>
                  <?php } ?>

                  
                </div>
                <button type="button" data-toggle="modal" data-target="#eksecenekekle" class="btn btn-default source" style="float: right;">Seçenek Ekle <i class="fa fa-plus"></i></button>
              </div>



              <div class="col-xl-12">
                <br><br>
                <label class="form-control-label active">Alerjik Ürünler</label>
                <div class="row"  id="alerjiliste"> 
                  <?php 
                  $alerji = $dbh->prepare("SELECT * FROM allergy   order by title asc");
                  $alerji-> execute(array());
                  $alerji= $alerji->fetchAll(PDO::FETCH_OBJ);

                  foreach ($alerji as $alerji) {  ?>
                    <div class="col-md-2 col-sm-4 col-xs-6" style="    padding-left: 38px;">
                      <input type="checkbox" class="custom-control-input" name="alerji[]" id="customChecka<?=$alerji->id?>" value="<?=$alerji->id?>">
                      <label class="custom-control-label" for="customChecka<?=$alerji->id?>"><?=$alerji->title?></label>
                    </div>
                  <?php } ?>

                </div>
                <button type="button" data-toggle="modal" data-target="#alerjikeklemodal" class="btn btn-default source" style="float: right;">Alerjik Seçenek Ekle <i class="fa fa-plus"></i></button>
              </div>



              <div class="col-md-12">
                <br><br>
                <label class="form-control-label active">Porsiyonlar ( <span>Porsiyon seçmek için seçenek seç butonunu kullanınız..</span>)</label>
                <div class="row" id="secnekliste"> 
                
                </div>


                
                <button type="button" data-toggle="modal" data-target="#porsiyonmodal" class="btn btn-default source" style="float: right;">Yeni Seçenek Ekle <i class="fa fa-plus"></i></button>
                <button type="button" onclick="seceneksec();" class="btn btn-default source" style="float: right;"> Seçenek Seç <i class="fa fa-plus"></i></button>

              </div>







            </div>



            <div class="row">
              <div class="col-xl-12">  
              </div><!--end card-header-->
              <div class="card-body">
                <input type="file" name="foto" accept="image/x-png,image/gif,image/png,image/jpeg,application/pdf" id="input-file-now" class="dropify" / required="">      
              </div><!--end col--> 
            </div><!--end row-->

            <!-- dil başlangıçı -->

            <div class="row"> 
              <div class="col-lg-12 col-xl-12">  
                <div class="accordion" id="accordionExample">
                  <div class="card border mb-1 shadow-none">
                    <div class="card-header custom-accordion  rounded-0" id="headingOne">
                      <a href="" class="text-dark" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                        <i data-feather="activity"></i> Seo Ayarları
                      </a>
                    </div>
                    <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
                      <div class="card-body">

                        <div class="row">
                          <div class="col-lg-4">
                           <div class="form-group">
                            <label for="exampleInputPassword1">Sayfa Başlığı</label>
                            <input type="text" class="form-control" name="page_title" maxlength="999" id="thresholdconfig" placeholder="Sayfa Başlığı">
                          </div>
                        </div>

                        <div class="col-lg-4">
                         <div class="form-group">
                          <label for="exampleInputPassword1">Sayfa Açıklaması</label>
                          <input type="text" class="form-control" name="page_description" id="exampleInputPassword1" placeholder="Sayfa Açıklaması">
                        </div>
                      </div>

                      <div class="col-lg-4">
                       <div class="form-group">
                        <label for="exampleInputPassword1">Sayfa Anahtar Kelimeleri</label>
                        <input type="text" class="form-control" name="page_keywords" maxlength="999" id="thresholdconfig" placeholder="Sayfa Anahtar Kelimeleri">
                      </div>
                    </div>

                  </div>
                </div>
              </div>
            </div>
            <div class="card mb-1 border shadow-none">
              <div class="card-header  rounded-0" id="headingTwo">
                <a href="" class="collapsed text-dark" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                  <i data-feather="sliders"></i> İngilizce Dil Ayarları
                </a>
              </div>
              <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
                <div class="card-body">

                  <div class="row">

                   <div class="col-xl-12">  
                    <div class="form-group">
                      <label for="exampleInputEmail1">Ürün Adı *</label>
                      <input type="text" class="form-control" maxlength="999" id="thresholdconfig" aria-describedby="emailHelp" placeholder="Lütfen bir kısa başlık giriniz" name="en[product_name]"> 
                    </div>
                  </div>


                  <div class="col-xl-12">  
                    <div class="form-group">
                      <label for="exampleInputEmail1">Ürün Açıklaması *</label>
                      <textarea name="en[product_description]" id="metining" class="ckeditor" cols="30" rows="10"></textarea>
                    </div>
                  </div>

                </div>

                <div class="row">
                  <div class="col-lg-4">
                   <div class="form-group">
                    <label for="exampleInputPassword1">Sayfa Başlığı [ENG]</label>
                    <input type="text" class="form-control" name="en[page_title]" maxlength="999" id="thresholdconfig" placeholder="Sayfa Başlığı">
                  </div>
                </div>

                <div class="col-lg-4">
                 <div class="form-group">
                  <label for="exampleInputPassword1">Sayfa Açıklaması [ENG]</label>
                  <input type="text" class="form-control" name="en[page_description]" id="exampleInputPassword1" placeholder="Sayfa Açıklaması">
                </div>
              </div>

              <div class="col-lg-4">
               <div class="form-group">
                <label for="exampleInputPassword1">Sayfa Anahtar Kelimeleri [ENG]</label>
                <input type="text" class="form-control" name="en[page_keywords]" maxlength="999" id="thresholdconfig" placeholder="Sayfa Anahtar Kelimeleri">
              </div>
            </div>

          </div>

        </div>
      </div>
    </div> 
  </div>      
</div><!--end col-->
</div><!--end row-->

<!-- dil bitişi --> 

<br>    
<button class="btn btn-success btn-block" type="submit">
  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
  KAYDET
</button>
</form>                                           
</div><!--end card-body-->
</div><!--end card-->
</div><!--end col-->


</div><!--end row-->



</div><!-- container -->

<?php include 'inc/footer.php';?>

<script type="text/javascript">



  function eksecenekekle(){

    var eksece = document.getElementById("eksecenekek").value;

    $.ajax({
      url     : 'urunislem.php',
      data    :'durum=eksecenekekle&eksecenek='+eksece,
      type    : 'POST',
      success : function(sonuc){

        gelensonuc = jQuery.parseJSON(sonuc);
        if(gelensonuc.onay==1){



          $('#liste').append('<div class="col-md-2 col-sm-4 col-xs-6" id="secfff'+gelensonuc.id+'" style="    padding-left: 38px;"><input type="checkbox" class="custom-control-input" onchange="secf('+gelensonuc.id+')"  name="eksecenek[]" id="customCheck'+gelensonuc.id+'" value="'+gelensonuc.id+'"><label style="float: left ; margin-right: 5px;" class="custom-control-label" for="customCheck'+gelensonuc.id+'">'+eksece+'</label></div>');


          


          $('#eksecenekekle').modal('hide');
        }else{
          $("#ekshata").text("Aynı İsimde Seçenek Bulunmaktadır");
        }
      }
    });
  }



  function alerjikekle(){

    var alerji = document.getElementById("alerjisecenek").value;

    $.ajax({
      url     : 'urunislem.php',
      data    :'durum=alerjisecenek&alerji='+alerji,
      type    : 'POST',
      success : function(sonuc){
        gelensonuc = jQuery.parseJSON(sonuc);
        if(gelensonuc.onay==1){

          $('#alerjiliste ').append('<div class="col-md-2 col-sm-4 col-xs-6" style=" padding-left: 38px;"><input type="checkbox" checked class="custom-control-input" name="alerji[]" id="customChecka'+gelensonuc.id+'" value="'+gelensonuc.id+'"><label class="custom-control-label" for="customChecka'+gelensonuc.id+'">'+alerji+'</label></div>');


          $('#alerjikeklemodal').modal('hide');
        }else{
          $("#alerjikeklemodalhata").text("Aynı İsimde Seçenek Bulunmaktadır");
        }
      }
    });
  }

  function seceneksec(){

    $.ajax({
      url     : 'urunislem.php',
      data    :'durum=seceneksec',
      type    : 'POST',
      success : function(sonuc){
        $('#secnekliste').append(sonuc);
        /*$('#alerjiliste ').append('<div class="col-md-2 col-sm-4 col-xs-6"><input type="checkbox" checked class="custom-control-input" name="alerji[]" id="customChecka'+gelensonuc.id+'" value="'+gelensonuc.id+'"><label class="custom-control-label" for="customChecka'+gelensonuc.id+'">'+alerji+'</label></div>');*/
      }
    });
  }



  function porsiyonekle(){

    var sece = document.getElementById("porsekleval").value;

    
    $.ajax({
      url     : 'urunislem.php',
      data    :'durum=porsiyonekle&secenek='+sece,
      type    : 'POST',
      success : function(sonuc){
        gelensonuc = jQuery.parseJSON(sonuc);

        if(gelensonuc.onay==1){
          porsiyonsec(gelensonuc.idd);

          $('#porsiyonmodal').modal('hide');
        }else if(gelensonuc.onay==2){
          $("#shata").text("Aynı İsimde Seçenek Bulunmaktadır");
        }else if(gelensonuc.onay==3){
          $("#shata").text("Lütfen Seçenek Adı Giriniz");

        }
      }
    });
  }

  function porsiyonsec($id){

    $.ajax({
      url     : 'urunislem.php',
      data    :'durum=seceneksecid&id='+$id,
      type    : 'POST',
      success : function(sonuc){
        $('#secnekliste ').append(sonuc);
        /*$('#alerjiliste ').append('<div class="col-md-2 col-sm-4 col-xs-6"><input type="checkbox" checked class="custom-control-input" name="alerji[]" id="customChecka'+gelensonuc.id+'" value="'+gelensonuc.id+'"><label class="custom-control-label" for="customChecka'+gelensonuc.id+'">'+alerji+'</label></div>');*/
      }
    });
  }

  function delitem($id){

    $("#s"+$id).remove();
  }
  function secf(id){
    if ($('#customCheck'+id).is(':checked')) {
      $('#secfff'+id).append('<input type="number" id="eksecfiyat'+id+'" name="eksecenekfiyat[]" style="width: 50px;padding: 4px;height: 26px;" min="0" value="0" class="form-control" placeholder="fiyat">')
    }else{
     $("#eksecfiyat"+id).remove();
   }
 }

</script>