class Panel::Announcement::BackEnd::BulletinsController < ApplicationController
  
  layout 'new_admin'
  
  before_filter :authenticate_user!
  before_filter :is_admin?

  def index
    # @bulletins = Bulletin.all
    # @bulletins = Bulletin.desc("postdate desc")
	  get_categorys(params[:bulletin_category_id])
	
    # @bulletins = Bulletin.where("bulletin_category_id" => params[:bulletin_category_id]).desc("postdate") if params[:bulletin_category_id]
    
  	@bulletins = Bulletin.search(params[:search],params[:category_id])
  	@bulletin_categories = BulletinCategory.all
  	
  	module_app = ModuleApp.first(:conditions => {:key => 'announcement'})
  	@tags = Tag.all(:conditions => {:module_app_id => module_app.id})

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

  # GET /bulletins/1
  # GET /bulletins/1.xml
  def show
    @bulletin = Bulletin.find(params[:id])
	# get_categorys
	
    # @bulletin_categorys = BulletinCategory.where("_id" => params[:id])
	
    # if params[:id]
      # @bulletins = Bulletin.where("bulletin_category_id" => params[:id])
    # else
      # @bulletins = Bulletin.desc("postdate desc")
    # end

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

  # GET /bulletins/new
  # GET /bulletins/new.xml
  def new
    @bulletin = Bulletin.new
    
	# @bulletin.bulletin_files.build
	# @bulletin.bulletin_files.new
	
	get_categorys

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

  # GET /bulletins/1/edit
  def edit
    @bulletin = Bulletin.find(params[:id])
    # @summary_variable = @bulletin.summary_variable
	get_categorys
  end

  # POST /bulletins
  # POST /bulletins.xml
  def create
    @bulletin = Bulletin.new(params[:bulletin])
	
    @bulletin.create_user_id = current_user.id
    @bulletin.update_user_id = current_user.id

    respond_to do |format|
      if @bulletin.save
        # format.html { redirect_to(panel_announcement_back_end_bulletin_url(@bulletin), :notice => t('announcement.create_bulletin_success')) }
        format.html { redirect_to(panel_announcement_back_end_bulletins_url, :notice => t('announcement.create_bulletin_success')) }
        format.xml  { render :xml => @bulletin, :status => :created, :location => @bulletin }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @bulletin.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /bulletins/1
  # PUT /bulletins/1.xml
  def update
    @bulletin = Bulletin.find(params[:id])
	
    @bulletin.update_user_id = current_user.id
	
	# @bulletin.image.clear if params[:bulletin][:image_del] == '1'
	if params[:bulletin][:image_del] == '1' 
		@bulletin.remove_image!
		@bulletin.image_filename = nil
		# @bulletin.image_del = nil
		params[:bulletin][:image_del] = nil
	end

    respond_to do |format|
      if @bulletin.update_attributes(params[:bulletin])
        # format.html { redirect_to(panel_announcement_back_end_bulletin_url(@bulletin), :notice => t('bulletin.update_bulletin_success')) }
        format.html { redirect_to(panel_announcement_back_end_bulletins_url, :notice => t('bulletin.update_bulletin_success')) }
		format.js  { render 'toggle_enable' }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @bulletin.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /bulletins/1
  # DELETE /bulletins/1.xml
  def destroy
    @bulletin = Bulletin.find(params[:id])
    # @bulletin.bulletin_files.destroy
    @bulletin.destroy

    respond_to do |format|
      format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
      # format.xml  { head :ok }
      format.js
    end
  end

  # def top_edit
	
	# Bulletin.where(id: params[:id]).update_all( is_top: true )
    # respond_to do |format|
      # format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
      # format.xml  { head :ok }
    # end
	
  # end
  
  
  protected
  
  def get_categorys(id = nil)
    @bulletin_categorys = (id ? BulletinCategory.find(id).to_a : BulletinCategory.excludes('disabled' => true))
  end
  
  
end
