var selectedRows = 0;
var selected=[];

function update(){

    
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    //e.preventDefault();
    
    $.ajax({
        url:"/admin/changerEtat",
        method:'POST',
        data:{
            'selected':selected,
            'etat': $('#etat option:selected').text()
            

        },
    
        success:function(res){
            var e = $('#etat option:selected').text()
            
            $('#datatable tr.selected td#status span').html(e);
			
			//$('table#myTable tr.selected').css('background-color','white');
			$('table#datatable tr.selected').removeClass('selected');  
			 
			selected=[];
             },
            
         error: function(jqXHR, textStatus, errorThrown){
           // window.location="/login";
                
      }         
    });
    
}


$("#print").click(function(e){
    // Prevent actual form submission
    
        e.preventDefault();

        $.ajaxSetup({

            headers: {

                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

                }
        });
        
        selectedids = JSON.parse("[" + selected + "]");
        
        //alert(selectedIds);
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        
        $.ajax({

        type:'POST',

        url:'/commandes/print',

        data: { ids:JSON.stringify(selectedids)},

        success:function(data) {
            var mywindow = window.open('','','left=0,top=0,width=950,height=600,toolbar=0,scrollbars=0,status=0,addressbar=0');

            var is_chrome = Boolean(mywindow.chrome);
            mywindow.document.write(data);
            mywindow.document.close(); // necessary for IE >= 10 and necessary before onload for chrome

            if (is_chrome) {
                mywindow.onload = function() { // wait until all resources loaded 
                    mywindow.focus(); // necessary for IE >= 10
                    mywindow.print();  // change window to mywindow
                    mywindow.close();// change window to mywindow
                };
            }
            else {
                mywindow.document.close(); // necessary for IE >= 10
                mywindow.focus(); // necessary for IE >= 10
                mywindow.print();
                mywindow.close();
            }
                                
            

        }                
        
        });

    });


$("#datatable tr").click( function(event){
  
  if($(this).hasClass("selected")) {
      $(this).removeClass('selected');
      selectedRows--; 
  }
  else {
    
      $(this).addClass("selected");

      var id = $(this).attr('id');
      selected.push(id);
      selectedRows++;
      console.log(selectedRows);
    
  }
});
