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
  #   
  # end
  
  def setting
    @sys_users = User.all(conditions: {admin: false}).includes(:avatar)
    @bulletin_categorys = BulletinCategory.all
    @options_from_collection_for_select_bulletin_categorys = @bulletin_categorys.collect{|bc| [bc.i18n_variable[I18n.locale],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("admin.object_auth.update_done")
    else
      flash[:notice] = t("admin.object_auth.update_failed")
    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
  
  def get_categorys(id = nil)
    @bulletin_categorys = []
    if(is_manager? || is_admin?)
      @bulletin_categorys = (id ? BulletinCategory.find(id).to_a : BulletinCategory.excludes('disabled' => true))
    elsif is_sub_manager?
      @bulletin_categorys = BulletinCategory.authed_for_user(current_user,'submit_new')
    end
  end
end
