= bootstrap_form_for(@crop) do |f|
  - if @crop.errors.any?
    #error_explanation.alert.alert-warning{role: "alert"}
      %h3
        = pluralize(@crop.errors.size, "error")
        prohibited this crop from being saved:
      %ul
        - @crop.errors.full_messages.each do |msg|
          %li= msg

  .card.col-12.col-md-8.mx-auto.float-none.white
    .card-header
      - if content_for? :title
        %h1.h2-responsive.text-center
          %strong=yield :title
    .card-body
      -# Handy link to crop wrangling policy/style guide, shown to wranglers only
      - if can? :wrangle, @crop
        %p
          %span.help-block
            For detailed crop wrangling guidelines, please consult the
            = link_to "crop wrangling guide", "https://github.com/Growstuff/growstuff/wiki/Crop-Wrangling"
            on the Growstuff wiki.

      -# Everyone (wranglers and requesters) sees the basic info section
      %h2 Basic information

      .form-group#new_crop
      = f.text_field :name, required: true
      %span.help-block
        The common name for the crop, in English (required).
        - if can? :wrangle, @crop
          Wranglers: please ensure this is singular, and capitalize
          proper nouns only.

      %h2 Lifespan
      %p
        = f.radio_button(:perennial, false, label: 'Annual')
        %span.help-block Living and reproducing in a single year or less
      %p
        = f.radio_button(:perennial, true, label: "Perennial")
        %span.help-block Living more than two years

      %h2 Data
      - if @crop.description.blank? || can?(:wrangle, @crop)
        = f.text_area :description, label: 'Description'
        - if @crop.parent
          %span.help-block Parent: #{@crop.parent.description}
      - if @crop.row_spacing.blank? || can?(:wrangle, @crop)
        = f.number_field :row_spacing, label: 'Row Spacing (cm)', min: 0
        - if @crop.parent
          %span.help-block Parent: #{@crop.parent.row_spacing}
      - if @crop.spread.blank? || can?(:wrangle, @crop)
        = f.number_field :spread, label: 'Spread (cm)', min: 0
        - if @crop.parent
          %span.help-block Parent: #{@crop.parent.spread}
      - if @crop.height.blank? || can?(:wrangle, @crop)
        = f.number_field :height, label: 'Height (cm)', min: 0
        - if @crop.parent
          %span.help-block Parent: #{@crop.parent.height}
      - if @crop.sowing_method.blank? || can?(:wrangle, @crop)
        = f.text_field :sowing_method
        - if @crop.parent
          %span.help-block Parent: #{@crop.parent.sowing_method}
      - if @crop.sun_requirements.blank? || can?(:wrangle, @crop)
        = f.text_field :sun_requirements
        - if @crop.parent
          %span.help-block Parent: #{@crop.parent.sun_requirements}
      - if @crop.growing_degree_days.blank? || can?(:wrangle, @crop)
        = f.number_field :growing_degree_days, min: 0
        - if @crop.parent
          %span.help-block Parent: #{@crop.parent.growing_degree_days}
      = f.text_field :public_food_key, label: 'Australian Food Composition Database Public Food Key'

      - unless @crop.approved?
        = link_to 'Search wikipedia', "https://en.wikipedia.org/w/index.php?search=#{@crop.name}", target: '_blank'
      - if @crop.en_wikipedia_url.blank? || can?(:wrangle, @crop)
        = f.url_field :en_wikipedia_url, id: "en_wikipedia_url", label: 'Wikipedia URL'
        %span.help-block
          Link to the crop's page on the English language Wikipedia (required).
      - if @crop.en_youtube_url.blank? || can?(:wrangle, @crop)
        = f.url_field :en_youtube_url, label: 'YouTube URL'
        %span.help-block
          Link to a YouTube video about the crop in English.

      -# Only crop wranglers see the crop hierarchy (for now)
      - if can? :wrangle, @crop
        = f.select(:parent_id, Crop.order(:name).pluck(:name, :id),
          { include_blank: true, label: 'Parent crop'})
        %span.help-block Optional. For setting up crop hierarchies for varieties etc.


      -# Everyone (wranglers and requesters) gets to add scientific names
      %h2 Scientific names
      = button_tag "+", class: "add-sciname-row", type: "button"
      = button_tag "-", class: "remove-sciname-row", type: "button"

      .form-group#scientific_names
        - @crop.scientific_names.each.with_index do |sci, index|
          .template.col-12{ id: "sci_template[#{index + 1}]" }
            .col-2
              = label_tag :scientific_names, "Scientific name #{index + 1}:", class: 'control-label'
            .col-8
              = text_field_tag "sci_name[#{index + 1}]", sci.name, id: "sci_name[#{index + 1}]",
                                                                    class: 'scientific-name-auto-suggest form-control',
                                                                    data: { source_url: gbif_suggest_scientific_names_path }
              %span.help-block Searches GBIF to determine scientific name of crop.
              = hidden_field_tag "sci_gbif_key[#{index + 1}]", sci.gbif_key, id: "sci_gbif_key[#{index + 1}]",
                                                                              class: 'scientific-name-auto-suggest-id'
      %h2 Alternate names
      = button_tag "+", class: "add-altname-row", type: "button"
      = button_tag "-", class: "remove-altname-row", type: "button"

      .form-group#alternate_names
        - @crop.alternate_names.each.with_index do |alt, index|
          .template.col-12{ id: "alt_template[#{index + 1}]" }
            .col-2
              = label_tag :alternate_names, "Alternate name #{index + 1}:", class: 'control-label'
            .col-8
              = text_field_tag "alt_name[#{index + 1}]", alt.name, id: "alt_name[#{index + 1}]", class: 'form-control'
              %span.help-block Alternate name of crop.
            .col-2

      -# This is used for comments from crop requesters.  We need to show it
      -# to everyone, but we don't include it on new crops from wranglers.

      - if (can?(:wrangle, @crop) && @crop.requester) || (cannot?(:wrangle, @crop) && @crop.new_record?)
        %h2 Crop request notes
        = f.text_area :request_notes, rows: 3, id: 'request_notes', label: 'Comments'

      -# A final explanation of what's going to happen next, for crop requesters
      - unless can? :wrangle, @crop
        %p
          When you submit this form, your suggestion will be sent to our team of
          volunteer crop wranglers for review. We'll let you know the outcome as soon as we can.

      -# Now, for crop wranglers, let's have approval/rejection at the bottom of the page
      - if can?(:wrangle, @crop) && @crop.requester
        -# %h2 Approve or reject pending crops
        -# = f.select(:approval_status, @crop.approval_statuses, {})
        = f.select(:reason_for_rejection, @crop.reasons_for_rejection, include_blank: true)

        = f.text_area :rejection_notes, rows: 3
        %span.help-block
          Please provide additional notes why this crop request was rejected if the above reasons do not apply.

    .card-footer
      .text-right
        - if @crop.approved?
          = f.submit 'Save'
        - else
          = f.submit 'Reject', class: 'btn btn-danger', name: 'reject'
          = f.submit 'Approve and save', class: 'btn btn-success', name: 'approve'
