<?php 

error_reporting(E_ALL ^ E_NOTICE); 
 ?>

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


            <div class="row page-titles">

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

                    <h3 class="text-themecolor"><?php echo e(trans('lang.driver')); ?> <?php echo e(trans('lang.payment_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.driver')); ?> <?php echo e(trans('lang.payment_plural')); ?></li>
                    </ol>
                </div>

                <div>

                </div>

            </div>

      

            <div class="container-fluid">

                <div class="row">

                    <div class="col-12">

                        <div class="card">

                            <div class="card-body">

                                <!-- <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="restaurant"><?php echo e(trans('lang.driver')); ?></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.driver')); ?></th>
                                                <th><?php echo e(trans('lang.total_amount')); ?></th>
                                                <th><?php echo e(trans('lang.paid_amount')); ?></th>
                                                <th><?php echo e(trans('lang.remaining_amount')); ?></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">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">Next</a></li></ul></div>
                </div>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>
    </div>



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


<?php $__env->startSection('scripts'); ?>
    <script>
    

    var database = firebase.firestore();
    var offest=1;
    var pagesize=10; 
    var end = null;
    var endarray=[];
    var start = null;
    var user_number = [];
// var ref=[];
    // var ref = database.collection('vendors_review');
    var ref = database.collection('users').where('role','==','driver');
   /* var  user_search = database.collection('users'); */

    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) {
                console.log(a);
              var keyA = a.createdAt,
               keyB = b.createdAt;
                 console.log(keyA);
              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("drivers.edit",":id")); ?>';
                route1 = route1.replace(':id', id);
                // const total_price=totalPrice(val.id);
                // html=html+'<td class="name_'+val.id+'"></td>';
          
                html = html+'<td><a href="'+route1+'">'+val.firstName+' '+val.lastName+'</a></td>';
                html=html+'<td class="total_'+val.id+'"></td>';           
                html=html+'<td class="name_'+val.id+'"></td>';
                const remaining_price = remainingPrice(val.id);
                html=html+'<td class="remaining_'+val.id+'"></td>';
                html=html+'</tr>';

          });
          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();
                 
            console.log(jQuery("#selected_search").val());
        if(jQuery("#selected_search").val()=='restaurant' && jQuery("#search").val().trim()!=''){
                listener = ref.orderBy('firstName').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();
            if(jQuery("#selected_search").val()=='restaurant' && jQuery("#search").val().trim()!=''){

            listener = ref.orderBy('firstName').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()=='restaurant' && jQuery("#search").val().trim()!=''){

     wherequery=ref.orderBy('firstName').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();
        }
    }
}); 

}

// async function totalPrice(driverID) {
// var price=0;
// await database.collection('restaurant_orders').where('driverID','==',driverID).where("status","==","Order Completed").get().then( async function(orderSnapshots){
            
//             orderSnapshots.docs.forEach((order)=>{

//               var orderData = order.data();
              
//               orderData.products.forEach((product)=> {

//                   if(product.price && product.quantity != 0){
//                     var productTotal = parseInt(product.price)*parseInt(product.quantity);
//                     price = price + productTotal;
//                   }
//                 })

//             })
// });
// jQuery(".name_"+driverID).html(price);
// return price;
// }

async function remainingPrice(driverID){
  var paid_price = 0;
  var total_price = 0;
  var remaining = 0;
  await database.collection('driver_payouts').where('driverID','==',driverID).get().then( async function(payoutSnapshots){        
       payoutSnapshots.docs.forEach((payout)=>{
          var payoutData = payout.data();
          paid_price = parseFloat(paid_price) + parseFloat(payoutData.amount);
        })

        await database.collection('restaurant_orders').where('driverID','==',driverID).where("status","in",["Order Completed"]).get().then( async function(orderSnapshots){

            orderSnapshots.docs.forEach((order)=>{
              var orderData = order.data();
                
                if(orderData.delivery_charge!=undefined && orderData.tip_amount!=undefined){
                    var orderDataTotal = parseInt(orderData.delivery_charge)+parseInt(orderData.tip_amount);
                    total_price = total_price + orderDataTotal;      
                  }else if(orderData.delivery_charge!=undefined){
                    var orderDataTotal = parseInt(orderData.delivery_charge);
                    total_price = total_price + orderDataTotal;      
                  }else if(orderData.tip_amount!=undefined){
                    var orderDataTotal = parseInt(orderData.tip_amount);
                    total_price = total_price + orderDataTotal;      
                  }
                  
            })

             remaining = total_price - paid_price;
             jQuery(".total_"+driverID).html(total_price);
             jQuery(".name_"+driverID).html(paid_price);
             jQuery(".remaining_"+driverID).html(remaining);
             jQuery("#data-table_processing").hide();
        });   
});
  return remaining; 
}



</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/payments/driver_index.blade.php ENDPATH**/ ?>