# encoding:utf-8
class CentBalance < ActiveRecord::Base
  has_paper_trail
  self.table_name = "cent_balances"
  belongs_to :wx_user, :foreign_key => :wx_uid
  validates_presence_of :wx_uid, :count, :relate_id, :source
  attr_accessor :remain_sum

  SOURCE_ENUM = [["首次登录", "first_login"],
                 ["新人注册",  "new_man"],
                 ["签到第一天", "one_day"],
                 ["签到第二天", "two_day"],
                 ["签到第三天", "three_day"],
                 ["签到第四天", "four_day"],
                 ["签到第五天", "five_day"],
                 ["签到第六天", "six_day"],
                 ["签到第七天", "seven_day"],
                 ["购物扣除", "exchange_product"],
                 ["促销赠送", "promotion_send"],
               ]
  def remain_sum
    b = CentBalance.where("wx_uid = ? and id < ?", self.wx_uid, self.id).last
    if !b.blank?
      return self.count + b.remain_sum
    else
      return self.count
    end
  end
  rails_admin do
    navigation_label '资金管理'
    weight -920

    list do
      items_per_page 10
      filters [:id, :source, :wx_user, :relate_id, :created_at]
      # include_all_fields
      field :id
      field :wx_user
      field :wx_uid
      field :count do
        formatted_value do
          value.to_f / 100.0
        end
      end
      field :remain_sum do
        formatted_value do
          value.to_f / 100.0
        end
      end
      field :relate_id
      field :source, :enum do
        enum do
          SOURCE_ENUM
        end
      end
      field :remark
      field :created_at
      # field :updated_at
    end

    show do
      field :wx_uid
      field :count do
        formatted_value do # used in form views
          value.to_f / 100.0
        end
      end
      field :remain_sum do
        formatted_value do
          value.to_f / 100.0
        end
      end
      field :source, :enum do
        enum do
          SOURCE_ENUM
        end
      end
      field :relate_id
      field :remark
      field :created_at
    end

    edit do
      field :wx_uid
      field :count do
        label "变动金额，单位(分)"
      end
      field :relate_id
      field :source, :enum do
        enum do
          SOURCE_ENUM
        end
      end
      field :remark
    end
  end
end
