class ApplyProjectMastersController < ApplicationController
  
  before_filter :require_login, :find_apply, :only => [:create, :delete]  
  
  # GET /apply_project_masters
  # GET /apply_project_masters.json
  def index
    @apply_project_masters = ApplyProjectMaster.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @apply_project_masters }
    end
  end

  # GET /apply_project_masters/1
  # GET /apply_project_masters/1.json
  def show
    @apply_project_master = ApplyProjectMaster.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @apply_project_master }
    end
  end

  # GET /apply_project_masters/new
  # GET /apply_project_masters/new.json
  def new
    @apply_project_master = ApplyProjectMaster.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @apply_project_master }
    end
  end

  # GET /apply_project_masters/1/edit
  def edit
    @apply_project_master = ApplyProjectMaster.find(params[:id])
  end

  # POST /apply_project_masters
  # POST /apply_project_masters.json
  def create
    # @apply_project_master = ApplyProjectMaster.new(params[:apply_project_master])
# 
    # respond_to do |format|
      # if @apply_project_master.save
        # format.html { redirect_to @apply_project_master, notice: 'Apply project master was successfully created.' }
        # format.json { render json: @apply_project_master, status: :created, location: @apply_project_master }
      # else
        # format.html { render action: "new" }
        # format.json { render json: @apply_project_master.errors, status: :unprocessable_entity }
      # end
    # end
    
    set_apply(@apply, User.current, true)
    
  end

  # PUT /apply_project_masters/1
  # PUT /apply_project_masters/1.json
  def update
    @apply_project_master = ApplyProjectMaster.find(params[:id])

    respond_to do |format|
      if @apply_project_master.update_attributes(params[:apply_project_master])
        format.html { redirect_to @apply_project_master, notice: 'Apply project master was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @apply_project_master.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /apply_project_masters/1
  # DELETE /apply_project_masters/1.json
  def destroy
    @apply_project_master = ApplyProjectMaster.find(params[:id])
    @apply_project_master.destroy

    respond_to do |format|
      format.html { redirect_to apply_project_masters_url }
      format.json { head :no_content }
    end
  end
  
  def delete
    set_apply(@apply, User.current, false)
  end
  
  private
  
  def find_apply
    klass = Object.const_get(params[:object_type].camelcase) rescue nil
    if klass && klass.respond_to?('applied_by')
      @apply = klass.find_all_by_id(Array.wrap(params[:object_id]))
    end
    render_404 unless @apply.present?
  end

  #flag标注功能，为1时设置‘申请版主’，为0时设置取消
  def set_apply(objects, user, flag)
    objects.each do |object|
      object.set_apply(user, flag)
    end
    respond_to do |format|
      format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
      format.js { render :partial => 'set_apply', :locals => {:user => user, :objects => objects} }
    end
  end
end
