# encoding:utf-8
require 'uuid'
class WxGongzhonghao < ActiveRecord::Base
  has_paper_trail
  self.table_name = "wx_gongzhonghaos"
  # has_many :wx_menus
  
  validates :wx_hao,:wx_name,:app_id,presence: true

  after_create :after_create

  #生成token
  def after_create
    _token = UUID.new.generate(:compact)
    if self.token.blank?
      self.token = _token
      self.save()
    end
  end

  def wx_url
    "#{CONFIG_FILE['api_host']}/mp/#{self.app_id}"
  end

  WX_TYPE_ENUM = [["服务号", "service"], ["订阅号", "subscribe"]]
  rails_admin do
    navigation_label '公众号管理'
    weight -350

    list do
      filters [:wx_hao, :wx_name, :wx_type,:is_authed]
      field :id
      field :wx_hao
      field :wx_name
      field :is_authed
      field :wx_type, :enum do
        enum do
          WX_TYPE_ENUM
        end
      end
      # field :wx_menus
      field :wx_url
      field :token do
      end
      include_all_fields
      field :created_at do
        visible false
      end
      field :updated_at do
        visible false
      end
    end
    show do
      field :id
      field :wx_hao
      field :wx_name
      field :is_authed
      field :wx_type, :enum do
        enum do
          WX_TYPE_ENUM
        end
      end
      # field :wx_menus
      field :wx_url
      field :token do
      end
      include_all_fields
      field :created_at
      field :updated_at do
        visible false
      end
    end

    edit do
      include_all_fields
      field :wx_type, :enum do
        enum do
          WX_TYPE_ENUM
        end
      end
      field :token do
        visible false
      end
    end
  end
end
