class Settings::CommissionersController < ApplicationController
  load_and_authorize_resource except: [:create]
  before_action :set_commissioner, only: [:show, :edit, :update, :destroy]
  before_action :check_user

  # GET /commissioners
  # GET /commissioners.json
  def index
    @commissioners = Commissioner.all.order("id asc")
  end

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

  # GET /commissioners/new
  def new
    @commissioner = Commissioner.new
    @employee = Employee.all.order('name asc')
  end

  # GET /commissioners/1/edit
  def edit
  end

  # POST /commissioners
  # POST /commissioners.json
  def create
    @commissioner = Commissioner.new(commissioner_params)
    @commissioner.created_by = current_user.id
    unless @commissioner.joined_date.nil?
      @commissioner.joined_date = @commissioner.joined_date.utc.getlocal.strftime('%Y-%b-%d')
    end
    unless @commissioner.resigned_date.nil?
      @commissioner.resigned_date = @commissioner.resigned_date.utc.getlocal.strftime('%Y-%b-%d')
    end
    respond_to do |format|
      if @commissioner.save
        format.html {redirect_to settings_commissioners_path, notice: 'Commissioner was successfully created.'}
        format.json {render :show, status: :created, location: @commissioner}
      else
        format.html {render :new}
        format.json {render json: @commissioner.errors, status: :unprocessable_entity}
      end
    end
  end

  # PATCH/PUT /commissioners/1
  # PATCH/PUT /commissioners/1.json
  def update
    respond_to do |format|
      if @commissioner.update(commissioner_params)
        format.html {redirect_to settings_commissioner_path(@commissioner), notice: 'Commissioner was successfully updated.'}
        format.json {render :show, status: :ok, location: @commissioner}
      else
        format.html {render :edit}
        format.json {render json: @commissioner.errors, status: :unprocessable_entity}
      end
    end
  end

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

  def get_transaction_by_commissioner
    commissioner_id = params[:commissioner_id]
    @transactions = []
    @product_commissions = ProductCommission.where(commissioner_id: commissioner_id).order('updated_at desc')
    @product_commissions.each_with_index do |p, i|
      @transactions[i] = []
      @transactions[i] << p
      @transactions[i] << p.commission.menu_item.name
    end
    render json: @transactions
  end
  def check_user
    if current_user.nil?
      redirect_to root_path
    end
  end
  private
  # Use callbacks to share common setup or constraints between actions.
  def set_commissioner
    @commissioner = Commissioner.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def commissioner_params
    params.require(:commissioner).permit(:name, :emp_id, :created_by, :commission_id, :joined_date, :resigned_date, :is_active, :image_path)
  end
end
