<% is_edit ||= false %>

<%= render 'posts/markdown_script' %>

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

<%= render 'posts/image_upload' %>

<%= form_for @question, url: is_edit ? edit_question_path(@question) : create_question_path do |f| %>
  <div class="form-group">
    <%= f.label :title, "Title your question:", class: "form-element" %>
    <%= f.text_field :title, class: "form-element" %>
  </div>

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

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

  <div class="form-group">
    <%= f.label :tags_cache, "Tags", class: "form-element" %>
    <div class="form-caption">
      Tags help to categorize questions. Separate them by space. Use hyphens for multiple-word tags.
    </div>
    <%= f.select :tags_cache, options_for_select(@question.tags_cache.map { |t| [t, t] }, selected: @question.tags_cache),
                 { include_blank: true }, multiple: true, class: "form-element js-tag-select",
                 data: { tag_set: @question.category.tag_set.id } %>
  </div>

  <% if is_edit %>
    <div class="form-group">
      <%= label_tag :edit_comment, 'Edit comment', class: "form-element" %>
      <div class="form-caption">
        Describe&mdash;if necessary&mdash;what you are changing and why you are making this edit.
      </div>
      <%= text_field_tag :edit_comment, params[:edit_comment], class: 'form-element' %>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.submit is_edit ? (check_your_post_privilege(@question, 'Edit') ? "Save changes" : "Suggest changes") : 'Ask Question', class: "button is-filled" %>
    <%= link_to 'Cancel', is_edit ? question_path(@question) : category_path(@question.category), class: 'button is-outlined is-muted' %>
  </div>
<% end %>
