class ItemsController < ApplicationController
  before_action :set_item, only: [:show, :edit, :update, :destroy]
  protect_from_forgery except: :index
  before_action { @service_page = true }
  # GET /items
  # GET /items.json
  def index
    params = items_search_params
    @item_type = ItemType.find(params[:item_type_id])
    @car_model = CarModel.find(params[:car_model_id]) unless params[:car_model_id].blank?

    @items = if params[:car_model_id].blank?
      Item.where(item_type_id: params[:item_type_id])
    else
      Item.where(item_type_id: params[:item_type_id], car_model_id: params[:car_model_id])
    end

    # items for selected car model from other item_types
    @items_related = if params[:car_model_id].blank?
      []
    else
      Item.where(car_model_id: params[:car_model_id]).where.not(item_type_id: params[:item_type_id]).order(:item_type_id)
    end

    respond_to do |format|
      format.html { render :index }
      format.js { render :index }
    end
  end

  def adm_index
    @items = Item.all
    @vendors = Vendor.all
    @item_types = ItemType.all

    @selected_item_type_id = items_search_params[:item_type_id]
    @selected_car_model_id = items_search_params[:car_model_id]
    @selected_vendor_id = items_search_params[:vendor_id]
  end

  # GET /items/1
  # GET /items/1.json
  def show
  end

  # GET /items/new
  def new
    @item = Item.new
    @item_image = @item.item_images.new
  end

  # GET /items/1/edit
  def edit
    @item_images = @item.item_images.all.order(:order)
    @item_image = @item.item_images.new
  end

  # POST /items
  # POST /items.json
  def create
    @item = Item.new(item_params)

    # add new images


    respond_to do |format|
      if @item.save
        unless params[:item_images].blank?
          params[:item_images]['image'].each do |i|
            @item_image = @item.item_images.create!(:image => i)
          end
        end
        format.html { redirect_to @item, notice: 'Item was successfully created.' }
        format.json { render :show, status: :created, location: @item }
      else
        format.html { render :new }
        format.json { render json: @item.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /items/1
  # PATCH/PUT /items/1.json
  def update
    respond_to do |format|
      if @item.update(item_params)

        if !@item.item_images.blank? && @item.item_images.last.image
          @item.item_images.each { |i| i.delete if i.image.blank? }
        end
        # add new images
        unless params[:item_images].blank?
          params[:item_images]['image'].each do |i|
            @item_image = @item.item_images.create!(:image => i)
          end
        end

        format.html { redirect_to @item, notice: 'Item was successfully updated.' }
        format.json { render :show, status: :ok, location: @item }
      else
        format.html { render :edit }
        format.json { render json: @item.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /items/1
  # DELETE /items/1.json
  def destroy
    @item.destroy
    respond_to do |format|
      format.html { redirect_to adm_index_items_url, notice: 'Товар был удалет' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_item
      @item = Item.find(params[:id])
    end

    def items_search_params
      params.permit(:item_type_id, :car_model_id, :vendor_id)
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def item_params
      params.require(:item).permit( :name, :description, :car_model_id, :item_type_id,
                                    :length, :height, :width, :weight, :color, :volume, :cost,
                                    item_images_attributes: [:id, :item_id, :image, :name, :order])
    end
end
