class Crm::DiningQueuesController < BaseCrmController
  load_and_authorize_resource
  before_action :set_dining_queue, only: [:show, :edit, :update, :destroy]

  # GET /crm/dining_queues
  # GET /crm/dining_queues.json
  def index
    today = DateTime.now.strftime('%Y-%m-%d')
     @dining_queues = DiningQueue.where("DATE_FORMAT(created_at,'%Y-%m-%d') = ? ", today).order("queue_no asc")
  end

  # GET /crm/dining_queues/1
  # GET /crm/dining_queues/1.json
  def show
  end

  # GET /crm/dining_queues/new
  def new
    @dining_queue = DiningQueue.new
    @queue_no     = DiningQueue.generate_queue_no
  end

  # GET /crm/dining_queues/1/edit
  def edit
  end

  # POST /crm/dining_queues
  # POST /crm/dining_queues.json
  def create
    @dining_queue = DiningQueue.new(dining_queue_params)
    
    respond_to do |format|
      if @dining_queue.save

      unique_code = "QueueNoPdf"   
      
      # get printer info
      print_settings = PrintSetting.find_by_unique_code(unique_code)

      printer = Printer::ReceiptPrinter.new(print_settings)   
      printer.print_queue_no(print_settings,@dining_queue)

        format.html { redirect_to crm_dining_queues_path, notice: 'Queue was successfully created.' }
        format.json { render :show, status: :created, location: @dining_queue }
      else
        format.html { render :new }
        format.json { render json: @dining_queue.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /crm/dining_queues/1
  # PATCH/PUT /crm/dining_queues/1.json
  def update
    respond_to do |format|
      if @dining_queue.update(dining_queue_params)
        format.html { redirect_to crm_dining_queues_path, notice: 'Queue was successfully updated.' }
        format.json { render :show, status: :ok, location: @dining_queue }
      else
        format.html { render :edit }
        format.json { render json: @dining_queue.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /crm/dining_queues/1
  # DELETE /crm/dining_queues/1.json
  def destroy
    @dining_queue.destroy
    respond_to do |format|
      format.html { redirect_to crm_dining_queues_path, notice: 'Dining queue was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def assign
    @queue = DiningQueue.find(params[:id])
    @tables = DiningFacility.where("status = 'available' ")
    respond_to do |format|
      format.html # index.html.erb
    end
  end

  def assign_table
    queue      = DiningQueue.find(params[:id])
    table_id   = params[:table_id]

    status = queue.update_attributes(dining_facility_id: table_id,status:"Assign")
    status = DiningFacility.find(table_id).update_attributes(status: "occupied")

  
    if status == true
      render json: JSON.generate({:status => true , notice: 'Dining queue was successfully assigned .'})
    else
      render json: JSON.generate({:status => false, :error_message => "Record not found"})
    end 
  end

  def cancel_queue
    queue      = DiningQueue.find(params[:id])

    status = queue.update_attributes(id: params[:id],status:"Cancel")
   
    if status == true
      render json: JSON.generate({:status => true , notice: 'Dining queue was successfully canceled .'})
    else
      render json: JSON.generate({:status => false, :error_message => "Record not found"})
    end 
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_dining_queue
      @dining_queue = DiningQueue.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def dining_queue_params
      params.require(:dining_queue).permit(:name, :contact_no, :queue_no,:status,:seater,:remark)
    end
end
