require 'uuid'
class PlatformCategory < ActiveRecord::Base
    has_paper_trail
    self.table_name = 'platform_categories'
    belongs_to :product, :foreign_key =>:product_id
    has_ancestry
    attr_accessor :v_cover
    validates :product_id,:name,:ancestry,:position,:platform,:url, presence: true

    IMG_STORE_PATH = "platform_categories" 

    URL_TYPE_ENUM = [["内部链接",0],["外部链接",1]]

    rails_admin do
        navigation_label '平台栏目管理'
        weight -240
        nestable_tree({
            position_field: :position,
            max_depth: 2
        })

        list do
            filters [:name,:position]
            field :id
            field :name
            field :platform
            field :position 
            field :ancestry
            field :url
            field :url_type, :enum do
                enum do 
                    URL_TYPE_ENUM
                end
            end

            field :cover do
                formatted_value do
                bindings[:view].tag(:img, {:src => bindings[:object].get_cover,
                :style => 'width: 100px;height: 100px;cursor: pointer;display:  block;max-width: 100px;',
                :onClick => "javascript:window.open('#{bindings[:object].get_cover}')"})
          
                end
            end
            field :state
            field :remark
            field :product
        end 

        show do 
            field :id
            field :name
            field :ancestry
            field :platform
            field :position
            field :url
            field :url_type, :enum do
                enum do 
                    URL_TYPE_ENUM
                end
            end
            field :cover do
                formatted_value do
                bindings[:view].tag(:img, {:src => bindings[:object].get_cover,
                :style => 'width: 100px;height: 100px;cursor: pointer;display:  block;max-width: 100px;',
                :onClick => "javascript:window.open('#{bindings[:object].get_cover}')"})
          
                end
            end
            field :state
            field :remark
            field :product
        end

        edit do 
            field :name
            field :platform
            field :position 
            field :ancestry
            field :url
            field :url_type, :enum do
                enum do 
                    URL_TYPE_ENUM
                end
            end
            field :v_cover,:file_upload do 
                pretty_value do 
                    bindings[:view].tag(:img, {:src => bindings[:object].get_cover, :class => 'preview'})
                end        
            end
            field :state
            field :remark
            field :product_id
        end

    end

    def v_cover=file
        unless file.blank?
          clear_cover
          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 clear_cover
        file_path = "#{self.cover}"
        Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
    end

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

    def after_destroy
      clear_cover       
    end

end