# encoding:utf-8
class SignUpChannelQrcodeResult < ActiveRecord::Base
  has_paper_trail
  self.table_name = "sign_up_channel_qrcode_results"
  #小程序码
  belongs_to :sign_up_channel, :foreign_key => :sign_up_channel_id
  #扫码微信用户
  belongs_to :wx_user, :foreign_key => :wx_uid
  validates_presence_of :wx_uid,:sign_up_channel_id

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

  def user_tel
    return "-" if self.blank?
    wx_user = WxUser.where("id = ?", self.wx_uid).first
    return "-" if wx_user.blank?
    user = User.where("id = ?", wx_user.user_id).first
    return "-" if user.blank?
    return user.tel
  end

  rails_admin do
    navigation_label '用户管理'
    parent SignUpChannel
    weight -500

    list do
      filters [:sign_up_channel, :id]
      field :id
      field :wx_uid
      field :wx_user
      field :sign_up_channel_id
      field :sign_up_channel
      field :user_tel
      field :wx_user_nickname
      field :created_at
    end
    show do
      field :id
      field :wx_uid
      field :sign_up_channel_id
      field :user_tel
      field :wx_user_nickname
      field :created_at
    end

    edit do
      field :wx_uid
      field :sign_up_channel_id
    end
  end
end
