# -*- encoding : utf-8 -*-
class Public::PCustomerAuthsController < ApplicationController
  layout "public"
  
  before_action :auth_p_customer, :only => [:index, :edit_password]
  
  def index
    
    #current_p_customer.consolide_operations
    #current_p_customer.get_particular
    @title = "Mon intranet"
    
  end
  
  def new
    @p_customer = true
  end

  def create
    @p_customer = true
    user = PCustomer.find_by_email(params[:email])
    if user && user.authenticate(params[:password])
      #session[:p_customer_user_id] = user.id
      
      
      if params[:remember_me]
        cookies.permanent[:p_customer_auth_token] = user.auth_token
      else
        cookies[:p_customer_auth_token] = user.auth_token
        
      end
      

      
      redirect_to public_my_account_path
      
      
      
      
      
    else
      flash.now.alert = "Email ou mot de passe incorect"
      render "new"
    end
  end
  
  def logout
    cookies.delete(:p_customer_auth_token)
    cookies.delete(:p_customer_auth_token)
    redirect_to public_my_account_path
  end
  
  
  
  
  def edit_password
    @edit_p_customer = current_p_customer
    @edit_p_customer.particular.skip_validation = false
    
    
  end
  
  
  def update_password
    @edit_p_customer = current_p_customer
    @edit_p_customer.actual_password = params[:p_customer][:actual_password]
    @edit_p_customer.valid_pswd_confirmation = true
    
    
    
    @edit_p_customer.password = params[:p_customer][:password]
    @edit_p_customer.password_confirmation = params[:p_customer][:password_confirmation]
    
    
    @edit_p_customer.valid_last_password = true
    
    
    
    
    if @edit_p_customer.save
      redirect_to public_my_account_path, :notice => "Votre mot de passe est à jour."
    else
      @edit_p_customer.verify_actual_password
      render :action => :edit_password
    end
    
  end
  
  
  def password_reset
    
  end
  
  def save_password_reset
    @edit_p_customer = PCustomer.where(:email => params[:email]).first
    
    if @edit_p_customer
      @edit_p_customer.generate_token(:reset_password_token)
      @edit_p_customer.reset_password_sent_at = Time.now
      
      @edit_p_customer.save
      
      #GeneralMailer.send_qi_mail("fr", "espace-donateur-mot-de-passe-perdu", @edit_p_customer.email, {"email" => @edit_p_customer.email, "token" => @edit_p_customer.reset_password_token,"lien_mot_de_passe" => edit_password_reset_public_p_customer_auths_url(:token => @edit_p_customer.reset_password_token)}).deliver
      
      #GeneralMailer.send_qi_mail("fr", "reset_mdp", @edit_p_customer.email, {:token => @edit_p_customer.reset_password_token, :lien_mot_de_passe: => "<a href='#{edit_password_reset_public_p_customer_auths_url(:token => @edit_p_customer.reset_password_token)}'>#{edit_password_reset_public_p_customer_auths_url(:token => @edit_p_customer.reset_password_token)}</a>"}).deliver
      
      mail_hist = MailHist.generate_mail(:fr, MailType.find_or_create("reset_mdp"), @edit_p_customer.email, {:arguments => {:token => @edit_p_customer.reset_password_token, :lien_mot_de_passe => "<a href='#{edit_password_reset_public_p_customer_auths_url(:token => @edit_p_customer.reset_password_token)}'>#{edit_password_reset_public_p_customer_auths_url(:token => @edit_p_customer.reset_password_token)}</a>", :civilite => @edit_p_customer.particular.civilite, :nom => @edit_p_customer.particular.name, :prenom => @edit_p_customer.particular.firstname  }, :p_customer => @edit_p_customer, :element => @edit_p_customer})



      redirect_to public_p_customer_auths_path, :notice => "Un email vous a été envoyé"
      
    else
      redirect_to :back, :notice => "Cet email ne possède pas de compte"
    end
    
    
    
  end
  
  
  def edit_password_reset
    
    @edit_p_customer = PCustomer.where(:reset_password_token => params[:token]).first
    
    if @edit_p_customer
      
      
    else
      redirect_to public_p_customer_auths_path, :notice => "Le compte n'a pas été retrouvé"
    end
    
  end
  
  
  
  def save_edit_password_reset
    
    puts "AAAA"
    
    puts params[:token]
    
    puts PCustomer.where(:reset_password_token => params[:token]).count
    @edit_p_customer = PCustomer.where(:reset_password_token => params[:token]).first
    
    
    #sdffsdfds = fsdsfd
    
    
    if @edit_p_customer
      @edit_p_customer.password = params[:p_customer][:password]
      @edit_p_customer.password_confirmation = params[:p_customer][:password_confirmation]
      @edit_p_customer.valid_pswd_confirmation = params[:p_customer][:valid_pswd_confirmation]
      
      if @edit_p_customer.save
        redirect_to public_p_customer_auths_path, :notice => "Votre mot de passe a été mis à jour"
      else
        render :action => :edit_password_reset
      end
      
      
    else
      redirect_to public_p_customers_path, :notice => "Le compte n'a pas été retrouvé"
    end
    
  end
  
  
  
  
  
end