class CarModelsController < ApplicationController
  before_action :set_car_model, only: [:show, :edit, :update, :destroy]
  before_action ->{ @service_page = true }, except: [:index]
  protect_from_forgery except: :index
  # GET /car_models
  # GET /car_models.json
  def index
    @car_models = Item.
      where(item_type_id: cars_moedls_searsh[:item_type_id]). # select by type
      map { |x| x.car_model }. # to vendor
      compact. # remove nil
      delete_if { |x| x.vendor_id.to_s != cars_moedls_searsh[:vendor_id].to_s }. # select by vendors
      uniq # unique

    @vendor = Vendor.friendly.find(cars_moedls_searsh[:vendor_id]) unless cars_moedls_searsh[:vendor_id].blank?
    @item_type = ItemType.friendly.find(cars_moedls_searsh[:item_type_id]) unless cars_moedls_searsh[:item_type_id].blank?

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

  def adm_index
    @car_models = CarModel.all
  end

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

  # GET /car_models/new
  def new
    @car_model = CarModel.new
    @vendor = Vendor.find(cars_moedls_searsh[:vendor_id]) unless cars_moedls_searsh[:vendor_id].blank?
  end

  # GET /car_models/1/edit
  def edit
    @vendor = Vendor.find @car_model.vendor_id unless @car_model.vendor_id.blank?
  end

  # POST /car_models
  # POST /car_models.json
  def create
    @car_model = CarModel.new(car_model_params)

    respond_to do |format|
      if @car_model.save
        format.html { redirect_to @car_model, notice: 'Car model was successfully created.' }
        format.json { render :show, status: :created, location: @car_model }
      else
        format.html { render :new }
        format.json { render json: @car_model.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /car_models/1
  # PATCH/PUT /car_models/1.json
  def update
    respond_to do |format|
      if @car_model.update(car_model_params)
        format.html { redirect_to @car_model, notice: 'Car model was successfully updated.' }
        format.json { render :show, status: :ok, location: @car_model }
      else
        format.html { render :edit }
        format.json { render json: @car_model.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /car_models/1
  # DELETE /car_models/1.json
  def destroy
    @car_model.destroy
    respond_to do |format|
      format.html { redirect_to car_models_url, notice: 'Car model was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def car_model_params
      params.require(:car_model).permit(:name, :vendor_id)
    end
    def cars_moedls_searsh
      params.permit(:vendor_id, :item_type_id)
    end
end
