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

class Public::MOdrFilesController < ApplicationController
  layout "public"
  
  before_action :auth_p_customer
  
  def index
    @m_odr_files = MOdrFile.all

    
  end


  def show
    @m_odr_file = MOdrFile.find(params[:id])

  end

  def new
    @m_odr_file = MOdrFile.new(:m_odr_file_type_id => params[:m_odr_file_type_id])
  
    if @m_odr_file.m_odr_file_type_id == 2
      @m_odr_file.m_odr_file_products.build
      @m_odr_file.m_odr_file_products.build
      
    else
      @m_odr_file.m_odr_file_roulages.build
    end
  end

  def edit
    @m_odr_file = MOdrFile.find(params[:id])
    
  end
  
  
  def download
    @m_odr_file = MOdrFile.find(params[:id])
    
    if @m_odr_file.file
      send_file @m_odr_file.file.path, :disposition => (params[:disposition] || :inline)
    end
  end



  def create
    @m_odr_file = MOdrFile.new(params.require(:m_odr_file).permit!)
    @m_odr_file.p_customer = current_p_customer
    if @m_odr_file.save
      
      #@m_odr_file.m_odr_rep.change_state("En cours de traitement")
      redirect_to public_my_account_path()
    else
      render action: "new"
 
    end

  end


  def update
    @m_odr_file = MOdrFile.find(params[:id])
    @m_odr_rep = @m_odr_file.m_odr_rep

    if @m_odr_file.update_attributes(params.require(:m_odr_file).permit!)
    
      #@m_odr_file.m_odr_rep.change_state("En cours de traitement")
      redirect_to admin_m_odr_files_path()
    else
     render action: "edit" 
      
    end
    
    
    
      
  end


  def destroy
    @m_odr_file = MOdrFile.find(params[:id])
    @m_odr_file.destroy
    
  end

  def rotate
    deg = params[:direction] == "right" ? -90 : 90
    
    @m_odr_file = MOdrFile.find(params[:id])
    @m_odr_file.rotate(deg)
  end


end
