module Admins::RenderHelper
  extend ActiveSupport::Concern

  def render_by_format(hash)
    format = request.format.symbol
    hash.key?(format) ? hash[format].call : hash[:html].call
  end

  def render_forbidden
    render_by_format(html: -> { current_user&.business? ? render('admins/shared/403') : redirect_to('/403') },
                     json: -> { render status: 403, json: { messages: I18n.t('error.forbidden') } } )
  end

  def render_not_found
    render_by_format(html: -> { render 'admins/shared/404' },
                     js: -> { render_js_error('资源未找到') },
                     json: -> { render status: 404, json: { message: '资源未找到' } })
  end

  def render_unprocessable_entity(message)
    render_by_format(html: -> { render 'admins/shared/422' },
                     js: -> { render_js_error(message) },
                     json: -> { render status: 422, json: { message: message } })
  end
  alias_method :render_error, :render_unprocessable_entity

  def internal_server_error(message = '系统错误')
    @message = message
    render_by_format(html: -> { render 'admins/shared/500' },
                     js: -> { render_js_error(message) },
                     json: -> { render status: 500, json: { message: message } })
  end

  def render_js_template(template, **opts)
    render({ template: template, formats: :js }.merge(opts))
  end

  def render_delete_success
    render_js_template 'admins/shared/delete'
  end
  alias_method :render_success_js, :render_delete_success

  def render_js_error(message)
    render_js_template 'admins/shared/error', locals: { message: message }
  end
end