# encoding:utf-8
class PosterQrcodeRecord < ActiveRecord::Base
  has_paper_trail  
  self.table_name = "poster_qrcode_records"
  belongs_to :wx_user , :foreign_key => :wx_uid
  attr_accessor :v_qrcode_url
  attr_accessor :relate_title

  after_destroy :after_destroy

  IMG_STORE_PATH = "poster_qrcode_record"

  PTYPE_ENUM = [["商品","project"],["项目","poster"]]

  def relate_title
    return "-" if self.ptype != "project" && self.ptype != "poster"
    if self.ptype == "project"
      item = Product.where(" id = ?" , self.relate_id).first
      return "-" if item.blank?
      return item.name
    elsif self.ptype == "poster"
      item = Project.where(" id = ?" , self.relate_id).first
      return "-" if item.blank?
      return item.title
    end
  end

  def v_qrcode_url=file
    unless file.blank?
      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.qrcode_url = file_path
      self.save
    end
  end

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

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

  def after_destroy
    clear_qrcode_url
  end

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

    list do
      filters [:id, :wx_user]
      field :id
      field :wx_uid
      field :wx_user    
      field :parent_relate_id
      field :relate_title
      field :relate_id 
      field :ptype, :enum do
        enum do
          PTYPE_ENUM
        end
      end
      field :qrcode_url do
        formatted_value do
            bindings[:view].tag(:img, {:src => bindings[:object].get_qrcode_url,
           :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
           :onClick => "javascript:window.open('#{bindings[:object].get_qrcode_url}')"})
        end
      end
      field :scan_times
      field :created_at
    end

    show do
      field :id
      field :wx_uid
      field :wx_user    
      field :parent_relate_id
      field :relate_title
      field :relate_id 
      field :ptype, :enum do
        enum do
          PTYPE_ENUM
        end
      end
      field :scan_times
      field :qrcode_url do
        formatted_value do
            bindings[:view].tag(:img, {:src => bindings[:object].get_qrcode_url,
           :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
           :onClick => "javascript:window.open('#{bindings[:object].get_qrcode_url}')"})
        end
      end
      field :created_at
      field :updated_at
    end

    edit do
      field :wx_uid
      field :parent_relate_id
      field :relate_id 
      field :ptype
      field :scan_times
      field :v_qrcode_url, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_qrcode_url, :class => 'preview'})
        end
      end
    end
  end

end
