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 = Time.current
				@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

        @shop = current_shop

        @lookup = Lookup.shift_sale_items_lookup_value

        if @lookup.to_i == 1
          @sale_items    = Sale.get_shift_sale_items(@shift.id)
          other_charges = Sale.get_other_charges()
          @total_other_charges_info = other_charges.where("sales.shift_sale_id IN (?) and sale_status='completed'",@shift)
        end

        @sale_taxes    = Sale.get_separate_tax(@shift,from=nil,to=nil,type='')
        @total_waste   = Sale.get_total_waste(shift_id).sum(:grand_total)
        @total_spoile  = Sale.get_total_spoile(shift_id).sum(:grand_total)
        #other payment details for mpu or visa like card
        @other_payment = ShiftSale.get_by_shift_other_payment(@shift)

        # Calculate price_by_accounts
        @total_amount_by_account    = ShiftSale.calculate_total_price_by_accounts(@shift,'amount')
        @total_discount_by_account  = ShiftSale.calculate_total_price_by_accounts(@shift,'discount')
        @total_member_discount      = ShiftSale.get_total_member_discount(@shift)
        @total_dinein               = ShiftSale.get_total_dinein(@shift).total_dinein_amount
        @total_takeway              = ShiftSale.get_total_takeway(@shift).total_takeway_amount
        @total_other_charges        = ShiftSale.get_total_other_charges(@shift).total_other_charges_amount
        @total_credit_payments      = ShiftSale.get_shift_sales_with_credit_payment(shift_id).total_credit_payments
        @payment_methods            = PaymentMethodSetting.where("is_active='1'").pluck("payment_method")

				logout_status = Employee.logout(current_token)
				if logout_status
          @status = true
          @message = "Shift close successfully."
		    else
          @status = false
          @message = "Session Token Invalid or Missing"
		    end
			else
        @status = false
        @message = "Shift already close!"
	    end
		else
      @status = false
      @message = "Wrong shift!"
		end
	end
end
