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

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

  before_action :admin_space
  
  def admin_space
    @admin_space = "ventes"
  end
  
  def index
    @order_hists = OrderHist.all


    if current_admin.p_commercial
      @order_hists = @order_hists.where(:p_commercial_id => current_admin.p_commercial.id )
      
    else
      if params[:search][:p_commercial_id].to_s != ""
        if params[:search][:p_commercial_id].to_s == "null"
          @order_hists = @order_hists.where(:p_commercial_id => nil)
        else
          @order_hists = @order_hists.where(:p_commercial_id => params[:search][:p_commercial_id])
        end
      end
      
      
      
    end
    
    
    @order_hists = sort_by_sorting(@order_hists, "id DESC")
    respond_to do |format|
      format.html{
      
        params[:search][:per_page] = params[:search][:per_page] || 100
        per_page = params[:search][:per_page]
        page = (params[:page] and params[:page] != "") ? params[:page] : 1 
        @order_hists = @order_hists.page(page).per(per_page)
      
      }
    end
  end

  def show
    @order_hist = OrderHist.find(params[:id])

  end

  def new
    @order_hist = OrderHist.new
  
  end

  def edit
    @order_hist = OrderHist.find(params[:id])
    
  end

  def create
    @order_hist = OrderHist.new(params.require(:order_hist).permit!)

    if @order_hist.save
      
    else
      render action: "new"
 
    end

  end


  def update
    @order_hist = OrderHist.find(params[:id])


    if @order_hist.update_attributes(params.require(:order_hist).permit!)
      
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @order_hist = OrderHist.find(params[:id])
    @order_hist.destroy
    
  end
end
