# encoding: UTF-8
module Admin
  class ProductCategoriesController < Admin::ApplicationController
    before_filter :set_relation_options, only: [:new, :edit, :create, :update]
    # GET /product_categories
    # GET /product_categories.json
    def index
      @product_categories = ProductCategory.all

      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @product_categories }
      end
    end

    # GET /product_categories/1
    # GET /product_categories/1.json
    def show
      @product_category = ProductCategory.find(params[:id])

      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @product_category }
      end
    end

    # GET /product_categories/new
    # GET /product_categories/new.json
    def new
      @product_category = ProductCategory.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @product_category }
      end
    end

    # GET /product_categories/1/edit
    def edit
      @product_category = ProductCategory.find(params[:id])
    end

    # POST /product_categories
    # POST /product_categories.json
    def create
      @product_category = ProductCategory.new(product_category_params)
      @product_category.supplier_id = params[:product_category][:supplier_id]

      respond_to do |format|
        if @product_category.save
          format.html { redirect_to [:admin, @product_category], notice: t('action.create.successfull', model: ProductCategory.model_name.human) }
          format.json { render json: @product_category, status: :created, location: @product_category }
        else
          format.html { render action: "new" }
          format.json { render json: @product_category.errors, status: :unprocessable_entity }
        end
      end
    end

    # PUT /product_categories/1
    # PUT /product_categories/1.json
    def update
      @product_category = ProductCategory.find(params[:id])

      respond_to do |format|
        if @product_category.update_attributes(product_category_params)
          format.html { redirect_to [:admin, @product_category], notice: t('action.update.successfull', model: ProductCategory.model_name.human) }
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @product_category.errors, status: :unprocessable_entity }
        end
      end
    end

    # DELETE /product_categories/1
    # DELETE /product_categories/1.json
    def destroy
      @product_category = ProductCategory.find(params[:id])
      @product_category.destroy

      respond_to do |format|
        format.html { redirect_to admin_product_categories_path, notice: t('action.destroy.successfull', model: ProductCategory.model_name.human) }
        format.json { head :no_content }
      end
    end

    # GET /product_categories/qrcode
    # GET /product_categories/qrcode.png
    # GET /product_categories/qrcode.svg
    def qrcode
      respond_to do |format|
        format.html
        format.svg { render :qrcode => request.url, :level => :l, :unit => 10 }
        format.png { render qrcode: request.url }
      end
    end

    private

    def set_relation_options
      @suppliers = Supplier.all
      @lists = List.all
    end

    def product_category_params
      params.require(:product_category).permit!
    end
  end
end
