# == Schema Information
#
# Table name: issue_statuses
#
#  id                 :integer          not null, primary key
#  name               :string(30)       default(""), not null
#  is_closed          :boolean          default("0"), not null
#  is_default         :boolean          default("0"), not null
#  position           :integer          default("1")
#  default_done_ratio :integer
#
# Indexes
#
#  index_issue_statuses_on_is_closed   (is_closed)
#  index_issue_statuses_on_is_default  (is_default)
#  index_issue_statuses_on_position    (position)
#

class IssueStatus < ApplicationRecord
  ADD      = 1
  SOLVING  = 2
  SOLVED   = 3
  # FEEDBACK = 4
  CLOSED   = 5
  REJECTED = 6

  has_many :issues
  belongs_to :project, optional: true

  def self.init_data 
    map = {
      "1" => "新增",
      "2" => "正在解决",
      "3" => "已解决", 
      "5" => "关闭",
      "6" => "拒绝"
    }
    IssueStatus.order(id: :asc).each do |stat| 
      if map["#{stat.id}"] == stat.name
        IssueStatus.find_or_create_by(id: stat.id, name: stat.name)
      else 
        Issue.where(status_id: stat.id).each{|i| i.update_column(:status_id, 1)}
        stat.destroy!
      end
    end
  end

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

  def pm_color
    case name
    when '新增'
      '#ff6f00'
    when '正在解决'
      '#0d5ef8'
    when '已解决'
      '#13b33e'
    when '关闭'
      '#b1aaa5'
    # when '反馈'
    #   '#13c2c2'
    when '拒绝'
      '#ff0000'
    else
      '#ff6f00'
    end
  end
end
