#p_payment_form_wrapper
  
  .content
    =semantic_form_for [:admin, @p_payment], :remote => true, :html => {:id => "p_payment_general_form", :onsubmit => ""} do |f|
      -@p_customer = @p_payment.p_customer if  @p_payment.p_customer
    
      :javascript
        $("#p_payment_general_form").submit(function(e){
         
                        e.preventDefault(e);
                        
          
          $this = $(this);
          if ($this.data('isSubmitted') == 'true') {
            return false; 
          }else{
            $this.data('isSubmitted','true');$('.p_payment_submit').val('Sauvegarde en cours...');
          
          };
        
        });
    
      =f.inputs do
    
    
        =hidden_field_tag :last_p_customer_id, @p_payment.p_customer_id
      
      
        %label Client :
        %input.p_customer_autocomplete_input.form-control{:type => "text", :value => ("#{f.object.p_customer.show_name}" if f.object.p_customer), :readonly => (true if f.object.compta_locked)}
     
    
        = f.hidden_field :p_customer_id, :class => "p_customer_id"
      
        :javascript
          $( function() {
            
            

              $('.p_customer_autocomplete_input').autocomplete({
                 source: function( request, response ) {
                         $.ajax( {
                         
                         
                           url: "/admin/p_customers/autocomplete.json",
                           dataType: "json",
                           data: {
                             search: request.term,
                          
                           },
                           success: function(data){
                         
                             arr = jQuery.map( data, function( item ) {
                                                 return {
                                                     label: item.show_name,
                                                     value: item.show_name,
                                                     id: item.id
                                                 }
                             });
                           
                             response(arr)
                 
                           }
                         
                         } );
                       },
                 minLength: 2,
                 select: function( event, ui ) {
         
                   $(this).next(".p_customer_id").val(ui.item.id)
                 
                   var change_address = false;
          
                   if (parseInt($("#last_p_customer_id").val()) != parseInt(ui.item.id)){
                     change_address = true;
          
                   }
                 
                   $.ajax({
                     url:"/admin/p_customers/"+ui.item.id+"/autocomplete_apercu.js", 
                     type : "GET",
                     data : {  change_address : change_address}
                 
                   });
                   
                   $.ajax({
                     url:"/admin/price_documents/search_to_affect?p_customer_id="+ui.item.id+".js", 
                     type : "GET"
                     //data : {  change_address : change_address}
                 
                   });
          
                   $("#last_p_customer_id").val(ui.item.id);
                 
                 
                   //return false
                 }
              });
            
            
            
            } );
      
      
    
      
        %br
    
      =render :partial => "admin/p_payments/essentials_form", :locals => {:form => f}
    
     
      #lettrage_panel
        =f.inputs do  
      
          %hr
      
          Lettrage :
          #lettrage_table
            %table.table
              %thead
                %tr
                  %th{:style => "width:30px"}
                  %th{:style => "width:100px"} Date
                  =#%th{:style => "width:90px"} Offre
                  %th Numéro de doc.
                  %th.numeraire{:style => "width:90px"} Montant TTC
                  %th.numeraire{:style => "width:90px"} Solde TTC
                  %th.numeraire{:style => "width:30px"} Montant à lettrer
                  %th{:style => "width:10px"}
              
              %tbody.p_payment_documents_form
        
                =f.semantic_fields_for :p_payment_documents do |form|
                  =render :partial => "admin/p_payment_documents/form", :locals => {:form => form}
      
          :scss
            #lettrage_table{
              font-size:14px;
              td,th{
                padding:2px 4px;
                vertical-align:middle;
                .form-group{
                  margin:0;
                }
              }
            }
    %p Pour sélectionner un document avec lequel lettrer ce paiement, cliquer sur le + dans la liste des paiements ci-dessous :
    =render :partial => "admin/price_documents/search_to_affect"
      
    

      
  .actions=submit_tag "sauvegarder", :class => "btn btn-primary p_payment_submit btn-ap-add", :onclick => "$(this).closest('#p_payment_form_wrapper').find('#p_payment_general_form').submit();"
  