namespace :survey do
  desc "TODO"
  
  
  task export_2017: :environment do
    
    
    @survey = SurveyType.find(9)
    
    @survey_sets = @survey.survey_sets.where(:is_fill => true)
    
    #@survey_sets = @survey_sets.where("created_at < ?", Date.parse("2017/06/27").beginning_of_day)
    @survey_sets = @survey_sets.where("created_at < ? and created_at >= ?", Date.parse("2017/07/19").beginning_of_day,Date.parse("2017/06/27").beginning_of_day)
    
   
     headers = []
    headers << "id"
    headers << "date"
    @survey.items.each do |survey_item|
      headers << survey_item.title
        

      if survey_item.questions_type == 3
        survey_item.answers.each do |survey_item_answer|
          headers << survey_item_answer.label_text
        end
      end
    end
   

    
    @csv = CSV.generate(:headers => true, :col_sep => ";") do |csv|
      csv << headers
      
      
      
      
      @survey_sets.each do |survey_set|
        line = []
        
        line << survey_set.id
        puts survey_set.id
        line << survey_set.created_at
        survey_set.survey_type.items.each do |survey_item|
          question_set = survey_set.question_sets.find_by_survey_item_id(survey_item.id)
          line <<question_set.content


          if survey_item.questions_type == 3
            survey_item.answers.each do |survey_item_answer|
            
              answer_set = question_set.answer_sets.find_by_survey_item_answer_id(survey_item_answer.id)
              rep = ""
              if answer_set
                if answer_set.content?
                  rep = answer_set.content
                else
                  rep = "oui" if answer_set.is_checked
                end
              end
              line << rep
  
            end
          end
        end
        csv << line
      end
    
      
    
    end
      
      
    @final_file = "#{Rails.root}/pdf/export_survey/enquete-2017-19-07-2017.csv"
    
    
    File.open(@final_file, 'wb') do |file|
    file << @csv.encode('iso-8859-1', :undef => :replace, :replace => '')
    end
    
    
    #send_data @csv.encode('iso-8859-1', :undef => :replace, :replace => ''), :filename => "export-survey-#{Date.today.to_s.to_slug}.csv", :type => 'text/csv; charset=iso-8859-1; header=present' }
    
    
    
    
  end
  
  
  
  task export_all: :environment do
    
    
    @survey = SurveyType.find(9)
    
    @survey_sets = @survey.survey_sets.where(:is_fill => true)
    
    #@survey_sets = @survey_sets.where("created_at < ?", Date.parse("2017/06/27").beginning_of_day)
    @survey_sets = @survey_sets.where("created_at < ? and created_at >= ?", Date.parse("2017/07/19").beginning_of_day,Date.parse("2000/06/27").beginning_of_day)
    
   
     headers = []
    headers << "id"
    headers << "date"
    @survey.items.each do |survey_item|
      headers << survey_item.title
        

      if survey_item.questions_type == 3
        survey_item.answers.each do |survey_item_answer|
          headers << survey_item_answer.label_text
        end
      end
    end
   

    
    @csv = CSV.generate(:headers => true, :col_sep => ";") do |csv|
      csv << headers
      
      
      
      
      @survey_sets.each do |survey_set|
        line = []
        
        line << survey_set.id
        puts survey_set.id
        line << survey_set.created_at
        survey_set.survey_type.items.each do |survey_item|
          question_set = survey_set.question_sets.find_by_survey_item_id(survey_item.id)
          line <<question_set.content


          if survey_item.questions_type == 3
            survey_item.answers.each do |survey_item_answer|
            
              answer_set = question_set.answer_sets.find_by_survey_item_answer_id(survey_item_answer.id)
              rep = ""
              if answer_set
                if answer_set.content?
                  rep = answer_set.content
                else
                  rep = "oui" if answer_set.is_checked
                end
              end
              line << rep
  
            end
          end
        end
        csv << line
      end
    
      
    
    end
      
      
    @final_file = "#{Rails.root}/pdf/export_survey/enquete-2017-19-07-2017-all.csv"
    
    
    File.open(@final_file, 'wb') do |file|
    file << @csv.encode('iso-8859-1', :undef => :replace, :replace => '')
    end
    
    
    #send_data @csv.encode('iso-8859-1', :undef => :replace, :replace => ''), :filename => "export-survey-#{Date.today.to_s.to_slug}.csv", :type => 'text/csv; charset=iso-8859-1; header=present' }
    
    
    
    
  end
  

end
