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

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

  before_action :admin_space
  
  def admin_space
    @admin_space = "payments"
  end
  
  def index
    @m_odr_virements = MOdrVirement.includes(:m_odr_rep)

    
    if params[:search][:m_odr_id].to_s != ""
      @m_odr_virements = @m_odr_virements.where(:m_odr_id => params[:search][:m_odr_id])
    end


    @m_odr_virements = sort_by_sorting(@m_odr_virements, "created_at DESC")
    
    params[:search][:per_page] = params[:search][:per_page] || 50
		per_page = params[:search][:per_page]
		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
		@m_odr_virements = @m_odr_virements.page(page).per(per_page)

  end

  def show
    @m_odr_virement = MOdrVirement.find(params[:id])

  end

  def new
    @m_odr_virement = MOdrVirement.new
  
  end

  def edit
    @m_odr_virement = MOdrVirement.find(params[:id])
    
  end

  def create
    @m_odr_virement = MOdrVirement.new(params.require(:m_odr_virement).permit!)

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

  end


  def update
    @m_odr_virement = MOdrVirement.find(params[:id])


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


  def destroy
    @m_odr_virement = MOdrVirement.find(params[:id])
    @m_odr_virement.destroy
    
  end
  
  def admin_valid
    @m_odr_virement = MOdrVirement.find(params[:id])
    @m_odr_virement.enabled = true
    @m_odr_virement.save
    
    @m_odr_remise = @m_odr_virement.m_odr_remise if @m_odr_virement.m_odr_remise
    
    
  end
  
  def refuse
    @m_odr_virement = MOdrVirement.find(params[:id])
    @m_odr_virement.refused = true
    @m_odr_virement.save
    @m_odr_virement.m_odr_rep.change_state("Erreur sur virement")
    
    @m_odr_rep = @m_odr_virement.m_odr_rep
    
    @m_odr_rep.send_mail_type("erreur-coordonnees-bancaire", @m_odr_virement)
    
    @m_odr_remise = @m_odr_virement.m_odr_remise if @m_odr_virement.m_odr_remise
    
  end
    
  
end
