# encoding:utf-8
class OrderDetail < ActiveRecord::Base
  has_paper_trail
  self.table_name = "order_details"
  belongs_to :product, :foreign_key => :product_id
  belongs_to :order, :foreign_key => :order_id

  def order_state
    order = Order.where("id = ?", self.order_id).first
    if !order.blank?
      return order.status
    end
  end

  def paid_time
    paidTime = 0
    order = Order.where("id = ?", self.order_id).first
    if !order.blank?
      paidTime=order.paied_at
    end
    return paidTime
  end

  def pay_way
    payWay = ""
    order = Order.where("id = ?", self.order_id).first
    if !order.blank?
      payWay=order.pay_way
    end
    return payWay
  end

  STATUS_ENUM = [["未支付","unpay"],["已关闭","closed"],["待确认","unconfirmed"],["处理中","processing"],["已完成","complete"],["待收货","dispatch"],["已退款","refunded"]]
  PAY_WAY_ENUM = [
      # ["Web网银连连支付","webbankllpay"],
      # ["Web认证连连支付","webauthllpay"],
      # ["Wap认证连连支付","wapauthllpay"],
      ["微信支付","weixinpay"],
      ["代金券支付","balance"],
      # ["公众号微信支付","gzhwxpay"],
      # ["公众号代金券支付","gzhbalance"],
      # ["统统微信支付","wx_sumpay"],
      # ["统统Web网银支付","webbanksumpay"],
  ]
  rails_admin do
    navigation_label '商品管理'
    weight -240
    parent Order
    
    list do   
      filters [:product,:order_no,:nums,:price,:order_state]
      field :id
      field :order_no do
        filterable true
      end
      field :product
      field :nums
      field :price do
        visible true
        label "单价(元)"
        formatted_value do
          value!=0 ? (value/100.0):0
        end
      end
      field :product_name
      field :order_state, :enum do
        filterable true
        enum do
          STATUS_ENUM
        end
      end
      field :paid_time do
        formatted_value do
          (value == 0 || value == nil) ? Time.at(0) : Time.at(value)
        end
      end
      field :pay_way, :enum do
        filterable true
        enum do
          PAY_WAY_ENUM
        end
      end
      field :is_zeng
      field :created_at
      field :updated_at
    end
    show do   
      field :id
      field :order_no
      field :product
      field :nums
      field :is_zeng
      field :price do
        visible true
        label "单价(元)"
        formatted_value do
          value!=0 ? (value/100.0):0
        end
      end
      field :product_name
      field :created_at
      field :updated_at
    end

    edit do
      field :order_no
      field :product_id
      field :nums
      field :is_zeng
      field :price
      field :product_name
    end
  end
end
