# encoding:utf-8
class ShareInfo < ActiveRecord::Base
  has_paper_trail
  self.table_name = "share_infos"
  validates_presence_of :share_code, :relate_id, :relate_parent, :wx_user, :share_to, :ip
  belongs_to :wx_user, :foreign_key => :wx_user_id
  attr_accessor :relate_title

  def relate_title
    return "-" if self.share_code != "article" && self.share_code != "product_sale" && self.share_code != "xcx_product" && share_code != "xcx_project"

    if self.share_code == "article"
      item = Article.where(" id = ?" , self.relate_id).first
      return "-" if item.blank?
      return item.title
    elsif self.share_code == "product_sale"
      item = ProductSaleSubject.where(" id = ?" , self.relate_id).first
      return "-" if item.blank?
      return item.subject_title
    elsif self.share_code == "xcx_product"
      item = Product.where(" id = ?" , self.relate_id).first
      return "-" if item.blank?
      return item.name
    elsif self.share_code == "xcx_project"
      item = Project.where(" id = ?" , self.relate_id).first
      return "-" if item.blank?
      return item.title
    end
  end

  SHARE_CODE_ENUM = [
    ["文章分享","article"],
    ["商品销售专题","product_sale"],
    ["小程序首页","xcx_home"],
    ["我的推广页面","xcx_invite"],
    ["我的推广正确转发页面","xcx_invite_detail"],
    ["小程序商品","xcx_product"],
    ["小程序项目","xcx_project"],
    ["小程序排行榜","xcx_rank"]
  ]
  SHARE_TO_ENUM = [
    ["朋友圈","timeline"],
    ["群","group"],
  ]
  
  rails_admin do
    navigation_label '分享管理'
    weight -750

    list do
      filters [:id, :wx_user, :relate_id, :ip]
      field :id
      # field :wx_user_id
      field :wx_user
      field :relate_title
      field :relate_id
      field :relate_parent
      field :share_code, :enum do
        enum do
          SHARE_CODE_ENUM
        end
      end
      field :share_to, :enum do
        enum do
          SHARE_TO_ENUM
        end
      end
      field :ip
      field :share_times
      field :share_latest_time
      field :created_at
      field :updated_at
    end

    show do
      field :id
      field :wx_user
      field :relate_id
      field :relate_title
      field :relate_parent
      field :share_code, :enum do
        enum do
          SHARE_CODE_ENUM
        end
      end
      field :share_to, :enum do
        enum do
          SHARE_TO_ENUM
        end
      end
      field :ip
      field :share_times
      field :share_latest_time
      field :created_at
      field :updated_at
    end

    edit do
      field :wx_user
      field :relate_id
      field :relate_parent
      field :share_code, :enum do
        enum do
          SHARE_CODE_ENUM
        end
      end
      field :share_to, :enum do
        enum do
          SHARE_TO_ENUM
        end
      end
      field :ip
      field :share_times
      field :share_latest_time
    end
  end

end