-checkbox = false
.qi_header
  .right
    =link_to ic(:plus)+" Demande de commande", admin_p_customers_path(:offre => true), :class => "btn btn-primary btn-ap-add"
    
  %h1
    Tableaux de bord achats


.qi_search_row
  =form_tag "", :method => "get",  :onsubmit => "" do
    -params[:search] =params[:search] || {}

    %table
      %tr.form-inline
        %td
          Date saisie commande :
          Début
        %td.input-group
          =text_field_tag :cc_creation_date_start, params[:cc_creation_date_start],:class => "form-control datepicker", :placeholder => "Début"
          .input-group-append
            %span.input-group-text{:onclick => "$(this).prev('input').val('');"}
              =ic(:times)
        %td.pl-4
          Fin
        %td.input-group
          =text_field_tag :cc_creation_date_stop, params[:cc_creation_date_stop],:class => "form-control datepicker", :placeholder => "Fin"
          .input-group-append
            %span.input-group-text{:onclick => "$(this).prev('input').val('');"}
              =ic(:times)
        %td
          Statut :
          =select_tag "search[state]", options_for_select([["",""]]+PCustomerSheet.group(:state).all.map{|a| [a.state, a.state]}, params[:search][:state]), class: "custom-select"

        %td
          Marque :
          =select_tag "search[s_brand]", options_for_select([["",""],["Aucune","null"]]+SBrand.pluck(:name, :id), params[:search][:s_brand]), class: "custom-select"
        %td
          Code client :
          =text_field_tag "search[p_customer_code]", params[:search][:p_customer_code],:class => "form-control"

        %tr.form-inline
          %td{width: "110px"}

          %td.right.mx-4
            -if @cc_creation_date_start and @cc_creation_date_stop

              =link_to "< Mois précédent",request.query_parameters.merge({cc_creation_date_start: ((@cc_creation_date_start - 1.month).beginning_of_month.strftime('%d/%m/%Y')), cc_creation_date_stop: ((@cc_creation_date_stop - 1.month).end_of_month.strftime('%d/%m/%Y'))}), :class => "btn btn-tertiary"
          %td{width: "20px"}
          %td.mr-auto
            -if @cc_creation_date_start and @cc_creation_date_stop
              =link_to "Mois suivant >",request.query_parameters.merge({cc_creation_date_start: ((@cc_creation_date_start + 1.month).beginning_of_month.strftime('%d/%m/%Y')), cc_creation_date_stop: ((@cc_creation_date_stop + 1.month).end_of_month.strftime('%d/%m/%Y'))}), :class => "btn btn-tertiary"

        %tr.form-inline
          %td
            Date ETA :
            Début
          %td.input-group
            =text_field_tag :wish_date_start, params[:wish_date_start],:class => "form-control datepicker", :placeholder => "Début"
            .input-group-append
              %span.input-group-text{:onclick => "$(this).prev('input').val('');"}
                =ic(:times)
          %td.pl-4
            Fin
          %td.input-group
            =text_field_tag :wish_date_stop, params[:wish_date_stop],:class => "form-control datepicker", :placeholder => "Fin"
            .input-group-append
              %span.input-group-text{:onclick => "$(this).prev('input').val('');"}
                =ic(:times)
      %tr.form-inline
        %td{width: "110px"}

        %td.right.mx-4
          -if @wish_date_start and @wish_date_stop

            =link_to "< Mois précédent",request.query_parameters.merge({wish_date_start: ((@wish_date_start - 1.month).beginning_of_month.strftime('%d/%m/%Y')), wish_date_stop: ((@wish_date_stop - 1.month).end_of_month.strftime('%d/%m/%Y'))}), :class => "btn btn-tertiary"
        %td{width: "20px"}
        %td.mr-auto
          -if @wish_date_start and @wish_date_stop
            =link_to "Mois suivant >",request.query_parameters.merge({wish_date_start: ((@wish_date_start + 1.month).beginning_of_month.strftime('%d/%m/%Y')), wish_date_stop: ((@wish_date_stop + 1.month).end_of_month.strftime('%d/%m/%Y'))}), :class => "btn btn-tertiary"


    =link_to ic(:search)+" Rechercher", "#", :class => "btn btn-default btn-qi-search", :onclick => "$(this).closest('form').submit();$(this).html('...');return false;"


      
      
  
    =#render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @p_customer_sheets}

-nbr_td = 10

.qi_pannel.padding.new_table_container
  .table-slider
    .inner
  
  .table-area{:style => "overflow:auto;"}
    .table-area-inner

      =form_tag new_admin_price_document_path(), :method => "get" do
      
        =hidden_field_tag :"document_type", "Demande prix"
        =hidden_field_tag :"p_product_ref_ids[]", ""
        
        .freezeTable
          %table.table.table-striped.table-hover.table-bordered.data_table
            %thead.header
              %tr
                -if checkbox
                  %th
                %th.mask.plus-all
                  =link_to ic("plus-square-o"), '#'
                %th
                  Produit
                %th
                  Référence
                / %th Qté
                %th.mask.col-detail
                  N° Commande
                %th.mask.col-detail
                  Client
                %th.mask.col-detail
                  Statut
              
                %th.mask.col-detail Commentaire
                %th.mask.col-detail Date saisie
                %th.mask.col-detail Date validation
                %th.mask Date livraison (ETA)
              
                %th.mask
                  Qté nécessaire
                %th.mask
                  Qté en stock
                %th.mask
                  Qté souhaité
                  %span.info
                    =ic("info-circle")
                    %span.info-text-bottom BPA - Qté en stock
                %th.mask BPA
                %th.mask AV BPA
                %th.mask PAS BPA
                
              


            %tbody
              -p_product_id = nil
              -@price_lines.where.not(:p_product_ref_id => nil).group(:p_product_ref_id).order("p_product_id ASC").each do |plr|
              
                -if p_product_id != plr.p_product_id
                  -p_product_id = plr.p_product_id
                  %tr.prdct_tr
                    -nbr_td.times.each do
                      %td
                    %td.col-detail
                    %td.col-detail
                    %td.col-detail
                    %td.col-detail
                    %td.col-detail
                    %td.col-detail
                  
              
              
                -ppr_lines = @price_lines.where(:p_product_ref_id => plr.p_product_ref.id)
                %tr.ref_tr
                  - bpa_qte = ppr_lines.where(:cc_state => "BPA").sum(:qte).to_i
                  - qte_available = LineStock.where(p_product_ref_id: plr.p_product_ref.id).sum(:qte_available).to_i

                  -if checkbox
                    %td

                  %td.plus
                    =link_to ic("plus-square-o"), "#"
                  %td
                    =link_to plr.p_product_ref.p_product.name, [:admin, plr.p_product_ref]
                  %td
                    =link_to [:admin, plr.p_product_ref] do
                      =plr.p_product_ref.ct_sub_name 
                      =plr.p_product_ref.p_product_color.name if plr.p_product_ref.p_product_color
                
                  %td.col-detail


                  %td.mask.col-detail
                  / %td.mask
                  %td.mask.col-detail
                  %td.mask.col-detail
                  %td.mask.col-detail
                  %td.mask.col-detail
                  %td.mask
                  %td.mask
                    &#8721
                    =ppr_lines.sum(:qte).to_i
                    %br
                    %span.asap
                      =ic(:fire)
                      =ppr_lines.asap_triage(20).first
                    %br
                    %span.non-asap
                      =ic(:hourglass)
                      =ppr_lines.asap_triage(20).last
                  %td.mask
                    = qte_available
                  %td.mask
                    =check_box_tag :"p_product_ref_ids[]", plr.p_product_ref.id
                  
                    =number_field_tag "qte[#{plr.p_product_ref.id}]", '', class: "small-input", value: ([bpa_qte - qte_available, 0].max)

                  %td.mask
                    = bpa_qte
                  %td.mask
                    =ppr_lines.where(:cc_state => "AV BPA").sum(:qte).to_i
                  %td.mask
                    =ppr_lines.where(:cc_state => "PAS BPA").sum(:qte).to_i
                
                %tbody.detail
                  -ppr_lines.each do |price_line|
                    %tr
                      - if checkbox
                        %td
                          =check_box_tag :"price_line_ids[]", price_line.id
                      / %td
                      %td
                      %td
                      %td
                      %td.col-detail
                        =link_to [:admin, price_line.price_line_block.price_lineable] do
                          =price_line.price_line_block.price_lineable.id
                          =price_line.price_line_block.price_lineable.past_id



                      %td.col-detail
                        =link_to price_line.p_customer.show_name, [:admin, price_line.p_customer]

                      %td
                        =state_helper price_line.price_line_block.price_lineable.state

                      %td
                        =price_line.comment
                      %td
                        = l price_line.cc_creation_date, :format => :short_date

                      %td
                        = l price_line.cc_validation_date, :format => :short_date if price_line.cc_validation_date

                      %td
                        = l price_line.cc_wish_date, :format => "semaine %V (%Y)"

                      %td
                        =price_line.qte.to_i
                      %td

                      %td


                      %td
                        =price_line.qte.to_i if price_line.cc_state == "BPA"

                      %td
                        =price_line.qte.to_i if price_line.cc_state == "AV BPA"

                      %td
                        =price_line.qte.to_i if price_line.cc_state == "PAS BPA"

                -p_product_id = plr.p_product_id

              %tr
                -nbr_td.times.each do
                  %td
                %td.col-detail
                %td.col-detail
                %td.col-detail
                %td.col-detail
                %td.col-detail
                %td.col-detail

          =submit_tag "Créer une demande de prix", :class => "btn btn-primary"
              
              
                  
                  
                  
:scss
  .ref_tr{
    td{
      border-top:3px solid gray;
    }
  }
  
  .prdct_tr{
    td{
      border-bottom:3px solid red;
    }
  }

  .asap {
    color: red;
  }

  .non-asap {
    color: darkblue;
  }

  .small-input {
    max-width: 100px;
  }
                  
:javascript
  $('.detail').hide()
  $('.col-detail').hide()
  let state = 0
  $('.plus').click(function(event){
    if( event.currentTarget.className == "plus") {
      $(event.currentTarget).removeClass('plus').addClass('minus')
      state += 1
      $('.col-detail').show()
      $(event.currentTarget).parent().parent().next('tbody.detail').toggle();
    }else{
      $(event.currentTarget).removeClass('minus').addClass('plus')
      state -= 1
      $(event.currentTarget).parent().parent().next('tbody.detail').toggle();
    }
    $(event.currentTarget).find('i').toggleClass('fa fa-plus-square-o').toggleClass('fa fa-minus-square-o')
    if(state == 0){
      $('.col-detail').hide()
    }else{
      $('.col-detail').show()
    }
  });
  $('.plus-all').click(function(event){
    if( event.currentTarget.className == "mask plus-all") {
      state += $('.plus').length
      $(event.currentTarget).removeClass('plus-all').addClass('minus-all')
      $('tbody.detail').show();
      $('.plus').find('i').toggleClass('fa fa-plus-square-o').toggleClass('fa fa-minus-square-o')
      $('.plus').removeClass('plus').addClass('minus')
      $(event.currentTarget).find('i').removeClass('fa fa-plus-square-o').addClass('fa fa-minus-square-o')
    }else{
      state -= $('.minus').length
      $(event.currentTarget).removeClass('minus-all').addClass('plus-all')
      $('.minus').find('i').removeClass('fa fa-minus-square-o').addClass('fa fa-plus-square-o')
      $('.minus').removeClass('minus').addClass('plus')

      $('tbody.detail').hide();
      $(event.currentTarget).find('i').removeClass('fa fa-minus-square-o').addClass('fa fa-plus-square-o')
    }
    if(state == 0){
      $('.col-detail').hide()
    }else{
      $('.col-detail').show()
    }
  });

                  
                  
                  
