<?php
    $sayi=0;

    $alan[]['alan']='{ "data": "urunler_no", "orderable": false },';
    $alan[]['izgara']='<th></th>';
    $alan[]['ozel']='{ "mRender": function ( data, type, full ) {
                            return \'<input type="checkbox" value="\'+data+\'" name="sec-\'+data+\'" id="sec-\'+data+\'"  >\';
                        }, "width": "10%" , "aTargets": [ '.$sayi.' ] },';
    $sayi++;

    $alan[]['alan']='{ "data": "urunler_no2", "orderable": false },';
    $alan[]['izgara']='<th>İŞLEMLER</th>';
    $alan[]['ozel']='{ "mRender": function ( data, type, full ) {
                            return "<a title=\'Düzelt\' href=\'javascript:;\' id=\'urun_duzelt-"+data+"\'><span class=\'fa-stack\'><i class=\'fa fa-square fa-stack-2x\'></i><i class=\'fa fa-pencil fa-stack-1x fa-inverse\'></i></span></a>"+
                            "<a  title=\'Sil\' href=\'javascript:;\' id=\'urun_sil-"+data+"\'><span class=\'fa-stack\'><i class=\'fa fa-square fa-stack-2x\'></i><i class=\'fa fa-trash-o fa-stack-1x fa-inverse\'></i></span></a>";
                        }, "width": "10%" , "aTargets": [ '.$sayi.' ] },';
    $sayi++;

    $alan[]['alan']='{ "data": "ad" },';
    $alan[]['izgara']='<th>Ad</th>';
    $sayi++;

    $alan[]['alan']='{ "data": "tarih" },';
    $alan[]['izgara']='<th>Tarih</th>';
    $sayi++;

    $alan[]['alan']='{ "data": "etkin" },';
    $alan[]['izgara']='<th>Etkin</th>';
    $alan[]['ozel']='{ "mRender": function ( data, type, full ) {


                      setTimeout(function() {
                        $("#isaret_etkin-"+full.urunler_no+"-"+data).bootstrapSwitch();
                            $("#isaret_etkin-"+full.urunler_no+"-"+data).on("switchChange.bootstrapSwitch", function(event, state) {
                                    $.ajax({
                                      method: "POST",
                                      url: "urunleri_listele_listesi.php",
                                      data: { urunler_no: full.urunler_no, durum: state, islem: "etkin" }
                                    }).fail(function() {
                                        alert( "'.YAPILAMADI.'" );
                                    })
                            });
                      }, 5);

                      if (data=="1")
                      {
                          return \'<input type="checkbox" value="\'+full.urunler_no+\'" name="isaret_etkin-\'+full.urunler_no+"-"+data+\'" id="isaret_etkin-\'+full.urunler_no+"-"+data+\'" data-on-text="'.EVET.'" checked data-off-text="'.HAYIR.'" data-size="mini">\';
                      }
                      else
                      {
                          return \'<input type="checkbox" value="\'+full.urunler_no+\'" name="isaret_etkin-\'+full.urunler_no+"-"+data+\'" id="isaret_etkin-\'+full.urunler_no+"-"+data+\'" data-on-text="'.EVET.'" data-off-text="'.HAYIR.'" data-size="mini">\';
                      }

                    }, "aTargets": [ '.$sayi.' ] },';
    $sayi++;



    $alan[]['alan']='{ "data": "siralama" },';
    $alan[]['izgara']='<th>Sıralama</th>';
    $alan[]['ozel']='{ "mRender": function ( data, type, full ) {

                        setTimeout(function() {


                            $("#siralama-"+full.urunler_no+"-"+data).focusout(function() {
                                $.ajax({
                                  method: "POST",
                                  url: "urunleri_listele_listesi.php",
                                  data: { urunler_no: full.urunler_no, siralama: $(this).val(), islem: "siralama" }
                                }).fail(function() {
                                    alert( "'.YAPILAMADI.'" );
                                });
                            });



                          }, 5);


                            return \'<input class="integer listeleme_siralama_kutusu" type="text" value="\'+data+\'" name="siralama-\'+full.urunler_no+"-"+data+\'" id="siralama-\'+full.urunler_no+"-"+data+\'" >\';
                        }, "aTargets": [ '.$sayi.' ] },';
    $sayi++;

    echo '<script type="text/javascript" language="javascript" class="init">


        $(document).ready(function() {

            var izgara = $("#veriler").dataTable( {
                "dom": "Trtlip",
                "autoWidth": false,
                "processing": true,
                "responsive": true,
                "stateSave": false,
                "bJQueryUI": false,
                "bAutoWidth": false,
                "order": [[ 2, "desc" ]],
                "serverSide": true,
                "ajax": {
                    "url": "urunleri_listele_listesi.php",
                    "type":"POST",
                    "data": { diller_no: "'.$gd_diller_no.'"},
                },
                pagingType: "full_numbers",
                lengthMenu: [[10, 25, 50, 100], [10, 25, 50, "100"]],
                "aoColumns": [';

                    foreach ($alan as $deger)
                    {
                        echo $deger['alan'];
                    }

                echo '],
                "aoColumnDefs": [';

                    foreach ($alan as $deger)
                    {
                        echo $deger['ozel'];
                    }

                echo '],
                "tableTools": {
                    sSwfPath: "kullanici/goruntu/tasarim/swf/copy_csv_xls_pdf.swf",
                    aButtons: [
                        {
                            "sExtends": "csv",
                            "sButtonText": "Csv '.KAYDET.'"
                        },
                        {
                            "sExtends": "xls",
                            "sButtonText": "Xls '.KAYDET.'"
                        },
                        {
                            "sExtends": "pdf",
                            "sButtonText": "Pdf '.KAYDET.'"
                        },
                        {
                            "sExtends": "print",
                            "sButtonText": "'.YAZDIR.'"
                        }

                    ]
                },
                "language": {
                    "emptyTable":       "'.JDT_KAYIT_BULUNAMADI.'",
                    "info":             "'.JDT_GORUNTULENEN_SAYFA.' _PAGE_ > _PAGES_",
                    "infoEmpty":        "'.JDT_KAYIT_YOK.'",
                    "infoFiltered":     "('.JDT_SUZULEN_TOPLAM_KAYIT.' _MAX_ )",
                    "infoPostFix":      "",
                    "lengthMenu":       "'.JDT_GORUNTULENIYOR.' _MENU_ '.JDT_GORUNTULENIYOR_SAYFA_BASI_KAYIT.'",
                    "loadingRecords":   "'.JDT_YUKLENIYOR.'",
                    "processing":       "'.JDT_ISLENIYOR.'",
                    "search":           "'.JDT_ARAMA.'",
                    "zeroRecords":      "'.JDT_SONUC_YOK.'",
                    "paginate": {
                        "first":        "'.JDT_ILK.'",
                        "previous":     "'.JDT_GERI.'",
                        "next":         "'.JDT_ILERI.'",
                        "last":         "'.JDT_SON.'"
                    },
                    "aria": {
                        "sortAscending":    ": '.JDT_SIRALA.'",
                        "sortDescending":   ": '.JDT_SIRALA_TERS.'"
                    },
                    "decimal":          "",
                    "thousands":        ","

                },


            });




                        $("#arama_dugme_tumu").click( function () {

                            $("#arama_tarih").val("");
                            $("#kelime").val("");
                            izgara.fnFilter("",0);

                        });



                        $("#kelime").keyup( function () {
                            $("#arama_dugme").click();
                        });


                        $("#arama_dugme").click( function () {


                            var kelime="";
                            kelime=kelime+"#####";

                            $("#arama_ozellikler option:selected").each(function(i, selected){
                              kelime = kelime+"|"+$(this).prop("value");
                            });

                            kelime=kelime+"#####"+$("#arama_tarih").val();

                            kelime=kelime+"#####"+$("#diller_no").val();

                            kelime=kelime+"#####"+$("#kelime").val();

                            izgara.fnFilter(kelime,0);

                         } );





                        $("#sil_dugme").click( function () {

                            $("#veriler tbody > tr.selected").each(function(key,value) {
                                $("#urunler").val($("#urunler").val()+"|"+$(value).children("td:first").children("input:first").val());
                            });

                            $("#sil_toplu_form").submit();

                        } );


                        $("#tumunu_sec").click( function () {
                            $("[id^=sec]").prop("checked", !($("[id^=sec]").is(":checked")));
                            $("[id^=sec]").parent().parent().toggleClass(" selected");
                            $("[id$=dugme_sor]").prop("disabled", !($("[id^=sec]").is(":checked")));
                        });


                        $("#veriler tbody").on( "click", "[id^=sec-]", function () {
                            $(this).parent().parent().toggleClass(" selected");

                            if ($("#veriler tbody > tr.selected").length>0)
                            {
                                $("#sil_dugme_sor").removeAttr("disabled");
                            }
                            else
                            {
                                $("#sil_dugme_sor").attr("disabled","disabled");
                            }

                        } );


                        $("body").on( "click", "[id^=urun_sil-]", function () {

                            var numara=$(this).attr("id").split("-");
                            
                            $.ajax
                            ({
                                type: "POST",
                                async: false,
                                url: "urunleri_listele_listesi.php",
                                data: { urunler_no: numara[1], islem: "sil" },
                                cache: false,
                                success: function(gelen)
                                {
                                    izgara.fnFilter("",0);                                
                                }
                            });

                        } );



                        $("body").on( "click", "[id^=urun_duzelt-]", function () {
                            var numara=$(this).attr("id").split("-");
                            
                            
                            $.ajax({
                              method: "POST",
                              url: "urunleri_listele_listesi.php",
                              data: { urunler_no: numara[1], islem: "guncelle_urun_ver"  }
                            }).fail(function() {
                                alert( "Hata" );
                            }).success(function(gelen) {
                                    $("#urun_duzelt_bilgi").html(gelen);
                                    $("#urunduzelt").modal("show");

                            });
                            
                            

                        } );


                        $("body").on( "click", "[id^=dugme_urun_guncelle]", function () {
                            
                            $.ajax({
                              method: "POST",
                              url: "urunleri_listele_listesi.php",
                              data: { urunler_no: $("#urunler_no").val(), ad: $("#ad").val(), siralama: $("#siralama").val(), islem: "guncelle"  }
                            }).fail(function() {
                                alert( "Hata" );
                            }).success(function(gelen) {
                                    
                                    $("#urunduzelt").modal("hide");
                                    izgara.fnFilter("",0);                  

                            });
                            
                            

                        } );












        } );


    </script>';




        echo '


            <div class="modal fade" id="urunduzelt" tabindex="-1" role="dialog" aria-labelledby="urun_duzeltLabel" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="urun_duzeltLabel">Bilgi</h4>
                  </div>
                  <div class="modal-body" id="urun_duzelt_bilgi">
                    
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Kapat</button>
                    <button type="button" class="btn btn-primary" id="dugme_urun_guncelle">Güncelle</button>
                  </div>
                </div>
              </div>
            </div>
















        <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#yeni_urun_ekle">
        Yeni Ekle
        </button>


        <div class="modal fade" id="yeni_urun_ekle" tabindex="-1" role="dialog" aria-labelledby="yeni_urun_ekleLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="yeni_urun_ekleLabel">Bilgi</h4>
              </div>
              <div class="modal-body">
                Eklensin mi?
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">'.KAPAT.'</button>
                <a href="?sayfa=urunler&islem=urun_duzelt&islem_alt=ekle"><button type="button" class="btn btn-primary">Ekleee</button></a>
              </div>
            </div>
          </div>
        </div>



        <div class="modal fade" id="toplu_urun_sil" tabindex="-1" role="dialog" aria-labelledby="toplu_urun_silLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="toplu_urun_silLabel">Toplu İşlem</h4>
              </div>
              <div class="modal-body">
                Toplu silem olmayacak sadece çalışma konumu
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">'.KAPAT.'</button>
                <button type="button" id="sil_dugme" class="btn btn-primary">Toplu Sil</button>
              </div>
            </div>
          </div>
        </div>';


echo '

        <div class="content-wrapper">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>Ürünler<small>ekranı</small></h1>
                    <ol class="breadcrumb">
                        <li><a href="./"><i class="fa fa-dashboard"></i> Anasayfa</a></li>
                        <li><a href="./">Ürünler</a></li>
                    </ol>
                </section>
        
                <!-- Main content -->
                <section class="content">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="box">
        
                                <div class="box-body">';





echo '<button type="button" id="tumunu_sec" class="btn btn-default navbar-btn">Tümünü Seç</button>



        <form id="sil_toplu_form" action="?" method="get" >
            <button disabled type="button" id="sil_dugme_sor" data-toggle="modal" data-target="#toplu_urun_sil" class="btn btn-default navbar-btn">Seçili olanları sil</button>
            <input type="hidden" id="urunler" name="urunler" value="">
            <input type="hidden" name="sayfa" value="urunler">
            <input type="hidden" name="islem" value="urun_sil">
            <input type="hidden" name="islem2" value="toplu">
        </form>

        <hr>
        <button type="button" id="arama_dugme" class="btn btn-default navbar-btn">'.ARA.'</button>
        <button type="button" id="arama_dugme_tumu" class="btn btn-default navbar-btn">'.TUMU.'</button>

        <input type="text" readonly="" id="arama_tarih" class="form-control">
        <input type="text" id="kelime" class="">
        <input type="hidden" readonly="" id="diller_no" value="'.$gd_diller_no.'">


        <select id="arama_ozellikler">
        <option value="">Seçiniz</option>';
        if (UR_VITRIN_ETKIN==1) {echo '<option value="vitrin">'.VITRIN.'</option>';};
        if (UR_OZEL_ETKIN==1) {echo '<option value="ozel">'.OZEL.'</option>';};
        if (UR_ONSAYFA_ETKIN==1) {echo '<option value="onsayfa">'.ONSAYFA.'</option>';};
        echo '<option value="etkin">'.ETKIN.'</option>';
        echo '<option value="etkin_degil">'.ETKIN_DEGIL.'</option>';


        echo '
        </select>';



        if ($_REQUEST['islem2']=="toplu")
        {
            $dizi=explode("|",$_REQUEST['urunler']);
            foreach ($dizi as $deger)
            {
                if (!empty($deger))
                {
                    //urun_sil($deger);
                    echo "ürün silindi";
                }
            }
        }
        else
        if ($_REQUEST['islem']=="urun_sil")
        {
            echo "ürün silindi";
        }



        echo '<table id="veriler" class="table table-bordered table-striped" cellspacing="0" width="100%">
            <thead>
            <tr>';
                foreach ($alan as $deger)
                {
                    echo $deger['izgara'];
                }

            echo '</tr>
            </thead>

            <tfoot>
            <tr>';

                foreach ($alan as $deger)
                {
                    echo $deger['izgara'];
                }

            echo '</tr>
            </tfoot>
        </table>';



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


    </section>

</div>';



//https://www.datatables.net/extensions/tabletools/initialisation

//gelen değişken listesi
//https://www.datatables.net/manual/server-side
//http://legacy.datatables.net/ref önemli
?>

