# encoding:utf-8
class XcxPushTmpl < ActiveRecord::Base
  has_paper_trail
  self.table_name = "xcx_push_tmpls"
  MSG_TYPE_ENUM = [
    ["物品状态提醒","MessageTemplateId_ItemStatusRemind"],
    ["众筹项目最新状态通知","MessageTemplateId_ProjectNewStateNofity"],
    ["提货通知","MessageTemplateId_DeliveryNotify"]
  ]
  EMPHASIS_ENUM = [
    ["内容1","keyword1.DATA"],
    ["内容2","keyword2.DATA"],
    ["内容3","keyword3.DATA"],
    ["内容4","keyword4.DATA"],
    ["内容5","keyword5.DATA"],
    ["无","-"],
  ]
  # def get_push_count
  #   if self.push_count.to_i >0
  #     return self.push_count 
  #   end
  #   if !self.user_id.blank?
  #     if self.user_id.start_with?("sql=select * ")
  #       sql = self.user_id.gsub("sql=select * ","select count(*) as sqlcount ")
  #       item = WxUser.find_by_sql(sql)
  #       if !item.blank?
  #         c = item[0].sqlcount
  #         if self.push_count == 0 
  #           self.push_count  = c
  #           self.save
  #         end
  #         return c
  #       end
  #     end
  #   end
  # end

  # def get_click_count
  #   if self.click_count.to_i >0
  #     return self.click_count 
  #   end
  #   if self.url.blank?
  #     return 
  #   end
  #   if self.url.start_with?("#{CONFIG_FILE["api_host"]}/v1/cfc/")
  #     size = "#{CONFIG_FILE["api_host"]}/v1/cfc/".size
  #     id = self.url[size..-1].to_i
  #     if id>0 
  #       item = ClickFromChannel.where("id=?",id).first
  #       if !item.blank?
  #         if item.click_times !=self.click_count 
  #           self.click_count  = item.click_times
  #           self.save
  #         end
  #         return item.click_times
  #       end
  #     end
  #   end
  # end

  rails_admin do
    navigation_label '推送管理'
    weight -220

    list do
      items_per_page 10
      filters [:id, :user_id]
      field :id
      field :user_id
      field :msg_type, :enum do
        enum do
          MSG_TYPE_ENUM
        end
      end
      field :push_force
      field :emphasis_keyword, :enum do
        enum do
          EMPHASIS_ENUM
        end
      end
      field :keyword1
      field :keyword2
      field :keyword3
      field :keyword4
      field :keyword5
      field :page
      field :last_updated_at
      field :times
      field :created_at
    end

    show do
      field :id
      field :user_id
      field :msg_type, :enum do
        enum do
          MSG_TYPE_ENUM
        end
      end
      field :push_force
      field :emphasis_keyword, :enum do
        enum do
          EMPHASIS_ENUM
        end
      end
      field :keyword1
      field :keyword2
      field :keyword3
      field :keyword4
      field :keyword5
      field :page
      field :last_updated_at
      field :times
      field :created_at
      field :updated_at
    end

    edit do
      field :user_id do
        label "微信用户ID(注意：填微信用户id(以逗号分隔)、填all(所有微信用户)、填allUser(所有注册用户)、填sql=xxx(xxx执行结果是WxUser结构体))"
      end
      field :msg_type, :enum do
        enum do
          MSG_TYPE_ENUM
        end
      end
      field :push_force
      field :emphasis_keyword, :enum do
        enum do
          EMPHASIS_ENUM
        end
      end
      field :keyword1
      field :keyword2
      field :keyword3
      field :keyword4
      field :keyword5
      field :page
      field :last_updated_at
      field :times
    end
  end

end
