class Promotion < ActiveRecord::Base
    has_paper_trail
    self.table_name = "promotions"
    validates :begin_time,:end_time,:name,:order_type, presence: true
    has_and_belongs_to_many :depart_record
    has_and_belongs_to_many :products

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

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


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


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


    def prodcut5
      prd1 = Product.where("id = ?", self.prod5).first
      if !prd1.blank?
        return format("%s-%d",prd1.name,self.nums5)
      else
        return "--"
      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

    TYPE_ENUM = [["普通",0],["秒杀",1],["店长",2]]


    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 :is_first
          field :is_more
          field :depart_record
          field :order_type, :enum do
            filterable true
            enum do
              TYPE_ENUM
            end
          end
          field :min_total do
            label "订单最小金额（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :max_total do
            label "订单最大金额（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :prodcut1
          field :prodcut2
          field :prodcut3
          field :prodcut4
          field :prodcut5
          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 :products
          field :is_enable
          field :created_at
        end


        show do
          field :id      
          field :name
          field :begin_time
          field :end_time
          field :is_first
          field :is_more
          field :depart_record
          field :order_type, :enum do
            filterable true
            enum do
              TYPE_ENUM
            end
          end
          field :min_total do
            label "订单最小金额（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :max_total do
            label "订单最大金额（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :prodcut1
          field :prodcut2
          field :prodcut3
          field :prodcut4
          field :prodcut5
          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 :is_first
          field :is_more
          field :depart_record
          field :order_type, :enum do
            filterable true
            enum do
              TYPE_ENUM
            end
          end
          field :min_total
          field :max_total
          field :prod1
          field :nums1
          field :prod2
          field :nums2
          field :prod3
          field :nums3
          field :prod4
          field :nums4
          field :prod5
          field :nums5
          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 :products
          field :is_enable
        end
    end
end