# encoding: utf-8
require 'date' # 包含DateTime
require 'time'
class OrderStatic < ActiveRecord::Base
  has_paper_trail
  self.table_name = "order_statics"
  #after_create :order_static
  #after_update :order_static
  after_destroy :del_static

  validates :begin_date,presence: true
  def order_static
      #统计订单商品销售数据
      url = "#{CONFIG_FILE["api_host"]}/railsadmin/order/static/#{self.id}"
      p url
      open(url)
  end

  def del_static
    # 删除商品统计数据
    details = OrderStaticDetail.find_by_sql("select * from order_static_details where static_id = #{self.id}")
    # 删除
    details.each do |u|
      u.delete
    end
  end

  def begin_date_fomat

    if !self.begin_date.nil?
      return self.begin_date.strftime
    end
  end
  def end_date_fomat
    if !self.end_date.nil?
      return self.end_date.strftime
    end
  end

  STATUS_ENUM = [["处理中,已完成,待收货",""],["未支付","unpay"],["已关闭","closed"],["待确认","unconfirmed"],["处理中","processing"],["已完成","complete"],["待收货","dispatch"],["已退款","refunded"]]
  
  rails_admin do
    navigation_label '统计汇总'
    weight -500
    list do
      filters [:state,:is_send]
      field :id
      field :begin_date_fomat
      field :end_date_fomat
      field :state, :enum do
          enum do
            STATUS_ENUM
          end
      end
      field :is_send
      field :created_at
      field :updated_at
    end

    show do
      field :id
      field :begin_date_fomat
      field :end_date_fomat
      field :state, :enum do
        enum do
          STATUS_ENUM
        end
      end
      field :is_send
      field :created_at
      field :updated_at
    end

    edit do
      field :begin_date
      field :end_date
      field :state, :enum do
        enum do
          STATUS_ENUM
        end
      end
      field :is_send
    end
  end

end
