# encoding: utf-8
class DepartRecord < ActiveRecord::Base
  has_paper_trail
  self.table_name = "depart_records"
  belongs_to :wx_user, :foreign_key => :wx_user_id
  after_save :after_save

  def after_save
    wxUsers = WxUser.where("depart=? and rank=3",self.id).all
    wxUsers.each do |wuser|
      wuser.rank=2
      wuser.save
    end

    wxUser = WxUser.where("id = ?", self.wx_user_id).first
    if !wxUser.blank?
      wxUser.rank=3
      wxUser.save
    end
  end


  rails_admin do
    navigation_label '用户管理'
    weight -500
    #parent WxUser
    list do
      filters [:wx_user_id,:remark]
      field :id
      field :name
      field :wx_user_id do
        filterable true
      end
      field :wx_user
      field :pick
      field :pick_address
      field :pick_title
      field :remark
      field :created_at
      field :updated_at
    end

    show do
      field :id
      field :name
      field :wx_user_id do
        filterable true
      end
      field :wx_user
      field :remark
      field :pick
      field :pick_address
      field :pick_title
      field :created_at
      field :updated_at
    end

    edit do
      field :name
      field :wx_user_id
      field :pick
      field :pick_address
      field :pick_title
      field :remark
    end
  end
end
