- "QI ActsAsCaching V0.6"

-if true
  -masqued_columns = masqued_columns || nil

  -qi_ordered_table_name = qi_ordered_table_collection.klass.to_s.tableize

  -qi_ordered_table_model = qi_ordered_table_collection.klass

  -key = key || ""
  -inherit_key = key

  -big_key = qi_ordered_table_name+key.to_s


  =render :partial => "qi/qi_ordered_table_head", :locals => {:qi_ordered_table_name => qi_ordered_table_name, :qi_ordered_table_model => qi_ordered_table_model, :key => key}


  -@qi_table_orders = @qi_table_orders || {}

  -if current_admin
    -if ap = current_admin.admin_preferences.where(:key => "sort_table_"+big_key).first
      -qi_ordered_table_personalised = ap.value
    
    -if ap = current_admin.admin_preferences.where(:key => "sort_table_"+big_key+"_nbr_col").first
      -qi_ordered_table_nbr_col = ap.value
    -else
      -qi_ordered_table_nbr_col = 1

  

  -else
    -qi_ordered_table_personalised = session[:qi_sort_string][big_key]

    -qi_ordered_table_nbr_col = session[:qi_sort_string][big_key+"_nbr_col"] || 1



  -if masqued_columns
    -masqued_columns.each do |key|
      -@qi_table_orders[big_key.to_sym] = @qi_table_orders[big_key.to_sym].except(key)
    
  
  %span.qi_ordered_table_column{:onclick => "$(this).next('.qi_ordered_table_sort_select_wrapper').toggle();"}
    =ic :columns
  

  .qi_ordered_table_sort_select_wrapper{:style => "display:none;"}
    =form_tag request.url, :method => "get",  :onsubmit => "", :remote => false do
    
   
      =hidden_field_tag "qi_sort_string[class]",big_key 
  
      =hidden_field_tag "qi_sort_string[list]", "", :class => "qi_sort_string"
      .qi_ordered_table_sort_select
        -my_keys = []
        -@qi_table_orders[big_key.to_sym].each do |key, value|
          .qi_order_item{:"data-ref" => "#{key}", :class => "active", :onclick => "$(this).toggleClass('active');"}
            -if value.instance_of? Hash
              =value[:name]
            -else
              =value
            -my_keys << key
      
    
        -qi_ordered_table_collection.klass.qi_table_order.each do |key, value|
          -if !my_keys.include?(key) and (!masqued_columns or !masqued_columns.include?(key))
            .qi_order_item{:"data-ref" => "#{key}", :onclick => "$(this).toggleClass('active');"}
              -if value.instance_of? Hash
                =value[:name]
              -else
                =value
      %hr{:style => "margin:3px 0;"}
      -if true
        Colonnes fixes :
        =select_tag "qi_sort_string[nbr_colonnes]", options_for_select([0,1,2,3], qi_ordered_table_nbr_col)
    
    
      =link_to "Mettre à jour", "#", :class => "btn btn-primary update_table_order_link"
      

  :scss
    .qi_ordered_table_column{
      position:absolute;
      color:rgba(0,0,0,0.2);
      font-size:1.2em;
      padding-top:3px;
      padding-left:10px;
      cursor:pointer;
    
      &:hover{
        color:rgba(0,0,0,0.6);
      }
    
    }

    .qi_ordered_table_sort_select_wrapper{
      background:rgba(white, 0.98);
      box-shadow:0 0 10px rgba(0,0,0,0.2);
      width:auto;
      position:absolute;
      margin-left:15px;
      margin-top:25px;
      z-index:5;
      padding:5px 10px;
      .qi_order_item{
        padding:2px 0;
        cursor:pointer;
      
      }
      .active{
        font-weight:bold;
      }
    }
  
  :coffeescript
    $(".update_table_order_link").click ->
      r = []
      $(this).closest('.qi_ordered_table_sort_select_wrapper').find(".qi_order_item").each ->
        if $(this).hasClass("active")
          r.push $(this).data("ref")
    
    
      $(this).closest("form").find(".qi_sort_string").val(r.join(','))
      $.ajax
        type: "GET",
        url: "/",
        data: $(this).closest("form").serialize(),
        success: ->
          location.reload()
        
    
    
    
    
    

    $( ".qi_ordered_table_sort_select" ).sortable
      axis: "y"
    




  .qi_pannel.padding.new_table_container
    .table-slider
      .inner
        <form class="range-field">
        <input type="range" min="0" max="100" step="0.1" class="range" value="0" />
        </form>
    .table-area
      .table-area-inner
  
  
        .freezeTable
          %table.table.table-striped.table-hover.table-bordered.data_table
            %thead.header
              %tr
                -@qi_table_orders[big_key.to_sym].each do |key, value|
                  -if value.instance_of? Hash
                    -if value[:reorder]
                      -sort_name = value[:sort_name] ? value[:sort_name] : key
                  
                      %th=raw sort_link sort_name.to_s, value[:name]
                    -else
                      %th=raw value[:name]
                    
                  -else
                    %th=raw value
            
            
                =#sort_link "code", "Code"
              
              
                -if false
                  %th
  

            %tbody{:id => "#{qi_ordered_table_name}_rows"}
              -qi_ordered_table_collection.each do |ob|
                -@inherit_key =  inherit_key
                =render :partial => "admin/#{qi_ordered_table_name}/#{qi_ordered_table_name.singularize}", :locals => {:inherit_key => "inherit_key", :"#{qi_ordered_table_name.singularize}" => ob }
      
      
      .qi_pagination
        = paginate qi_ordered_table_collection #, :remote => true

  :scss

    .freezeTable{
      overflow:auto;
    }

    .table-slider{
      position:fixed;
      bottom:0;
      width:200px;
      right:0;
      z-index:2;
      background:rgba(black,0.3);
      padding:10px 20px;
      padding-left:20px;
    
      border-radius:40px 0  0 0;
      opacity:0.4;
      &:hover{
        opacity:1;
        background:rgba(black,0.7);
        box-shadow:0 0 5px rgba(black,0.2);
      }
    }
  
  



  -if qi_ordered_table_nbr_col == 0.to_s
    :javascript
      resize();
      $('.freezeTable').freezeTable({fixedNavbar:'#qi_header_wrapper', freezeColumn: false});

  -else
    :javascript
      resize();
      $('.freezeTable').freezeTable({columnNum:#{qi_ordered_table_nbr_col},fixedNavbar:'#qi_header_wrapper'});

  
  

  :coffeescript
    #mousemove
  

  
    lastScrollLeft = 0
    $(".freezeTable").scroll ->
      #alert("t")
      documentScrollLeft = $(this).scrollLeft()
    
      if (lastScrollLeft != documentScrollLeft)
      
        lastScrollLeft = documentScrollLeft
      
        size = ($(this).find(".table").outerWidth() - $(this).outerWidth())
    
        value = ( $(this).scrollLeft() / size)*100 
      
      
        $(this).closest(".new_table_container").find(".range").val(value)
 
 
         
         
         
  
    $(".freezeTable").each ->
      freeze = $(this)
    
    
    
      $("document").on "scroll", ->
        if ($(this).scrollTop() < freeze.offset[0] + freeze.outerHeight())
          freeze.closest(".new_table_container").find(".table-slider").show()
        
        else
          freeze.closest(".new_table_container").find(".table-slider").hide()
  
  
    $(".range").on 'input change mousemove', ->
    
      freeze = $(this).closest(".new_table_container").find(".freezeTable")
    
      size = (freeze.find(".table").outerWidth() - freeze.outerWidth())
    
      value = size*($(this).val() / 100)

      freeze.scrollLeft(value);

    



  =#1

-else
  %p Table Order non initialisé
  
  =qi_ordered_table_name
  %br
  %br
  
  def self.human
  %br
  &nbsp;&nbsp;""
  %br
  end
  
  %br
  %br
  
  acts_as_sorting :fields => {
  %br
  -qi_ordered_table_model.columns.each do |c|
    -if c.name != "created_at" and c.name != "updated_at"
      &nbsp;&nbsp;
      ="  :#{c.name} => {:name => \"#{c.name.capitalize}\", :reorder => true},"
      %br
  &nbsp;&nbsp;
  ='  :actions => {:name => "Actions", :reorder => true} '
  %br
  
  }
  