class Public::CustomersController < ApplicationController
  layout "public"

  before_filter :auth_customer, :except => [:new, :create, :confirm, :show, :parrainage, :mail, :find_parrain]


  def parrainage
    cookies.permanent[:mlm_token] = params[:mlm_token]
    redirect_to "/"
  end

  def index

    @customers = Customer.all
  end


  def show
	  @customer = current_customer.own_customers.find(params[:id])

  end


  def new


    @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 show_details
    @no_search = true
    if current_customer.mlm_children_ids.include?(params[:id].to_i)

      @customer = Customer.find(params[:id])


    end

  end

  def edit
    @customer = Customer.find(params[:id])

  end



  def create
    params[:step] = "login"
    @no_search = true
  	@customer = Customer.new(params.require(:customer).permit!)

    if @customer.save

      CustomerMailer.confirm(@customer).deliver
      CustomerMailer.notify_ins(@customer).deliver


      @customer.authenticate(params[:password])

      cookies[:customer_auth_token] = @customer.token


      if session[:for_annonce]
        redirect_to new_public_annonce_path(:ins => "ins_finish"), notice: "Vous êtes désormais inscrit."
      else
        redirect_to public_my_account_path(:ins => "ins_finish"), notice: "Vous êtes désormais inscrit."
      end

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


  	else

  		render :action => "new"
  	end
  end

  def confirm
    @customer = Customer.find_by_token(params[:id])
    @customer.enabled = true
    @customer.save(:validate => false)

    CustomerMailer.confirm_ins(@customer).deliver

    cookies[:customer_auth_token] = @customer.token

    redirect_to public_my_account_path, notice: "Votre adresse mail a bien été validée, merci beaucoup."
    #redirect_to "/"
  end


  def update
    @customer = Customer.find(params[:id])

    @accepted_offers = @customer.accepted_offers.order(created_at: :desc).page(params[:page_offers]).per(5)
    @wishes = @customer.wishes.includes(:need).page(params[:page_wishes]).per(5)

    @needs = Kaminari.paginate_array(@customer.owned_needs.order(created_at: :desc))
      .page(params[:page_needs])
      .per(5)

    if params[:order]

    @customer.force_address = true
    end

    if @customer == current_customer


      if @customer.update_attributes(params.require(:customer).permit!)

        flash[:success] =  "Vos informations ont bien été enregistrées"
        redirect_to public_my_account_path

    	else
        flash[:error] =  "Certains champs sont invalides"
        render :template => "public/my_account/edit_infos"

      end
    end
  end






  def mail
    @no_search = true
    @customer = Customer.find_by_id(params[:id])
      
    if @customer
      @customer_message = CustomerMessage.new(:destinataire => @customer)
     
    
      @customer_message.expediteur  = current_customer if current_customer
      if @customer_message.expediteur
        @customer_message.expediteur_mail = current_customer.email

      end
    end
    
  end


end
