<% title "Edit Listing" %>

<div class="classifieds-container classifieds-form-container">
  <%= form_for(@classified_listing) do |f| %>
    <header>
      <a href="/listings/dashboard" class="listings-back-button">&lt; return to listings dashboard</a>
      <h2>You can only edit title/body/tags of drafts or within the first 24 hours of listing or bumping</h2>
    </header>
    <div class="classified-form-inner">
      <%= render partial: "form_errors", locals: { classified_listing: @classified_listing } %>

      <% if (@classified_listing.bumped_at && @classified_listing.bumped_at > 24.hours.ago) || @classified_listing.updated_at && !@classified_listing.published %>
        <div class="field">
          <%= f.label "title", "Title" %>
          <%= f.text_field "title", maxlength: 128, placeholder: "128 characters max, plain text" %>
        </div>
        <div class="field">
          <%= f.label "body_markdown", "Body Markdown" %>
          <%= f.text_area "body_markdown", maxlength: 400, placeholder: "400 characters max, 12 line break max, no images allowed" %>
        </div>
        <div class="field">
          <%= f.label "tag_list", "Tags" %>
          <%= f.text_field "tag_list", value: @classified_listing.cached_tag_list, placeholder: "8 tags max, comma separated, no spaces or special characters" %>
        </div>
        <div class="field">
          <%= f.label "location", "Location" %>
          <%= f.text_field "location", maxlength: 32, placeholder: "32 characters max, plain text" %>
        </div>
        <div class="field">
          <%= f.label "expires_at", "Custom Expire Date" %>
          <%= f.date_field "expires_at", min: Date.tomorrow, max: @classified_listing.natural_expiration_date %>
        </div>
        <div class="field">
          <%= f.label "contact_via_connect", "Allow Users to Message Me Via In-App Chat (#{community_name} Connect)" %>
          <%= f.check_box "contact_via_connect" %>
        </div>
        <% if @classified_listing.published %>
          <%= f.submit "Update Listings Info", class: "cta cta-main-listing-form" %>
        <% else %>
          <%= f.submit "Update Draft", class: "cta cta-main-listing-form cta-draft" %>
        <% end %>
      <% end %>
    </div>
  <% end %>
  <% if @classified_listing.bumped_at %>
    <%= form_for(@classified_listing) do |f| %>
      <header>
        <a href="<%= @classified_listing.path %>" class="listings-back-button" style="font-size: 1.1em;">Last Published/Bumped: <%= time_ago_in_words @classified_listing.bumped_at %> ago</a>
        <h2>You can bump your listing for the same price as the original listing</h2>
      </header>
      <div class="classified-form-inner">
        <% if (@classified_listing.natural_expiration_date) < Date.today %>
          <h4>
            (Expired <%= time_ago_in_words @classified_listing.natural_expiration_date %> ago)
          </h4>
        <% else %>
          <h4>
            (Expires in <%= time_ago_in_words @classified_listing.natural_expiration_date %>)
          </h4>
        <% end %>
        <input type="hidden" name="classified_listing[action]" value="bump" />
        <%= f.submit "Bump Listing", class: "cta cta-main-listing-form" %>
      </div>
    <% end %>
  <% end %>
  <%= form_for(@classified_listing) do |f| %>
    <div class="classified-form-inner">
      <% unless @classified_listing.published %>
        <h2 style="text-align: center; margin-top: 15px;">
          This listing is not published
        </h2>
      <% end %>
      <% if @classified_listing.published == false %>
        <input type="hidden" name="classified_listing[action]" value="publish" />
        <%= f.submit "Publish Listing", class: "cta classified-listings-publish cta-main-listing-form" %>
      <% else %>
        <input type="hidden" name="classified_listing[action]" value="unpublish" />
        <%= f.submit "Unpublish Listing", class: "cta classified-listings-unpublish cta-main-listing-form" %>
      <% end %>
    </div>
  <% end %>
</div>
