class ProductAttrConfig < ActiveRecord::Base
    has_paper_trail
    self.table_name = 'product_attr_configs'
    belongs_to :product_attr_key, :foreign_key => :attr_key_id
    belongs_to :product

    validates :size_type,:product_id,:product_attr_key,presence:true
    attr_accessor :show_products

    PRODUCT_ENUM =  []
    def get_show_product
        PRODUCT_ENUM.clear
        if PRODUCT_ENUM.length==0
            @products = Product.where("show_flag=1").order("created_at desc")
            @products.each do |pd|
                a=[pd.name,pd.id]
                PRODUCT_ENUM.push(a)
            end
        end
        return PRODUCT_ENUM
    end


    TYPE_ENUM = [["规格一","size"],["规格二","color"]]
    rails_admin do
        navigation_label '商品管理'
        weight -250
        parent ProductAttrKey
        nestable_tree({
            position_field: :position,
            max_depth: 2
        })

        list do 
            filters [:product,:product_attr_key]
            field :id
=begin
            field :product_id, :enum do
                enum do
                  PRODUCT_ENUM
                end
            end
=end
            field :product
            field :product_id
            field :product_attr_key
            field :size_type, :enum do
                enum do
                    TYPE_ENUM
                end
            end
        end

        show do
            field :id
            field :product_id, :enum do
                enum do
                  PRODUCT_ENUM
                end
            end
            field :product_id
            field :product_attr_key
            field :size_type, :enum do
                enum do
                    TYPE_ENUM
                end
            end
        end

        edit do
=begin
            field :product_id, :enum do
                enum do
                  PRODUCT_ENUM
                end
            end
=end
            field :product_id, :enum do
                enum do
                    bindings[:object].get_show_product
                end
            end
            field :product_attr_key
            field :size_type, :enum do
                enum do
                    TYPE_ENUM
                end
            end
        end
    end

end