<div class="row">
  <div class="col-xs-12">

    <div class="page-header">
      <h1>Recipes</h1>
    </div>

    <% if current_user? %>
      <%= link_to 'New Recipe', new_recipe_path, class: 'btn btn-default' %><br/>
    <% end %>

    <%= paginate @recipes %>

    <%= form_for(@criteria, as: :criteria, url: recipes_path, method: :get, html: {id: 'search_form'}) do |f| %>
      <%= f.hidden_field :sort_column %>
      <%= f.hidden_field :sort_direction %>
      <%= f.hidden_field :page %>
      <%= f.hidden_field :per %>
      <%= f.hidden_field :name %>
      <%= f.hidden_field :tags %>
    <% end %>

    <div class="table-responsive">
      <table class="recipe-table table table-striped table-hover">
        <thead>
          <tr>
            <th><%= index_sort_header('Name', :name, @criteria) %></th>
            <th>Tags</th>
            <th><%= index_sort_header('Rating', :rating, @criteria) %></th>
            <th>Yields</th>
            <th><%= index_sort_header('Time', :total_time, @criteria) %></th>
            <th><%= index_sort_header('Created', :created_at, @criteria) %></th>
            <th></th>
          </tr>
          <tr>
            <th><%= text_field_tag('name_search', @criteria.name) %></th>
            <th><%= text_field_tag('tags_search', @criteria.tags) %></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th><button id="recipe_index_search_button" class="btn btn-sm btn-default">Search</button></th>
          </tr>
        </thead>

        <tbody>
          <% decorate(@recipes, RecipeDecorator).each do |recipe| %>
            <tr>
              <td><%= link_to recipe.short_name, recipe %></td>
              <td><div class="table_tags"><%= recipe.tag_names %></div></td>
              <td>
                <% if recipe.rating %>
                  <%= text_field_tag('rating', recipe.rating, disabled: true, data: {rating: true, size: '20px', interval: '0.25'}) %>
                <% else %>
                  --
                <% end %>
              </td>
              <td><%= recipe.yields %></td>
              <td><%= recipe_time(recipe) %></td>
              <td><%= recipe.created_at %></td>

              <td>
                <% if current_user? %>
                  <div class="recipe_list_controls">
                    <%= link_to new_recipe_log_path(recipe), class: 'btn btn-xs btn-primary' do %>
                      <span class="glyphicon glyphicon-copy"></span>
                    <% end %>
                    <%= link_to edit_recipe_path(recipe), class: 'btn btn-xs btn-primary' do %>
                      <span class="glyphicon glyphicon-pencil"></span>
                    <% end %>
                    <%= link_to recipe, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-xs btn-danger' do %>
                      <span class="glyphicon glyphicon-remove"></span>
                    <% end %>
                  </div>
                <% end %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    </div>

    <% if @recipes.empty? %>
      <p>No Recipes</p>
    <% end %>

    <%= paginate @recipes %>

    <br>

    <% if current_user? %>
      <%= link_to 'New Recipe', new_recipe_path, class: 'btn btn-default' %><br/>
    <% end %>

  </div>
</div>
