<% is_question = post.post_type_id == Question.post_type_id %>
<% is_top_level = post.parent_id.nil? %>
<% has_tags = is_top_level && !post.tag_ids.empty? %>

<div class="post <%= post.meta? ? 'is-meta' : '' %> <%= is_top_level ? '' : 'has-border-bottom-style-solid has-border-bottom-width-1 has-border-color-tertiary-100' %>" data-post-id="<%= post.id %>" id="<%= (is_question ? 'question-' : 'answer-') + post.id.to_s %>" data-ckb-list-item data-ckb-item-type="post" data-ckb-post-id="<%= post.id %>">
  <% if is_top_level %>
    <h1 class="post--title has-border-top-width-4 has-border-top-style-solid has-border-color-<%= post.meta? ? 'tertiary' : 'primary' %>-400 has-padding-2">
      <%= post.title %>
      <%= is_question && post.closed ? "[closed]" : "" %>
      <% category = defined?(@category) ? @category : post.category %>
      <% if category.display_post_types.reject { |e| e.to_s.empty? }.size > 1 %>
        <%= post_type_badge(post.post_type.name) %>
      <% end %>
    </h1>
  <% end %>

  <div class="post--container <%= 'deleted-content' if post.deleted? %> grid is-nowrap">
    <div class="grid--cell">
      <div class="post--votes has-text-align-center" title="<%= post.score %>">
        <% existing_vote = my_vote(post) %>
        <button class="vote-button button is-icon-only-button <%= (existing_vote&.vote_type == 1) ? 'is-active' : '' %>"
                data-vote-type="1" data-vote-id="<%= existing_vote&.id %>" id="post-<%= post.id %>-up" aria-label="Upvote">
            <svg width="2em" height="1.33em" viewbox="0 0 100 50">
              <path d="M50,0 L100,50 L0,50 Z" fill="currentColor" />
            </svg>
        </button>
        <div class="score has-font-size-subheading js-upvote-count">
          +<%= post.upvote_count %>
        </div>
        <div class="score has-font-size-subheading js-downvote-count">
          &minus;<%= post.downvote_count %>
        </div>
        <button class="vote-button button is-icon-only-button <%= (existing_vote&.vote_type == -1) ? 'is-active' : '' %>"
                data-vote-type="-1" data-vote-id="<%= existing_vote&.id %>" id="post-<%= post.id %>-up" aria-label="Downvote">
            <svg width="2em" height="1.33em" viewbox="0 0 100 50">
              <path d="M0,0 L100,0 L50,50 Z" fill="currentColor" />
            </svg>
        </button>
      </div>
    </div>

    <div class="grid--cell is-flexible has-padding-2">
      <% if is_question && post.closed %>
        <div class="notice is-warning has-margin-2">
          <h3 class="has-font-weight-normal">
            <strong>closed</strong>
            <% unless post.close_reason.nil? %>
              as <%= post.close_reason.name %>
            <% end %>
            by <%= link_to post.closed_by.username, user_path(post.closed_by) %>
            on <%= post.closed_at.strftime('%b %e, %Y at %H:%M') %>
          </h3>
          <% unless post.close_reason.nil? %>
            <p>
              <%= post.close_reason.description.gsub "$SiteName", SiteSetting['SiteName'] %>
              <% if post.close_reason.requires_other_post %>
                See: <%= link_to post.duplicate_post.title, question_path(post.duplicate_post), class: "has-font-size-larger" %>
              <% end %>
            </p>
          <% end %>
          <p>
            This question was closed; new answers can no longer be added.
            Users with the reopen privilege may vote to reopen this question if it has been improved or closed incorrectly.
          </p>
        </div>
      <% end %>

      <% if post.deleted %>
        <div class="notice is-warning has-margin-2">
          <h3 class="has-font-weight-normal">
            <strong>deleted</strong> by <%= link_to post.deleted_by.username, user_path(post.deleted_by) %>
            on <%= post.deleted_at.strftime('%b %e, %Y at %H:%M') %>
          </h3>
          <% if post.deleted_by.id == post.user.id %>
            <p>
              The author of this post decided to delete it. It can only be seen by users with the delete privilege.
            </p>
          <% else %>
            <p>
              This post was deleted because it violates the rules of this site. It can only be seen by users with the
              delete privilege.
            </p>
          <% end %>

          <p>Users with the undelete privilege may vote to undelete this post if it has been deleted incorrectly.</p>
        </div>
      <% end %>

      <% if post.pending_suggested_edit? %>
        <% if check_your_post_privilege(post, 'Edit') %>
          <div class="notice h-p-2">
            <p class="h-m-0"><i class="fa fa-pencil-alt h-m-l-2 h-c-red-600"></i> There is a <strong>pending suggested edit</strong> on this post. <a href="<%= suggested_edit_url(post.pending_suggested_edit.id) %>" class="button is-outlined is-muted is-small">Review changes</a></p>
          </div>
        <% elsif post.pending_suggested_edit.user.id == current_user&.id %>
          <div class="notice h-p-2">
            <p class="h-m-0"><i class="fa fa-pencil-alt h-m-l-2 h-c-tertiary-600"></i> Your suggested edit on this post is <strong><a href="<%= suggested_edit_url(post.pending_suggested_edit.id) %>">pending review</a></strong>.</p>
          </div>
        <% end %>
      <% end %>

      <div class="post--body">
        <%= raw(sanitize(post.body, scrubber: scrubber)) %>
  
        <div class="post--meta has-margin-bottom-4">
          <%= render 'users/post_usercard', post: post %>
          <% if has_tags %>
            <div class="post--tags has-padding-2">
              <% tag_set = post.tag_set %>
              <% required_ids = post.category&.required_tag_ids %>
              <% moderator_ids = post.category&.moderator_tag_ids %>
              <% topic_ids = post.category&.topic_tag_ids %>
              <% category_sort_tags(post.tags, required_ids, topic_ids, moderator_ids).each do |tag| %>
                <% required = required_ids&.include?(tag.id) ? 'is-filled' : '' %>
                <% topic = topic_ids&.include?(tag.id) ? 'is-outlined' : '' %>
                <% moderator = moderator_ids.include?(tag.id) ? 'is-red is-outlined' : '' %>
                <%= link_to tag.name, tag_path(id: post.category_id, tag_id: tag.id),
                            class: "badge is-tag #{required} #{topic} #{moderator}" %>
              <% end %>
            </div>
          <% end %>
          <% if post.license.present? %>
            <div class="post--licensing has-margin-left-2">
              <i class="fas fa-balance-scale" title="License"></i>
              <%= link_to post.license.name, post.license.url %>
            </div>
          <% end %>
        </div>
        <div class="has-clear-clear"></div>


        <div class="post--actions">
          <div class="tools">
              <%= link_to generic_share_link(post), class: 'tools--item' do %>
                <i class="fa fa-link"></i>
                Permalink
              <% end %>
              <%= link_to post_history_path(post), class: 'tools--item' do %>
                <i class="fa fa-history"></i>
                History
              <% end %>
            <% if check_your_post_privilege(post, 'Edit') %>
              <% if post.pending_suggested_edit? %>
                <%= link_to suggested_edit_url(post.pending_suggested_edit.id), class: 'tools--item is-danger is-filled' do %>
                  <i class="fa fa-spell-check"></i>
                  Review suggested edit
                <% end %>
              <% else %>
                <%= link_to generic_edit_link(post), class: 'tools--item' do %>
                  <i class="fa fa-pencil-alt"></i>
                  Edit
                <% end %>
              <% end %>
            <% elsif !current_user.nil? %>
              <% if post.pending_suggested_edit? %>
                <span class="tools--item">suggested edit pending...</span>
              <% else %>
                <%= link_to generic_edit_link(post), class: 'tools--item' do %>
                  <i class="fa fa-pencil-alt"></i>
                  Suggest edit
                <% end %>
              <% end %>
            <% end %>
            <% if is_question && !post.closed %>
              <a href="#" class="close-dialog-link tools--item">
                <i class="fa fa-lock"></i>
                Close
              </a>
            <% elsif is_question && post.closed %>
              <%= link_to reopen_question_path(post), method: :post, class: 'reopen-question tools--item' do %>
                <i class="fa fa-unlock"></i>
                Reopen
              <% end %>
            <% end %>
            <% if !post.deleted %>
              <%= link_to url_for(controller: post.post_type.name.pluralize.downcase.to_sym, action: :destroy, id: post.id),
                          method: :delete, data: { confirm: 'Are you sure you want to delete this post?' }, class: "tools--item is-danger" do %>
                <i class="fa fa-trash"></i>
                Delete
              <% end %>
            <% else %>
              <%= link_to url_for(controller: post.post_type.name.pluralize.downcase.to_sym, action: :undelete, id: post.id),
                          method: :post, data: { confirm: 'Restore this post, making it visible to regular users?' }, class: "tools--item is-danger is-filled" do %>
                <i class="fa fa-undo"></i>
                Restore
              <% end %>
            <% end %>
            <a href="#" class="flag-dialog-link tools--item">
              <i class="fa fa-flag"></i>
              Flag
            </a>
            <% if current_user&.has_privilege?('SeeTools') %>
              <a href="javascript:void(0);" data-modal="#mod-tools-<%= post.id %>" class="tools--item">
                <i class="fa fa-wrench"></i>
                Tools
              </a>
            <% end %>
            <% flags_count = post.flags.where(handled_by_id: nil).count %>
            <% if moderator? && flags_count > 0 %>
              <a href="#" class="show-all-flags-dialog-link tools--item">
                <i class="fa fa-exclamation-triangle"></i>
                Show <%= pluralize(flags_count, "flag") %>
              </a>
            <% end %>
          </div>
        </div>

        <div class="post--action-dialog js-flag-box">
          <div class="widget">
            <div class="widget--body">
              <label class="form-element" for="flag-post-<%= post.id %>">
                Why does this post require moderator attention?
              </label>
              <input class="form-element js-flag-comment" id="flag-post-<%= post.id %>">
            </div>
            <div class="widget--footer">
              <button class="flag-link button is-filled is-muted" data-post-type="<%= is_question ? 'Question' : 'Answer' %>" data-post-id="<%= post.id %>">
                Flag for moderator attention
              </button>
            </div>
          </div>
        </div>

        <% if is_question %>
          <div class="post--action-dialog js-close-box">
            <div class="widget">
              <div class="widget--header">Why should this post be closed?</div>
              <% (@close_reasons || CloseReason.all).each do |reason| %>
                <div class="widget--body">
                  <div class="grid">
                    <div class="grid--cell">
                      <input class="form-radio-element" type="radio" name="close-reason" value="<%= reason.id %>" id="close-reason-<%= reason.id %>">
                    </div>
                    <div class="grid--cell is-flexible">
                      <label class="form-element" for="close-reason-<%= reason.id %>">
                        <%= reason.name %>
                        <div class="form-caption">
                          <%= reason.description.gsub "$SiteName", SiteSetting['SiteName'] %>
                        </div>
                      </label>
                      <% if reason.requires_other_post %>
                        <input class="form-element has-margin-top-8 js-close-other-post" id="close-reason-other-post-<%= reason.id %>" placeholder="Please enter the URL or ID of the other post." />
                      <% end %>
                    </div>
                  </div>
                </div>
              <% end %>
              <div class="widget--footer">
                <button class="close-question button is-filled is-muted" data-post-type="<%= is_question ? 'Question' : 'Answer' %>" data-post-id="<%= post.id %>">
                  Close this post
                </button>
              </div>
            </div>
          </div>
        <% end %>

        <% if moderator? %>
          <div class="post--action-dialog js-flags">
            <div class="widget">
              <div class="widget--header">Flags on this post</div>
              <% post.flags.where(handled_by_id: nil).each do |flag| %>
                <div class="widget--body">
                  <div>
                  <p><%= flag.reason %> &mdash; <a href="/users/<%= flag.user.id %>"><%= flag.user.username %></a></p>
                  <input class="form-element flag-resolve-comment has-float-left" style="max-width: 300px;" placeholder="Optional comment for flagger">
                  <div class="has-float-right has-padding-1">
                    <a href="#" class="flag-resolve button" data-result='helpful' data-flag-id="<%= flag.id %>">Helpful</a>
                    <a href="#" class="flag-resolve button" data-result='declined' data-flag-id="<%= flag.id %>">Decline</a>
                  </div>
                  <div class="has-clear-clear"></div>
                  </div>
               </div>
              <% end %>
            </div>
          </div>
        <% end %>

        <div class="post--status">
  
          <% if post.att_source.present? %>
            <div class="notice has-margin-2">
              <p>
                This post was sourced from <%= link_to post.att_source, post.att_source %>.
                <% if post.att_license_name.present? %>
                  It is licensed under <%= link_to post.att_license_name, post.att_license_link %>.
                <% end %>
              </p>
            </div>
          <% end %>
       </div>
         <div class="post--comments has-padding-4">
          <h4 class="has-margin-0">
            <%= pluralize(post.comments.undeleted.size, 'comment') %>
            <span class="has-color-tertiary-500"><%= (moderator? && post.comments.deleted.size != 0) ? '(deleted: ' + post.comments.deleted.size.to_s + ')' : '' %></span>
          </h4>
          <div class="post--comments-container">
            <% comments = moderator? ? post.comments : post.comments.undeleted %>
            <% comments.first(5).each do |comment| %>
              <%= render 'comments/comment', comment: comment %>
            <% end %>
          </div>
  
          <div class="post--comments-links has-margin-top-1">
            <% if comments.count > 5 %>
              <a href="#" class="js-more-comments" data-post-id="<%= post.id %>">
                <strong>Show <%= comments.count - 5 %> more comments</strong>
              </a>
              <% if user_signed_in? %>
                &middot;
              <% end %>
            <% end %>
            <% if user_signed_in? %>
              <% if post.deleted %>
                <p class="has-color-tertiary-500">Comments disabled on deleted posts</p>
              <% elsif post.comments_disabled && !moderator? && !admin? %>
                <p class="has-color-red-500"><i class="fa fa-lock"></i> Comments have been disabled on this post.</p>
              <% else %>
                <% if post.comments_disabled %>
                  <p class="has-color-red-500"><i class="fa fa-lock"></i> Comments have been disabled on this post, but as a moderator you are exempt from that block.</p>
                <% end %>
                <a href="#" class="js-add-comment">Add a comment</a>
  
                <%= form_for Comment.new, url: create_comment_path, remote: true, html: { class: 'comment-form js-comment-form', id: nil } do |f| %>
                  <%= f.hidden_field :post_id, value: post.id, id: nil %>

                  <div class="form-group-horizontal">
                    <div class="form-group">
                      <%= f.label :content, 'Your comment' %>
                      <%= f.text_area :content, class: 'form-element is-small js-comment-content', id: nil, autocomplete: 'off' %>
                      <span class="has-float-right has-font-size-caption js-character-count" data-target=".js-comment-content" data-max="500">0 / 500</span>
                    </div>
                    <div class="actions">
                      <%= f.submit 'Post', class: 'button is-outlined' %>
                    </div>
                  </div>
                <% end %>
             <% end %>
            <% end %>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<% if moderator? %>
  <%= render 'posts/post_tools', post: post %>
<% end %>
