# -*- encoding : utf-8 -*-
class Public::CustomersAuthsController < ApplicationController
  layout "public"

  def new
    @biglogo = true
    @no_search = true
     params[:step] = "login"
    if params[:for_annonce]
      session[:for_annonce] = true

    else
      session[:for_annonce] = nil
    end

    @customer = Customer.new()
    if cookies[:mlm_token] and @parent = Customer.find_parrain(cookies[:mlm_token])
      @customer.parent_code = @parent.mlm_token.upcase

    elsif params[:p] and @parent = Customer.find_parrain(params[:p])
      @customer.parent_code = @parent.mlm_token.upcase

    end


  end

  def create

    @biglogo = true
    @customer = Customer.new()
    if cookies[:mlm_token] and @parent = Customer.find_parrain(cookies[:mlm_token])
      @customer.parent_code = @parent.mlm_token.upcase

    elsif params[:p] and @parent = Customer.find_parrain(params[:p])
      @customer.parent_code = @parent.mlm_token.upcase
    end

    params[:step] = "login"
    @no_search = true
    user = Customer.find_by_email(params[:email])
    if user && user.authenticate(params[:password])
      #session[:forum_user_id] = user.id


      if params[:remember_me]
        cookies.permanent[:customer_auth_token] = user.token
      else
        cookies[:customer_auth_token] = user.token
      end

      user.last_sign_in_at = Time.now
      user.save(:validate => false)

      if cookies[:video_read] == "true"
        flash[:read_video] = false
      else
        cookies.permanent[:video_read] = "true"
        flash[:read_video] = true
      end

      redirect_to :root
    else
      flash.now.alert = "Email ou mot de passe incorect"

      render :action => "new"
    end
  end

  def logout
    cookies.delete(:customer_auth_token)
    redirect_to "/", notice: "Déconnecté."
  end
end
