<% with_post_type ||= false %>

<% content_for :head do %>
  <%= render 'posts/markdown_script' %>
<% end %>

<div class="notice is-info">
  <p><strong>Posting Tips</strong></p>
  <div class="has-font-size-caption">
  <% guidance = @category.asking_guidance_override %>
  <%= raw(sanitize(render_markdown(guidance.present? ? guidance : SiteSetting['AskingGuidance']), scrubber: scrubber)) %>
  </div>
</div>

<% if @post.errors.any? %>
  <div class="notice is-danger is-filled">
    <p>The following errors prevented your post being saved:</p>
    <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

<%= render 'posts/image_upload' %>

<%= form_for @post, url: submit_path, html: { class: 'has-margin-top-4' } do |f| %>
  <%= f.hidden_field :category_id %>

  <% if with_post_type %>
    <div class="form-group">
      <%= f.label :post_type_id, 'Post type', class: 'form-element' %>
      <span class="form-caption">What kind of post is this? Questions can have answers; articles only have comments.</span>
      <% ids = @category.display_post_types.reject { |e| e.to_s.empty? } %>
      <% post_types = PostType.where(id: ids) %>
      <% opts = post_types.map { |pt| [pt.name, pt.id] } %>
      <%= f.select :post_type_id, options_for_select(opts, selected: @post.post_type_id),
                   { include_blank: true }, class: 'form-element' %>
    </div>
  <% else %>
    <%= f.hidden_field :post_type_id %>
  <% end %>

  <%= render 'shared/body_field', f: f, field_name: :body_markdown, field_label: 'Body', post: @post %>

  <div class="post-preview"></div>

  <div class="form-group">
    <%= f.label :title, 'Summarize your post with a title:', class: 'form-element' %>
    <%= f.text_field :title, class: 'form-element' %>
  </div>

  <div class="form-group">
    <%= f.label :tags_cache, 'Tags (at least one):', class: 'form-element' %>
    <% required_tags = @category.required_tags.to_a %>
    <% unless required_tags.empty? %>
      <span class="form-caption">
        Requires at least one of
        <% required_tags.each do |tag| %>
          <a class="badge is-tag is-filled js-add-required-tag" href="javascript:void(0)" data-tag-id="<%= tag.id %>"
             data-tag-name="<%= tag.name %>">
            <%= tag.name %>
          </a>
        <% end %>
      </span>
    <% end %>
    <%= f.select :tags_cache, options_for_select(@post.tags_cache.map { |t| [t, t] }, selected: @post.tags_cache),
                 { include_blank: true }, multiple: true, class: "form-element js-tag-select",
                 data: { tag_set: @category.tag_set_id } %>
  </div>

  <% unless @post.id.present? %>
    <div class="form-group">
      <%= f.label :license_id, 'License', class: 'form-element' %>
      <span class="form-caption">
        <% site_default = License.site_default %>
        <% category_default = @category.license %>
        <% if site_default.present? %>
          site default: <a href="javascript:void(0)" class="js-license-autofill" data-license-id="<%= site_default.id %>">
            <%= site_default.name %>
          </a>
        <% end %>
        <% if site_default.present? && category_default.present? %>
          &middot;
        <% end %>
        <% if category_default.present? %>
          category default: <a href="javascript:void(0)" class="js-license-autofill" data-license-id="<%= category_default.id %>">
            <%= category_default.name %>
          </a>
        <% end %>
      </span>
      <%= f.select :license_id, options_for_select(License.enabled.default_order(@category).map { |l| [l.name, l.id] },
                                                   selected: @post.license_id), {}, class: 'form-element' %>
    </div>
  <% end %>

  <div class="actions">
    <%= f.submit "Save Post in #{@category.name}", class: 'button is-filled' %>
    <%= link_to 'Cancel', category_path(@category), class: 'button is-muted is-outlined' %>
  </div>
<% end %>