class Induties::AssignInDutiesController < ApplicationController
  def index
    @commissioners =[]
    @commiss=Commissioner.active.all.order(created_at: :desc)
    @commiss.each do|commissioner|
      check_in_count =InDuty.where('commissioner_ids =? and out_time is null', commissioner.id.to_json).count;
      @commissioners.push({ "id"=>commissioner.id,
                            "name"=>commissioner.name,
                            "image_path"=>commissioner.image_path.url,
                            "check_in_count"=>check_in_count
                          })

    end
    @booking_id = params[:booking_id]

  end

  def get_all_occupied_dining
    commissionerIds =params[:id]
    @check_in_commissioner =InDuty.select("in_duties.*,dining_facilities.type as dinning_type")
          .joins(" LEFT JOIN dining_facilities on dining_facilities.id = in_duties.dinning_id").where('commissioner_ids =? and out_time is null', commissionerIds).order(created_at: :asc);
    respond_to do |format|
      format.json { render json: @check_in_commissioner.to_json }
    end
  end
  def induties_create
    commissioner_ids =params[:commissionerId]
    booking_id =params[:booking_id]
    dinning_id = params[:dinning_id]
    induty = InDuty.new
    induty.dinning_id  = dinning_id
    induty.booking_id = booking_id
    induty.commissioner_ids = commissioner_ids
    induty.by_name = params[:by_name]
    induty.in_time = Time.now
    if induty.save
      dinning = DiningFacility.find(induty.dinning_id)
      @induty= {"id"=>induty.id,
                "dinning_id"=>induty.dinning_id,
                "in_time"=>induty.in_time,
                "out_time"=>induty.out_time,
                "dinning_type"=>dinning.type }
      respond_to do |format|
      format.json { render json: @induty.to_json }
    end
    end
  end
  def induties_checkout
    induty_id = params[:induty_id]
    induty = InDuty.find(induty_id)
    induty.out_time = Time.current
    induty.save
    @induty =induty
    respond_to do |format|
      format.json { render json: @induty.to_json }
    end
  end
  def induties_remove
    induty_id = params[:induty_id]
    induty = InDuty.find(induty_id)
    induty.destroy
  end
  def get_current_booking
    @current_booking =InDuty.select("in_duties.*,dining_facilities.type as dinning_type")
          .joins(" LEFT JOIN dining_facilities on dining_facilities.id = in_duties.dinning_id").where('booking_id =? and out_time is null', params[:booking_id]).order(created_at: :asc);
    respond_to do |format|
      format.json { render json: @current_booking.to_json }
    end
  end

end
