class Origami::SurveysController < BaseOrigamiController
	def new
		@webview = false
	    if check_mobile
	      @webview = true  
	    end
    
		@survey = Survey.new
		@id             = params[:id]
		@cashier_type   = params[:type]

	    if(@id[0,3] == "SAL")
	       @sale             = Sale.find(@id)
	       @receipt_no       = @sale.receipt_no
	       @grand_total      = @sale.grand_total
	       @booking          = Booking.find_by_sale_id(@id)

	       if @booking.dining_facility_id.to_i>0
	       	   @dining_facility  = DiningFacility.find(@booking.dining_facility_id)
		       @table_type       = @dining_facility.type
		       @survey_data  	 = Survey.find_by_dining_name(@dining_facility.name)
		       survey_process  	 = Survey.find_by_receipt_no(@receipt_no)
		       if !survey_process.nil?
		       		@survey_data = survey_process
		       end
	       else
	       	@dining_facility  = nil
	        @table_type       = nil
	       end
	       
	    else
	       @dining_facility  = DiningFacility.find(@id)
	       @table_type       = @dining_facility.type
	       @receipt_no       = nil
	       @grand_total      = nil
	       @survey_data  	 = Survey.find_by_dining_name(@dining_facility.name)
	    end 
	end

	def create
		@type = params[:cashier_type]
		@sale_id = params[:sale_id]

		if params[:table_id].to_i>0
		  @dining_facility  = DiningFacility.find(params[:table_id])
		  cashier_zone      = CashierTerminalByZone.find_by_zone_id(@dining_facility.zone_id)
		  shift_by_terminal = ShiftSale.find_by_cashier_terminal_id_and_shift_closed_at(cashier_zone.cashier_terminal_id,nil)
		else
		  sale     = Sale.find(@sale_id)
          shift = ShiftSale.find(sale.shift_sale_id)
		  shift_by_terminal = ShiftSale.find_by_cashier_terminal_id_and_shift_closed_at(shift.cashier_terminal_id,nil)
		end
		
		if @type == "quick_service"
			@url = "/origami/sale/"+@sale_id+"/"+@type+"/payment"
		else
			@url = "/origami/"+@dining_facility.type.downcase+"/"+params[:table_id]
		end
		

		if params[:survey_id].nil? || params[:survey_id] == ""
			@survey = Survey.new(survey_params)
			@survey.shift_id = shift_by_terminal.id
			@survey.foreigner = params["survey"]["foreigner"].to_json
			# respond_to do |format|
			  if @survey.save
			     redirect_to @url
			  end
			# end
		else
			survey = Survey.find_by_id(params[:survey_id])
			survey.child = params["survey"]["child"]
			survey.adult = params["survey"]["adult"]
			survey.male = params["survey"]["male"]
			survey.female = params["survey"]["female"]
			survey.local = params["survey"]["local"]
			survey.dining_name = params["survey"]["dining_name"]
			survey.receipt_no = params["survey"]["receipt_no"]
			survey.shift_id = shift_by_terminal.id
			survey.created_by = params["survey"]["created_by"]
			survey.total_customer = params["survey"]["total_customer"]
			survey.total_amount = params["survey"]["total_amount"]
			survey.foreigner = params["survey"]["foreigner"].to_json
			if survey.save!
				redirect_to @url
			end
		end
	end

	private

    # Never trust parameters from the scary internet, only allow the white list through.
    def survey_params
      params.require(:survey).permit(:child, :adult,:male,:female,:local,:foreigner, :dining_name,:receipt_no,:shift_id,:created_by,:total_customer,:total_amount,:survey_id)
    end
        
end
