class SimManagementsController < ApplicationController
  before_action :authenticate_user!
  before_action :dashboard_topbar
  load_and_authorize_resource :class => SimManagement
  skip_load_resource :only => [:new, :create]


  def index
    @sim_router_inventories = current_user.organisation.sim_managements
    @sim_plans = SimManagement.get_sim_plans
  end

  def new
    @sim_management = current_user.organisation.sim_managements.build
    @router_inventories = current_user.organisation.router_inventories.where.not(id: current_user.organisation.sim_managements.pluck(:router_inventory_id))
    @sim_router_inventories = @router_inventories.where.not(:sim_iccid=>nil)
  end

  def create
    @router_inventories = current_user.organisation.router_inventories.where.not(id: current_user.organisation.sim_managements.pluck(:router_inventory_id))
    @sim_router_inventories = @router_inventories.where.not(:sim_iccid=>nil)
    #@r_ids = RouterInventory.where(id: params["mac_id"])
    @sim_router_inventories.each do |mac_id|
      @sim_management = mac_id.build_sim_management rate_plan_id: plan_type 
      @sim_management.organisation_id = current_user.organisation_id
      @sim_management[:imei] = mac_id.sim_imei
      @sim_management[:iccid] = mac_id.sim_iccid
      # @sim_management[:plan_type] = SimManagement.get_rate_plans.select {|r| r[1] == params["sim_management"]["rate_plan_id"]}.first.first
      # @sim_management[:data_limit] = SimManagement.get_rate_plans.select {|r| r[1] == params["sim_management"]["rate_plan_id"]}.first.last
      @sim_management.save
    end
    #flash[:success] = "Plan was selected.Please make a payment"
    #redirect_to new_payment_invoice_path(:amount=> @sim_management.amount,:id =>@sim_management.id)
    # redirect_to invoice_sim_managements_path(:invoice_number=> @sim_management.invoice_number)
    # else 
    #   flash[:error] = "Oops! Problem in Purchasing Plan.Please try later"
    #   Rails.logger.warn "#{sim_managements.errors.full_messages}"
    #   redirect_to new_sim_management_path
    # end
  end 

  def download_csv
    sim_management_csv = SimManagement.download_csv
    respond_to do |format|
      format.html
      format.csv { send_data sim_management_csv, filename: "sim_management_details_#{Date.today}.csv" }
    end
  end

  def change_auto_recharge_state
    params
    render :json=> {:status=>false,:reload => false}.to_json and return if params[:id].blank?
    @sim_management =  SimManagement.where(id: params[:id]).last
    if @sim_management.present?
      if params[:auto_recharge] == "true"
        state = true 
      else 
       state = false
      end   
      @sim_management.update_attributes({auto_recharge: state})
      if @sim_management.save!
        render :json=> {:status=>'success',:msg=>'Successfully updated auto recharge status',:reload => true}.to_json and return
      else
        render :json=> {:status=>'error',:msg=>'Oops! Problem updating auto recharge status Please try after some time',:reload => false}.to_json and return
      end
    else  
    end  
  end


  def edit
    if params[:id].present?
      @sim_managements = SimManagement.where(id: params["id"])
    else
    end  
  end

  def change_plan
    @sim_managements = SimManagement.where(id: params["id"])
    if @sim_managements.update_attributes(sim_management_params)
      flash[:success] = "Sucessfully updated the plan"
      # redirect_to home_path @network
    else
      flash[:error] = "Oops! there was some problem in updating plan"
      Rails.logger.warn "#{@sim_managements.errors.full_messages}"
      render :action=> :edit
    end
  end

  def get_sim_rate_plan_details
    if params[:plan_id].present?
      @sim_details  = SimRatePlan.where(plan_id: params["plan_id"])
    end
    respond_to do |format|
      format.html
      format.json { render json: @sim_details }
    end
  end


  private
    def sim_management_params
      params.require(:sim_management).permit(:id,:plan_type,:rate_plan_id, :mac_id,:iccid,:router_inventory_id,:sim_rate_plan_id,:amount,:auto_recharge)
    end
   
end
