class Resume < ApplicationRecord
  belongs_to :user

  has_one :attachment, as: :container

  serialize :job_position, Array
  serialize :city, Array

  serialize :education, Array
  serialize :experience, Array
  serialize :job_category, Array

  SALARY_LIST = %w(1000元/月以下 1000—2000元/月 2001—4000元/月
                  4001—6000元/月 6001—8000元/月 8001—10000元/月
                  10001—15000元/月 15001—25000元/月 25001—35000元/月
                  35001—50000元/月 50001—70000元/月 70001—100000元/月 100000元/月以上)

  EDUCATION_LIST = %w(其他 大专 本科 硕士 博士)


  
  JOB_CATEGORY_LIST = %w(全职 兼职 实习)


  enum sex: ['男', '女']

  enum salary: SALARY_LIST


  validates :user_id, uniqueness: true
  validates :name, :sex, :birth_on, presence: { message: '不能为空' }
  validates :mobile, presence: true, format: { with: /(\A1[3|4|5|7|8|9][0-9]\d{8}\z)/, message: '请确认正确的手机号' }
  validates :job_position, :job_category, :city, :salary, :education, presence: { message: '不能为空' }



  def age
    birth = self.birth_on.to_datetime
    year_nums = Date.today.year - birth.year
    year_nums + (Date.today < (birth + year_nums.year) ? -1 : 0)
  end

  def work_year
    if self.job_year
      job = self.job_year.to_datetime
      year_nums = Date.today.year - job.year
      years = year_nums + (Date.today < (job + year_nums.year) ? -1 : 0)
      years > 0 ? years.to_s : '少于1'
    else
      ""
    end
  end


  def highest_education
    i = 0
    high = self.education[0]
    self.education.each do |d|
      if i < Resume::EDUCATION_LIST.index(d['edu_bg']).to_i
        i = Resume::EDUCATION_LIST.index(d['edu_bg'])
        high = d
      end
    end
    high
  end

end
