class Panel::Announcement::BackEnd::ApprovalsController  < OrbitBackendController
  before_filter :authenticate_user!
  before_filter :is_admin?
  include AdminHelper
  # layout 'admin'

  
  def preview_and_approve
    @bulletin = Bulletin.find params[:bulletin_id]
  end
  
  def approve
    notice = ""
    @bulletin = Bulletin.find params[:bulletin_id]
    @bulletin.proc_check(params[:bulletin][:is_checked],params[:bulletin][:not_checked_reason])
    @bulletin.de_pending
     if @bulletin.save
        notice = t('announcement.approve_bulletin_success')
     else
        notice = t('announcement.approve_bulletin_fail')
     end
    redirect_to(panel_announcement_back_end_bulletins_url,:notice => notice)
  end
  
  def setting
    @sys_users = User.all(conditions: {admin: false}).includes(:avatar).not_guest_user
    @bulletin_categorys = BulletinCategory.all
    @options_from_collection_for_select_bulletin_categorys = @bulletin_categorys.collect{|bc| [bc.title,bc.id] }
    if params.has_key? :category
      @bulletin_category = BulletinCategory.find params[:category][:id]
    else
      @bulletin_category =  @bulletin_categorys.first
    end
    preload_object_auth = @bulletin_category.get_object_auth_by_title('fact_check')
    @users_array =  preload_object_auth.privilege_users rescue []
    respond_to do |format|
      format.html
      format.js
    end
  end
  
  def update_setting
    object_auth = update_setting_by_params
    if object_auth.save!
      flash[:notice] = t('update.success_')
    else
      flash[:notice] = t('update.fail')
    end
  end
  
  def user_list
    @bulletin_category = BulletinCategory.find params[:category][:id]
  end

  protected
  def update_setting_by_params
    category = BulletinCategory.find params[:category][:id]
    privilege_users = params[:users].collect{|key,value| User.find key }  rescue []
    object_auth_ary = category.object_auths.where(title: 'fact_check') || (category.object_auths.create :title=> 'fact_check')
    object_auth = object_auth_ary.first
    object_auth.privilege_users = privilege_users
    object_auth
  end
  
end
