class Api::ShiftsController <  Api::ApiController
   	# skip_before_action  :authenticate

  
  	def create
      	opening_balance = 0
	    cashier_terminal_param = params[:cashier_terminal]

	    # Multiple Cashier
	    cashier_terminal = CashierTerminal.find(cashier_terminal_param)
	    if cashier_terminal.is_currently_login
	      	render json: JSON.generate({:status => false, :error_message => "Cashier Terminal already signin!"})
	    else
		    # Update Cashier Terminal
		    cashier_terminal.is_currently_login = 1
		    cashier_terminal.save

		    @shift_sale = ShiftSale.new
		    if @shift_sale.create(opening_balance,cashier_terminal_param, current_login_employee)
		    	@status = true
		    	@message = "Cashier Terminal successfully signin."
		    	# render json: JSON.generate({:status => true, :message => "Cashier Terminal successfully signin.", :shift => ActiveSupport::JSON.encode(@shift_sale)})
		    else
		    	@status = false
		    	@error_message = "Some error occurred!"
		    	# render json: JSON.generate({:status => false, :error_message => "Some error occurred!"})
		    end
		end
  	end

  	def update
		closing_balance = 0
		shift_id = params[:shift_id]

		@shift = ShiftSale.find_by_id(shift_id)
		if !@shift.nil?
			if @shift.shift_closed_at.nil?
				@shift.shift_closed_at = DateTime.now.utc
				@shift.closing_balance = closing_balance.to_f     
				@shift.save

				# Multiple Cashier
				cashier_terminal = @shift.cashier_terminal      
				cashier_terminal.is_currently_login = 0
				cashier_terminal.save

				logout_status = Employee.logout(current_token)
				if logout_status
			     	render json: JSON.generate({:status => true, :message => "Shift close successfully."})
			    else
			     	render json: JSON.generate({:status => false, :error_message => "Session Token Invalid or Missing"})
			    end
			else
				render json: JSON.generate({:status => false, :error_message => "Shift already close!"})
		    end
		else
			render json: JSON.generate({:status => false, :error_message => "Wrong shift!"})
		end
	end
end

