class Admins::UpgradeNoticesController < Admins::BaseController

  def index
    params[:sort_by] ||= 'created_at'
    params[:sort_direction] ||= 'desc'

    @upgrade_notice = UpgradeNotice.new(
      content: '为了给大家提供更优质的体验，平台将于2018年5月22日00:00开始对系统进行升级。升级期间实训和基于项目的分组作业将暂停服务。
       系统拟于2018年5月22日10:00恢复服务。请大家知悉，并提前做好教学安排。
       带来不便，敬请谅解。',
      title: 'educoder系统升级暂停服务通知')

    @upgrade_notices = UpgradeNotice.order("created_at #{params[:sort_direction]}")
    @upgrade_notices = paginate @upgrade_notices
  end

  def create
    upgrade_notice = UpgradeNotice.new(upgrade_notice_params)
    if upgrade_notice.save
      render_ok
    else
      render_error upgrade_notice.error.full_messages.join(",")
    end
  end

  def destroy
    check_upgrade_notice
    @upgrade_notice.destroy
  end

  def edit
    check_upgrade_notice
  end

  def update
    check_upgrade_notice
    if @upgrade_notice.update(upgrade_notice_params)
      render_ok
    else
      render_error @upgrade_notice.error.full_messages.join(",")
    end
  end


  private

  def upgrade_notice_params
    params.require(:upgrade_notice).permit(:title, :content, :start_at, :end_at)
  end

  def check_upgrade_notice
    @upgrade_notice = UpgradeNotice.find(params[:id])
  end
end
