
<% has_ndbn = @ingredient.ndbn.present? %>

<%= form_for(@ingredient, html: {id: 'ingredient_form'}) do |f| %>

  <%= render partial: 'shared/error_list', locals: {model: @ingredient} %>

  <%= f.hidden_field :ndbn, class: 'ndbn' %>
  <%= f.hidden_field :id, class: 'id', disabled: true %>

  <div class="form-group">
    <%= f.label :name, class: 'control-label' %>
    <%= f.text_field :name, class: 'form-control name', autofocus: true %>
  </div>

  <div class="form-group">
    <%= f.label :ndbn, "Nutrient Databank Number", class: 'control-label' %>
    <div class="input-group">
      <span class="input-group-btn">
        <button type="button" class="<%= ndbn_button_class(@ingredient) %>">
          <span class="glyphicon glyphicon-link"></span><span class="ndbn"><%= @ingredient.ndbn %></span>
        </button>
      </span>
      <input type="text" class="ndbn_typeahead form-control" value="<%= @ingredient.usda_food_name %>" />
    </div>
  </div>

  <div class="ndbn_results">
  </div>

  <div class="form-group">
    <%= f.label :density, class: 'control-label' %>
    <%= f.text_field :density, class: 'form-control', disabled: has_ndbn %>
  </div>

  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title">Ingredient Units</h3>
    </div>
    <div class="panel-body">
      <div class="ingredient-unit-list">
        <%= f.fields_for :ingredient_units do |iu_form| %>
          <%= render partial: 'ingredients/ingredient_unit_fields', locals: {f: iu_form } %>
        <% end %>

        <%= link_to_add_association 'add unit', f, :ingredient_units, class: 'btn btn-primary' %>
      </div>
    </div>
  </div>

  <% if has_ndbn %>

    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">NDBN Unit Weights</h3>
      </div>
      <div class="panel-body">
        <table class="table table-condensed">
          <thead>
          <tr>
            <th>Name</th>
            <th>Grams</th>
          </tr>
          </thead>
          <% @ingredient.usda_food.usda_food_weights.each do |w| %>
            <tr>
              <td><%= "#{w.amount} #{w.description}" %></td>
              <td><%= w.gram_weight %></td>
            </tr>
          <% end %>
        </table>
      </div>
    </div>

  <% end %>

  <div class="form-group">
    <%= f.label :notes, class: 'control-label' %>
    <%= f.text_area :notes, class: 'form-control' %>
  </div>

  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title">Nutrition Per 100 grams</h3>
    </div>
    <div class="panel-body">
      <fieldset <%= 'disabled=disabled' if has_ndbn %>>

        <div class="row">
          <div class="col-xs-12 col-md-6">
            <div class="form-group">
              <%= f.label :water, "Grams of Water", class: 'control-label' %>
              <%= f.text_field :water, class: 'form-control' %>
            </div>

            <div class="form-group">
              <%= f.label :protein, "Grams of Protein", class: 'control-label' %>
              <%= f.text_field :protein, class: 'form-control' %>
            </div>

            <div class="form-group">
              <%= f.label :lipids, "Grams of Fat", class: 'control-label' %>
              <%= f.text_field :lipids, class: 'form-control' %>
            </div>

            <div class="form-group">
              <%= f.label :carbohydrates, "Grams of Carbohydrates", class: 'control-label' %>
              <%= f.text_field :carbohydrates, class: 'form-control' %>
            </div>

            <div class="form-group">
              <%= f.label :kcal, "Calories", class: 'control-label' %>
              <%= f.text_field :kcal, class: 'form-control' %>
            </div>

            <div class="form-group">
              <%= f.label :fiber, "Grams of Fiber", class: 'control-label' %>
              <%= f.text_field :fiber, class: 'form-control' %>
            </div>

            <div class="form-group">
              <%= f.label :sugar, "Grams of Sugar", class: 'control-label' %>
              <%= f.text_field :sugar, class: 'form-control' %>
            </div>
          </div>
          <div class="col-xs-12 col-md-6">
            <div class="form-group">
              <%= f.label :calcium, "Miligrams of Calcium", class: 'control-label' %>
              <%= f.text_field :calcium, class: 'form-control' %>
            </div>

            <div class="form-group">
              <%= f.label :sodium, "Miligrams of Sodium", class: 'control-label' %>
              <%= f.text_field :sodium, class: 'form-control' %>
            </div>

            <div class="form-group">
              <%= f.label :vit_k, "Micrograms of Vitamin K", class: 'control-label' %>
              <%= f.text_field :vit_k, class: 'form-control' %>
            </div>
          </div>
        </div>
      </fieldset>
    </div>
  </div>

  <div class="actions">
    <%= f.submit class: 'btn btn-primary' %>
  </div>

<% end %>
