<!--REVIEW: trying out articles on these fieldsets to see if that's a better chunker, but may revert after testing. Have also tossed in landmark headings on top level groups-->
<%= form_for(@work, html: { id: "work-form", class: "verbose post work" }) do |f| %>

  <p class="required notice">* <%= ts("Required information") %></p>

  <%= render "work_form_tags", include_blank: false %>

  <fieldset class="preface" role="article">
    <legend><%= t("works.preface.title") %></legend>
    <h3 class="landmark heading"><%= t("works.preface.title") %></h3>
    <dl>
      <dt class="title required">
        <%= f.label :title, ts("Work Title") + "*" %>
      </dt>
      <dd class="title required">
        <%= f.text_field :title, class: "observe_textlength text" %>
        <%= live_validation_for_field("work_title",
              maximum_length: ArchiveConfig.TITLE_MAX,
              minimum_length: ArchiveConfig.TITLE_MIN,
              failureMessage: ts("We need a title! (At least %{min_length} character long, please.)",
                min_length: ArchiveConfig.TITLE_MIN.to_s))
        %>
        <%= generate_countdown_html("work_title", ArchiveConfig.TITLE_MAX) %>
      </dd>

      <!-- Add coauthors-->
      <%= render "pseuds/byline", form: f, object: @work %>

      <dt class="summary">
        <%= f.label :summary, ts("Summary", max: ArchiveConfig.SUMMARY_MAX.to_s) %>
      </dt>
      <dd class="summary">
        <%= f.text_area :summary, class: "observe_textlength" %>
        <%= live_validation_for_field("work_summary", presence: false,
              maximum_length: ArchiveConfig.SUMMARY_MAX) %>
        <%= generate_countdown_html("work_summary", ArchiveConfig.SUMMARY_MAX) %>
      </dd>

      <!-- notes -->
      <%= render "notes_form", f: f, type: "work" %>

    </dl>
  </fieldset>

  <fieldset id="associations" role="article">
    <legend><%= t("works.associations.title") %></legend>
    <h3 class="landmark heading"><%= t("works.associations.title") %></h3>
    <dl>
      <% @assignments = get_open_assignments(current_user) %>
      <% if @assignments.any? || @work.challenge_assignments.any? %>
        <!-- if the user has open assignments? -->
        <dt class="collection">
          <%= f.label "challenge_assignment_ids[]", ts("Does this fulfill a challenge assignment") %>
          <%= link_to_help "add-work-to-assignment" %>
        </dt>
        <dd class="collection listbox group">
          <h4 class="heading"><%= ts("Open Assignments") %></h4>
          <%= checkbox_section f, :challenge_assignment_ids, (@work.challenge_assignments + @assignments).uniq,
                :checked_method => "challenge_assignments", :name_method => "title" %>
        </dd>
      <% end %>

      <% if !(@claims = current_user.request_claims.unstarted).empty? || !@work.challenge_claims.empty? %>
        <!-- if the user has open claims? -->
        <dt class="collection">
          <%= f.label "challenge_claim_ids[]", ts("Fulfill a Claim") %>
          <%= link_to_help "add-work-to-assignment" %>
        </dt>
        <dd class="collection listbox group">
          <h4 class="heading"><%= ts("Open Claims") %></h4>
          <%= checkbox_section f, :challenge_claim_ids, (@work.challenge_claims + @claims).uniq,
              :checked_method => "challenge_claims", :name_helper_method => "claim_title" %>
        </dd>
      <% end %>

      <!-- Add collection -->
      <dt class="collection" title="<%= ts("post to collection or challenge") %>">
        <%= f.label :collection_names, ts("Post to Collections / Challenges") %>
        <%= link_to_help "add-collectible-to-collection" %>
      </dt>
      <dd class="collection" title="<%= ts("post to collection or challenge") %>">
        <%= f.text_field :collection_names, autocomplete_options("open_collection_names") %>
      </dd>

      <dt class="recipient" title="<%= ts("gift to") %>">
        <%= f.label :recipients, ts("Gift this work to") %>
        <%= link_to_help "recipients" %>
      </dt>
      <dd class="recipient" title="<%= ts("gift to") %>">
        <%= f.text_field :recipients, autocomplete_options("pseud", value: @work.recipients(for_form = true)) %>
      </dd>

      <!-- Inspiration / remix -->
      <dt class="parent">
        <%= check_box_tag "parent-options-show", "1", check_parent_box(@work), class: "toggle_formfield" %>
      </dt>
      <dd class="parent">
        <%= label_tag "parent-options-show", ts("This work is a remix, a translation, a podfic, or was inspired by another work") %>
        <%= link_to_help "parent-works-help" %>

        <!-- Toggles on with parent checkbox -->
        <fieldset id="parent-options">
          <dl>
            <% new_parent, existing_parents = @work.parent_work_relationships.partition(&:new_record?) %>
            <% new_parent << @work.parent_work_relationships.build if new_parent.empty? %>

            <%= f.fields_for :parent_work_relationships, new_parent do |p| %>
              <dt><%= p.label :url, ts("URL") %></dt>
              <dd>
                <%= p.text_field :url,
                      "aria-describedby" => "parent-attributes-url-field-description" %>
                <p class="important footnote" id="parent-attributes-url-field-description">
                  <%= ts("For a work in the Archive, only the URL is required.") %>
                </p>
              </dd>

              <dt><%= p.label :title %></dt>
              <dd><%= p.text_field :title %></dd>

              <dt><%= p.label :author %></dt>
              <dd><%= p.text_field :author %></dd>

              <dt>
                <%= p.label :language_id, ts("Language") %>
                <%= link_to_help "languages-help" %>
              </dt>
              <dd>
                <%= p.select(:language_id, language_options_for_select(sorted_languages, "id"), prompt: " ") %>
              </dd>

              <dt class="translation"><%= p.check_box :translation %></dt>
              <dd class="translation">
                <%= p.label :translation, ts("This is a translation") %>
                <%= link_to_help "translation-link" %>
              </dd>
            <% end %>

            <% unless existing_parents.blank? %>
              <dt><%= ts("Current parent works") %></dt>
              <% existing_parents.each do |related_work| %>
                <% if related_work.parent %>
                  <dd>
                    <ul class="actions" role="navigation">
                      <li>
                        <%= link_to related_work.parent.title, related_work.parent %>
                      </li>
                      <li>
                        <%= link_to ts("Remove"), related_work,
                                data: { confirm: ts("Are you sure you want to delete the connection to this work?") },
                                method: :delete %>
                      </li>
                    </ul>
                  </dd>
                <% end %>
              <% end %>
            <% end %>
          </dl>
        </fieldset>
      </dd>

      <!-- Series checkbox (storyseriescheck) -->
      <!-- Checking this box causes the "manage series" dropdown to appear; unchecking it causes it to disappear. -->
      <dt class="serial">
        <%= check_box_tag "series-options-show", "1", check_series_box(@work), class: "toggle_formfield" %>
      </dt>
      <dd class="serial">
        <%= label_tag "series-options-show", ts("This work is part of a series") %>
        <%= link_to_help "choosing-series" %>

        <!-- Toggles on with series checkbox -->
        <fieldset id="series-options">
          <dl>
            <%= fields_for "work[series_attributes]" do |s| %>
              <dt><%= s.label "id", ts("Choose one of your existing series:") %></dt>
              <dd>
                <%= s.collection_select(:id, @series, :id, :title, { selected: work_series_value(:id), prompt: true }) %>
              </dd>

              <dt><%= s.label :title, ts("Or create and use a new one:") %></dt>
              <dd><%= s.text_field :title, value: work_series_value(:title) %></dd>
            <% end %>

            <% unless @serial_works.blank? || @work.new_record? %>
              <dt><%= ts("Current Series") %></dt>
              <% for serial in @serial_works %>
                <% unless serial.new_record? %>
                  <dd>
                    <ul class="actions" role="navigation">
                      <li>
                        <%= link_to serial.series.title, serial.series %>
                      </li>
                      <li>
                        <%= link_to ts("Remove Work From Series"), serial_work_path(serial),
                                method: :delete %>
                      </li>
                    </ul>
                  </dd>
                <% end %>
              <% end %>
            <% end %>
          </dl>
        </fieldset>
      </dd>

      <%= fields_for "work[chapter_attributes]", @chapter do |c| %>
        <!-- Chaptered checkbox (work_is_wip) -->
        <!-- Checking this box causes the expected number of chapters box to appear; unchecking it causes it to disappear. -->
        <dt class="chaptered wip">
          <%= check_box_tag "chapters-options-show", "1", @work.chaptered?, class: "toggle_formfield" %>
        </dt>
        <dd class="chaptered wip">
          <%= label_tag "chapters-options-show", ts("This work has multiple chapters") %>

          <fieldset id="chapters-options">
            <dl>
              <dt><%= f.label :wip_length, ts("Chapter 1 of") %></dt>
              <dd><%= f.text_field :wip_length, class: "number" %></dd>
              <dt><%= c.label :title, ts("Chapter Title:") %></dt>
              <dd><%= c.text_field :title, value: @chapter.title %></dd>
            </dl>
          </fieldset>
        </dd>

        <!--backdate-->
        <dt class="backdate">
          <%= f.check_box :backdate, { id: "backdate-options-show", class: "toggle_formfield" } %>
        </dt>
        <dd class="backdate">
          <%= label_tag "backdate-options-show", ts("Set a different publication date") %>
          <%= link_to_help "backdating-help" %>

          <fieldset id="backdate-options" class="optional">
            <dl>
              <dt><%= label_tag "published_at", ts("Set publication date") %></dt>
              <dd id="managePublicationDate" class="datetime" title="<%= ts("set date") %>">
                <!--BACK END, this select box does not seem to be acquiring the label published_at -->
                <%= c.date_select("published_at", :start_year => Date.current.year, :end_year => 1950, :default => Date.current, :value => @chapter.published_at, :order => [:day, :month, :year]) %>
              </dd>
            </dl>
          </fieldset>
        </dd>

        <%= render "work_form_associations_language", f: f %>

        <dt class="skin">
          <%= f.label :work_skin_id, t("works.skin.select") %>
          <%= link_to_help "work-skins" %>
        </dt>
        <dd class="skin">
          <%= f.collection_select :work_skin_id, all_coauthor_skins, :id, :title,
                { :selected => (@work.work_skin ? @work.work_skin.id.to_s : nil), include_blank: true } %>
          <p class="actions" role="navigation">
            <%= link_to ts("Public Work Skins"), skins_path(:skin_type => "WorkSkin") %>
          </p>
        </dd>
      </dl>

  </fieldset>

  <!-- privacy settings -->
  <fieldset class="privacy" role="article">
    <legend><%= t("works.permissions.privacy") %></legend>
    <h3 class="landmark heading"><%= t("works.permissions.privacy") %></h3>
    <dl>
      <!-- Privacy dropdown (storyprivacy) -->
      <dt class="restrict">
        <%= f.check_box :restricted %>
      </dt>

      <dd class="restrict">
        <%= f.label :restricted, t("works.permissions.visibility.restricted") %>
        <%= link_to_help "registered-users" %>
      </dd>

      <dt class="moderated comments">
        <%= f.check_box :moderated_commenting_enabled %>
      </dt>
      <dd class="moderated comments">
        <%= f.label :moderated_commenting_enabled, t("comments.commentable.permissions.moderated_commenting.enable") %>
        <%= link_to_help "comments-moderated" %>
      </dd>

      <dt class="permissions comments">
        <%= t("comments.commentable.permissions.options.label") %>
        <%= link_to_help "who-can-comment-on-this-work" %>
      </dt>
      <dd class="permissions comments">
        <fieldset>
          <%=
            radio_button_list(f, :comment_permissions, [
              [:enable_all, t("comments.commentable.permissions.options.enable_all")],
              [:disable_anon, t("comments.commentable.permissions.options.disable_anon")],
              [:disable_all, t("comments.commentable.permissions.options.disable_all")]
            ])
          %>
        </fieldset>
      </dd>

    </dl>
  </fieldset>
  <% unless @chapters %>
    <!-- Work text field (chapter_attributes_content) -->
    <fieldset class="work text" role="article">
      <legend class="required"><%= ts("Work Text") %>*</legend>
      <ul class="hidden rtf-html-switch actions" role="menu">
        <li><a class="rtf-link" href="#"><%= ts("Rich Text") %></a></li>
        <li><a class="html-link" href="#"><%= ts("HTML") %></a></li>
      </ul>
      <p class="rtf-html-instructions note">
        <span class="html-notes"><%= allowed_html_instructions %></span>
        <span class="hidden rtf-notes">
          <%= ts("Type or paste formatted text.") %><%= link_to_help "rte-help" %>
        </span>
      </p>
      <p class="notice">
        <%= ts("<strong>Note:</strong> Text entered in the posting form is <strong>not</strong> automatically saved. Always keep a backup copy of your work.").html_safe %>
      </p>

      <% use_tinymce %>
        <div class="rtf-html-field">
          <h4 class="landmark heading"><%= ts("Work Text") %>*</h4>
          <%= c.text_area :content, value: @chapter.content,
                class: "mce-editor observe_textlength large", id: "content" %>
          <%= live_validation_for_field("content",
            maximum_length: ArchiveConfig.CONTENT_MAX_DISPLAYED,
            minimum_length: ArchiveConfig.CONTENT_MIN,
            tooLongMessage: ts("We salute your ambition! But sadly the content must be less than %{max} characters long. (Maybe you want to create a multi-chaptered work?)",
              max: ArchiveConfig.CONTENT_MAX_DISPLAYED.to_s),
            tooShortMessage: ts("Brevity is the soul of wit, but your content does have to be at least %{min} characters long.",
              min: ArchiveConfig.CONTENT_MIN.to_s),
            failureMessage: ts("Brevity is the soul of wit, but your content does have to be at least %{min} characters long.",
              min: ArchiveConfig.CONTENT_MIN.to_s))
          %>

          <%= generate_countdown_html("content", ArchiveConfig.CONTENT_MAX_DISPLAYED) %>
        </div>
      </fieldset>
    <% end %>

  <% end %><!-- end of fields for chapters -->

  <fieldset class="create">
    <legend><%= ts("Post") %></legend>
    <p class="notice">
      <%= t(".post_notice_html",
            content_policy_link: link_to(t(".content_policy"), content_path),
            tos_faq_link: link_to(t(".tos_faq"), tos_faq_path(anchor: "content_faq"))) %>
    </p>
    <ul class="actions">
      <% unless @work.new_record? || @work.posted? %>
        <li>
          <%= submit_tag ts("Save As Draft"), name: "save_button" %>
        </li>
      <% end %>
      <li>
        <%= submit_tag ts("Preview"), name: "preview_button" %>
      </li>
      <li>
        <%= submit_tag ts("Post"), name: "post_button",
              data: { disable_with: ts("Please wait...") } %>
      </li>
      <li>
        <%= submit_tag ts("Cancel"), name: "cancel_button" %>
      </li>
    </ul>
  </fieldset>
<% end %> <!-- end of form_for -->

<%= content_for :footer_js do %>
  <%= javascript_tag do %>
    $j(document).ready(function(){
      $j(".toggle_formfield").click(function() {
        var targetId = $j(this).attr("id").replace("-show", "");
        toggleFormField(targetId);
      });
    })
  <% end %>
<% end %>
