class LiveAward < ActiveRecord::Base
    has_paper_trail
    self.table_name = "live_awards"
    validates :openid, presence: true
    belongs_to :wx_user, :foreign_key => :wx_user_id
    before_save :update_user_info

    def update_user_info
      # 找出会员
      wxUser = WxUser.where("openid = ?", self.openid).first
      if !wxUser.blank?
        self.wx_user_id=wxUser.id
      end

    end

    def prodcut1
      prd1 = Product.where("id = ?", self.prod1).first
      if !prd1.blank?
        return format("%s-%d",prd1.name,self.nums1)
      else
        return "--"
      end
    end

    def prodcut2
      prd1 = Product.where("id = ?", self.prod2).first
      if !prd1.blank?
        return format("%s-%d",prd1.name,self.nums2)
      else
        return "--"
      end
    end


    def prodcut3
      prd1 = Product.where("id = ?", self.prod3).first
      if !prd1.blank?
        return format("%s-%d",prd1.name,self.nums3)
      else
        return "--"
      end
    end

    rails_admin do 

        navigation_label '直播管理'
        weight -99

        list do 
          filters [:id,:openid,:wx_user_id]
          field :id
          field :openid
          field :wx_user_id do
            filterable true
          end
          field :wx_user
          field :room_id
          field :prodcut1
          field :prodcut2
          field :prodcut3
          field :cash do
            label "代办金（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :cent do
            label "积分（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :status
          field :address
          field :created_at
        end
        
        show do
          field :id
          field :openid
          field :wx_user_id do
            filterable true
          end
          field :wx_user
          field :room_id
          field :prodcut1
          field :prodcut2
          field :prodcut3
          field :cash do
            label "代办金（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :cent do
            label "积分（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :status
          field :address
          field :contact
          field :tel
          field :created_at
          field :created_at
          field :updated_at
        end

        edit do
          field :openid
          field :wx_user_id
          field :room_id
          field :prod1
          field :nums1
          field :prod2
          field :nums2
          field :prod3
          field :nums3
          field :cash
          field :cent
          field :status
          field :address
          field :contact
          field :tel
        end
    end
end