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

class Admin::ITasksController < ApplicationController
  layout "admin"
  before_filter :auth_admin

  before_filter :admin_space
  
  def admin_space
    @admin_space = "sites"
  end
  

  
  
  def index
    @i_tasks = ITask.order("p_customer_id ASC").all

   
  end

  def show
    @i_task = ITask.find(params[:id])

  end

  def new
    
    
		@i_task = ITask.new
    

  end

 

  def edit
  
    
    @i_task = ITask.find(params[:id])
  end

  def create
    @i_task = ITask.new(params.require(:i_task).permit!)

    if @i_task.name? and (tasks = @i_task.name.split(";")).size > 1
      tasks.each do |task|
        t = @i_task.dup
        t.name = task.lstrip
        t.save
      end
    
      if @i_task.i_website
        @i_website = @i_task
        @new_i_task = @i_task.i_website.i_tasks.new
        @i_tasks = @i_task.i_website.i_tasks.order("end_at ASC").all
      else
        @i_tasks = ITask.order("end_at ASC").all
      end
      
    else
      
      
      
      
      if @i_task.save
        if @i_task.i_website
          @new_i_task = @i_task.i_website.i_tasks.new
          @i_tasks = @i_task.i_website.i_tasks.order("end_at ASC").all
        else
          @i_tasks = ITask.order("end_at ASC").all
        end
      
     
      
      else
        render action: "new"
 
      end
    end

  end


  def update
    @i_task = ITask.find(params[:id])


    if @i_task.update_attributes(params.require(:i_task).permit!)
      
      if @i_task.i_website
        #@new_i_task = @i_task.i_website.i_tasks.new
        @i_tasks = @i_task.i_website.i_tasks.order("end_at ASC").all
      else
        @i_tasks = ITask.order("end_at ASC").all
      end
      
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @i_task = ITask.find(params[:id])
    @i_task.destroy
    
    
  end
end
