# encoding:utf-8
class ChannelQrcode < ActiveRecord::Base
  has_paper_trail
  self.table_name = "channel_qrcodes"
  belongs_to :wx_gongzhonghao
  belongs_to :parent_signup_channel, :foreign_key => :parent_sign_up_channel_id
  belongs_to :push_after_sub
  validates :remark, :wx_gongzhonghao, presence: true
  attr_accessor :v_qrcode_img

  IMG_STORE_PATH = "channelqrcode"

  after_destroy :after_destroy
  after_create :after_create
  # after_update :after_update

  validate :generate_validation
  def generate_validation
    if (self.is_permanent == true) && (!self.expired_at.blank? && self.expired_at.to_i > 0)
      self.errors.add(:expired_at,"永久性和过期时间不能同时填写")
    elsif (self.is_permanent == false) && (self.expired_at.blank?) 
      self.errors.add(:expired_at,"永久性和过期时间不能同时为空")
    end
  end

  def v_qrcode_img=file
    unless file.blank?
      clear_qrcode_img
      file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
      file_path = "#{IMG_STORE_PATH}/thumb/#{file_name}"
      Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
      self.qrcode_img = file_path
      self.save
    end
  end

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

  def get_qrcode_img
    return self.qrcode_img
  end

  def after_destroy
    clear_qrcode_img
  end

  rails_admin do
    navigation_label '二维码管理'
    weight -500
    
    list do
      filters [:wx_gongzhonghao, :is_permanent, :remark]
      field :id
      field :wx_gongzhonghao
      field :scene_id
      field :scene_str
      field :remark
      field :qrcode_img do
        formatted_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_qrcode_img, :style => 'width:50px;cursor:pointer', :onClick => "javascript:window.open('#{bindings[:object].get_qrcode_img}')"})
        end
      end
      field :qrcode_img_url
      field :push_after_sub
      field :is_permanent
      field :scan_times
      field :parent_signup_channel
      field :expired_at
      field :ad_cost do
        label "投放广告费"
      end

    end

    show do
      field :id
      field :wx_gongzhonghao
      field :scene_id
      field :scene_str do
        label "渠道场景标识符"
      end
      field :remark
      field :qrcode_img do
        formatted_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_qrcode_img, :style => 'width:50px;cursor:pointer', :onClick => "javascript:window.open('#{bindings[:object].get_qrcode_img}')"})
        end
      end
      field :qrcode_img_url
      field :push_after_sub
      field :is_permanent
      field :scan_times
      field :parent_signup_channel
      field :expired_at
      field :created_at
    end

    edit do
      field :wx_gongzhonghao
      field :scene_str do
        label "渠道场景标识符"
      end
      field :remark
      field :v_qrcode_img, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_qrcode_img, :class => 'preview'})
        end
      end
      field :push_after_sub
      field :is_permanent
      field :parent_signup_channel
      field :expired_at   
      field :ad_cost
    end
  end

  def qrcode_img_url
    url = "#{CONFIG_FILE["api_host"]}/railsadmin/create_qrcode/#{self.id}"
    if !self.scene_str.blank?
      url = "#{CONFIG_FILE["api_host"]}/railsadmin/create_qrcode/scene_str/#{self.id}"
    end 
    if self.qrcode_img.blank?
      open(url)
    end
    return url
  end

  def after_create
    self.scene_id = self.id
    self.save
  end
end
