class ShopPromotion < ActiveRecord::Base
    has_paper_trail
    self.table_name = "shop_promotions"

    has_and_belongs_to_many :depart_record

    validates :begin_time,:end_time,:depart_record,:name,:total, presence: true
    validate :product_validation
    attr_accessor :get_depart_enum

    def product_validation
      if self.send_prod1 > 0
        prd1 = Product.where("id = ?", self.send_prod1).first
        if prd1.blank?
          self.errors.add(:send_prod1,"赠品1不存在，请重新填写。")
        end
      end

      if self.send_prod2 > 0
        prd2 = Product.where("id = ?", self.send_prod2).first
        if prd2.blank?
          self.errors.add(:send_prod2,"赠品2不存在，请重新填写。")
        end
      end


      if self.send_prod3 > 0
        prd3 = Product.where("id = ?", self.send_prod3).first
        if prd3.blank?
          self.errors.add(:send_prod3,"赠品3不存在，请重新填写。")
        end
      end

    end
    def send_prodcut1
      prd1 = Product.where("id = ?", self.send_prod1).first
      if !prd1.blank?
        return format("%s-%d",prd1.name,self.send_nums1)
      else
        return "--"
      end
    end

    def send_prodcut2
      prd1 = Product.where("id = ?", self.send_prod2).first
      if !prd1.blank?
        return format("%s-%d",prd1.name,self.send_nums2)
      else
        return "--"
      end
    end

    def send_prodcut3
      prd1 = Product.where("id = ?", self.send_prod3).first
      if !prd1.blank?
        return format("%s-%d",prd1.name,self.send_nums3)
      else
        return "--"
      end
    end

    def send_prodcut4
      prd1 = Product.where("id = ?", self.send_prod4).first
      if !prd1.blank?
        return format("%s-%d",prd1.name,self.send_nums4)
      else
        return "--"
      end
    end


    def send_prodcut5
      prd1 = Product.where("id = ?", self.send_prod5).first
      if !prd1.blank?
        return format("%s-%d",prd1.name,self.send_nums5)
      else
        return "--"
      end
    end

    DEPART_ENUM =  []
    def get_depart_enum
      DEPART_ENUM.clear
      if DEPART_ENUM.length ==0
        departAttrs = DepartRecord.where("1 ").order("id desc")
        departAttrs.each do |attr|
            a=[attr.name,attr.id]
            DEPART_ENUM.push(a)
          end
      end
      return DEPART_ENUM
    end

    
    rails_admin do 

        navigation_label '促销管理'
        weight -100

        list do 
          filters [:id,:name,:begin_time,:end_time]
          field :id
          field :name
          field :begin_time
          field :end_time
          field :total do
            label "充值金额（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :depart_record
          field :send_prodcut1
          field :send_prodcut1
          field :send_prodcut2
          field :send_prodcut3
          field :send_prodcut4
          field :send_prodcut5
          field :cash do
            label "代办金（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :cent do
            label "积分（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :is_enable
          field :created_at
        end


        show do
          field :id      
          field :name
          field :begin_time
          field :end_time
      
          field :total do
            label "充值金额（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end

          field :depart_record
          field :send_prodcut1
          field :send_prodcut2
          field :send_prodcut3
          field :send_prodcut4
          field :send_prodcut5
          field :is_enable
          field :cash
          field :cent
          field :created_at
          field :created_at
          field :updated_at
        end

        edit do
          field :name
          field :begin_time
          field :end_time
          field :total
=begin
          field :depart, :enum do
            enum do
              bindings[:object].get_depart_enum
            end
          end
=end
          field :depart_record
          field :send_prod1
          field :send_nums1
          field :send_prod2
          field :send_nums2
          field :send_prod3
          field :send_nums3
          field :send_prod4
          field :send_nums4
          field :send_prod5
          field :send_nums5
          field :cash
          field :cent
          field :is_enable
        end
    end
end