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

class Admin::PCustomerSheetsController < ApplicationController
  layout "admin"
  before_action :auth_admin

  before_action :admin_space
 
  def admin_space
    @admin_space = "ventes"
  end

  def index
    if !params[:column]
      params[:column] = "created_at"
      params[:direction] = "asc"
      
    end
    @p_customer_sheets = PCustomerSheet.all
    
		per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 50
		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    
    
    if current_admin.p_commercial
      @p_customer_sheets = @p_customer_sheets.where(:p_commercial_id => current_admin.p_commercial.id )
    end
    
    @p_customer_sheets = @p_customer_sheets.joins(:price_line_block)
    @p_customer_sheets = sort_by_sorting(@p_customer_sheets)
		@p_customer_sheets = @p_customer_sheets.page(page).per(per_page)
   
  end

  def show
    @p_customer_sheet = PCustomerSheet.find(params[:id])

  end
  
  

  def new
    
    
		@p_customer_sheet = PCustomerSheet.new()
    
    @p_customer = PCustomer.find(params[:p_customer_id]) if params[:p_customer_id]
    
    @p_customer_sheet.p_customer = @p_customer
    
    @p_customer_sheet.price_line_block = PriceLineBlock.new()
    @p_customer_sheet.price_line_block.price_lines.build
    @p_customer_sheet.price_line_block.p_customer = @p_customer
  end

 

  def edit
  
    
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    
    @p_customer = @p_customer_sheet.p_customer
  end

  def create
    @p_customer_sheet = PCustomerSheet.new(params.require(:p_customer_sheet).permit!)
    
    @p_customer = @p_customer_sheet.p_customer

    if @p_customer_sheet.save
     @p_customer_sheets = PCustomerSheet.order(:id).all
      redirect_to admin_p_customer_sheet_path(@p_customer_sheet)
    else
      render action: "new"
 
    end

  end


  def update
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer = @p_customer_sheet.p_customer

    
    if @p_customer_sheet.update_attributes(params.require(:p_customer_sheet).permit!)
      
       @p_customer_sheets = PCustomerSheet.order(:id).all
      
       redirect_to admin_p_customer_sheet_path(@p_customer_sheet)
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.destroy
    
    
  end
  
  
  def generate_d
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_d
    
    redirect_back(fallback_location: admin_p_customer_sheets_path)
    
  end
  
  def generate_fp
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_fp
    
    redirect_back(fallback_location: admin_p_customer_sheets_path)
    
  end
  
  def generate_bc
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_bc
    
    redirect_back(fallback_location: admin_p_customer_sheets_path)
    
  end
  
  def generate_bl
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_bl
    
    redirect_back(fallback_location: admin_p_customer_sheets_path)
    
  end
  
  def generate_f
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.generate_f
    
    redirect_back(fallback_location: admin_p_customer_sheets_path)
    
  end
  
  def reject
    @p_customer_sheet = PCustomerSheet.find(params[:id])
    @p_customer_sheet.state = "refusée"
    
    @p_customer_sheet.save
    @p_customer_sheet.archive_now if !@p_customer_sheet.archived
    
    redirect_back(fallback_location: admin_p_customer_sheets_path)
    
  end
  
  
  
  
end