<?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.accepted_orders')); ?></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 "><a href="<?php echo e(url('orders')); ?>"><?php echo e(trans('lang.order_plural')); ?></a></li>
                        <li class="breadcrumb-item active"><?php echo e(trans('lang.accepted_orders')); ?></li>
                    </ol>
                </div>

                <div>

                </div>

            </div>

      

            <div class="container-fluid">

                <div class="row">

                    <div class="col-12">

                        <div class="card">

                            <div class="card-body">
                                <div id="data-table_processing" class="dataTables_processing panel panel-default" style="display: none;"><?php echo e(trans('lang.processing')); ?></div>
                                <!-- <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="id"><?php echo e(trans('lang.order_id')); ?></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.order_id')); ?></th>

                                                <th><?php echo e(trans('lang.order_user_id')); ?></th>
                                                <!-- <th><?php echo e(trans('lang.order_order_status_id')); ?></th> -->
                                                <th><?php echo e(trans('lang.amount')); ?></th>
                                                <th><?php echo e(trans('lang.order_type')); ?></th>
                                                
                                                <th><?php echo e(trans('lang.actions')); ?></th>

                                            </tr>

                                        </thead>

                                        <tbody id="append_list1">


                                        </tbody>

                                    </table>
                                        <div id="data-table_paginate" style="display:none">
                                            <nav aria-label="Page navigation example" class="pagination_div">
                                                <ul class="pagination justify-content-center">
                                                    <li class="page-item ">
                                                        <a class="page-link" href="javascript:void(0);" id="users_table_previous_btn" onclick="prev()"  data-dt-idx="0" tabindex="0"><?php echo e(trans('lang.previous')); ?></a>
                                                    </li>
                                                    <li class="page-item">
                                                        <a class="page-link" href="javascript:void(0);" id="users_table_next_btn" onclick="next()"  data-dt-idx="2" tabindex="0"><?php echo e(trans('lang.next')); ?></a>
                                                    </li>
                                                </ul>
                                            </nav>
                                        </div>
                </div>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>
    </div>



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


<?php $__env->startSection('scripts'); ?>
   <script type="text/javascript">
 
       var database = firebase.firestore();
    var offest=1;
    var pagesize=50; 
    var end = null;
    var endarray=[];
    var start = null;
    var user_id = "<?php echo $id; ?>";
    //var ref = database.collection('restaurant_orders').orderBy('createdAt', 'desc').where('vendor.author',"==",user_id).where('status','==','Order Accepted');

    var refData = database.collection('restaurant_orders').where('vendor.author',"==",user_id).where('status','==','Order Accepted');
    var ref = database.collection('restaurant_orders').orderBy('createdAt', 'desc').where('vendor.author',"==",user_id).where('status','==','Order Accepted');

    var append_list = '';
    var user_number = [];

    var currentCurrency ='';
    var currencyAtRight = false;
    var refCurrency = database.collection('currencies').where('isActive', '==' , true);
    refCurrency.get().then( async function(snapshots){
        var currencyData = snapshots.docs[0].data();
        currentCurrency = currencyData.symbol;
        currencyAtRight = currencyData.symbolAtRight;
    });

    $(document).ready(function() {

        $(document.body).on('click', '.redirecttopage' ,function(){    
            var url=$(this).attr('data-url');
            window.location.href = url;
        });

        jQuery("#data-table_processing").show();  
        append_list = document.getElementById('append_list1');
        append_list.innerHTML='';
         ref.limit(pagesize).get().then( async function(snapshots){
            html='';
            console.log(snapshots.docs);
            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]);
                disableClick();
            }

             if(snapshots.docs.length < pagesize){ 
   
            jQuery("#data-table_paginate").hide();
        }else{

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

    function buildHTML(snapshots){
        var html='';
        var alldata=[];
        var number= [];
        snapshots.docs.forEach((listval) => {
            var datas=listval.data();
            
                let result = user_number.filter(obj => {
            return obj.id == datas.author;
        })
        if(result.length >0){
          datas.phoneNumber = result[0].phoneNumber;
          datas.isActive = result[0].isActive;
          
        }else{
          datas.phoneNumber = '';
          datas.isActive = false;
        }
        

            
             
            alldata.push(datas);
        });
                
        var count = 0;
        alldata.forEach((listval) => {
            
            var val=listval;
            
                html=html+'<tr>';
                newdate='';
                var id = val.id;
                var route1 =  '<?php echo e(route("orders.edit",":id")); ?>';
                route1 = route1.replace(':id', id);

            
                html=html+'<td data-url="'+route1+'" class="redirecttopage">'+val.id+'</td>';
                html=html+'<td>'+val.author.firstName+' '+val.author.lastName+'</td>';
                // html=html+'<td>'+val.status+'</td>'; 
                var price = 0;
                // val.products.forEach((product)=> {

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

                var price = buildHTMLProductstotal(val);

                    html=html+'<td>'+price+'</td>';

                if(val.takeAway){
                    html=html+'<td><?php echo e(trans("lang.order_takeaway")); ?></td>';    
                }else{
                    html=html+'<td><?php echo e(trans("lang.order_delivery")); ?></td>';
                }

                html=html+'<td class="action-btn"><a href="'+route1+'"><i class="fa fa-edit"></i></a><a id="'+val.id+'" class="do_not_delete" name="order-delete" 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()=='id' && jQuery("#search").val().trim()!=''){

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

                    listener=refData.orderBy('id').limit(pagesize).startAt(jQuery("#search").val()).endAt(jQuery("#search").val()+'\uf8ff').startAt(end).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]);
                }
            });
    }
}

$(document).on("click","a[name='order-delete']", function (e) {
        var id = this.id;
    database.collection('restaurant_orders').doc(id).delete().then(function(result){
        window.location.href = '<?php echo e(url()->current()); ?>';
    });


});

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

function searchtext(){

  var offest=1;
  jQuery("#data-table_processing").show();
  
  append_list.innerHTML='';  

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

     wherequery=refData.orderBy('id').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("#data-table_paginate").hide();
        }else{

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

}

function buildHTMLProductstotal(snapshotsProducts){

        var adminCommission = snapshotsProducts.adminCommission;
        var discount = snapshotsProducts.discount;
        var couponCode = snapshotsProducts.couponCode;
        var extras = snapshotsProducts.extras;
        var extras_price = snapshotsProducts.extras_price;
        var rejectedByDrivers = snapshotsProducts.rejectedByDrivers;
        var takeAway = snapshotsProducts.takeAway;
        var tip_amount = snapshotsProducts.tip_amount;
        var status = snapshotsProducts.status;
        var products = snapshotsProducts.products;
        var delivery_charge = snapshotsProducts.delivery_charge;
        var totalProductPrice=0;
        var total_price = 0;
        
        var intRegex = /^\d+$/;
        var floatRegex = /^((\d+(\.\d *)?)|((\d*\.)?\d+))$/;
        
        if (products) {

          products.forEach((product) => {

                 var val=product;

                //price_item=parseFloat(val.price).toFixed(2);
                if(val.hasOwnProperty('discountPrice') && val.discountPrice != ''){
                    price_item=parseFloat(val.discountPrice).toFixed(2);
                }else{
                    price_item=parseFloat(val.price).toFixed(2);  
                }
                extras_price_item=parseFloat(val.extras_price).toFixed(2);

                totalProductPrice =  parseFloat(price_item)*parseInt(val.quantity);
                var extras_price=0;
                if(parseFloat(extras_price_item)!=NaN && val.extras_price!=undefined){
                    extras_price=extras_price_item;
                }
                totalProductPrice =parseFloat(extras_price)+parseFloat(totalProductPrice);
                totalProductPrice=parseFloat(totalProductPrice).toFixed(2);

                total_price +=parseFloat(totalProductPrice);

          });
        }
          
          if(intRegex.test(discount) || floatRegex.test(discount)) {

            discount=parseFloat(discount).toFixed(2);
            total_price -=parseFloat(discount);

            if(currencyAtRight){
                discount_val = discount+""+currentCurrency;
            }else{
               discount_val = currentCurrency+""+discount;
            }

          }
          
          if(intRegex.test(delivery_charge) || floatRegex.test(delivery_charge)) {

                delivery_charge=parseFloat(delivery_charge).toFixed(2);
                total_price +=parseFloat(delivery_charge);
                
                if(currencyAtRight){
                  delivery_charge_val = delivery_charge+""+currentCurrency;
                }else{
                   delivery_charge_val = currentCurrency+""+delivery_charge;
                }
          }

          
          if(intRegex.test(tip_amount) || floatRegex.test(tip_amount)) {

              tip_amount=parseFloat(tip_amount).toFixed(2);
              total_price +=parseFloat(tip_amount);
              total_price=parseFloat(total_price).toFixed(2);
              
                if(currencyAtRight){
                  tip_amount_val = tip_amount+""+currentCurrency;
                }else{
                   tip_amount_val = currentCurrency+""+tip_amount;
                }
            }

            if(currencyAtRight){
            var  total_price_val = total_price+""+currentCurrency;
            }else{
              var total_price_val = currentCurrency+""+total_price;
            }


return total_price_val;
}

function disableClick(){
    var is_disable_delete = "<?php echo env('IS_DISABLE_DELETE'); ?>";
    if(is_disable_delete == 1){
        jQuery("a.do_not_delete").removeAttr("name");
        jQuery("a.do_not_delete").attr("name","alert_demo");       
    }
}


$(document).on("click","a[name='alert_demo']", function (e) {
    
    alert(doNotDeleteAlert);
}); 

</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/foodierestaurant.siswebapp.com/resources/views/orders/accepted.blade.php ENDPATH**/ ?>