-content_for :header do
  .qi_header
    .right
      -if current_admin.has_permission?("products-c")
        = link_to ic(:plus)+' Ajouter un produit', new_admin_p_product_path(), :class => "btn btn-primary btn-ap-add", :remote => false
    %h1
      Achats
      %span
        Produits


%table
  -PProduct.where(:s_brand_id => nil).each do |p_product|

    -p_product.code
  

    - ref_p_product = PProduct.where("s_brand_id is not NULL").where("code LIKE ?", "%"+p_product.code.sub("6KG", "").sub("RSTO", "").sub("RST", "").to_s+"%").first
    
    -if ref_p_product
    
      
      
      
      -if p_product_ref = ref_p_product.p_product_refs.first
      
      
        -pprf = p_product.p_product_refs.first
      
        - pprf.uv = p_product_ref.uv if pprf and p_product_ref.uv
        - pprf.save
        
      - p_product.uv = ref_p_product.uv if ref_p_product.uv
      - p_product.s_brand_id = ref_p_product.s_brand_id if ref_p_product.s_brand_id
      - p_product.country = ref_p_product.country if ref_p_product.country
      - p_product.sub_country = ref_p_product.sub_country if ref_p_product.sub_country
      - p_product.domaine = ref_p_product.domaine if ref_p_product.domaine
      
      - p_product.save


.qi_search_row
  
  =form_tag "", :method => "get",  :onsubmit => "" do
    
    =hidden_field_tag :column, params[:column]
    =hidden_field_tag :direction, params[:direction]
    
    %table
      %tr
        %td
          Actif :
          =select_tag "search[enabled]", options_for_select([[""], "Oui", "Non"], params[:search][:enabled])
        
          
        %td=text_field_tag :code, params[:code],:class => "form-control", :placeholder => "Code"
        
        %td=text_field_tag :name, params[:name],:class => "form-control", :placeholder => "Nom"
       

    %table
      %tr
        %td
          Catégorie :
          =select_tag "search[p_product_cat_id]", options_for_select([["",""]]+PProductCat.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:p_product_cat_id])
        
        %td
          Catégorie client :
          =select_tag "search[p_customer_cat_id]", options_for_select([["",""]]+PCustomerCat.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:p_customer_cat_id])
        
        %td
          Marque :
          =select_tag "search[s_brand_id]", options_for_select([["",""]]+SBrand.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:s_brand_id])
        
          
          
    =link_to ic(:search)+" Rechercher", "#", :class => "btn btn-default btn-qi-search", :onclick => "$(this).closest('form').submit();$(this).html('...');return false;"
  
  
    .results_count
      ="(#{@p_products.total_count}) résultat(s)"
    
      ="-"
      =select_tag :per_page, options_for_select([50,100,200,500], params[:per_page])
      résultats par page





.qi_pannel.padding.new_table_container
  
  .table-area
    .table-area-inner
  
  
      .freezeTable
        %table.table.table-striped.table-hover.table-bordered.data_table
          %thead.header
            %tr
              %th=sort_link "p_products.code", "Code"
              %th=sort_link "p_products.name", "Nom"
        
              %th Cat. client
              %th=sort_link "p_product_cats.name", "Catégorie"
              %th=sort_link "s_brands.name", "Marque"
           
              %th=sort_link "p_product_refs.uv", "Format"
              %th=sort_link "p_product_refs.capacite", "PCB"
              %th=sort_link "p_products.price_calc", "Calc. prix"
           
              %th=sort_link "p_product_refs.ct_price_ht", "Prix HT"
              %th Prix HT [Kg]
              %th TVA Fr
              
              %th=sort_link "p_product_refs.weight", "Poids"
        
              %th=sort_link "p_products.country", "Pays"
              %th=sort_link "p_products.sub_country", "Region"
              %th=sort_link "p_products.domaine", "Domaine"
             
            

              %th



          %tbody#p_products_rows
            =render @p_products
  
  
      -if false
        =link_to ic(:"expand"), "#", :onclick => "$(this).closest('.table-area').addClass('large_table_area');$('.freezeTable').freezeTable({columnNum:2,fixedNavbar:'#qi_header_wrapper'});return false;", :class => "zoom"
  
        =link_to ic(:compress), "#", :onclick => "$(this).closest('.table-area').removeClass('large_table_area');return false;", :class => "unzoom"
  
  
  .qi_pagination
    = paginate @p_products
  
  
    
  -if true
    :javascript
      resize();
      $('.freezeTable').freezeTable({columnNum:2,fixedNavbar:'#qi_header_wrapper'});
  
  
  -if false
      
    :javascript
    
      $(".data_table").DataTable({
        info:false,
        scrollX:        true,
        scrollY: (($(window).height()-400)+"px"), 
        scrollCollapse: false,
        paging:         false,
        ordering:false,
        keys: true,
        fixedHeader: false,
        searching: false,
        fixedColumns:   {
            leftColumns: 2,
            rightColumns: 1
        },
        colReorder: true,
              
              
      
     
      });
    
  

      
    