# -*- encoding : utf-8 -*-
class Portlet::TableRowsController < ApplicationController
  layout "admin"

  def new
    @title_content = TitleContent.new

    respond_to do |format|
      format.html {
        if request.xhr?
          render :layout => false
        end
      }
      format.js { render :template => "portlet/shared/new" }
    end
  end

  def create
    @table_row = TableRow.new(params.require(:table_row).permit!)
    
    respond_to do |format|
      if @table_row.save

        @table = @table_row.table_content
        @table.nbr_cols.times do
          cel_table = CelTable.new
          @table_row.cel_tables << cel_table
        end
        nbr_past = @table.nbr_rows
        @table.nbr_rows = nbr_past +1
        @table.save
        
        
        @portlet = Portlet.find(@table.portlet.id)

      #format.html { redirect_to([:admin, @portlet.block.blockable], :notice => 'Elément ajouté avec succès.') }
        format.js
      else
      #format.html { render :action => "new" }
      #format.js { render :template => "portlet/shared/new" }
      end
    end
  end

  def edit
    @title_content = TitleContent.find(params[:id])
    @portlet = Portlet.find(params[:portlet_id])
    respond_to do |format|
      format.html {
        if request.xhr?
          render :template => "portlet/shared/edit", :layout => false
        end
      }
      format.js { render :template => "portlet/shared/edit" }
    end
  end
  
  



  def show


  end

  def update
    @title_content = TitleContent.find(params[:id])
    @portlet = Portlet.find(params[:portlet_id])
    
    respond_to do |format|
          if @title_content.update_attributes(params.require(:table_row).permit!)
        if @portlet.block and @portlet.block.blockable
          @portlet.block.blockable.updated_at = Time.now
          @portlet.block.blockable.save
          if @portlet.block.blockable_type == "Page"

            ##to_my_log("[Page_"+@portlet.block.blockable.id.to_s+"] "+"Modification de la page "+@portlet.block.blockable.title.to_s+" (modification d'un portlet)")
          end
        end
    
            format.html { redirect_to([:admin, @portlet.block.blockable], :notice => 'Titre mis à jour.') }
        format.js { render :template => "portlet/shared/update" }
          else
            format.html { render :action => "portlet/shared/edit" } 
        format.js { render :template => "portlet/shared/edit" }
          end

    end
  end

  def destroy
    
    @table_row = TableRow.find(params[:id])
    @table_content = @table_row.table_content
    @table_row.destroy
    
    
    respond_to do |format|
      format.html {}
      format.js 
    end
  end

 
end
