.qi_header
  .right
    =raw @p_ship_tour.state_html
  %h1
    Tournées
    %span
      Tournée du 
      =l @p_ship_tour.start_at, :format => :date

-if !@p_ship_tour.state
  .qi_row
    .qi_pannel.qi_plain.padding
      %h3 Produits à livrer
      .sheet_line_to_ship
        -PSheetLine.joins(:p_customer_sheet).joins(:p_product).where(:p_products => {:p_product_cat_id =>[6]}, :p_customer_sheets => {:state => "commande"}, :externe => false, :lock => nil).where("wish_start <= ? and wish_stop >= ?", (@p_ship_tour.start_at+5.hour), @p_ship_tour.start_at).each do |p_sheet_line|
          -if !@p_ship_tour.p_sheet_line_ids.include?(p_sheet_line.id)
            =render p_sheet_line
            
      .clear
      -if false
        .remove_to_ship_tour.btn.btn-primary
          Retirer de la tournée
-if @p_ship_tour.state == "en-cours"
  .qi_row
    .qi_pannel.qi_plain.padding
      =link_to "Saisir le retour de livraison", ship_return_admin_p_ship_tour_path(@p_ship_tour), :class => "btn btn-primary"
  
  
.qi_row
  .qi_pannel.qi_plain.padding
  
    -if false
      .update_trucks.btn.btn-primary
        Mettre à jour
  
    %table.table
      %tr
        -@p_ship_tour.p_ship_tour_trucks.where(:remorque => false).each do |p_ship_tour_truck|
          -i = 0.0
          %td
            %h3
              =p_ship_tour_truck.p_truck.name
            %p
              Chauffeur :
              -if p_ship_tour_truck.p_driver
                %strong
                  =p_ship_tour_truck.p_driver.name
            
            #ship_tour_truck_place.ship_tour_truck_place{:id => p_ship_tour_truck.id, :data => {:p_ship_tour_truck_id => p_ship_tour_truck.id}}
              =render p_ship_tour_truck.p_sheet_lines.order("position")
              
              
            .cuves
             
              %table{:style => "width:100%;"}
                
              
                -p_ship_tour_truck.p_ship_spaces.each do |p_ship_space|
                  %tr
                    %td{:style => "width:20%;"}
                      N°
                      = p_ship_space.p_truck_space.code
      
                    %td

                      = p_ship_space.p_truck_space.volume
                      -i += p_ship_space.p_truck_space.volume
                      L
                  
            -if p_ship_tour_truck.p_ship_tour_trucks.count > 0
              %br
              %p
                %strong Remorques : 
              - p_ship_tour_truck.p_ship_tour_trucks.each do |p_ship_tour_truck|
                %h4=p_ship_tour_truck.p_truck.name
                .cuves
                  
                  %table{:style => "width:100%;"}
                
              
                    -p_ship_tour_truck.p_ship_spaces.each do |p_ship_space|
                      %tr
                        %td{:style => "width:20%;"}
                          N°
                          = p_ship_space.p_truck_space.code
      
                        %td


                          = p_ship_space.p_truck_space.volume
                          -i += p_ship_space.p_truck_space.volume
                          L
            %br
            %p
              Volume total :
              %strong
                =i.to_i
            
            -if false
              .add_to_ship_tour.btn.btn-primary{:data => {:p_ship_tour_truck_id => p_ship_tour_truck.id}}
                Ajouter à ce camion pour cette tournée
              %br
            %br
              
-if !@p_ship_tour.state
  .qi_row
    .qi_pannel.qi_plain.padding
      =link_to "Mettre en livraison", to_ship_admin_p_ship_tour_path(@p_ship_tour), :data => {:confirm => "Voulez-vous vraiment mettre en livraison cette tournée ?"}, :class => "btn btn-primary"
  
  
:coffeescript
  @sheet_line_id = []
    
  
  @get_sheet_line_id = ->
    sheet_line_id = []
    $('.sheet_line .active').each ->
      sheet_line_id.push $(this).closest(".sheet_line").data("p-sheet-line-id")
    return sheet_line_id

  @update_truck_list = ->
    $('.ship_tour_truck_place').each ->
      ship_tour_truck_id = $(this).data("p-ship-tour-truck-id")
      ids = []
      $(this).find(".sheet_line").each ->
        ids.push $(this).data("p-sheet-line-id")
      
      
      $.ajax({url:"/admin/p_ship_tour_trucks/"+ship_tour_truck_id+"/update_ship_tour_truck_sheet_lines", type: "POST", data : { sheet_line_ids : ids }});


  $('.sheet_line .take').click ->
    $(this).toggleClass("active")
    get_sheet_line_id()
    return false
    
  $('.add_to_ship_tour').click ->
    ship_tour_truck_id = $(this).data("p-ship-tour-truck-id")
    sheet_line_ids = get_sheet_line_id()
    
    
    $('.sheet_line .take').removeClass("active")
    
    
    for value in sheet_line_ids
      $("#ship_tour_truck_place_"+ship_tour_truck_id).append $("#sheet_line_"+value)
   
    update_truck_list()

    return false
  
  $('.remove_to_ship_tour').click ->
    sheet_line_ids = get_sheet_line_id()
    
    $('.sheet_line .take').removeClass("active")
    
    for value in sheet_line_ids
      $(".sheet_line_to_ship").prepend $("#sheet_line_"+value)
   
    update_truck_list()

    return false
  
  
  $(".update_trucks").click -> 
    update_truck_list()
    return false
  

-if !@p_ship_tour.state
  :coffeescript
    $(".ship_tour_truck_place, .sheet_line_to_ship").sortable
      connectWith: ".ship_tour_truck_place, .sheet_line_to_ship"
      handle : ".take"
      stop : ->
        update_truck_list()

:scss
  .ship_tour_truck_place{
    border:2px solid black;
    padding:5px;
    margin-bottom:10px;
    min-height:30px;
  }

  .sheet_line_to_ship{
    margin:5px -5px;
    min-height:50px;
  }
  
  .sheet_line{
    float:left;
    max-width:250px;
    padding:10px;
    margin:5px;
    border:1px solid gray;
    position:relative;
    
    .take{
      cursor:pointer;
      
      position:absolute;
      top:3px;
      right:7px;
      
      font-size:1.5em;
      
      &.active{
       
      }
    }
    
  }
  .ship_tour_truck_place{
    
    .sheet_line{
      float:none;
    }
    
  }