module PsAdmin
  # Products
  class ProductController < ApplicationController
    skip_before_action :verify_authenticity_token, only: [:xedit]
    before_action :authenticate_admin!

    layout 'admin'

    def index
      @products = Product.order('name ASC')
      @products.each do |p|
        p.description_short =
          ActionController::Base.helpers.strip_tags(p.description_short)
        p.save
      end
    end

    def show
      @product = Product.where(id: params[:id]).first
    end

    def new
      @category = Category.where(id: params[:category_id]).first
      @product = Product.new(default_category_id: params[:category_id])
    end

    def create
      @product = Product.new(product_params)
      @category = Category.where(id: params[:product][:default_category_id]).first
      @product.updated_by = current_admin.id
      if @product.save
        @category.products << @product
        if params[:images]
          params[:images].each do |image|
            if @product.images.blank?
              @product.images.create(image: image, updated_by: current_admin.id,
                                     position: @product.images.size + 1,
                                     cover: true)
            else
              @product.images.create(image: image, updated_by: current_admin.id,
                                     position: @product.images.size + 1)
            end
          end
        end
        respond_to do |format|
          format.html do
            redirect_to controller: 'admin/category', action: 'index',
                        parent_id: @product.default_category_id
          end
          format.js {}
        end
      else
        render 'new'
      end
    end

    def edit
      @category = Category.where(id: params[:category_id]).first
      @product = Product.where(id: params[:id]).first
    end

    def update
      @product = Product.where(id: params[:id]).first
      @category = Category.where(id: params[:category_id]).first
      @product.updated_by = current_admin.id
      if @product.update_attributes(product_params)
        respond_to do |format|
          format.html { redirect_to controller: 'admin/category', action: 'index', parent_id: @category.id }
          format.js {}
        end
      else
        render 'edit'
      end
    end

    def destroy
      @product = Product.find(params[:id])
      @category = Category.where(id: params[:category_id]).first
      unless @product.blank?
        if @category.id == @product.default_category_id
          @category.products.delete(@product) if @product.destroy
        else
          @category.products.delete(@product)
        end
      end
      respond_to do |format|
        format.html do
          redirect_to controller: 'admin/category',
                      action: 'index', parent_id: @category.id
        end
        format.js {}
      end
    end

    def set_active
      if !params[:id].blank? && !params[:active].blank?
        @product = Product.where(id: params[:id]).first
        unless @product.blank?
          @product.active = params[:active].to_i
          @product.save
          unless params[:category_id].blank?
            @category = Category.where(id: params[:category_id]).first
          end
        end
      end
    end

    def set_chk_quantity
      if !params[:id].blank? && !params[:chk_qnt].blank?
        @product = Product.where(id: params[:id]).first
        unless @product.blank?
          @product.chk_quantity = params[:chk_qnt].to_i
          @product.save
          unless params[:category_id].blank?
            @category = Category.where(id: params[:category_id]).first
          end
        end
      end
    end

    def xedit
      @product = Product.where(id: params[:pk]).first
      unless @product.blank?
        if params[:name] == 'price'
          @product.price = params[:value]
        elsif params[:name] == 'qty'
          @product.quantity = params[:value]
        end
        if @product.save
          head :ok
        else
          render json: { success: false, msg: "Błąd zapisu" }
        end
      end
    end

    # tylko przy poczatkowym imporcie
    def import_products
      pcls = PsProductLang.where('id_lang = 3')
      for pcl in pcls
        product = Product.where(id: pcl.id_product).first
        next unless product.blank?
        pc = PsProduct.where('id_product = ?', pcl.id_product).first
        next if pc.blank?
        prod = Product.new(description: pcl.description, description_short: ActionController::Base.helpers.strip_tags(pcl.description_short), default_category_id: pc.id_category_default,
                           price: pc.price, quantity: pc.quantity, active: pc.active, name: pcl.name, created_by: current_admin.id, updated_by: current_admin.id)
        prod.id = pc.id_product
        prod.save
      end
      render text: 'Done'
    end

    private

    def product_params
      params.require(:product).permit(:name, :description, :description_short,
                                      :price, :quantity, :active, :chk_quantity,
                                      :default_category_id)
    end
  end
end
