# == Schema Information
#
# Table name: issue_priorities
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  position   :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
# Indexes
#
#  index_issue_priorities_on_name  (name)
#

class IssuePriority < ApplicationRecord
  has_many :issues

  def self.init_data 
    map = {
      "1" => "低",
      "2" => "正常",
      "3" => "高", 
      "4" => "紧急"
    }
    IssuePriority.order(id: :asc).each do |prty| 
      if map["#{prty.id}"] == prty.name
        IssuePriority.find_or_create_by(id: prty.id, name: prty.name)
      else 
        Issue.where(priority_id: prty.id).each{|i| i.update_column(:priority_id, 2)}
        prty.destroy!
      end
    end
  end

  def to_builder 
    Jbuilder.new do |priority|
      priority.(self, :id, :name)
    end
  end

  def pm_color
    case name
    when '低'
      '#13b33e'
    when '正常'
      '#0d5ef8'
    when '高'
      '#ff6f00'
    when '紧急'
      '#d20f0f'
    # when '立刻'
    #   '#f5222d'
    else
      '#13b33e'
    end
  end
end
