App.order_queue_station = App.cable.subscriptions.create('OrderQueueStationChannel', {
//	App.messages = App.cable.subscriptions.create('MessagesChannel', {  

  connected: function() {},

  disconnected: function() {},

  received: function(data) {
    var hostname = location.hostname.trim();
    if(data.from == "" || hostname == data.from){
    	var oqs_id = $('.oqs_active').attr('data-id');
    	items = data.order;
    	for(var field in items) {
    		var price = parseFloat(items[field].price).toFixed(2);
    		if (items[field]["options"] == "[]" || items[field]["options"] == "") {
    			var options = "";
    		}else{
    			var options = items[field]["options"];
    		}
    		//for count
    		test = document.getElementsByClassName("oqs_count");

          for (var i = 0; i < test.length; i++) {
          	oqs_count_id = $(".oqs_count"+i).attr("data-id");
          	oqs_count = $(".oqs_count"+i).text();
         	    if ( oqs_count_id ==  items[field]["order_queue_station_id"]) {
         	    	oqs_count_total = +oqs_count +1 ;
         	    	$(".oqs_count"+i).text(oqs_count_total)
         	    }
          }
          //end count

    		var date = new Date(items[field]["created_at"]); 
    		 // var show_date = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear() + ' ' +  date.getHours()+ ':' +  date.getMinutes();
        var show_date =date.getHours()+ ':' +  date.getMinutes() +' '+(date.getHours() >= 12 ? 'PM' : 'AM');

    		if (oqs_id == items[field]["order_queue_station_id"]) {
          var table_type = (items[field]["table_type"] != null) ? items[field]["table_type"]:"";
          var zone       = (items[field]["zone"] != null) ? "-"+items[field]["zone"]:"No Table";
          (name === 'true') ? 'Y' :'N';
    			row ='<div class="card queue_station animated shake queue_station_box" data-order-no="'+items[field]["order_id"]+'">'
            +'<strong class="hidden">'+items[field]["order_id"]+'</strong>'
  		  		+'<div class="card-block">'
  		  

           +'<div class="row">'
            +'<span class="col-md-4 order-zone-type font-13">'+table_type+''+ zone+'</span>'
             +'<span class="order-zone hidden font-14">'+ zone +'</span>'
            +'<span class="col-md-8"><small class="float-right font-13">'+items[field]["order_id"]+'</small></span>'
           +'</div>'
  		  	  +'<p class="m-b--10">'
             +'<span class="order-item font-15">'+ items[field]["item_name"] +'- </span>'
             +'<span class="order-qty"> [x'+ items[field]["qty"] +'] </span>'
            +'</p>'

  		  		+'<br/><p class="card-text item-options">'+ options +'</p>'

  		  		+'<span class="card-text">'
              +'<span class="text-muted">Order at - '
                +'<span class="order-at">'+ show_date +'</span> <br>'

                +'Order By - <span class="order-by">'+ items[field]["item_order_by"] +'</span> '
                +'</span> '
            +'</span>'

  		  		+'<p class="hidden order-customer">'+ items[field]["customer_name"] +'</p> '
  		  		+'<p class="hidden assigned-order-item">'+ items[field]["assigned_order_item_id"] +'</p> '
  		  		+'</div>'

  		  		+'<div class="card-footer">'
  		  		+'<button id="edit_'+ items[field]["assigned_order_item_id"]+'" class="oqs-btn btn-warning order-item order-item-edit">EDIT</button>'
  		  		+' <button id="assigned_queue_' + items[field]["assigned_order_item_id"] +'" class="oqs-btn btn-primary order-item order-complete">COMPLETE</button>'
  		  		+'</div>'
  			    +'</div>'; 
    		  $('.oqs_append').append(row);
    		}
    	}//end looping

      var $divs = $("div.queue_station");
      var SortListDivs = $divs.sort(function (a, b) {

        first = $(a).attr('data-order-no');
        next  = $(b).attr('data-order-no');

        return parseInt(next.substring(4, 16)) - parseInt(first.substring(4, 16));
      });
      $("#oqs_container").html(SortListDivs);
    }
 },
});

