App.order_reservation = App.cable.subscriptions.create('OrderReservationChannel', {
  connected: function() {},

  disconnected: function() {},

  received: function(data) {
    var hostname = location.hostname.trim();
    if(data.from == "" || hostname == data.from){
      var shop_code = data.shop_code; 
      var order  = data.data;
      var audio  = data.audio;
      if(order.length > 0){
        $('.custom-table.'+shop_code+'_order_reserve_cable tbody').html("");
        $("."+shop_code+" > .nav-item.red > a > p.num").text(order.length);
        $.each(order, function(key,value){
          var active_class = "";
          var rowCount = key+1;
          var date = new Date(value.created_at); 
          var time = timeFormat(date);
          var created_at = date.getFullYear() +'-'+ (date.getMonth() >= 10? date.getMonth() : '0' + (date.getMonth() + 1)) +'-'+ (date.getDate() >=10?date.getDate() : '0'+date.getDate());

          var delivery_type = "";
          if(value.provider == "food2u" || value.provider == "yangondoor2door"){
            delivery_type = "DELIVERY";
          }else if(value.provider == "pick_up"){
            delivery_type = "PICK-UP";
          }else if(value.provider == "turbo"){
            delivery_type = "TURBO";
          }else{
            delivery_type = "DIRECT DELIVERY";
          }

          var payment_type = "";
          if(value.payment_type=="cash_on_delivery"){
            payment_type = "COD";
          }else if(value.payment_type =="dinga"){
            payment_type = "DINGA";
          }

          if(key==0){
            var order_id  = value.order_reservation_id;
            var sr_no = rowCount;
            var url       = "/origami/order_reservation/get_order/"+order_id;
            if (typeof show_order_detail !== 'undefined' && $.isFunction(show_order_detail)) {
              show_order_detail(url,sr_no);
            }
            active_class = "tr-active";
            if (typeof audioPlayBackground !== 'undefined' && $.isFunction(audioPlayBackground)) {
              audioPlayBackground(shop_code,audio);
            }
            if (typeof showNewOrderAlert !== 'undefined' && $.isFunction(showNewOrderAlert)) {
              showNewOrderAlert(order[key],shop_code);
            }
          }

          row = '<tr class="custom-tr first-'+rowCount+' '+active_class+'" style="" data-id="'+value.order_reservation_id+'" data-sr-no="'+rowCount+'">'
                +'<td width ="5%" class="align-left">'+rowCount
                +'</td>'
                +'<td width ="20%" class="align-center">'+created_at
                +'</td>'
                +'<td width ="15%" class="align-center">'+time
                +'</td>'
                +'<td width ="20%" class="align-center">'+value.grand_total
                +'</td>'
                +'<td width ="20%" class="align-center col-blue">'+payment_type
                +'</td>'
                +'<td width ="20%" class="align-center">'
                  +'<span class="font-10 col-blue">'+ delivery_type +'</span>'
                +'</td>'
              +' </tr>';
          
          $('.custom-table.'+shop_code+'_order_reserve_cable tbody').append(row);
        });
      }

      if (typeof customTableClick !== 'undefined' && $.isFunction(customTableClick)) {
        customTableClick();
      }

      if(parseInt(getOnlineOrderCount()) > 0){
          $('.order_no').addClass("order-badge");
          $('.order_no').html(parseInt(getOnlineOrderCount()));
      }
    }
  }
});

