<?php 

error_reporting(E_ALL ^ E_NOTICE); 
 ?>

<?php $__env->startSection('content'); ?>
        <div class="page-wrapper">

            <!-- ============================================================== -->

            <!-- Bread crumb and right sidebar toggle -->

            <!-- ============================================================== -->

            <div class="row page-titles">

                <div class="col-md-5 align-self-center">

                    <h3 class="text-themecolor"><?php echo e(trans('lang.coupon_plural')); ?></h3>

                </div>

                <div class="col-md-7 align-self-center">

                    <ol class="breadcrumb">

                        <li class="breadcrumb-item"><a href="<?php echo e(url('/dashboard')); ?>"><?php echo e(trans('lang.dashboard')); ?></a></li>

                        <li class="breadcrumb-item active"><?php echo e(trans('lang.coupon_table')); ?></li>

                    </ol>

                </div>

                <div>

                </div>

            </div>

      

            <div class="container-fluid">

                <div class="row">

                    <div class="col-12">

                        <div class="card">

                            <div class="card-body">

                                <h4 class="card-title"><?php echo $customers_list ?></h4>

                                <!-- <h6 class="card-subtitle">Export data to Copy, CSV, Excel, PDF & Print</h6> -->
                            <div id="users-table_filter" class="pull-right"><label><?php echo e(trans('lang.search_by')); ?>

                                
                                <select name="selected_search" id="selected_search" class="form-control input-sm">
                                        <option value="description"><?php echo e(trans('lang.coupon_description')); ?></option>
                                        <option value="code"><?php echo e(trans('lang.coupon_code')); ?></option>
                                </select>

                                <div class="form-group">
                                <input type="search" id="search" class="search form-control" placeholder="Search" aria-controls="users-table"></label>&nbsp;<button onclick="searchtext();" class="btn btn-warning btn-flat"><?php echo e(trans('lang.search')); ?></button>&nbsp;<button onclick="searchclear();" class="btn btn-warning btn-flat"><?php echo e(trans('lang.clear')); ?></button>
                            </div>
                            </div>

                                <div class="table-responsive m-t-10">

                                    <table id="example24" class="display nowrap table table-hover table-striped table-bordered table table-striped" cellspacing="0" width="100%">

                                        <thead>

                                            <tr>

                                                <th><?php echo e(trans('lang.coupon_code')); ?></th>
                                                
                                                <th ><?php echo e(trans('lang.coupon_discount')); ?></th>
                                                
                                                <th ><?php echo e(trans('lang.coupon_description')); ?></th>
                                                
                                                <th ><?php echo e(trans('lang.coupon_expires_at')); ?></th>
                                                
                                                <th ><?php echo e(trans('lang.coupon_enabled')); ?></th>
                                                
                                                <th ><?php echo e(trans('lang.actions')); ?></th>

                                            </tr>

                                        </thead>

                                        <tbody id="append_list1">

                                        </tbody>

                                    </table>

                                    <div class="dataTables_paginate paging_simple_numbers" id="data-table_paginate">
                                        <ul class="pagination">
                                            <li class="paginate_button previous" id="users-table_previous">
                                                <a href="javascript:void(0);" id="users_table_previous_btn" onclick="prev()" aria-controls="users-table" data-dt-idx="0" tabindex="0"><?php echo e(trans('lang.previous')); ?></a>
                                            </li>
                                            <li class="paginate_button">
                                                <a href="javascript:void(0);" id="users_table_next_btn" onclick="next()" aria-controls="users-table" data-dt-idx="2" tabindex="0"><?php echo e(trans('lang.next')); ?></a>
                                            </li>
                                        </ul>
                                    </div>
                                
                                </div>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>
    </div>



<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
  <!--   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.1.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.1.0/firebase-firestore.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.1.0/firebase-database.js"></script>
    <script type="text/javascript"><?php echo $__env->make('vendor.notifications.init_firebase', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?></script> -->

<script type="text/javascript">
 
    var database = firebase.firestore();
    var offest=1;
    var pagesize=10; 
    var end = null;
    var endarray=[];
    var start = null;
    var user_number = [];
    var ref = database.collection('coupons');

    var append_list = '';

$(document).ready(function() {

    var inx= parseInt(offest) * parseInt(pagesize);
    jQuery("#data-table_processing").show();
  
    append_list = document.getElementById('append_list1');
    append_list.innerHTML='';
    ref.limit(pagesize).get().then( async function(snapshots){  
    html='';
    
    html=buildHTML(snapshots);
    jQuery("#data-table_processing").hide();
    if(html!=''){
        append_list.innerHTML=html;
        start = snapshots.docs[snapshots.docs.length - 1];
        endarray.push(snapshots.docs[0]);
        if(snapshots.docs.length < pagesize){
            jQuery("#data-table_paginate").hide();
        }
     }
  }); 
 
});


function buildHTML(snapshots){
        var html='';
        var alldata=[];
        var number= [];
        snapshots.docs.forEach((listval) => {
            var datas=listval.data();
            alldata.push(datas);
        });
                

        //     alldata.sort(function(a, b) {
                
        //       var keyA = a.createdAt.seconds,
        //         keyB = b.createdAt.seconds;
                
        //       if (keyA < keyB) return -1;
        //       if (keyA > keyB) return 1;
        //       return 0;
        // });
        var count = 0;
        alldata.forEach((listval) => {
            
            var val=listval;
            
                html=html+'<tr>';
                newdate='';

                var id = val.id;
                var route1 =  '<?php echo e(route("coupons.edit",":id")); ?>';
                route1 = route1.replace(':id', id);

                html=html+'<td>'+val.code+'</td>';
                html=html+'<td>'+val.discount+'</td>';
                html=html+'<td>'+val.description+'</td>';
                if(val.hasOwnProperty("expiresAt")){
                  var date =  val.expiresAt.toDate().toDateString();
                  var time = val.expiresAt.toDate().toLocaleTimeString('en-US');
                  html = html+'<td>'+date+'</td>';
                }else{
                  html = html + '<td></td>';
                }
                if(val.isEnabled){
                  html=html+'<td><span class="badge badge-success">Yes</span></td>';
                }else{                
                  html=html+'<td><span class="badge badge-danger">No</span></td>';
                }

                // if(val.hasOwnProperty("updatedAt")){
                //   var date =  val.updatedAt.toDate().toDateString();
                //   var time = val.updatedAt.toDate().toLocaleTimeString('en-US');
                //   html = html+'<td>'+time+' '+date+'</td>';
                // }else{
                //   html = html + '<td></td>';
                // }
                
                html=html+'<td class="action-btn"><a href="'+route1+'"><i class="fa fa-edit"></i></a><a id="'+val.id+'" name="coupon_delete_btn" href="javascript:void(0)"><i class="fa fa-trash"></i></a></td>';
                
                            

                html=html+'</tr>';
                count =count +1;
          });
          return html;      
}

  function prev(){
      if(endarray.length==1){
        return false;
    }
    end=endarray[endarray.length-2];
  
  if(end!=undefined || end!=null){
            jQuery("#data-table_processing").show();
                 
            if(jQuery("#selected_search").val()=='code' && jQuery("#search").val().trim()!=''){

                  listener=ref.orderBy('code').limit(pagesize).startAt(jQuery("#search").val()).endAt(jQuery("#search").val()+'\uf8ff').startAt(end).get();
              } else if(jQuery("#selected_search").val()=='description' && jQuery("#search").val().trim()!=''){

              listener=ref.orderBy('description').limit(pagesize).startAt(jQuery("#search").val()).endAt(jQuery("#search").val()+'\uf8ff').startAt(end).get();

              }else{
                    listener = ref.startAt(end).limit(pagesize).get();
                }
                
                listener.then((snapshots) => {
                html='';
                html=buildHTML(snapshots);
                jQuery("#data-table_processing").hide();
                if(html!=''){
                    append_list.innerHTML=html;
                    start = snapshots.docs[snapshots.docs.length - 1];
                    endarray.splice(endarray.indexOf(endarray[endarray.length-1]),1);

                    if(snapshots.docs.length < pagesize){ 
   
                        jQuery("#users_table_previous_btn").hide();
                    }
                    
                }
            });
  }
}


function next(){
  if(start!=undefined || start!=null){

        jQuery("#data-table_processing").hide();
          // listener = ref.startAfter(start).limit(pagesize).get();

          if(jQuery("#selected_search").val()=='code' && jQuery("#search").val().trim()!=''){

                listener=ref.orderBy('code').limit(pagesize).startAt(jQuery("#search").val()).endAt(jQuery("#search").val()+'\uf8ff').startAfter(start).get();
            } else if(jQuery("#selected_search").val() =='description' && jQuery("#search").val().trim()!=''){
              console.log("Dsecription");
                listener=ref.orderBy('description').limit(pagesize).startAt(jQuery("#search").val()).endAt(jQuery("#search").val()+'\uf8ff').startAfter(start).get();
            } else{
                listener = ref.startAfter(start).limit(pagesize).get();
            }
          listener.then((snapshots) => {
            
                html='';
                html=buildHTML(snapshots);
                console.log(snapshots);
                jQuery("#data-table_processing").hide();
                if(html!=''){
                    append_list.innerHTML=html;
                    start = snapshots.docs[snapshots.docs.length - 1];

                    if(endarray.indexOf(snapshots.docs[0])!=-1){
                        endarray.splice(endarray.indexOf(snapshots.docs[0]),1);
                    }
                    endarray.push(snapshots.docs[0]);
                }
            });
    }
}

function searchclear(){
    jQuery("#search").val('');
    searchtext();
}

function searchtext(){

  /* var offest=1;
  var pagesize=10;
  var start = null;
  var end = null;
  var endarray=[];
  var inx= parseInt(offest) * parseInt(pagesize); */
  jQuery("#data-table_processing").show();
  
  append_list.innerHTML='';  

   if(jQuery("#selected_search").val()=='code' && jQuery("#search").val().trim()!=''){

     wherequery=ref.orderBy('code').limit(pagesize).startAt(jQuery("#search").val()).endAt(jQuery("#search").val()+'\uf8ff').get();

   } else if(jQuery("#selected_search").val()=='description' && jQuery("#search").val().trim()!=''){

     wherequery=ref.orderBy('description').limit(pagesize).startAt(jQuery("#search").val()).endAt(jQuery("#search").val()+'\uf8ff').get();

   } else{

    wherequery=ref.limit(pagesize).get();
  }
  
  wherequery.then((snapshots) => {
    html='';
    html=buildHTML(snapshots);
    jQuery("#data-table_processing").hide();
    if(html!=''){
        append_list.innerHTML=html;
        start = snapshots.docs[snapshots.docs.length - 1];
        endarray.push(snapshots.docs[0]);
        /*if(snapshots.docs.length<pagesize && jQuery("#selected_search").val().trim()!='' && jQuery("#search").val().trim()!=''){*/
        if(snapshots.docs.length < pagesize){ 
   
            jQuery("#data-table_paginate").hide();
        }else{

            jQuery("#data-table_paginate").show();
        }
    }
}); 

}

$(document).on("click","a[name='coupon_delete_btn']", function (e) {
        var id = this.id;
        alert("This is for demo, We can't allow to delete");
    /* database.collection('coupons').doc(id).delete().then(function(){

      window.location = "<?php echo e(! url()->current()); ?>";
    }); */ 


});

</script>

<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/customer/www/foodie.siswebapp.com/public_html/resources/views/coupons/index.blade.php ENDPATH**/ ?>