class Admin::MailCronsController <  OrbitBackendController
  
  layout "new_admin"
  before_filter :authenticate_user!
  before_filter :is_admin?

  def index  

    # @mail_crons = MailCron.all.asc(:mail_sentdate).page(params[:page_main] ).per('20')
    @mail_crons = (params[:sort]) ? get_sorted_and_filtered("mail_cron") : get_viewable("mail_cron")

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @mail_crons }
      format.js
    end

  end
  
  def show  
  end
  
  def new 
    @mail_cron = MailCron.new 

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @mail_cron }
    end
  end
  
  def create 
    @mail_cron = MailCron.new(params[:mail_cron])

    @mail_cron.create_user_id = current_user.id
    @mail_cron.update_user_id = current_user.id

    respond_to do |format|
      if @mail_cron.save
        format.html { redirect_to(admin_mail_crons_url) }
        format.xml  { render :xml => @mail_cron, :status => :created, :location => @mail_cron }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @mail_cron.errors, :status => :unprocessable_entity }
      end
    end
  end 

  def edit 
    @mail_cron = MailCron.find(params[:id])
  end 
  
  def update   



    @datas = MailCron.where(:mail_sentdate.lte => Time.now, :mail_sent => false)

    @datas.each do |mail_data| 

      @mail_j = 0
      @read_mails = Array.new 

      @mails = mail_data.mail_to.split(",")

      @mails.each do |send_mail|

        @read_mails << send_mail

        @mail_j += 1 

        if  0 == @mail_j % 30 
          MailCronMailer.cron_mail( mail_data.id, @read_mails.join(',') ).deliver
          @read_mails = Array.new

          sleep 10
        end

      end

      if !@read_mails.join(',').blank?
        MailCronMailer.cron_mail( mail_data.id, @read_mails.join(',') ).deliver
        @read_mails = Array.new

        sleep 10
      end  

    end
    
    @mail_cron = MailCron.find(params[:id])
  
    @mail_cron.update_user_id = current_user.id 

    respond_to do |format|
      if @mail_cron.update_attributes(params[:mail_cron])
        format.html { redirect_to(admin_mail_crons_url) }
        format.js  { render 'toggle_enable' }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @mail_cron.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def destroy 
    @mail_cron = MailCron.find(params[:id])
    @mail_cron.destroy

    respond_to do |format|
      format.html { redirect_to(admin_mail_crons_url) }
      # format.xml  { head :ok }
      format.js
    end
  end

  def delete
    if params[:ids]
      mail_crons = MailCron.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to admin_mail_crons_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end
  
  protected 
  
end