class Admins::NpsController < Admins::BaseController
  before_action :require_business
  def index
    @on_off_switch = EduSetting.get("nps-on-off-switch").to_s == 'true'
    @user_nps = UserNp.order(created_at: :desc)
    keyword = params[:keyword].to_s.strip.presence
    if keyword
      sql = 'CONCAT(users.lastname, users.firstname) LIKE :keyword OR users.nickname LIKE :keyword OR users.login LIKE :keyword OR users.mail LIKE :keyword OR users.phone LIKE :keyword'
      @user_nps = @user_nps.where(sql, keyword: "%#{keyword}%")
    end
    @user_nps = @user_nps.where("action_type != 'close'") if params[:done_score].present?
    @min_score = @user_nps.where("action_type != 'close'").minimum("score")
    @max_score = @user_nps.where("action_type != 'close'").maximum("score")
    @score_total_count = UserNp.where("action_type !='close'").count
    @user_nps = paginate @user_nps.includes(:user)
  end

  def switch_change
    edu_setting = EduSetting.find_by(name: "nps-on-off-switch")
    if edu_setting.blank?
      edu_setting = EduSetting.new(name: "nps-on-off-switch")
    end
    edu_setting.value = params[:switch].to_s
    edu_setting.save
    render_ok
  end
end
