class Public::ProductCustomersController < ApplicationController
  layout "public"
  
  before_filter :auth_product_customer, :except => [:new, :create, :confirm, :find_parrain, :parrainage]
  
 
  def index
    
    @product_customers = ProductCustomer.all
  end




  def new
    if params[:r] and params[:r] != ""
      session[:before_auth_url] = nil
    end
    
    
    @no_search = true
    if params[:for_annonce] and params[:for_annonce] != ""
      session[:for_annonce] = true
      params[:step] = "login"
    else
      session[:for_annonce] = nil
      params[:for_annonce] = nil
    end
    
     
    
    params[:for_mail] =nil if params[:for_mail] == ""
    
    @product_customer = ProductCustomer.new()
    if cookies[:mlm_token] and @parent = ProductCustomer.find_parrain(cookies[:mlm_token]) 
      @product_customer.parent_code = @parent.mlm_token.upcase
    
    elsif params[:p] and @parent = ProductCustomer.find_parrain(params[:p]) 
      @product_customer.parent_code = @parent.mlm_token.upcase
      
    end 
    
    if cookies[:mlm_token] and @parent = ProductCustomer.find_parrain(cookies[:mlm_token]) 
      @product_customer.parent_code = @parent.mlm_token.upcase
    
    elsif params[:p] and @parent = ProductCustomer.find_parrain(params[:p]) 
      @product_customer.parent_code = @parent.mlm_token.upcase
      
    end  

  end


  def edit
    @product_customer = ProductCustomer.find(params[:id])
  
  end

  

  def create
    

  	@product_customer = ProductCustomer.new(params.require(:product_customer).permit!)
    @product_customer.remote_ip = request.remote_ip
    
    if @product_customer.save
      #ProductCustomerMailer.confirm(@product_customer).deliver
      
      @product_customer.authenticate(params[:password])
      
      cookies[:product_customer_auth_token] = @product_customer.token
      
      
      redirect_to particulars_public_product_orders_path
    
          
  	else
  		render :action => "new" 
  	end
  end

  def confirm
    @product_customer = ProductCustomer.find_by_token(params[:id])
    @product_customer.enabled = true
    @product_customer.save(:validate => false)
    
    
    cookies[:product_customer_auth_token] = @product_customer.token
    
    redirect_to public_my_account_path
    #redirect_to "/"
  end
  
  
  def update
    @product_customer = ProductCustomer.find(params[:id])
    
    if params[:order]
      
    @product_customer.force_address = true  
    end
    
    if @product_customer == current_product_customer
    	
    
      if @product_customer.update_attributes(params.require(:product_customer).permit!)
        
        if params[:order]
          
    
          @product_customer = current_product_customer
          @order.product_customer = @product_customer
          
          render :template => "public/orders/new"
        else
          
          redirect_to public_my_account_path
        end
    	else
        if params[:order]
          @order = Order.new(session[:order])
    		  render :template => "public/orders/billing"
        else
          render :template => "public/my_account/edit_infos" 
          
        end
      end
    end
  end
  
  def find_parrain
    @parent = ProductCustomer.find_parrain(params[:parent_code]) 

    if @parent
      cookies.permanent[:mlm_token] = @parent.mlm_token
    else
      cookies.permanent[:mlm_token] = nil
    end
    render layout: false
  end
  
  
  def parrainage
    cookies.permanent[:mlm_token] = params[:mlm_token]
    redirect_to "/"
  end
    
  		



end
