class Panel::News::BackEnd::NewsApprovalsController  < OrbitBackendController
  before_filter :authenticate_user!
  before_filter :is_admin?
  include AdminHelper
  
  def preview_and_approve
    @bulletin = NewsBulletin.find params[:bulletin_id]
  end
  
  def approve
    
  end

  def setting
    @news_bulletin_categorys = NewsBulletinCategory.all
    if params.has_key?(:category_id)
      first_category = NewsBulletinCategory.find params[:category_id]
      if params[:commit] == 'Update'
        privilege_users = params[:users].collect{|key,value| User.find key }  rescue []
        preload_object_auth_ary = first_category.object_auths.where(title: 'fact_check') || (first_category.object_auths.create :title=> 'fact_check')
        preload_object_auth = preload_object_auth_ary.first
        preload_object_auth.privilege_users = privilege_users
        preload_object_auth.save!
      end
    else
      first_category =  @news_bulletin_categorys.first
    end  
    preload_object_auth = first_category.object_auths.where(title: 'fact_check').empty?? (first_category.object_auths.create! :title=> 'fact_check') : first_category.object_auths.where(title: 'fact_check')
    @users_array =  preload_object_auth.first.privilege_users rescue []
    respond_to do |format|
      format.html
      format.js
    end
  end
  
  def update_setting
    
  end

  protected

  def get_categorys(id = nil)
    @news_bulletin_categorys = []
    if(is_manager? || is_admin?)
      @news_bulletin_categorys = (id ? NewsBulletinCategory.find(id).to_a : NewsBulletinCategory.excludes('disabled' => true))
    elsif is_sub_manager?
      @news_bulletin_categorys = NewsBulletinCategory.authed_for_user(current_user,'submit_new')
    end
  end
end
