class Origami::ProductCommissionsController < BaseOrigamiController
  before_action :set_product_commission, only: [:show, :edit, :update, :destroy]

  # GET /product_commissions
  # GET /product_commissions.json
  def index
    @product_commissions = ProductCommission.all
  end

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

  # GET /product_commissions/new
  def new
    @product_commission = ProductCommission.new
  end

  # GET /product_commissions/1/edit
  def edit
  end

  # POST /product_commissions
  # POST /product_commissions.json
  def create
    @product_commission = ProductCommission.new(product_commission_params)

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

  # PATCH/PUT /product_commissions/1
  # PATCH/PUT /product_commissions/1.json
  def update
    respond_to do |format|
      if @product_commission.update(product_commission_params)
        format.html {redirect_to @product_commission, notice: 'Product commission was successfully updated.'}
        format.json {render :show, status: :ok, location: @product_commission}
      else
        format.html {render :edit}
        format.json {render json: @product_commission.errors, status: :unprocessable_entity}
      end
    end
  end

  # DELETE /product_commissions/1
  # DELETE /product_commissions/1.json
  def destroy
    @product_commission.destroy
    respond_to do |format|
      format.html {redirect_to product_commissions_url, notice: 'Product commission was successfully destroyed.'}
      format.json {head :no_content}
    end
  end

  def load_commissioners
    sale_id = params[:sale_id]
    @table_id = params[:table_id]
    @saleobj = Sale.find(sale_id)

    @commissioners = []
  end

  def select_sale_item
    sale_item_id = params[:sale_item_id]
    @selected_sale_item = SaleItem.find_by_sale_item_id(sale_item_id)
    @product_commission = ProductCommission.find_by_sale_item_id(@selected_sale_item.id)
    unless @product_commission.nil?
      selected_commissioner = @product_commission.commissioner
    end
    @commissioners = Commissioner.active.all

    render json: {commissioner: @commissioners, selected_commissioner: selected_commissioner}
  end

  def set_commissioner_to_sale_item
    # byebug
    deselect        = false
    type            = nil
    sale_item_id    = params[:sale_item_id]
    commissioner_id = params[:commissioner_id]
    @sale_item      = SaleItem.find(sale_item_id)
    @menu_item      = MenuItem.find_by_item_code(@sale_item.product_code)
    # @commission = Commission.where('product_code = ? AND is_active = ?', @menu_item.item_code, true).take
     
    Commission.all.active.each do |com|
      if com.product_code != "[]"     
        if com.product_code.to_s.include? @menu_item.item_code
          @commission = Commission.find(com.id)
          break
        end
      end
    end
    @commissioner       = Commissioner.where('id = ? AND is_active = ?', commissioner_id, true).take
    @product_commission = ProductCommission.where('sale_item_id = ?', @sale_item.id).take

    if !@commission.nil? && @commissioner.commission_id == @commission.commission_id
      if !@product_commission.nil?
          if @product_commission.commissioner_id == @commissioner.id
            @product_commission.destroy
            deselect = true
            type      = "remove"
            # render json: {status: true,deselect:deselect, message: "Remove Success !"}
          else
            @product_commission.commissioner_id = @commissioner.id
            deselect = false
          end
      else
          @product_commission = ProductCommission.new
          @product_commission.product_code = @menu_item.item_code
          @product_commission.product_type = 'menu_item' # use for dummy data ToDo::need to change product type
          unless @commission.nil?
            @product_commission.commission_id = @commission.id
            if @commission.commission_type == 'Percentage'
              @product_commission.price = @sale_item.unit_price * (@commission.amount / 100.0)
              @product_commission.amount = @product_commission.price * @sale_item.qty
            elsif @commission.commission_type == 'Net Amount'
              @product_commission.price = @commission.amount
              @product_commission.amount = @product_commission.price * @sale_item.qty
            end
          end
          @product_commission.commissioner_id = @commissioner.id
          @product_commission.qty = @sale_item.qty
          @product_commission.sale_id = @sale_item.sale_id
          @product_commission.sale_item_id = @sale_item.sale_item_id
      end
      if @product_commission.save
        message = "Success !"
        if type == "remove"
          message = "Remove Success !"
        end
        render json: {status: true, deselect:deselect,type:type,message: message}
      else
        render json: {status: false, deselect:deselect,type:type,message: "No Commission"}
      end
    else
      render json: {status: false, deselect:deselect,type:type,message: "No Commission"}
    end
  end

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

  # Never trust parameters from the scary internet, only allow the white list through.
  def product_commission_params
    params.fetch(:product_commission, {})
  end

end
