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

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

  before_action :admin_space
  
  def admin_space
    @admin_space = "todos"
  end

  
  
  def index
    @i_tasks = ITask.order("p_customer_id ASC").all
    
    if !params[:admin_id]
      params[:admin_id] = current_admin.id
    end

   
  end

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

  end

  def new
    
    
		@i_task = ITask.new
    @i_task.creator = current_admin
    @i_task.admin = current_admin

  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
      @all_tasks = []
      tasks.each do |task|
        t = @i_task.dup
        t.name = task.lstrip
        t.save
        @all_tasks << t
      end
    
      
      
    else
      
      
      
      
      if @i_task.save
        
      
     
      
      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!)
      
      
    
      
    else
     render action: "edit" 
      
    end
      
  end


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