<% allow_return_item_changes = !@return_authorization.customer_returned_items? %>

<div data-hook="admin_return_authorization_form_fields">
  <table class="index return-items-table selectable-table">
    <thead>
      <tr data-hook="rma_header">
        <th>
        </th>
        <th class="return-item-product"><%= Spree::Product.model_name.human %></th>
        <th class="return-item-state"><%= Spree::ReturnAuthorization.human_attribute_name(:state) %></th>
        <th class="return-item-charged"><%= Spree::ReturnItem.human_attribute_name(:charged) %></th>
        <th class="return-item-pre-tax-refund-amount wrap-text"><%= Spree::ReturnItem.human_attribute_name(:amount) %></th>
        <th class="return-item-reimbursement-type wrap-text"><%= Spree::ReimbursementType.model_name.human %></th>
        <th class="return-item-exchange-for"><%= Spree::ReturnItem.human_attribute_name(:exchange_variant) %></th>
        <th class="return-item-reason"><%= Spree::ReturnItem.human_attribute_name(:return_reason) %></th>
      </tr>
    </thead>
    <tbody>
      <%= f.fields_for :return_items, @form_return_items do |item_fields| %>
        <% return_item = item_fields.object %>
        <% inventory_unit = return_item.inventory_unit %>
        <% editable = inventory_unit.shipped? && !inventory_unit.exchange_requested? && allow_return_item_changes && return_item.reimbursement.nil? %>
        <tr>
          <td class="return-item-checkbox inventory-unit-checkbox">
            <% if editable %>
              <%= item_fields.hidden_field :inventory_unit_id %>
              <%= item_fields.check_box :_destroy, {checked: return_item.persisted?, class: 'selectable add-item', "data-price" => return_item.amount}, '0', '1' %>
            <% end %>
          </td>
          <td class="return-item-product">
            <div class="variant-name"><%= inventory_unit.variant.name %></div>
            <div class="variant-options"><%= inventory_unit.variant.options_text %></div>
          </td>
          <td class="return-item-state">
            <span class="pill pill-<%= inventory_unit.state %>">
              <%= t(inventory_unit.state, scope: 'spree.inventory_states') %>
            </span>
          </td>
          <td class="return-item-charged">
            <%= return_item.display_amount %>
          </td>
          <td class="return-item-pre-tax-refund-amount">
            <% if editable %>
              <%= item_fields.text_field :amount, { class: 'refund-amount-input' } %>
            <% else %>
              <%= return_item.display_amount %>
            <% end %>
          </td>
          <td class="return-item-reimbursement-type">
            <% if editable %>
              <%= item_fields.select :preferred_reimbursement_type_id, @reimbursement_types.collect{|r|[r.name.humanize, r.id]}, {include_blank: true}, {class: 'custom-select fullwidth'} %>
            <% else %>
              <%= return_item.preferred_reimbursement_type.try(:name) %>
            <% end %>
          </td>
          <td class="return-item-exchange-for">
            <% if editable %>
              <%= item_fields.collection_select :exchange_variant_id, return_item.eligible_exchange_variants(@stock_locations), :id, :exchange_name, { include_blank: true }, { class: "custom-select fullwidth return-item-exchange-selection" } %>
            <% elsif return_item.exchange_processed? %>
              <%= return_item.exchange_variant.exchange_name %>
            <% end %>
          </td>
          <td class="return-item-reason">
            <% if editable %>
              <%= item_fields.select :return_reason_id, @reasons.collect{|r|[r.name, r.id]}, {include_blank: true}, {class: 'custom-select fullwidth'} %>
            <% else %>
              <%= return_item.return_reason.try(:name) %>
            <% end %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>

  <%= f.field_container :stock_location do %>
    <%= f.label :stock_location_id, Spree::StockLocation.model_name.human %>
    <%= f.select :stock_location_id, @stock_locations.to_a.collect{|l|[l.name, l.id]}, {include_blank: true}, {class: 'custom-select fullwidth', "data-placeholder" => t('spree.select_a_stock_location')} %>
    <%= f.error_message_on :stock_location_id %>
  <% end %>

  <%= f.field_container :memo do %>
    <%= f.label :memo %>
    <%= f.text_area :memo, {style: 'height:100px;', class: 'fullwidth'} %>
    <%= f.error_message_on :memo %>
  <% end %>
</div>
