# == Schema Information
#
# Table name: products
#
#  id          :bigint           not null, primary key
#  sn          :string(255)
#  materiel_id :bigint           not null
#  status      :integer
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  lot_no      :string(255)
#  materiel_sn :string(255)
#
class Product < ApplicationRecord
  belongs_to :materiel
  has_many :product_records
  has_many :rma_records
  has_many :refund_records

  validates :sn, presence: true, uniqueness: true
  enum status: { initial: 0, sent: 1, tofix: 2, fixed: 3, dead: 4, refund: 7 }, _suffix: true
  STATUS_OPTIONS =  [["所有", ""], ["未发货", "0"], ["已发货", "1"], ["待维修", "2"], ["已修复", "3"], ["已报废", "4"], ["已退货", "7"]]

  before_create :init_before_create

  def status_name
    self.class.human_attribute_name("status.#{self.status}")
  end

  private
  def init_before_create
    self.materiel_sn = self.materiel.sn
  end

end
