# encoding: utf-8

class Panel::Ask::BackEnd::AskRequestsController < OrbitBackendController
  include AdminHelper
  #include OrbitControllerLib::DivisionForDisable

  #before_filter :for_app_manager
  skip_before_filter :verify_authenticity_token
  open_for_visitor :only => [:transferred_request]

  def transferred_request
    app_title = request.fullpath.split('/')[2]
    module_app ||= ModuleApp.first(conditions: {:key => app_title} )

    if request.headers["HTTP_X_AUTH_TOKEN"] == '3kjlfksjDFJ' 
      if !module_app.categories.where("title.en" => "Transferred Question").any?
        ask_category = module_app.categories.build
        ask_category.title_translations = { "zh_tw" => "轉介的問題", "en" => "Transferred Question" }
        ask_category.save!
      end

      module_app.categories.enabled
      category = module_app.categories.where("title.en" => "Transferred Question").first

      @transferred_question = AskQuestion.new(
        name:     params['name'],
        identity: params['identity'],
        email:    params['email'],
        phone:    params['phone'],
        tax:      params['tax'],
        title:    params['title'],
        content:  params['content'],
        category_id: category.id)

      @transferred_question.ask_reply = AskReply.new
      @transferred_question.ask_reply.update_attributes(status: "待處理 (轉)")
      @transferred_question.save

      render :nothing => true
    else
      render :status => 500 
    end
  end
end

