# encoding:utf-8
#会员表
require 'digest/md5'
class User < ActiveRecord::Base
  has_paper_trail
  self.table_name = "users"
  belongs_to :parent_signup_channel, :foreign_key => :signup_channel_id
  after_create :after_create
  after_destroy :after_destroy
  attr_accessor :sub, :wx_user, :v_head, :getNickname, :getProvince

  validates :tel , uniqueness: true
  # validates :pwd , :trade_pwd , presence: true
  # vaildates_numericality_of :tel

  # def wx_user
  #   wx_user = WxUser.where("user_id = ?", self.id).first
  #   if !wx_user.blank?
  #     return wx_user.nickname
  #   else
  #     return "未关联"
  #   end
  # end

  # def sub
  #   wx_user = WxUser.where("user_id = ?", self.id).first
  #   if !wx_user.blank?
  #     return wx_user.subscribe
  #   end    
  # end


  def after_create
    md5_trade_pwd = Digest::MD5.hexdigest(self.trade_pwd)
    self.trade_pwd = md5_trade_pwd
    self.save
  end

  SEX_ENUM= [["未知","0"], ["男性","1"], ["女性", "2"]]
  IMG_STORE_PATH = "user"

  rails_admin do
    navigation_label '用户管理'
    weight -400

    list do
      filters [:id,:tel]
      # include_all_fields
      field :id 
      field :nickname     
      field :getNickname
      field :real_name
      field :sex, :enum do
        enum do
          SEX_ENUM
        end
      end
      # field :age
      field :head do
        formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_head,
          :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
          :onClick => "javascript:window.open('#{bindings[:object].get_head}')"})
        end
      end
      field :tel
      field :email            
      field :province
      field :city
      field :signup_channel_id
      # field :sign_up_channel
      field :signup_ip
      field :invite_id
      field :age
      field :is_black_user
      field :created_at
      # field :updated_at
    end

    show do
      field :id
      field :nickname   
      field :real_name
      field :identity_card
      field :is_certification   
      field :tel
      field :email     
      # field :pwd
      # field :trade_pwd
      field :birthday
      field :city
      field :country
      field :province
      field :sex, :enum do
         enum do
           SEX_ENUM
         end
      end
      field :age
      field :head do
        formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_head,
          :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
          :onClick => "javascript:window.open('#{bindings[:object].get_head}')"})
        end
      end
      field :invite_id
      field :signup_channel_id
      field :signup_ip
      field :age
      field :is_black_user
      field :bank_name
      field :bank_account
      field :account_name
      field :created_at
      field :updated_at
    end


    edit do
      field :real_name
      field :identity_card
      field :is_certification
      field :tel 
      #field :pwd     
      field :trade_pwd
      field :birthday
      field :nickname
      field :city
      field :country
      field :province
      field :sex, :enum do
        enum do
          SEX_ENUM
        end
      end
      field :age
      field :v_head, :file_upload do
         pretty_value do
           bindings[:view].tag(:img, {:src => bindings[:object].get_head,
            :class => 'preview'})
         end
      end
      field :invite_id
      field :signup_ip
      field :signup_channel_id
      field :bank_name
      field :bank_account
      field :account_name
      field :is_black_user
    end
    
  end

  def get_head
    url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.head}"
    return url
  end

  def v_head=file
    unless file.blank?
      clear_head
      file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
      file_path = "#{IMG_STORE_PATH}/#{file_name}"
      Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
      self.head = file_path
      self.save
    end
  end

  def clear_head
    file_path = "#{self.head}"
    Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  end

  def after_destroy
    clear_head
  end

  def getProvince
    if self.province.blank?
      wx_user = WxUser.where("user_id =?", self.id).first
      if !wx_user.blank?
        self.signup_channel_id = wx_user.parent_signup_channel
         self.save
      end
    end
    return self.province
  end

  def getNickname

    needUpdate = false
    wxUser = WxUser.where("user_id = ?", self.id).first
    return "-" if wxUser.blank?

    if self.nickname == "" && wxUser.nickname != ""
      self.nickname = wxUser.nickname
      needUpdate = true if !needUpdate
    end
    if self.sex == 0 && wxUser.sex != 0
      self.sex = wxUser.sex
      needUpdate = true if !needUpdate
    end
    if self.country = "" && wxUser.country != ""
      self.country = wxUser.country
      needUpdate = true if !needUpdate
    end
    if self.province = "" && wxUser.province != ""
      self.province = wxUser.province
      needUpdate = true if !needUpdate
    end
    if self.city = "" && wxUser.city != ""
      self.city = wxUser.city
      needUpdate = true if !needUpdate
    end

    self.save if needUpdate
    return self.nickname
  end

end
