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


class PeopleController < ApplicationController
  # GET /people
  # GET /people.xml
  
  before_filter :authenticate_admin!, except: [:show]
 
  def index
    @people = Person.all

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

  # GET /people/1
  # GET /people/1.xml
  def show
    @person = Person.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @person }
      format.js { render(:update){|page| page.replace 'sheet_person_'+@person.id.to_s, :partial => "person", :object => @person} }

    end
  end

  # GET /people/new
  # GET /people/new.xml
  def new
    @person = Person.new
    @person.sheet_id = params[:sheet_id]
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @person }
    end
  end

  # GET /people/1/edit
  def edit
    @person = Person.find(params[:id])
    respond_to do |format|
      format.js { render(:update){|page| page.replace_html 'sheet_person_'+@person.id.to_s, :partial => "form", :object => @person} }
    end
  end

  # POST /people
  # POST /people.xml
  def create
    @person = Person.new(params[:person])

    respond_to do |format|
      if @person.save
        @sheet = @person.sheet
         if !@sheet.person
      
            @sheet.person = @person
          end

        @sheet.save
        format.html { redirect_to(@person, :notice => 'Person was successfully created.') }
        format.xml  { render :xml => @person, :status => :created, :location => @person }
        format.js 
      else
        @sheet = @person.sheet
        format.html { render :action => "new" }
        format.js { render(:update){|page| page.replace_html 'new_sheet_people', :partial => "form", :object => @person} }

        format.xml  { render :xml => @person.errors, :status => :unprocessable_entity }
      end
    end
     
  
  end

  # PUT /people/1
  # PUT /people/1.xml
  def update
    @person = Person.find(params[:id])

    respond_to do |format|
      if @person.update_attributes(params[:person])
        format.html { redirect_to(@person, :notice => 'Person was successfully updated.') }
        format.xml  { head :ok }
        format.js { render(:update){|page| page.replace 'sheet_person_'+@person.id.to_s, :partial => "person", :object => @person} }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @person.errors, :status => :unprocessable_entity }
        format.js { render(:update){|page| page.replace_html 'sheet_person_'+@person.id.to_s, :partial => "form", :object => @person} }
      end
    end
  end

  # DELETE /people/1
  # DELETE /people/1.xml
  def destroy
    @person = Person.find(params[:id])
    @sheet = @person.sheet
    @person.destroy
  
    if !@sheet.person
      @new_person = @sheet.people.find(:first)
      @sheet.person = @new_person if @new_person
    end
        
    @sheet.save

    respond_to do |format|
      format.html { redirect_to(people_url) }
      format.xml  { head :ok }
      if @sheet.people and @sheet.people.size > 0
        format.js { render(:update){|page| page.replace_html 'sheet_peoples', :partial => "people/person",:collection => @sheet.people} }
      else
        format.js { render(:update){|page| page.replace_html 'sheet_peoples', " "} }
      end
    end
  end
  
  
  def principal
    @person = Person.find(params[:id])
    @sheet = @person.sheet
    @sheet.person = @person
    @sheet.save

    respond_to do |format|
      format.html { redirect_to(people_url) }
      format.xml  { head :ok }
      format.js { render(:update){|page| page.replace_html 'sheet_peoples', :partial => "people/person",:collection => @sheet.people} }
    end
  end
end
