class Origami::InDutiesController < BaseOrigamiController
  before_action :set_in_duty, only: %i[show edit update edit_in_duty update_for_in_duty destroy destroy_in_duty]
  # GET /in_duties
  # GET /in_duties.json
  def index
    @in_duties = InDuty.all
  end

  def index_in_duty
    @duty_in = InDuty.where('dinning_id=?', params[:table_id])
    @table = DiningFacility.find(params[:table_id])
    @in_duty = InDuty.new
    @duties_in = Kaminari.paginate_array(@duty_in).page(params[:page]).per(10)
  end

  # GET /in_duties/1
  # GET /in_duties/1.json
  def show; end

  # GET /in_duties/new
  def new
    # this one use for new
    @in_duty = InDuty.new
    @table = DiningFacility.find(params[:table_id])
    @commissioner = @in_duty.commissioner
    render partial: 'form'
  end

  # GET /in_duties/1/edit
  def edit; end

  def edit_in_duty
    @in_duty = InDuty.find(params[:id])
    @table = DiningFacility.find(params[:table_id])
    @commissioner = @in_duty.commissioner

    # render json: {in_duty: @in_duty, commissioner: @commissioner}
    render partial: 'form'
  end

  def assign_in_duty
    @in_duty = InDuty.new
    @table = DiningFacility.find(params[:table_id])
  end

  # POST /in_duties
  # POST /in_duties.json
  def create
    @in_duty = InDuty.new(in_duty_params)

    respond_to do |format|
      if @in_duty.save
        format.html { redirect_to origami_in_duties_path, notice: 'In duty was successfully created.' }
        format.json { render :show, status: :created, location: @in_duty }
      else
        format.html { render :new }
        format.json { render json: @in_duty.errors, status: :unprocessable_entity }
      end
    end
  end

  def create_for_in_duty
    # this one use for create and update
    in_duty = in_duty_params
    # in_time = DateTime.new in_duty['in_time(1i)'].to_i, in_duty['in_time(2i)'].to_i, in_duty['in_time(3i)'].to_i, in_duty['in_time(4i)'].to_i, in_duty['in_time(5i)'].to_i
    # in_time = in_time.change(offset: '+06:30')
    # out_time = DateTime.new in_duty['out_time(1i)'].to_i, in_duty['out_time(2i)'].to_i, in_duty['out_time(3i)'].to_i, in_duty['out_time(4i)'].to_i, in_duty['out_time(5i)'].to_i
    # out_time = out_time.change(offset: '+06:30')
    puts in_duty.to_json
    puts "sssssssssssssssssss"
    @in_duty = InDuty.new
    in_duty_id = in_duty[:id]
    unless in_duty_id.empty?
      @in_duty = InDuty.find(in_duty_id.to_i)
    end

    @in_duty.dinning_id = in_duty_params[:dinning_id]
    @in_duty.commissioner_ids = in_duty_params[:commissioner_ids]
    @in_duty.in_time = in_duty['in_time']
    @in_duty.out_time = in_duty['out_time']

    respond_to do |format|
      if @in_duty.save
        if in_duty_id.nil?
          format.html { redirect_to origami_index_in_duty_path, notice: 'In duty was successfully created.' }
          format.json { render :show, status: :created, location: @in_duty }
        else
          format.html { redirect_to origami_index_in_duty_path, notice: 'In duty was successfully updated.' }
          format.json { render :show, status: :created, location: @in_duty }
        end
      else
        format.html { render :new }
        format.json { render json: @in_duty.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /in_duties/1
  # PATCH/PUT /in_duties/1.json
  def update
    in_duty = in_duty_params
    in_time = DateTime.new in_duty['in_time(1i)'].to_i, in_duty['in_time(2i)'].to_i, in_duty['in_time(3i)'].to_i, in_duty['in_time(4i)'].to_i, in_duty['in_time(5i)'].to_i
    in_time = in_time.change(offset: '+06:30')
    out_time = DateTime.new in_duty['out_time(1i)'].to_i, in_duty['out_time(2i)'].to_i, in_duty['out_time(3i)'].to_i, in_duty['out_time(4i)'].to_i, in_duty['out_time(5i)'].to_i
    out_time = out_time.change(offset: '+06:30')
    @in_duty.commissioner_ids = in_duty_params[:commissioner_ids]
    @in_duty.in_time = in_time
    @in_duty.out_time = out_time
    respond_to do |format|
      if @in_duty.save
        format.html { redirect_to origami_index_in_duty_path(in_duty_params[:dinning_id]), notice: 'In duty was successfully updated.' }
        format.json { render :show, status: :ok, location: @in_duty }
      else
        format.html { render :edit }
        format.json { render json: @in_duty.errors, status: :unprocessable_entity }
      end
    end
  end

  def update_for_in_duty
    in_duty = in_duty_params
    in_time = DateTime.new in_duty['in_time(1i)'].to_i, in_duty['in_time(2i)'].to_i, in_duty['in_time(3i)'].to_i, in_duty['in_time(4i)'].to_i, in_duty['in_time(5i)'].to_i
    in_time = in_time.change(offset: '+06:30')
    out_time = DateTime.new in_duty['out_time(1i)'].to_i, in_duty['out_time(2i)'].to_i, in_duty['out_time(3i)'].to_i, in_duty['out_time(4i)'].to_i, in_duty['out_time(5i)'].to_i
    out_time = out_time.change(offset: '+06:30')
    @in_duty.commissioner_ids = in_duty_params[:commissioner_ids]
    @in_duty.in_time = in_time
    @in_duty.out_time = out_time
    respond_to do |format|
      if @in_duty.save
        format.html { redirect_to origami_index_in_duty_path(in_duty_params[:dinning_id]), notice: 'In duty was successfully updated.' }
        format.json { render :show, status: :ok, location: @in_duty }
      else
        format.html { render :edit }
        format.json { render json: @in_duty.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /in_duties/1
  # DELETE /in_duties/1.json
  def destroy
    @in_duty.destroy
    respond_to do |format|
      format.html { redirect_to origami_in_duties_path, notice: 'In duty was successfully removed.' }
      format.json { head :no_content }
    end
  end

  def destroy_in_duty
    @table_id = params[:table_id]
    @in_duty.destroy

    respond_to do |format|
      format.html { redirect_to origami_index_in_duty_path, notice: 'In duty was successfully removed.' }
      format.json { head :no_content }
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_in_duty
    @in_duty = InDuty.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def in_duty_params
    params.require(:in_duty).permit(:id, :dinning_id, :commissioner_ids, :in_time, :out_time)
  end
end
