# encoding: utf-8
class SysConfig < ActiveRecord::Base
  has_paper_trail
  self.table_name = "sys_configs"
  validates :code,:config_type,:code_value, presence: true
  validates :code, uniqueness: true
  belongs_to :depart_record
  
  CONFIG_TYPE_ENUM= [["数值",1], ["百分比",2], ["字符串", 3]]

  def code_value_format
    case self.config_type
    when 1
      code_value_f = self.code_value.length<=0? "0":((Integer(self.code_value))/100.0)
      return code_value_f.to_s  + "元"
    when 2
      return self.code_value + "%"
    when 3
      return self.code_value
    else
      return self.code_value
    end
  end

  rails_admin do
    navigation_label '系统参数配置'
    weight -1000

    list do
      filters [:code]
      field :id
      field :code
      field :config_type, :enum do
        enum do
          CONFIG_TYPE_ENUM
        end
      end
      field :code_value_format
      field :depart_record
      field :descrption
      field :sort
      field :created_at
     end

    show do
      field :id
      field :code
      field :config_type, :enum do
        enum do
          CONFIG_TYPE_ENUM
        end
      end
      field :code_value_format
      field :depart_record
      field :descrption
      field :sort
    end

    edit do
      field :code
      field :config_type, :enum do
        enum do
          CONFIG_TYPE_ENUM
        end
      end
      field :code_value
      field :depart_record
      field :descrption
      field :sort
    end
  end
end
