class MailerController < ApplicationController
  # Авторизация (файл: controllers/application.rb)
 before_filter :isAuthorization

  # Конструктор
 before_filter :construct

  def construct

    # Определяется при авторизации
    @isAdmin = session[:isAdmin].to_s
    @isRead = session[:isRead].to_s
    @isWrite = session[:isWrite].to_s
    @isDelete = session[:isDelete].to_s
    @isSuperUser = session[:isSuperUser].to_s

    if(@isAdmin)
      if(params[:d]===nil) then $department = '' else $department = params[:d].gsub(/[^a-z]/, '') end
    else
      if(cookies[:sys_dep]===nil) then $department = '' else $department = cookies[:sys_dep].gsub(/[^a-z]/, '') end
    end

    # Пункты меню
      $menu =
        [
          {"url"=>"/"+$department+"", "title"=>"Вернуться в систему отчетов"}
        ]


  end

  
  def index
    @error = ""

    # OLD
    if(@isAdmin!='true')
      if(@isSuperUser=='true')
        if($department!='')

          checkDeparment = UserAccessWork.isSetDepAccess($department)
          if(checkDeparment == false)
            redirect_to :action => "error", :code=>2
          else
            redirect_to :action => "edit", :d => $department
          end

        else
          redirect_to :action => "error", :code=>1
        end
      else
        redirect_to :action => "error", :code=>0
      end
    end

  end


  def edit
    # темп массив для поиска повторений сотрудников
    getEmployees = ''
    # массив для результата
    @allEmployees=[]


    # OLD
    if(@isAdmin!='true')
      if(@isSuperUser=='true')
        if($department!='')

          if(UserAccessWork.isSetDepAccess($department) === false)
            redirect_to :action => "error", :code=>2
          end

        else
          redirect_to :action => "error", :code=>1
        end
      else
        redirect_to :action => "error", :code=>0
      end
    end
    ####


    # Page info
		@page_title = 'Рассылки e-mail'
		@currPage = "index"

    # Date
    now = Time.now
    @month = (params[:month]) ? params[:month].to_i : now.month
    @year = (params[:year]) ? params[:year].to_i : now.year

    # Days in month
    @countDays = Time.days_in_month(@month, @year)

    @dateEmployees = EmployeesDoneDB.getDateEmployees(@month, @year, $department)
    @dateEmployees2 = EmployeesDB.getEmployeeInfo('1049')
  end

 
  
  
  def sendMail
    require 'send_mail'

    employees = params[:empl]
    template = params[:template].to_i
    department = params[:dep]

    # Date
    now = Time.now
    day = (params[:filter_day]) ? params[:filter_day].to_i : 0
    month = (params[:filter_month]) ? params[:filter_month].to_i : now.month
    year = (params[:filter_year]) ? params[:filter_year].to_i : now.year

    if(template>0 && employees.size>0 && department!="")

      @emails = []
      for empl_id in employees
        email = EmployeesDB.getEmail(empl_id)
        if(email)
          message = ApplicationHelper::HTMLtemplatesMail.template2(empl_id, department, day, month, year)
          # Submit
          @emails += [message]
        end
      end



      #SendMail::send_mail(
      #  :to => [''+email+''],
      #  :subject => 'Внутренняя рассылка компании RINET.',
      #  :reply_to => 'sdelka@rinet.ru',
      #  :body => ""+message+"",
      #  :options => {
      #    :priority => 2,
      #    :attachments_transfer_encoding => :quoted_printable
      #  }
      #)
    end
    #redirect_to :action => "index"
  end







  def error
    code = params[:code].gsub(/[^0-9]/, '')

    # Ошибки
    @error = 'Ошибка код: 111.'
    if(code.to_s == '0') then @error = "У вас недостаточно прав для просмотра этой страницы!" end
    if(code.to_s == '1') then @error = "Ошибка код: 112." end
    if(code.to_s == '2') then @error = "Ошибка код: 114." end

  end

end
