module PsAdmin
  # ustawienia glowne aplikacji
  class SettingsController < ApplicationController
    skip_before_action :verify_authenticity_token, only: [:xedit]
    before_action :authenticate_admin!
    before_action :set_settings
    layout 'admin'
    def index; end

    def home_page_update
      @sc_setting.update_attributes(sc_setting_params)
      @sc_setting.save
    end

    def xedit
      if params[:name] == 'shop_owners_emails'
        @sc_setting.shop_owners_emails = params[:value]
      elsif params[:name] == 'shop_name'
        @sc_setting.shop_name = params[:value]
      elsif params[:name] == 'shop_active'
        @sc_setting.shop_active = params[:value]
      elsif params[:name] == 'chk_quantity_global'
        @sc_setting.chk_quantity_global = params[:value]
        ProductsQuantityCheckChangeJob.perform_later params[:value]
      end
      if @sc_setting.save
        head :ok
      else
        render json: { success: false, msg: 'Błąd zapisu' }
      end
    end

    private

    def set_settings
      @sc_setting = ScSetting.first
    end

    def sc_setting_params
      params.require(:sc_setting)
            .permit(:index_page_content, :index_page_second_info,
                    :index_page_first_info)
    end
  end
end
