<?php require admin_view('static/header') ?>
<div class="container-fluid">
   <div class="row">
      <div class="col-lg-12">
         <div class="mb-3" style="margin-bottom:20px;">
            <a href="javascript:;" class="btn btn-default" data-toggle="modal" data-toggle="modal" data-target="#modalDefault" data-action="get_service">Servis Oluştur</a>
            <a href="javascript:;" class="btn btn-default" data-toggle="modal" data-toggle="modal" data-target="#categoryModal">Kategori Oluştur</a>
            <button onclick="change_ordering('servicesrow')" class="btn btn-default">Servis Sırasını Düzenle</button>
            <a href="javascript:;" class="btn btn-default pull-right" data-toggle="modal" data-toggle="modal" data-target="#modalDefault" data-action="get_apiservice">Toplu Servis Ekle</a>
         </div>
         <div class="panel panel-default" style="margin-bottom:0;border-bottom-left-radius: 0;border-bottom-right-radius: 0;">
            <div class="panel-body">
               <small><b>Seçili Servisleri:</b></small>
               <button onclick="service_multiple_on();" class="btn btn-default btn-xs">Aktifleştir</button>
               <button onclick="service_multiple_off();" class="btn btn-default btn-xs">Pasifleştir</button>
               <button onclick="service_multiple_delete();" class="btn btn-default btn-xs">Sil</button>
               <button onclick="toplusec();" class="btn btn-default btn-xs pull-right">Tüm Servisleri Seç</button>

            </div>
            <div class="panel-body">
               <input type="text" id="myInput" class="form-control" onkeyup="servisarama()" placeholder="Arama" title="Arama">

            </div>
         </div>


         <script>

         </script>

         <table class="table order-table table-grey" style="margin:0;">
            <thead>
               <tr id="serviceTable">
                  <th style="min-width:50px;"></th>
                  <th style="min-width:75px">ID</th>
                  <th style="min-width:350px">Servis Adı</th>
                  <th style="min-width:175px;">Sağlayıcı</th>
                  <th style="min-width:75px;">Min</th>
                  <th style="min-width:75px;">Max</th>
                  <th style="min-width:75px;">Fiyat</th>
                  <th style="min-width:90px;">Durum</th>
                  <th style="min-width:90px;"></th>
               </tr>
            </thead>
         </table>
         <ul class="list-unstyled sortable" id="category">
            <?php $i = 1; ?>
            <?php foreach ($categories as $category) : ?>
               <li class="sort" data-id="<?= $category['category_id'] ?>">
                  <div class="panel panel-default" style="border-radius:0;margin:0;min-width: 1060px;">
                     <div class="panel-heading">
                        <i class="fas fa-arrows-alt handle"></i>
                        <b><?= $category["category_name"] ?></b> <?php if ($category['category_status'] == 0) : ?><span class="label label-danger">Kategori Pasif</span><?php endif; ?>
                        <div class="dropdown pull-right">
                           <button class="btn btn-xs btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">İşlemler<span class="caret"></span></button>
                           <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                              <li><a href="javascript:;" data-toggle="modal" data-toggle="modal" data-target="#modalDefault" data-action="get_category" data-id="<?= $category["category_id"]; ?>">Kategoriyi Düzenle</a></li>
                              <?php if ($category['category_status'] == 1) : ?>
                                 <li><a href="javascript:;" onclick="change_category_status(<?= $category["category_id"]; ?>)">Kategoriyi Pasifleştir</a></li>
                                 <?php else : ?>
                                    <li><a href="javascript:;" onclick="change_category_status(<?= $category["category_id"]; ?>)">Kategoriyi Aktifleştir</a></li>
                                 <?php endif; ?>
                                 <li><a href="javascript:;" onclick="delete_category(<?= $category["category_id"]; ?>)">Kategoriyi Sil</a></li>
                              </ul>
                           </div>
                        </div>
                        <!-- /.panel-heading -->
                        <?php if ($category['category_status'] == 1) : ?>
                           <div class="panel-body" style="padding:0;">
                              <table id="deposits" class="table order-table table-grey teble-hovered deposits" style="margin-bottom:0;">
                                 <tbody id="services<?= $i ?>">
                                    <?php $a = 0; ?>
                                    <?php foreach ($services as $service) : ?>
                                       <?php if ($service['category_id'] == $category['category_id']) : ?>
                                          <tr data-id="<?= $service["service_id"]; ?>">
                                             <td style="width:50px;">
                                                <input type="checkbox" id="checkbox<?= $a; ?>" name="checkbox<?= $service["service_id"]; ?>" value='<?= $service["service_id"] ?>'>
                                             </td>
                                             <td style="width:75px;"><?= $service["service_id"]; ?></td>
                                             <td style="width:350px" id="filterName"><?= $service["service_name"]; ?></td>
                                             <td style="width:175px;"><?= ($service["api_url"]) ? $service["api_url"] : "Manuel" ?></td>
                                             <td style="width:75px;"><?= $service["service_min"]; ?><?= $service['api_min'] ?></td>
                                             <td style="width:75px;"><?= $service["service_max"]; ?><?= $service['api_max'] ?></td>
                                             <td style="width:75px;"><b><?= $service["service_charge"] . ' ' . CURRENCY_SYMBOL ?></b><?= $service['api_rate'] ?></td>
                                             <td style="width:90px;"><?= $service["service_status"] == 1 ? '<span class="label label-success">Aktif</span>' : '<span class="label label-danger">Pasif</span>'; ?></td>
                                             <td style="width:90px;">
                                                <div class="dropdown pull-right">
                                                   <button class="btn btn-xs btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">İşlemler<span class="caret"></span></button>
                                                   <ul class="dropdown-menu pull-right" aria-labelledby="dropdownMenu1">
                                                      <li><a href="javascript:;" data-toggle="modal" data-toggle="modal" data-target="#modalDefault" data-action="edit_service" data-id="<?= $service["service_id"] ?>">Servisi Düzenle</a></li>
                                                      <li><a href="javascript:;" data-toggle="modal" data-toggle="modal" data-target="#modalDefault" data-action="get_servicedescription" data-id="<?= $service["service_id"] ?>">Açıklamayı Düzenle</a></li>
                                                      <?php if ($service['service_status'] == 1) : ?>
                                                         <li><a href="javascript:;" onclick="servicestatus(<?= $service["service_id"] ?>, 'off')">Servisi Pasifleştir</a></li>
                                                         <?php elseif ($service['service_status'] == 0) : ?>
                                                            <li><a href="javascript:;" onclick="servicestatus(<?= $service["service_id"] ?>, 'on')">Servisi Aktifleştir</a></li>
                                                         <?php endif; ?>
                                                         <li><a href="javascript:;" onclick="service_indirim_sil(<?= $service["service_id"] ?>, 'on')">Servis İndirimlerini Sil</a></li>
                                                         <li><a href="javascript:;" onclick="delete_service(<?= $service["service_id"]; ?>)">Servisi Sil</a></li>
                                                      </ul>
                                                   </div>
                                                </td>
                                             </tr>
                                          <?php endif; ?>
                                          <?php $a++; ?>
                                       <?php endforeach; ?>
                                    </tbody>
                                 </table>
                              </div>
                           <?php endif; ?>
                        </div>
                     </li>
                     <?php $i++; ?>
                  <?php endforeach; ?>
               </ul>
               <input type="hidden" id="totalcheckbox" name="totalcheckbox" value="<?= $a ?>">
            </div>
         </div>
      </div>
      <div class="modal fade in" id="serviceModal" tabindex="-1" aria-hidden="true">
         <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
               <div class="modal-header">
                  <h4 class="modal-title">Yeni Servis Oluştur</h4>
               </div>
               <div class="modal-body">
                  <form id="add_service" onsubmit="return false;" data-reset="1">
                     <div class="form-group">
                        <div class="input-group">
                           <span class="input-group-addon"><b>Servis Adı</b></span>
                           <input type="text" name="service_name" class="form-control">
                        </div>
                     </div>
                     <div class="form-group">
                        <div class="input-group">
                           <span class="input-group-addon"><b>Servis Kategorisi</b></span>
                           <select name="category" class="form-control">
                              <?php foreach ($categories as $category) : ?>
                                 <?php if ($category["category_id"] == $service["category_id"]) : ?>
                                    <option value="<?= $category["category_id"]; ?>" selected><?= $category["category_name"]; ?></option>
                                    <?php else : ?>
                                       <option value="<?= $category["category_id"]; ?>"><?= $category["category_name"]; ?></option>
                                    <?php endif; ?>
                                 <?php endforeach; ?>
                              </select>
                           </div>
                        </div>
                        <div class="box">
                           <div class="form-group">
                              <div class="input-group">
                                 <span class="input-group-addon"><b>Servis Tipi</b></span>
                                 <select id="type" name="type" onchange="change_type()" class="form-control form-control-sm">
                                    <option value="1">Normal</option>
                                    <option value="2">Paket</option>
                                    <option value="3">Özel Yorum</option>
                                    <option value="4">Abonelik</option>
                                 </select>
                              </div>
                           </div>
                           <div id="auto_div" class="form-group hide">
                              <div class="input-group">
                                 <span class="input-group-addon">Abonelik Tipi</span>
                                 <select name="auto_type" class="form-control">
                                    <option value="1">Instagram İzlenme</option>
                                    <option value="2">Instagram Beğeni</option>
                                 </select>
                              </div>
                           </div>
                           <hr>
                           <div class="form-group">
                              <div class="input-group">
                                 <span class="input-group-addon"><b>Servis Sağlayıcı</b></span>
                                 <select id="api" name="api" onchange="change_api()" class="form-control form-control-sm mb-2">
                                    <option value="0">Manuel Servis (Sağlayıcı Seçilmedi)</option>
                                    <?php foreach ($apis as $api) : ?>
                                       <option value="<?= $api["api_id"]; ?>"><?= $api["api_url"]; ?></option>
                                    <?php endforeach; ?>
                                 </select>
                              </div>
                           </div>
                           <div class="form-group">
                              <div class="input-group">
                                 <span class="input-group-addon"><input type="checkbox" class="custom-control-input" id="autoUpdate" checked=""></span>
                                 <input style="font-weight: 700;" type="text" class="form-control" readonly="" value="Bilgileri Otomatik Ayarla (Fiyat ve diğer bilgileri otomatik çeker)">
                              </div>
                           </div>
                           <div class="form-group">
                              <div class="input-group">
                                 <span class="input-group-addon"><b>API Servisi</b></span>
                                 <select id="api_service" name="api_service" onchange="change_api_service()" class="form-control form-control-sm mb-2">
                                    <option value="0">Seçilmedi</option>
                                 </select>
                              </div>
                           </div>
                        </div>
                        <div class="form-group">
                           <div class="input-group">
                              <span class="input-group-addon"><b>Servis Fiyatı (1000 Adet)</b></span>
                              <input type="text" name="charge" class="form-control" required>
                           </div>
                        </div>
                        <div class="row" id="quantity">
                           <div class="col-md-6">
                              <div class="form-group">
                                 <div class="input-group">
                                    <span class="input-group-addon"><b>Minimum Sipariş</b></span>
                                    <input type="number" name="min" id="min" class="form-control" required>
                                 </div>
                              </div>
                           </div>
                           <div class="col-md-6">
                              <div class="form-group">
                                 <div class="input-group">
                                    <span class="input-group-addon"><b>Maksimum Sipariş</b></span>
                                    <input type="number" name="max" id="max" class="form-control" required>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <hr>
                        <div class="form-group" id="drip_div">
                           <div class="input-group">
                              <span class="input-group-addon"><b>Süreli Sipariş</b></span>
                              <select name="drip" class="form-control">
                                 <option value="0">Hayır</option>
                                 <option value="1">Evet</option>
                              </select>
                           </div>
                        </div>
                        <div class="form-group">
                           <div class="input-group">
                              <span class="input-group-addon"><b>Servis Durumu</b></span>
                              <select name="service_status" class="form-control">
                                 <option value="1" selected>Aktif</option>
                                 <option value="0">Pasif</option>
                              </select>
                           </div>
                        </div>
                        <input type="hidden" name="id" value="<?= $service["service_id"]; ?>" />
                        <div class="form-group">
                           <button type="submit" class="btn btn-success"><span class="text">Yeni Servisi Oluştur</span></button>
                        </div>
                     </form>
                  </div>
               </div>
            </div>
         </div>
         <div class="modal fade in" id="categoryModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
               <div class="modal-content">
                  <div class="modal-header">
                     <h4 class="modal-title">Yeni Kategori Oluştur</h4>
                  </div>
                  <div class="modal-body">
                     <form id="add_category" onsubmit="return false;" data-reset="1">
                        <div class="form-loader">
                           <span class="intro-banner-vdo-play-btn pinkBg">
                              <span class="ripple pinkBg"></span>
                              <span class="ripple pinkBg"></span>
                              <span class="ripple pinkBg"></span>
                           </span>
                        </div>
                        <div class="form-group">
                           <div class="input-group">
                              <label>Kategori adı</label>
                              <select id="lang" style="width:100%;" class="form-control mb-2" onchange="select()">
                                 <?php foreach ($langs as $lang) : ?>
                                    <option value="<?= $lang["lang_id"]; ?>"><?= $lang["lang_code"]; ?></option>
                                 <?php endforeach; ?>
                              </select>
                              <?php foreach ($langs as $lang) : ?>
                                 <input type="text" style="width:100%;" id="lang_<?= $lang["lang_id"]; ?>" name="name[<?= $lang["lang_id"]; ?>]" class="form-control hide" />
                              <?php endforeach; ?>
                           </div>
                        </div>
                        <div class="form-group">
                           <label>Kategori ICON</label>
                           <div class="input-group iconpicker-container">
                              <input name="category_icon" class="form-control icp icp-auto-ekle iconpicker-element iconpicker-input" value="far fa-star" type="text">
                              <span class="input-group-addon"></span>
                           </div>
                        </div>
                        <div class="form-group">
                           <label>ICON Rengi</label>
                           <input name="category_icon_color" class="form-control" data-jscolor="{}" value=" " type="text">
                        </div>
                        <div class="form-group text-right">
                           <button type="submit" class="btn btn-success">Oluştur</button>
                        </div>
                     </form>
                  </div>
               </div>
            </div>
         </div>
         <script>
            window.addEventListener('load', function() {
               select($("#lang").val(), 'input');
               select($("#desclang").val(), 'textarea');
               change_api();
               change_type();
               $(function() {
                  var selectedId = <?= (isset($service["api_service"]) && $service["api_service"] != null) ? $service["api_service"] : 0; ?>;
                  var selectedApı = <?= (isset($service["api_id"]) && $service["api_id"] != null) ? $service["api_id"] : 0; ?>;
                  $("#api_service").bind("DOMSubtreeModified", function() {
                     if (selectedApı != 0 && (selectedApı == $("#api").val())) {
                        $("#api_service").val(selectedId);
                     }
                  });
               });
            }, false);

            function select(id = null, element = "input") {
               if (element == "input") {
                  $('input[name^="name"]').each(function() {
                     $(this).addClass("hide");
                  });

                  if (id != null) {
                     $("#lang_" + id).removeClass("hide");
                  } else {
                     $("#lang_" + $("#lang").val()).removeClass("hide");
                  }

               } else if (element == "textarea") {
                  $('.lang').each(function() {
                     $(this).addClass("hide");
                  });

                  if (id != null) {
                     $("#desclang_" + id).removeClass("hide");
                  } else {
                     $("#desclang_" + $("#desclang").val()).removeClass("hide");
                  }
               }
            }

            function change_type() {
               var type = $("#type").val();
               if (type == 1) {
                  $("#quantity").removeClass("hide");
                  $("#drip_div").removeClass("hide");
                  $("#auto_div").addClass("hide");
               } else if (type == 2) {
                  $("#quantity").addClass("hide");
                  $("#min, #max").val("1");
                  $("#drip_div").removeClass("hide");
                  $("#auto_div").addClass("hide");
               } else if (type == 3) {
                  $("#quantity").removeClass("hide");
                  $("#drip_div").addClass("hide");
                  $("select[name=drip]").val("0");
                  $("#auto_div").addClass("hide");
               } else if (type == 4) {
                  $("#quantity").removeClass("hide");
                  $("#drip_div").addClass("hide");
                  $("select[name=drip]").val("0");
                  $("#auto_div").removeClass("hide");
               }
            }

            function change_api() {
               var api = $("#api").val();
               if (api != "0") {
                  post("get_api_services", {
                     api: api
                  });
               }

            }


            function change_api_service() {
               var type = $("#type").val();
               var selected = $("#api_service").find("option:selected");
               if ($("#autoUpdate").is(":checked")) {

                  if (type != 2) {

                     $("#min").val(selected.data("min"));
                     $("#max").val(selected.data("max"));
                  }

                  var price = parseFloat(selected.data("price"));
                  if ($("#times").val() != "") {
                     price = Decimal.mul(price, parseFloat($("#times").val()));
                  }

                  if ($("#sum").val() != "") {
                     price = parseFloat(price) + parseFloat($("#sum").val());
                  }
                  $("input[name=charge]").val(price);
               }
            }
         </script>
         <?php require admin_view('static/footer') ?>
         <script>
            function servisarama() {
               var input, filter, ul, li, a, i, txtValue;
               input = document.getElementById("myInput");
               filter = input.value.toUpperCase();
               ul = document.getElementsByClassName("deposits");
               for (e = 0; e < ul.length; e++) {
                  li = ul[e].getElementsByTagName("tr");
                  for (i = 0; i < li.length; i++) {
                     a = li[i];
                     txtValue = a.innerHTML;
            //console.log(a.innerHTML+"+");
            if (txtValue.toUpperCase().indexOf(filter) > -1) {
               li[i].style.display = "";
            } else {
               li[i].style.display = "none";
            }
         }
      }
   }
</script>
<script>
   $(document).on("click", "select[name='api']", function() {
    
      if ($(this).val() == 0) {
        
         $(".api_service_list_area").hide();  
         $(".aktif_pasif_area").hide();
      } else {
       
         $(".api_service_list_area").show();
         $(".aktif_pasif_area").show();
      }


   });
</script>