# encoding:utf-8
require 'uuid'
class WxkuCommodity < ActiveRecord::Base
  has_paper_trail
  self.table_name = "wxku_commodities"
  before_save :before_save
  after_destroy :after_destroy
  belongs_to :product
  validates :product_id, :title,presence: true
  attr_accessor :v_image_info_1, :v_image_info_2, :v_image_info_3, :v_image_info_4, :v_image_info_5
  attr_accessor :is_delete_v_image_info_1, :is_delete_v_image_info_2, :is_delete_v_image_info_3, :is_delete_v_image_info_4, :is_delete_v_image_info_5
   
  IMG_STORE_PATH = "wxku_commodities" 

  #图书，女装，男装，美妆个护，母婴，食品生鲜，数码家电，珠宝配饰，鞋靴箱包，运动户外，家居百货，汽车用品
  OFFICIALCATEGORYINFO_ENUM = [
                                ["食品生鲜","食品生鲜"],
                                ["图书","图书"],
                                ["女装","女装"],
                                ["男装","男装"],
                                ["美妆个护","美妆个护"],
                                ["母婴","母婴"],
                                ["数码家电","数码家电"],
                                ["珠宝配饰","珠宝配饰"],
                                ["鞋靴箱包","鞋靴箱包"],
                                ["运动户外","运动户外"],
                                ["家居百货","家居百货"],
                                ["汽车用品","汽车用品"]]

  SOURCE_ENUM = [["平台自营",1],["非平台自营",0]]

  SALE_STATUS_ENUM = [["上架","on"],["下架","off"]]

  rails_admin do
    navigation_label '商品管理'
    weight -240

    list do
      filters [:id]
      field :id
      field :product
      field :image_info_1 do 
          formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_image_info_1,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_image_info_1}')"})
          end
      end
      field :image_info_2 do 
          formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_image_info_2,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_image_info_2}')"})
          end
      end
      field :category_info
      field :official_category_info, :enum do 
        enum do
          OFFICIALCATEGORYINFO_ENUM
        end
      end
      # field :url
      field :title
      field :sub_title
      field :brand
      field :source, :enum do
        enum do
          SOURCE_ENUM
        end
      end
      # field :desc
      field :min_price do
        label "现价最低价（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :max_price do
        label "现价最高价（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :min_ori_price do
        label "原价最低价（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :max_ori_price do
        label "原价最高价（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :sale_status, :enum do
        enum do
          SALE_STATUS_ENUM
        end
      end
      field :stock
      field :created_at
    end

    show do
      field :id
      field :product
      field :product_id
      field :image_info_1 do 
          formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_image_info_1,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_image_info_1}')"})
          end
      end
      field :image_info_2 do 
          formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_image_info_2,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_image_info_2}')"})
          end
      end
      field :image_info_3 do 
          formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_image_info_3,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_image_info_3}')"})
          end
      end
      field :image_info_4 do 
          formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_image_info_4,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_image_info_4}')"})
          end
      end
      field :image_info_5 do 
          formatted_value do
          bindings[:view].tag(:img,{:src => bindings[:object].get_image_info_5,
              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
              :onClick => "javascript:window.open('#{bindings[:object].get_image_info_5}')"})
          end
      end
      field :category_info
      field :official_category_info, :enum do 
        enum do
          OFFICIALCATEGORYINFO_ENUM
        end
      end
      field :url
      field :title
      field :sub_title
      field :brand
      field :source, :enum do
        enum do
          SOURCE_ENUM
        end
      end
      field :desc
      field :min_price do
        label "现价最低价（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :max_price do
        label "现价最高价（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :min_ori_price do
        label "原价最低价（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :max_ori_price do
        label "原价最高价（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :sale_status, :enum do
        enum do
          SALE_STATUS_ENUM
        end
      end
      field :stock
      field :status_ticket
      field :created_at
      field :updated_at
    end

    edit do
      field :product_id
      field :v_image_info_1, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_image_info_1, :class => 'preview'})
        end        
      end
      field :is_delete_v_image_info_1, :boolean do
        visible do
          bindings[:object].image_info_1 != "" && bindings[:object].image_info_1 != nil
        end
      end
      field :v_image_info_2, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_image_info_2, :class => 'preview'})
        end        
      end
      field :is_delete_v_image_info_2, :boolean do
        visible do
          bindings[:object].image_info_2 != "" && bindings[:object].image_info_2 != nil
        end
      end
      field :v_image_info_3, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_image_info_3, :class => 'preview'})
        end        
      end
      field :is_delete_v_image_info_3, :boolean do
        visible do
          bindings[:object].image_info_3 != "" && bindings[:object].image_info_3 != nil
        end
      end
      field :v_image_info_4, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_image_info_4, :class => 'preview'})
        end        
      end
      field :is_delete_v_image_info_4, :boolean do
        visible do
          bindings[:object].image_info_4 != "" && bindings[:object].image_info_4 != nil
        end
      end
      field :v_image_info_5, :file_upload do
        pretty_value do
          bindings[:view].tag(:img, {:src => bindings[:object].get_image_info_5, :class => 'preview'})
        end        
      end
      field :is_delete_v_image_info_5, :boolean do
        visible do
          bindings[:object].image_info_5 != "" && bindings[:object].image_info_5 != nil
        end
      end
      field :category_info
      field :official_category_info, :enum do 
        enum do
          OFFICIALCATEGORYINFO_ENUM
        end
      end
      field :url
      field :title
      field :sub_title
      field :brand
      field :source, :enum do
        enum do
          SOURCE_ENUM
        end
      end
      field :desc
      field :min_price do
        label "现价最低价（分）"
      end
      field :max_price do
        label "现价最高价（分）"
      end
      field :min_ori_price do
        label "原价最低价（分）"
      end
      field :max_ori_price do
        label "原价最高价（分）"
      end
      field :sale_status, :enum do
        enum do
          SALE_STATUS_ENUM
        end
      end
      field :stock
      field :status_ticket
    end
  end

  def v_image_info_1=file
    unless file.blank?
      clear_image_info_1
      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.image_info_1 = file_path
      self.save
    end
  end

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

  def get_image_info_1
    if self.image_info_1 != ""
      url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.image_info_1}"
      return url
    end
    return ""
  end

  def v_image_info_2=file
    unless file.blank?
      clear_image_info_2
      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.image_info_2 = file_path
      self.save
    end
  end

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

  def get_image_info_2
    if self.image_info_2 != ""
      url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.image_info_2}"
      return url
    end
    return ""
  end

  def v_image_info_3=file
    unless file.blank?
      clear_image_info_3
      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.image_info_3 = file_path
      self.save
    end
  end

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

  def get_image_info_3
    if self.image_info_3 != ""
      url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.image_info_3}"
      return url
    end
    return ""
  end

  def v_image_info_4=file
    unless file.blank?
      clear_image_info_4
      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.image_info_4 = file_path
      self.save
    end
  end

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

  def get_image_info_4
    if self.image_info_4 != ""
      url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.image_info_4}"
      return url
    end
    return ""
  end

  def v_image_info_5=file
    unless file.blank?
      clear_image_info_5
      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.image_info_5 = file_path
      self.save
    end
  end

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

  def get_image_info_5
    if self.image_info_5 != ""
      url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.image_info_5}"
      return url
    end
    return ""
  end

  def before_save
    if self.is_delete_v_image_info_1.to_i == 1
      clear_image_info_1
      self.image_info_1 = ""
    end
    if self.is_delete_v_image_info_2.to_i == 1
      clear_image_info_2
      self.image_info_2 = ""
    end
    if self.is_delete_v_image_info_3.to_i == 1
      clear_image_info_3
      self.image_info_3 = ""
    end
    if self.is_delete_v_image_info_4.to_i == 1
      clear_image_info_4
      self.image_info_4 = ""
    end
    if self.is_delete_v_image_info_5.to_i == 1
      clear_image_info_5
      self.image_info_5 = ""
    end
  end

  def after_destroy
    clear_image_info_1   
    clear_image_info_2
    clear_image_info_3
    clear_image_info_4
    clear_image_info_5  
  end

end
