require 'uuid'
class NavigateIconConfig < ActiveRecord::Base
    has_paper_trail
    self.table_name = 'navigate_icon_configs'
    attr_accessor :v_cover
    validates :name,:sort,:url, presence: true

    IMG_STORE_PATH = "navigate_icon_config" 

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

    rails_admin do
        navigation_label '平台栏目管理'
        weight -240

        list do
            filters [:name,:url_type]
            field :id
            field :name
            field :sort 
            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 :product_cat_index
            field :state
            field :app_state
            field :remark
        end 

        show do 
            field :id
            field :name
            field :sort
            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 :product_cat_index
            field :state
            field :app_state
            field :remark
        end

        edit do 
            field :name
            field :sort 
            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 :product_cat_index
            field :state
            field :app_state
            field :remark
        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