# encoding:utf-8
class ChannelQrcodeResult < ActiveRecord::Base
  has_paper_trail
  self.table_name = "channel_qrcode_results"
  belongs_to :channel_qrcode
  # belongs_to :wx_user, :foreign_key => :mp_openid
  validates_presence_of :mp_openid,:channel_qrcode

  def wx_user_nickname
    return "-" if self.blank?
    wx_user_gzh = WxUserGongzhonghao.where("gzh_open_id = ?", self.mp_openid).first
    return "-" if wx_user_gzh.blank?
    wx_user = WxUser.where("id = ?", wx_user_gzh.wx_user_id).first
    return "-" if wx_user.blank?
    return wx_user.nickname
  end

  def wx_user_id
    return "-" if self.blank?
    wx_user_gzh = WxUserGongzhonghao.where("gzh_open_id = ?", self.mp_openid).first
    if !wx_user_gzh.blank?
      return wx_user_gzh.wx_user_id
    end
  end

  def user_tel
    return "-" if self.blank?
    wx_user_gzh = WxUserGongzhonghao.where("gzh_open_id = ?", self.mp_openid).first
    return "-" if wx_user_gzh.blank?
    wx_user = WxUser.where("id = ?", wx_user_gzh.wx_user_id).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 ChannelQrcode
    weight -500

    list do
      filters [:channel_qrcode, :id]
      field :id
      field :channel_qrcode
      field :channel_qrcode_id
      # field :mp_openid
      field :user_tel
      field :wx_user_nickname
      field :wx_user_id
      field :created_at
    end
    show do
      field :id
      field :channel_qrcode
      field :channel_qrcode_id
      field :mp_openid
      field :user_tel
      field :wx_user_nickname
      field :wx_user_id
      field :created_at
    end

    edit do
      field :mp_openid
      field :channel_qrcode
    end
  end
end
