class ItemsController < ApplicationController
  active_scaffold :"item" do |conf|
    conf.columns[:unit].form_ui = :select
    conf.columns[:sub_category].form_ui = :select
  end

  def check_availability
    @suppliers = Supplier.order(:name).all
    @selected_supplier = Supplier.find_by_id(params[:supplier_id])
    @items = @selected_supplier.try(:items) || []
    @codes_to_check = params[:codes] || ""
    @codes_to_check_array = @codes_to_check.split("\n").reject { |code| code.strip.blank? }.map(&:strip)
    items_to_check = @items.map { |i| i.code.strip }

    @missing_from_database =  (@codes_to_check_array - items_to_check) || []
    @missing_from_codes = (items_to_check - @codes_to_check_array) || []
  end
end
