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

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

  before_action :admin_space
  
  def admin_space
    @admin_space = "cartes"
  end
  
  def index
    @volume_periodiques = VolumePeriodique.all

    @volume_periodiques = @volume_periodiques.where(:codeindicateur => "MGI000000001")
    if params[:v_contact_id].to_s != ""
      params[:codemanaginn] = VContact.find(params[:v_contact_id]).codemanaginn
    end

    if params[:codemanaginn].to_s != ""
      @volume_periodiques = @volume_periodiques.where(:codemanaginn => params[:codemanaginn])
    end
    
    @volume_periodiques = @volume_periodiques.where(:codesociete => current_admin.societes.pluck(:socmanaginn))
    
    @volume_periodiques = sort_by_sorting(@volume_periodiques, "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 
        @volume_periodiques = @volume_periodiques.page(page).per(per_page)
      
      }
    end
  end

  def show
    @volume_periodique = VolumePeriodique.find(params[:id])

  end

  def new
    @volume_periodique = VolumePeriodique.new(:codemanaginn => params[:codemanaginn], :codesociete => params[:codesociete])
    @volume_periodique.datedebut = Time.now
  
  end

  def edit
    @volume_periodique = VolumePeriodique.find(params[:id])
    
  end

  def create
    @volume_periodique = VolumePeriodique.new(params.require(:volume_periodique).permit!)
    @v_contact = @volume_periodique.v_contact
    
    
    @volume_periodique.super_admin_power = true if current_admin.super_admin?
    
    
    if @volume_periodique.save
      
    else
      render action: "new"
 
    end

  end


  def update
    @volume_periodique = VolumePeriodique.find(params[:id])
    @v_contact = @volume_periodique.v_contact

    @volume_periodique.super_admin_power = true if current_admin.super_admin?

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


  def destroy
    @volume_periodique = VolumePeriodique.find(params[:id])
    @volume_periodique.destroy
    
  end
end
