# -*- encoding : utf-8 -*-

class Public::KapsmesController < ApplicationController
  layout "public"
  before_filter :auth_p_customer, :except => :validate_by_token
  skip_before_action :update


  def validate_by_token
    @kapsme = Kapsme.where(:token => params[:id]).first
    @kapsme.done = true
  
    @kapsme.save
    
    
    user = @kapsme.p_customer
    
    cookies[:p_customer_auth_token] = user.token
    
    
    user.last_sign_in_at = Time.now
    
    
    user.save(:validate => false)
    
    
         
    
    
    redirect_to today_public_kaps_days_path
    
    
    
    
  end

  def index
    @kapsmes = Kapsme.order(:name).all

   
  end

  def show
    @kapsme = Kapsme.find(params[:id])

  end

  def new
    
    
		@kapsme = Kapsme.new
    

  end

 

  def edit
  
    
    @kapsme = Kapsme.find(params[:id])
  end

  def create
    @kapsme = Kapsme.new(params.require(:kapsme).permit!)
    @kapsme.p_customer = current_p_customer

    if @kapsme.save
     @kapsmes = Kapsme.order(:name).all
      
    else
      render action: "new"
 
    end

  end


  def update
    @kapsme = Kapsme.find(params[:id])


    if @kapsme.update_attributes(params.require(:kapsme).permit!)
      
      @kaps_day = @kapsme.kaps_day
      
      @kapsmes = Kapsme.order(:name).all
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @kapsme = Kapsme.find(params[:id])
    @kapsme.destroy
    
    
  end
end
