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

%div{:style => "margin-right:210px;"}
  -if !@p_ship_tour.state
    .qi_row
      .qi_pannel.qi_plain.padding
        %h3 Produits à livrer
        %table.ship_sheet_table.table.table-striped.table-hover{:style => "min-height:40px;"}
          %thead
            %tr
              
              %th=sort_link "p_products.name", "Produit"
              %th=sort_link "qte", "Volume"
              %th=sort_link "particulars.organisation, particulars.name, particulars.firstname", "Client"
              %th=sort_link "particulars.cp", "Code postal"
              %th=sort_link "particulars.city", "Ville"
              %th
          %tbody.sheet_line_to_ship
            %tr
              %td{:style => "height:0.2px;padding:0 !important;background:transparent !important;", :colspan => 6}
            -p_sheet_lines = PSheetLine.joins(:p_customer_sheet).joins(:particular_send).joins(:p_product).joins(:p_customer).joins(:p_product).where(:p_products => {:p_product_cat_id =>[6]}, :p_customer_sheets => {:state => ["commande", "livraison-en-cours"]}, :externe => false, :lock => nil).where("p_customer_sheets.wish_start <= ? and p_customer_sheets.wish_stop >= ?", (@p_ship_tour.start_at+5.hour), @p_ship_tour.start_at)
            -p_sheet_lines = sort_by_sorting(p_sheet_lines)
            -p_sheet_lines.each do |p_sheet_line|
              -if !@p_ship_tour.p_sheet_line_ids.include?(p_sheet_line.id)
                =render p_sheet_line
        :scss
          .ship_sheet_table{
            td,th,.sheet_line{
              padding:3px !important;
              font-size:0.95em;
              vertical-align:middle !important;
              .take{
                font-size:1em !important;
              }
            }
            
          }
        .clear
        -if false
          .remove_to_ship_tour.btn.btn-primary
            Retirer de la tournée
  
  .qi_row
    .qi_pannel.qi_plain.padding
      .right
        -if @p_ship_tour.state == "en-cours"
          =link_to "Fiches ADR", adr_admin_p_ship_tour_path(@p_ship_tour), :class => "btn"
          =link_to "Saisir le retour de livraison", ship_return_admin_p_ship_tour_path(@p_ship_tour), :class => "btn btn-primary"
  
          
      
        -if @p_ship_tour.state == "livrée"
          =link_to "Fiches ADR", adr_admin_p_ship_tour_path(@p_ship_tour), :class => "btn"
          =link_to "Annuler le retour de livraison", unship_return_admin_p_ship_tour_path(@p_ship_tour), :class => "btn btn-danger", :data => {:confirm => "Voulez-vous vraiment annuler le retour de livraison ?\n ATTENTION cela peut engendrer des problèmes dans les données enregistrées sur la base."}
          =link_to ic(:eye)+" Détail du retour de livraison", ship_return_admin_p_ship_tour_path(@p_ship_tour), :class => "btn btn-primary"
          
        
      -if !@p_ship_tour.state


        =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"
      -elsif @p_ship_tour.state == "en-cours"

        =link_to "Annuler la livraison", to_unship_admin_p_ship_tour_path(@p_ship_tour), :data => {:confirm => "Voulez-vous vraiment annuler la livraison de cette tournée ?"}, :class => "btn btn-danger"
        
        
        
      .clear
  
  
  .qi_row
    %div{:style => "position:absolute;top:110px;right:10px;bottom:0;width:200px;"}
  
      -if false
        .update_trucks.btn.btn-primary
          Mettre à jour

      -@p_ship_tour.p_ship_tour_trucks.where(:remorque => false).each do |p_ship_tour_truck|
        -i = 0.0
    
      
        %div.ship_tour_truck_container
          .close{:onclick => "$(this).closest('.ship_tour_truck_container').removeClass('active');return false;"}
            =ic :close
          
          .truck_header
            .right
              =link_to "Modifier le chauffeur ou le tracteur (remorques)", edit_admin_p_ship_tour_truck_path(p_ship_tour_truck), :class => "btn btn-primary"
            
            %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
          
        
          %div.truck_label{:style => ""}
            %p
              =p_ship_tour_truck.p_truck.name
              %br
              -if p_ship_tour_truck.p_driver
                %strong
                  =p_ship_tour_truck.p_driver.name
            %p.result
            %p
              =link_to "Voir le détail", "#", :onclick => "show_truck_detail($(this));return false;"
          
          %table.table.table-striped.table-hover{:style => "min-height:120px;margin:0;"}

            %tbody#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}}
              %tr
                %td{:style => "height:0.2px;padding:0;", :colspan => 6}
              -@p_ship_tour_truck = p_ship_tour_truck
              =render p_ship_tour_truck.p_sheet_lines.order("position")
        
          .truck_footer.cuves
            %br
            %br
            %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
  
  :coffeescript
    @update_lines_count = ()->
      $(".ship_tour_truck_container").each ->
        tot = 0.0
        nbr = 0
      
        sheet_lines = $(this).find(".sheet_line").each ->
          tot += parseFloat($(this).find(".qte strong").html())
          nbr += 1
        
        $(this).find(".result").html("Nbr com. : <strong>"+nbr+"</strong>&nbsp;&nbsp; Qte : <strong>"+(Math.round(tot*100)/ 100)+"</strong>")
    update_lines_count()
       

        
        
              




  .qi_row
    .qi_pannel.qi_plain.padding  
      .right
        =link_to "Télécharger tous les BL", multiple_admin_p_documents_path(:ids => @p_ship_tour.p_documents.map { |p| p.token }, :p_ship_tour_id => @p_ship_tour.id), :class => "btn btn-primary", :target => "_blank"
      
      %h3 Documents
    
    
      %table.table
        %tr
          %th Date
          %th Offre
          %th Client
          %th Type
          %th Numéro
          %th Total HT
          %th Total TTC
          %th
  

        %tbody#p_customer_sheets_rows
          =render @p_ship_tour.p_documents#.order("created_at DESC")


  
  .qi_row
    .qi_pannel.qi_plain.padding  
      .right
        -if @p_ship_tour.state == "livrée"
          =link_to ic(:file)+" Générer les factures pour les commandes livrées liées à la tournée", generate_bills_admin_p_ship_tour_path(@p_ship_tour), :class => "btn btn-primary bgbd-documents"
      .clear
      %br
      %table.table
        =render @p_ship_tour.p_customer_sheets.where(:state => "livrée")
      
      -if @p_ship_tour.p_customer_sheets.where("state != 'livrée'").count > 0
        %p Commandes avec un statut non livré :
        %table.table
          =render @p_ship_tour.p_customer_sheets.where("state != 'livrée' and state != 'facturée'")
      
  .qi_row
    .qi_pannel.qi_plain.padding
      =form_tag multiple_admin_p_documents_path, :method => :get do
        .right=submit_tag "Télécharger les factures", :class => "btn btn-primary"
        %h3 Factures
        -if @p_ship_tour.all_p_documents.order("created_at DESC").first and @p_ship_tour.all_p_documents.order("created_at DESC").first.created_at != @p_ship_tour.start_at
          -@p_ship_tour.all_p_documents.where(:label => "Facture").all.each do |pd|
            -pd.created_at = @p_ship_tour.start_at
            -pd.save
        
        %p
          =link_to "Tout sélectionner", "#", :onclick => "$(this).closest('.qi_pannel').find('input:checkbox').prop('checked', true);return false"
          =link_to "Tout désélectionner", "#", :onclick => "$(this).closest('.qi_pannel').find('input:checkbox').prop('checked', false);return false"
         
        
        -if @p_ship_tour.all_p_documents.where(:label => "Facture").count > 0

          -@case_multiple = true
      
          
          .clear
        
          %table.table
            =render @p_ship_tour.all_p_documents.where(:label => "Facture").uniq
      
      =#%table=render @p_ship_tour.all_p_documents
      
      

: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 }});
    
    update_lines_count();


  $('.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
    
    
  @show_truck_detail = (element) ->
    element.closest(".ship_tour_truck_container").addClass("active")
    
  

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

:scss

  .ship_tour_truck_container{
    border:2px solid black;
    position:relative;
    margin-bottom:10px;
    
    .truck_header{
      display:none;
      
    }
    
    .truck_label{
      position:absolute;
      top:10px;
      left:0;
      right:0;
      font-size:14px;
      text-align:center;
      
      
    }
    
    .close{
      position:absolute;
      top:10px;
      right:10px;
      font-size:18px;
      display:none;
      
    }
    
    .truck_footer{
      display:none;
    }
    
    
    &.active{
      .close{
        display:block;
        color:black;
        cursor:pointer;
      }
      
      position:fixed;
      z-index:5;
      top:300px;
      right:226px;
      left:114px;
      bottom:0px;
      margin-bottom:0;
      background:white;
      box-shadow:0 0 10px rgba(0,0,0,0.8);
      border:0;
      padding:10px;
      overflow:auto;
      
      .truck_header{
        display:block;
      }
      
      .truck_footer{
        display:block;
      }
     
      .ship_tour_truck_place{

        background:white;
        .sheet_line{
          display: table-row;
        }
    
      }
      
      
      .truck_label{
        display:none;
        
      }
      
      
      
    }
    
  }

  .ship_tour_truck_place{
    
   
  }

  .sheet_line_to_ship{

  }
  
  .sheet_line{
    .take{
      cursor:pointer;
    
      
      font-size:1.5em;
      
      &.active{
       
      }
    }
    
  }
  .ship_tour_truck_place{
    
    
    
    
    background:white;
    .sheet_line{
      display:none;
    }
    
    
    
  }