<%= render partial: 'spree/shared/error_messages', locals: { target: @promotion } %>
<fieldset class="form-group no-border-bottom no-border-top">
  <div class="row">
    <div id="general_fields" class="col-9">
      <div class="row">
        <div class="col-12">
          <%= f.field_container :name do %>
            <%= f.label :name, class: 'required' %>
            <%= f.text_field :name, class: 'fullwidth', required: true %>
          <% end %>

          <%= f.field_container :customer_label do %>
            <%= f.label :customer_label, class: 'required' %>
            <%= f.text_field :customer_label, class: 'fullwidth', required: true %>
          <% end %>

          <%= f.field_container :description do %>
            <%= f.label :description %><br>
            <%= f.text_area :description, rows: 7, class: 'fullwidth' %>
            <span class="info">
              <%= t('spree.character_limit') %>
            </span>
          <% end %>

          <%= f.field_container :category do %>
            <%= f.label :promotion_category_id, SolidusPromotions::PromotionCategory.model_name.human %><br>
            <%=
              f.collection_select(:promotion_category_id, @promotion_categories, :id, :name, { include_blank: t('solidus_promotions.match_choices.none') },
              { class: 'custom-select fullwidth' })
            %>
          <% end %>
        </div>
      </div>
    </div>

    <div id="expiry_fields" class="col-3">
      <%= f.field_container :overall_usage_limit do %>
        <%= f.label :usage_limit %><br>
        <%= f.number_field :usage_limit, min: 0, class: 'fullwidth' %><br>
        <span class="info">
          <%= t('solidus_promotions.current_promotion_usage', count: @promotion.usage_count) %>
        </span>
      <% end %>

      <% if @promotion.persisted? %>
        <%= f.field_container :per_code_usage_limit do %>
          <%= f.label :per_code_usage_limit %><br>
          <%= f.number_field :per_code_usage_limit, min: 0, class: 'fullwidth' %><br>
        <% end %>
      <% end %>

      <div id="starts_at_field" class="form-group">
        <%= f.label :starts_at %>
        <%= f.field_hint :starts_at %>
        <%=
          f.text_field :starts_at,
          value: datepicker_field_value(@promotion.starts_at, with_time: true),
          placeholder: t(".starts_at_placeholder"),
          class: 'datepicker datepicker-from fullwidth',
          data: { :'enable-time' => true, :'default-hour' => 0 }
        %>
      </div>

      <div id="expires_at_field" class="form-group">
        <%= f.label :expires_at %>
        <%= f.field_hint :expires_at %>
        <%=
          f.text_field :expires_at,
          value: datepicker_field_value(@promotion.expires_at,
          with_time: true),
          placeholder: t(".expires_at_placeholder"),
          class: 'datepicker datepicker-to fullwidth',
          data: { :'enable-time' => true, :'default-hour' => 0 }
        %>
      </div>
      <div class="form-group">
        <%= f.field_container :lane do %>
          <%= f.label :lane %><br>
          <%=
            f.select(:lane, SolidusPromotions::Promotion.lane_options, {}, { class: 'custom-select fullwidth' })
          %>
        <% end %>
      </div>
    </div>
  </div>
</fieldset>

<fieldset class="form-group row no-border-bottom">
  <legend><%= t '.activation' %></legend>

  <div class="col-4">
    <%= f.field_container :apply_automatically do %>
      <%= f.label :apply_automatically do %>
        <%= f.check_box :apply_automatically, disabled: f.object.codes.any? || f.object.path.present? %>
        <%= SolidusPromotions::Promotion.human_attribute_name(:apply_automatically) %>
        <%= f.field_hint :promo_code_will_be_disabled %>
      <% end %>
    <% end %>
  </div>

  <% if f.object.new_record? || f.object.present? %>
    <div class="col-4">
      <%= f.field_container :path do %>
        <%= f.label :path %>
        <%= f.text_field :path, class: "fullwidth", disabled: f.object.apply_automatically || f.object.codes.present? %>
      <% end %>
    </div>
  <% end %>

    <div class="col-4">
      <% if f.object.new_record? %>
        <div id="promotion_single_code" class="form-group">
          <%= label_tag :single_code, SolidusPromotions::PromotionCode.model_name.human %>
          <%= text_field_tag :single_code, @promotion.codes.first.try!(:value), class: "fullwidth", disabled: f.object.apply_automatically || f.object.path.present? %>
        </div>
      <% else %>
        <div class="codes-present">
          <p>
            <%= t('.codes_present') %>
          </p>
        </div>
      <% end %>
    </div>
</fieldset>
