class Origami::HomeController < BaseOrigamiController
  authorize_resource :class => false
  before_action :set_dining, only: [:show]

  def index
    @webview = check_mobile
    @tables   = Table.unscope(:order).includes(:zone, :current_checkin_booking, :current_checkout_booking, :current_reserved_booking).active.order('status desc')
    @rooms    = Room.unscope(:order).includes(:zone, :current_checkin_booking, :current_checkout_booking, :current_reserved_booking).active.order('status desc')
    @complete = Sale.completed_sale("cashier")
    @orders   = Order.includes("sale_orders").where("DATE_FORMAT(CONVERT_TZ(date, '+00:00', '<%= Time.zone.formatted_offset %>'), '%Y-%m-%d') = ? and status != 'billed' and source != 'quick_service'",Time.current.strftime('%Y-%m-%d')).order('date desc')


    @customers = Customer.pluck("customer_id, name")
    @occupied_table = DiningFacility.where("status='occupied'").count
    @shift = ShiftSale.current_open_shift(current_user)
  end

 # origami table detail
  def show
    # get printer info
    @print_settings = PrintSetting.get_precision_delimiter()
    @webview = check_mobile

    @tables = Table.unscope(:order).includes(:zone, :current_checkin_booking, :current_checkout_booking, :current_reserved_booking).all.active.order('status desc')
    @rooms = Room.unscope(:order).includes(:zone, :current_checkin_booking, :current_checkout_booking, :current_reserved_booking).all.active.order('status desc')
    @complete = Sale.completed_sale("cashier")
    @orders = Order.includes("sale_orders").where("DATE_FORMAT(CONVERT_TZ(date, '+00:00', '<%= Time.zone.formatted_offset %>'), '%Y-%m-%d') = ? and status != 'billed' and source != 'quick_service'",Time.current.strftime('%Y-%m-%d')).order('date desc')

    @customers = Customer.pluck("customer_id, name")
    @shift = ShiftSale.current_open_shift(current_user)

    @status_order = ""
    @status_sale = ""

    @shop =  current_shop
    @membership = MembershipSetting::MembershipSetting
    @payment_methods = PaymentMethodSetting.all

    @sale_array = @dining.current_sales
    if (booking = @dining.current_checkin_booking)
      @booking = booking
      @order_items = booking.order_items
      @obj_order = booking.orders.first
    end

    if (booking = @dining.current_checkout_booking)
      @booking = booking
      @obj_sale = booking.sale
      @sale_taxes = @obj_sale.sale_taxes
      @status_sale = 'sale'
    end

    if @obj_sale || @customer.blank?
      if obj = @obj_sale || @obj_order
        @customer = obj.customer
        @date = obj.created_at
      end
    end

    if @obj_sale
      @status_order = 'sale'
    elsif @obj_order
      @status_order = 'order'
    end

    if (@obj_sale || @account_arr.blank?) && @customer
      @account_arr = TaxProfile.find_by(id: @customer.tax_profiles)
    end

    #for bank integration
    @checkout_time = Lookup.collection_of('checkout_time')
    @checkout_alert_time = Lookup.collection_of('checkout_alert_time')

    accounts = TaxProfile.where("group_type = ?","cashier").order("order_by ASC")
    @tax_arr =[]
    accounts.each do |acc|
      @tax_arr.push(acc.name)
    end

    lookup_spit_bill = Lookup.collection_of('split_bill')
    @split_bill = 0
    if !lookup_spit_bill[0].nil?
      @split_bill = lookup_spit_bill[0][1]
    end

    #for edit order on/off
    @edit_order_origami = true
    lookup_edit_order = Lookup.collection_of('edit_order')
    if !lookup_edit_order.empty?
      lookup_edit_order.each do |edit_order|
        if edit_order[0].downcase == "editorderorigami"
          if edit_order[1] == '0' && (current_login_employee.role == 'cashier' || current_login_employee.role == 'waiter')
            @edit_order_origami = false
          end
        end
      end
    end

    #for changable on/off
    @changable_tax = true
    lookup_changable_tax = Lookup.collection_of('changable_tax')
    if !lookup_changable_tax.empty?
      lookup_changable_tax.each do |changable_tax|
        if changable_tax[0].downcase == "change"
          if changable_tax[1] == '0'
            @changable_tax = false
          end
        end
      end
    end

  end

def check_emp_access_code
  pin_code = params[:code]
  employee = Employee.find_by_emp_id(pin_code)
  if employee && (employee.role == "manager" || employee.role == "supervisor")
   result = {:status=> true, :message=>"Success" }
  else
    result = {:status=> false, :message=>"Invalid Access Code" }
  end
  render :json => result.to_json

end

private

def set_dining
   @dining = DiningFacility.find(params[:dining_id])
end

end
