class Panel::SurveyB::BackEnd::SurveyBController < OrbitBackendController
  # include AdminHelper
  # include OrbitControllerLib::DivisionForDisable

  # before_filter :for_app_manager
  
  open_for_manager

  #before_filter :force_order_for_user,:only => :index
  #before_filter :for_app_sub_manager,:except => :index


  # def initialize
  #   super
  #   @app_title = 'survey_b'
  # end

  def index
    if params[:period] && @period = SurveyBPeriod.find(params[:period])
      @answers = ::SurveyBAnswer.where(:created_at.gte => @period.start_date, :created_at.lte => @period.end_date)
    else
      @answers = ::SurveyBAnswer.all
    end
    if params[:sort]
      options = params[:sort_options]
      options = [options] if !options.class.eql?(Array)
      options.each do |option|
        @answers = @answers.order_by(option, params[:direction])
      end
    end
    @answers = @answers.page(params[:page]).per(10)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @answers }
      format.js
    end
  end

  def destroy
    @answers = SurveyBAnswer.find(params[:id])
    # @bulletin.bulletin_files.destroy
    @answers.destroy

    respond_to do |format|

      format.html { redirect_to(panel_survey_b_back_end_survey_b_index_path(:period => params[:period], :page => params[:page])) }
      # format.xml  { head :ok }
      format.js
    end
  end

  def delete
    if params[:ids]
      answers = SurveyBAnswer.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to panel_survey_b_back_end_survey_b_index_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end

  def result
    if params[:period] && @period = SurveyBPeriod.find(params[:period])
      @answers = ::SurveyBAnswer.where(:created_at.gte => @period.start_date, :created_at.lte => @period.end_date)
    else
      @answers = ::SurveyBAnswer.all
    end
    @results = Hash[(t('survey_b.institutions').keys + [ 'all' ]).map do |i_key| 
      [i_key, Hash[(t('survey_b.questions').keys + ['count', 'sum', 'sum7'] ).map{|q_key| [q_key, 0] }].merge({ 'count' => 0, 'sum' => 0, 'sum7' => 0 })] 
    end]
    [:good_institutions, :bad_institutions].each do |item|
      @results[item] = Hash[t('survey_b.institutions').map{|k, i| [k, 0] }]
    end
    [:person_question, :rate_question, :quality_question].each do |item|
      @results[item] = t("survey_b.#{item}_options").map{|o| 0 }
    end
    @answers.each do |answer|
      t('survey_b.institutions').each do |i_key, institution|
        if answer[i_key.to_s]
          @results[i_key]['count'] += 1
          t('survey_b.questions').keys.each do |q_key|
            point = 10 - ( answer[i_key.to_s][q_key.to_s].to_i * 2 )
            @results[i_key][q_key] += point
            @results[i_key]['sum'] += point
            if q_key != :question6
              @results[i_key]['sum7'] += point
            end
          end
        end
      end
      [:good_institutions, :bad_institutions].each do |item|
        if answer[item.to_s]
          answer[item.to_s].each do |institution|
            @results[item][institution] += 1
          end
        end
      end
      [:person_question, :rate_question, :quality_question].each do |item|
        if answer[item.to_s]
          @results[item][answer[item.to_s].to_i] += 1
        end
      end
    end
    t('survey_b.institutions').each do |i_key, institution|
      (t('survey_b.questions').keys + ['count', 'sum', 'sum7'] ).each do |q_key|
        @results['all'][q_key] += @results[i_key][q_key]
      end
    end
  end

  def export

    if params[:period] && @period = SurveyBPeriod.find(params[:period])
      @answers = ::SurveyBAnswer.where(:created_at.gte => @period.start_date, :created_at.lte => @period.end_date)
      @comment_question = ::SurveyBAnswer.where(:created_at.gte => @period.start_date, :created_at.lte => @period.end_date, :comment_question.nin => [''])
    else
      @answers = ::SurveyBAnswer.all
      @comment_question = ::SurveyBAnswer.where(:comment_question.nin => [''])
    end
    @results = Hash[(t('survey_b.institutions').keys + [ 'all' ]).map do |i_key| 
      [i_key, Hash[(t('survey_b.questions').keys + ['count', 'sum', 'sum7'] ).map{|q_key| [q_key, 0] }].merge({ 'count' => 0, 'sum' => 0, 'sum7' => 0 })] 
    end]
    [:good_institutions, :bad_institutions].each do |item|
      @results[item] = Hash[t('survey_b.institutions').map{|k, i| [k, 0] }]
    end
    [:person_question, :rate_question, :quality_question].each do |item|
      @results[item] = t("survey_b.#{item}_options").map{|o| 0 }
    end
    @answers.each do |answer|
      t('survey_b.institutions').each do |i_key, institution|
        if answer[i_key.to_s]
          @results[i_key]['count'] += 1
          t('survey_b.questions').keys.each do |q_key|
            point = 10 - ( answer[i_key.to_s][q_key.to_s].to_i * 2 )
            @results[i_key][q_key] += point
            @results[i_key]['sum'] += point
            if q_key != :question6
              @results[i_key]['sum7'] += point
            end
          end
        end
      end
      [:good_institutions, :bad_institutions].each do |item|
        if answer[item.to_s]
          answer[item.to_s].each do |institution|
            @results[item][institution] += 1
          end
        end
      end
      [:person_question, :rate_question, :quality_question].each do |item|
        if answer[item.to_s]
          @results[item][answer[item.to_s].to_i] += 1
        end
      end
    end
    t('survey_b.institutions').each do |i_key, institution|
      (t('survey_b.questions').keys + ['count', 'sum', 'sum7'] ).each do |q_key|
        @results['all'][q_key] += @results[i_key][q_key]
      end
    end

    @setting = SurveyBSetting.first

    headers['Content-Type'] = "application/vnd.ms-excel"
    headers['Content-Disposition'] = "attachment; filename=#{@period.title}.xls"
    headers['Cache-Control'] = ''

    render :layout => false   
  end

end