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

class Admin::PShipToursController < ApplicationController
  layout "admin"
  before_action :auth_admin, :except => [:adr]

  before_action :admin_space
  
  
  def generate_bills
    @p_ship_tour = PShipTour.find(params[:id])
    @p_ship_tour.generate_bills
    
    redirect_to :back
  end
  
  def adr
    @p_ship_tour = PShipTour.find(params[:id])
    params[:inline] = true
    if !params[:html]
      doc_number = "tournee-"+params[:id].to_s
      url = adr_admin_p_ship_tour_path(:id => params[:id],:html => true)
    
      @temp_file = "#{Rails.root}/pdf/p_documents/ADR-#{doc_number}.pdf"
    
    
    
    
      url = (Rails.env.development? ? "http://localhost:4000" : "http://3p.quartz.xyz").to_s+url
      puts url
      pdf = "pdfadr"
      command = "node #{pdf}.js #{Shellwords.escape(url)} #{Shellwords.escape(@temp_file)}"
      puts command
      system(command)
  
      @data_to_send = File.open( @temp_file).read
     
    
      send_data @data_to_send, :filename =>"#{doc_number}.pdf" , :type => 'application/pdf',:disposition => (params[:inline] ? 'inline' : "attachment")
    else
       render :layout => false
    end
    
    
    
    
    
  end
  
  def to_ship
    @p_ship_tour = PShipTour.find(params[:id])
    @p_ship_tour.to_ship
    redirect_to :back
  end
  
  def to_unship
    @p_ship_tour = PShipTour.find(params[:id])
    @p_ship_tour.to_unship
    redirect_to :back
  end
  
  
  def unship_return
    @p_ship_tour = PShipTour.find(params[:id])
    @p_ship_tour.undone_return
    redirect_to :back
  end
  
  def ship_return
    @p_ship_tour = PShipTour.find(params[:id])
    
    
    if false
      @p_ship_tour.p_ship_tour_trucks.each do |p_ship_tour_truck|
      
        i = 0.0
      
        if p_ship_tour_truck.p_volucompteurs.count == 0
          last_volucompteur = PVolucompteur.where(:p_truck_id => p_ship_tour_truck.p_truck_id).order("end_vol DESC").first
      
          i = last_volucompteur.end_vol if last_volucompteur
      
          p_ship_tour_truck.p_volucompteurs.new(:start_vol => i, :p_truck_id => p_ship_tour_truck.p_truck_id) if !p_ship_tour_truck.remorque
    
        end
      
      end
    end
  end
  
  def ship_return_save
    @p_ship_tour = PShipTour.find(params[:id])
    #@p_ship_tour.reset_validation
    
    
    test_var = true
    
    @p_ship_tour.p_ship_tour_trucks.each do |p_ship_tour_truck|
      if !p_ship_tour_truck.ok?
        test_var = false
        break
      end
    end
    
    
    if test_var
    

      
      
     
     # @p_ship_tour.p_ship_tour_trucks.each do |p_ship_tour_truck|
    #     p_ship_tour_truck.p_volucompteurs.each do |p_volucompteur|
     #      p_volucompteur.check_delta = true
      #     p_volucompteur.valid?
        
      #   end
      #end
      
      
      
      #render :inline => @p_ship_tour.valid?
     # if @p_ship_tour.save
      
        @p_ship_tour.done_return
        
        redirect_to admin_p_ship_tour_path(@p_ship_tour)
    #  else
    #    render action: "ship_return"
    #  end

    else
     
     redirect_to ship_return_admin_p_ship_tour_path(@p_ship_tour), :flash => {:error => "Des erreurs empêchent la sauvegarde, vérifier les emplacements rouges."}
 
    end
    
  end
  
  
  def admin_space
    @admin_space = "tournees"
  end
  
  def index
    @p_ship_tours = PShipTour.order("start_at DESC").all

   
  end

  def show
    @p_ship_tour = PShipTour.find(params[:id])

  end

  def new
    
    
		@p_ship_tour = PShipTour.new(:start_at => Time.now)
    
    PTruck.all.each do |p_truck|
      p_ship_tour_truck = PShipTourTruck.new(:p_truck => p_truck)
      p_truck.p_truck_spaces.each do |p_truck_space|
        p_ship_space = PShipSpace.new(:p_truck_space => p_truck_space)
        p_ship_tour_truck.p_ship_spaces << p_ship_space
      end
      @p_ship_tour.p_ship_tour_trucks << p_ship_tour_truck
      
    end
    

  end

 

  def edit
  
    
    @p_ship_tour = PShipTour.find(params[:id])
  end

  def create
    @p_ship_tour = PShipTour.new(params.require(:p_ship_tour).permit!)


    if @p_ship_tour.save
     @p_ship_tours = PShipTour.order("start_at DESC").all
      
    else
      render action: "new"
 
    end

  end


  def update
    @p_ship_tour = PShipTour.find(params[:id])


    if @p_ship_tour.update_attributes(params.require(:p_ship_tour).permit!)
      
       @p_ship_tours = PShipTour.order("start_at DESC").all
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @p_ship_tour = PShipTour.find(params[:id])
    @p_ship_tour.destroy
    
    
  end
end
