# encoding: utf-8
class InviteBenefitOrder < ActiveRecord::Base
  has_paper_trail
  belongs_to :wx_user, :foreign_key => :benefit_wx_uid
  self.table_name = "invite_benefit_orders"  
  attr_accessor :wx_user_name, :ind_wx_user_name

  SOURCE_ENUM = [["店长申请佣金","shop_benefit"],["提货券充值佣金","balance_benefit"],["邀请绑定","binding"],["直推商品佣金","product_benefit"],["分享商品佣金","fx_product_benefit"],["项目佣金","project_benefit"]]

  def wx_user_name
    u = WxUser.where(" id = ?", self.wx_uid).first
    if !u.blank?
      return u.nickname
    end
    return "-"
  end

  def wx_user_copartner
    u = WxUser.where(" id = ?", self.benefit_wx_uid).first
    if !u.blank?
      return u.copartner_state
    end
    return "0"
  end

  def ind_wx_user_name
    u = WxUser.where(" id = ?", self.ind_wx_uid).first
    if !u.blank?
      return u.nickname
    end
    return "-"
  end
  
  rails_admin do
    navigation_label '资金管理'
    weight -500
    list do
      filters [:wx_user, :source,:relate_id]
      field :id
      field :benefit_wx_uid
      field :wx_user
      field :ind_wx_uid
      field :ind_wx_user_name
      field :count do
        label "佣金金额(元)"
        formatted_value do 
          value.to_f / 100
        end
      end
      field :amount do
        label "订单价格(元)"
        formatted_value do 
          value.to_f / 100
        end
      end
      field :source, :enum do
          enum do 
              SOURCE_ENUM
          end
      end
      field :relate_id
      field :is_enter_balance
      field :enter_time
      field :created_at
      field :updated_at
    end

    show do
      field :id
      field :benefit_wx_uid
      field :wx_user
      field :ind_wx_uid
      field :ind_wx_user_name
      field :count do
        label "佣金金额(元)"
        formatted_value do 
          value.to_f / 100
        end
      end
      field :amount do
        label "订单价格(元)"
        formatted_value do 
          value.to_f / 100
        end
      end
      field :source, :enum do
          enum do 
              SOURCE_ENUM
          end
      end
      field :relate_id
      field :is_enter_balance
      field :enter_time
      field :created_at
      field :updated_at
    end

    edit do       
      field :benefit_wx_uid
      field :ind_wx_uid
      field :count
      field :amount
      field :source, :enum do
          enum do 
              SOURCE_ENUM
          end
      end
      field :relate_id      
      field :is_enter_balance
      field :enter_time
    end
  end  
end
