= render partial: 'spree/admin/shared/product_sub_menu'

= render partial: 'spree/admin/shared/product_tabs', locals: { current: 'Product Properties' }

= render partial: 'spree/shared/error_messages', locals: { target: @product }

- content_for :page_actions do
  %ul.tollbar.inline-menu
    %li
      = link_to_add_fields t('.add_product_properties'), 'tbody#product_properties', class: 'icon-plus button'

= form_for @product, url: admin_product_url(@product, @url_filters), method: :put do |f|
  %fieldset.no-border-top
    .add_product_properties
    = image_pack_tag 'select2-spinner.gif', plugin: 'spree', style: 'display:none;', id: 'busy_indicator'

    %table.index.sortable{"data-sortable-link" => update_positions_admin_product_product_properties_url}
      %thead
        %tr
          %th.no-border
          %th= t('admin.products.properties.property_name')
          %th= t('admin.description')
          %th.actions

      %tbody#product_properties
        = f.fields_for :product_properties do |pp_form|
          = render partial: 'product_property_fields', locals: { f: pp_form }

    = f.check_box :inherits_properties
    = f.label :inherits_properties, t('.inherits_properties_checkbox_hint', supplier: @product.supplier.name)
    %br
    %br

    #inherited_properties
      %table.index
        %thead
          %tr
            %th= t('admin.products.properties.inherited_property')
            %th= t('admin.description')
            %th.actions
        %tbody#producer_properties
          - @product.supplier.producer_properties.each do |producer_property|
            %tr
              %td= producer_property.property.presentation
              %td= producer_property.value
              %td.actions


    .form-buttons.filter-actions.actions
      = button t('spree.actions.update'), 'icon-refresh'
      = link_to t('spree.actions.cancel'), admin_product_product_properties_url(@product, @url_filters), id: 'cancel_link', class: 'button icon-remove'

    = hidden_field_tag 'clear_product_properties', 'true'

:coffee
  $(document).ready ->
    $("#inherited_properties").toggle $("input#product_inherits_properties").is(':checked')
  $("input#product_inherits_properties").change ->
    $("#inherited_properties").toggle $(this).is(':checked')

:javascript
  var properties = #{raw(@properties.to_json)};

  $("#product_properties").on("keydown", "input.autocomplete", function(){
    var already_auto_completed = $(this).is('ac_input');
    if (!already_auto_completed) {
      $(this).autocomplete({source: properties});
      $(this).focus();
    }
  });
