# encoding:utf-8
require 'uuid'
class Article < ActiveRecord::Base
  has_paper_trail
  self.table_name = "articles"
  after_destroy :after_destroy
  belongs_to :article_cat
  validates :title,:content,:article_cat_id,presence: true
  attr_accessor :v_cover 
  attr_accessor :v_s_img
   
  IMG_STORE_PATH = "articles" 

  STATE_ENUM = [["是",1],["否",0]]
  DISPLAY_COVER_ENUM = [["显示",1],["不显示",0]]

  rails_admin do
    navigation_label '文章栏目管理'
    weight -240

    list do
      filters [:id,:source,:title]
      # include_all_fields
      field :id                                                                                         
      field :title
      field :subtitle
      field :about
      # field :content
      field :cover do
        formatted_value do
            bindings[:view].tag(:img, {:src => bindings[:object].get_img,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_img}')"})
          
        end
      end
      field :s_img do
        formatted_value do
            bindings[:view].tag(:img, {:src => bindings[:object].get_s_img,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_s_img}')"})
          
        end
      end
      field :source
      field :editor
      field :click
      field :sort
      field :recommend
      field :state
      field :show_cs_qrcode
      field :created_by
      # # field :category_id
      field :article_cat
      # field :agree
      # field :disagree
      # # field :comments_count
      # field :v_cover
      field :tags
      field :display_cover, :enum do
        enum do
          DISPLAY_COVER_ENUM
        end
      end      
      field :url
      field :share_benefit_desc
      # field :is_recommand
      field :created_at
      # field :updated_at
    end

    show do
      field :id                                                                                                              
      field :title
      field :subtitle
      field :about
      field :s_title 
      field :cover do
        formatted_value do
            bindings[:view].tag(:img, {:src => bindings[:object].get_img,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_img}')"})   
        end
      end
      field :s_img do
        formatted_value do
            bindings[:view].tag(:img, {:src => bindings[:object].get_s_img,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_s_img}')"})
          
        end
      end
      field :source
      field :editor
      field :click
      field :sort
      field :recommend
      field :state
      field :show_cs_qrcode
      field :created_by
      field :article_cat
      field :seo_title
      field :seo_keyword
      field :seo_desc
      field :tags
      field :display_cover, :enum do
        enum do
          DISPLAY_COVER_ENUM
        end
      end
      field :url      
      field :share_benefit_desc
      field :created_at
      field :updated_at
    end

    edit do
      field :title
      field :subtitle
      field :about
      field :s_title
      field :display_cover, :enum do
        enum do
          DISPLAY_COVER_ENUM
        end
      end
      field :url
      field :source
      field :editor
      field :sort
      field :recommend
      field :state
      field :show_cs_qrcode
      field :created_by
      field :article_cat
      field :seo_title
      field :seo_keyword
      field :seo_desc
      field :tags
      field :content, :ck_editor
      # field :cover
      field :v_cover, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_img, :class => 'preview'})
        end        
      end
      field :v_s_img, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_s_img, :class => 'preview'})
        end        
      end
      #field :share_benefit_min
      #field :share_benefit_max
      #field :share_benefit_must_follow
      #field :benefit_max
      #field :single_benefit_max
      #field :same_ip_limit
 
      # field :click
      # field :status
      # field :category_id
      # field :agree
      # field :disagree
      # field :comments_count
      # field :is_recommand
      field :url_guide, :ck_editor
      field :share_benefit_desc
    end
  end

  def v_cover=file
    unless file.blank?
      clear_img
      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.cover = file_path
      self.save
    end
  end

  def v_s_img=file
    unless file.blank?
      clear_s_img
      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.s_img = file_path
      self.save
    end
  end

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

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

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

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

  def after_destroy
    clear_img   
    clear_s_img     
  end

  # def cover=(file)
  #   unless file.blank?
  #     clear_cover
  #     _ = UUID.new.generate[0...8]
  #     Ali::Oss.store_article_cover(_, file.read)
  #     write_attribute :cover, _
  #   end
  # end


  # def cover(size='')
  #   _ = read_attribute(:cover)
  #   _.blank? ? '' : Ali::Oss.url_for_article_cover(_, size)
  # end

  # def clear_cover
  #   Ali::Oss.del_file_article_cover(read_attribute(:cover))
  # end

  # private

  # def resource_url
  #   self.cover
  # end

  # def after_create
  #   Search.create_index(Search::INDEX_NAME_OF_ARTICLES, self.id, self.title,
  #                       self.about, self.editor, self.source, self.created_at.to_i, "#{CONFIG_FILE["host"]}/article/#{self.id}?from=search", "文章")
  # end

  # def after_destroy
  #   #删除在阿里的索引
  #   Ali::Css.delete_doc(Search::INDEX_NAME_OF_ARTICLES, self.id)
  # end
end
