class Settings::DiningChargesController < ApplicationController
  before_action :set_dining_charge, only: [:show, :edit, :update, :destroy]
  before_action :set_settings_facility
  before_action :set_settings_zone
  # GET /dining_charges
  # GET /dining_charges.json
  def index
    @dining_charges = DiningCharge.all
  end

  # GET /dining_charges/1
  # GET /dining_charges/1.json
  def show
  end

  # GET /dining_charges/new
  def new
    @dining_charge = DiningCharge.new
    @dining_charge.minimum_free_time = "00:15:00".to_datetime
    @dining_charge.charge_block= "01:00:00".to_datetime
    @dining_charge.time_rounding_block="00:15:00".to_datetime
  end

  # GET /dining_charges/1/edit
  def edit
  end

  # POST /dining_charges
  # POST /dining_charges.json
  def create    
    @dining_charge = DiningCharge.new(dining_charge_params)
    @dining_charge.dining_facility_id = @settings_dining_facility.id
    @dining_charge.minimum_free_time = Time.zone.parse(dining_charge_params["minimum_free_time"])
    @dining_charge.charge_block = Time.zone.parse(dining_charge_params["charge_block"])
    @dining_charge.time_rounding_block = Time.zone.parse(dining_charge_params["time_rounding_block"])
    
    respond_to do |format|
      if @dining_charge.save
        if @table
          format.html { redirect_to settings_zone_table_path(@zone,@settings_dining_facility), notice: 'Dining charge was successfully created.' }
        else
          format.html { redirect_to settings_zone_room_path(@zone,@settings_dining_facility), notice: 'Dining charge was successfully created.' }
        end
        format.json { render :show, status: :created, location: @dining_charge }
      else
        format.html { render :new }
        format.json { render json: @dining_charge.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /dining_charges/1
  # PATCH/PUT /dining_charges/1.json
  def update
    respond_to do |format|
      @dining_charge.dining_facility_id = @settings_dining_facility.id
      @dining_charge.minimum_free_time = Time.zone.parse(dining_charge_params["minimum_free_time"])
      @dining_charge.charge_block = Time.zone.parse(dining_charge_params["charge_block"])
      @dining_charge.time_rounding_block = Time.zone.parse(dining_charge_params["time_rounding_block"])

      if @dining_charge.update(dining_charge_params)
        @dining_charge.minimum_free_time = Time.zone.parse(dining_charge_params["minimum_free_time"])
        @dining_charge.charge_block = Time.zone.parse(dining_charge_params["charge_block"])
        @dining_charge.time_rounding_block = Time.zone.parse(dining_charge_params["time_rounding_block"])
        # @dining_charge.minimum_free_time = @dining_charge.minimum_free_time.to_datetime.advance(hours: +6, minutes: +30)
        # @dining_charge.charge_block = @dining_charge.charge_block.to_datetime.advance(hours: +6, minutes: +30)
        # @dining_charge.time_rounding_block = @dining_charge.time_rounding_block.to_datetime.advance(hours: +6, minutes: +30)
        @dining_charge.save
        if @table
          format.html { redirect_to settings_zone_table_path(@zone,@settings_dining_facility), notice: 'Dining charge was successfully updated.' }
        else
          format.html { redirect_to settings_zone_room_path(@zone,@settings_dining_facility), notice: 'Dining charge was successfully updated.' }
        end
        format.json { render :show, status: :ok, location: @dining_charge }
      else
        format.html { render :edit }
        format.json { render json: @dining_charge.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /dining_charges/1
  # DELETE /dining_charges/1.json
  def destroy
    @dining_charge.destroy
    flash[:notice] = 'Dining charge was successfully destroyed.'
    render :json => {:status=> "Success", :url => dining_charges_url }.to_json
    # respond_to do |format|
    #   format.html { redirect_to dining_charges_url, notice: 'Dining charge was successfully destroyed.' }
    #   format.json { head :no_content }
    # end
  end

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

    def set_settings_facility
      if params[:table_id]
        @table = true
        @settings_dining_facility = Table.find(params[:table_id])
      elsif params[:room_id]
        @room = true
        @settings_dining_facility = Room.find(params[:room_id])
      end
    end

    def set_settings_zone
      @zone = Zone.find(params[:zone_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def dining_charge_params
      # params.fetch(:dining_charge, {})
      params.require(:dining_charge).permit(:item_code, :unit_price, :taxable, :charge_type,:minimum_free_time ,:charge_block,:time_rounding,:time_rounding_block, :zone_id, :time_rounding_block_price)
    end
end
