class OfficeController < ApplicationController
    before_filter :check_access, :set_locale
  def activity
  end
  
  def get_office_activity
      source_array=ActiveDatum.global_filter(session[:user_id]).check_period(params[:start_time],params[:end_time])
      data=source_array\
            .group(	
			'"offices"."id"',
            '"offices"."name"'
	        ).joins(:login => :office).joins(:operation).perfomance_data('count')\
	        .select(
	        '"offices"."name" as office_name',
	        'COUNT(login_id) as opercount',
	        'COUNT(DISTINCT(login_id)) as uniqueuser',
	        '"offices"."id" as office_id'	
	        ).order(params[:order])
    all_logins=Login.global_filter(session[:user_id]).group(:office_id).select('COUNT(id) as alluser',:office_id)
    render :text=> {:data => data, :logins => all_logins}.to_json
  end
  
  ######################## Office List ##############################
  def get_office_list
   output_array=[]
   source_array=ActiveDatum.global_filter(session[:user_id]).check_period(params[:start_time],params[:end_time])
   data=source_array\
      .group(	
         :office_id,
         '"offices"."name"'
      ).joins(:login => :office).joins(:operation).perfomance_data('count')\
      .select(
		   '"offices"."name" as office_name',
		   :office_id,
           'COUNT("active_data"."login_id") as count'	
     ).order('COUNT("active_data"."login_id") DESC')
     render :text=> data.to_json(:except => [ :created_at, :updated_at ])
  end
  def get_office_hour_list
  	source_array=ActiveDatum.global_filter(session[:user_id]).check_period(params[:start_time],params[:end_time])
  	data=source_array.joins(:login).where('"logins"."office_id" = ?',params[:id])\
        .time_part_group('hour','end_time').select(
           'COUNT("active_data"."login_id") as count',
           'COUNT(DISTINCT("active_data"."login_id")) as users'
        )
  	render :text=> data.to_json(:except => [ :created_at, :updated_at ])
  end
  
  def get_office_list_user_list
    source_array=ActiveDatum.global_filter(session[:user_id]).check_period(params[:start_time],params[:end_time])
    data=source_array.perfomance_data('count')\
        .group(:login_id).joins(:operation).joins(:login)\
          .select(
          'COUNT(operation_id) as count',			
          :login_id
          ).where('"logins"."office_id" = ?',params[:officeid].to_i)      
      result=[]
      logins=Login.login_name('login_name').select(:id)
      summ = {:red => 0,:yellow => 0, :green => 0, :oper_count => 0, :unique_oper => 0, :user_count => 0, :active_user => 0}
      data.each do |row|
          for i in 0..logins.length-1
              if logins[i][:id] == row[:login_id]
                  result.push({
                      'login_id' => logins[i][:id],
                      'login_name' => logins[i][:login_name],
                      'red' => row[:red],
                      'yellow' => row[:yellow],
                      'green' => row[:green],
                      'count' => row[:count],
                      
                  })
                  summ[:red]+=row[:red]
                  summ[:yellow]+=row[:yellow]
                  summ[:green]+=row[:green]
                  summ[:oper_count]+=row[:count]
                  summ[:user_count]+=1
                  if row[:count] > 0
                      summ[:active_user]+=1
                  end
                  i=logins.length
              end
          end
      end
      summ[:unique_oper]=ActiveDatum.global_filter(session[:user_id]).check_period(params[:start_time],params[:end_time])\
          .where('"logins"."office_id" = ?',params[:officeid].to_i).joins(:login)\
          .select('COUNT(DISTINCT(operation_id)) as count')[0][:count]
      gt_table_export(params[:export],result,params[:headers]) && return
      render :text=> {
          :data => format_array_for_table(result,params[:order],params[:page],params[:offset]), 
          :count => result.length,
          :sum => summ
      }.to_json
  end
end
