# encoding:utf-8
class BaseDetail < ActiveRecord::Base
  has_paper_trail
  self.table_name = "base_details"
  belongs_to :product
  belongs_to :order, :foreign_key => :order_id
  # has_one :order, :foreign_key=>:order_id, :primary_key=>:order_id
  validates :product_id,:nums,:dt_id, presence: true
  validate :order_validation
  def order_validation
    if self.product_id > 0
      prd = Product.where("id = ?", self.product_id).first
      if prd.blank?
        self.errors.add(:product_id,"商品不存在，请重新填写商品ID")
      else
        self.product_name=prd.name
        #self.price=prd.price
      end
    end

    if self.dt_id > 0
      orDt = OrderDetail.where("id = ?", self.dt_id).first
      if orDt.blank?
        self.errors.add(:dt_id,"订单明细ID不存在，请重新填写明细ID")
      else
        self.order_no=orDt.order_no
        self.order_id=orDt.order_id
        #self.price=prd.price
      end
    end


  end

  def order_state
    order = Order.where("id = ?", self.order_id).first
    if !order.blank?
      return order.status
    end
  end
  
  STATUS_ENUM = [["未支付","unpay"],["已关闭","closed"],["待确认","unconfirmed"],["处理中","processing"],["已完成","complete"],["待收货","dispatch"],["已退款","refunded"]]


  def paid_time
    order = Order.where("id = ?", self.order_id).first
    if !order.blank?
      paidTime=order.paied_at == 0 || order.paied_at == nil ? Time.at(0) : Time.at(order.paied_at)
      return paidTime
    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"],
      ["通联支付","allinpay"],
      ["提货券支付","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_status]
      field :id
      field :order_no do
        filterable true
      end
      #field :order
      #field :product
      field :product do
        visible false
      end
      field :nums
      field :price do
        visible true
        label "单价(元)"
        formatted_value do
          value!=0 ? (value/100.0):0
        end
      end
      field :product_name
      field :order_status, :enum do
        filterable true
        enum do
          STATUS_ENUM
        end
      end
      # field :paid_time do
      #   filterable true
      #   formatted_value do
      #     #bindings[:object].order.paied_time
      #     (bindings[:object].order.paied_at == 0 || bindings[:object].order.paied_at == nil) ? Time.at(0) : Time.at(bindings[:object].order.paied_at)
      #   end
      # end
      field :paid_time
      field :pay_way, :enum do
        filterable true
        enum do
          PAY_WAY_ENUM
        end
      end
     field :order_status, :enum do
        enum do
          STATUS_ENUM
        end
      end
      field :is_zeng
      #field :size_name
      #field :color_name
      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 :size_name
      #field :color_name
      field :created_at
      field :updated_at
    end

    edit do
      # field :order_no
      #field :order_id
      field :dt_id
      field :product_id
      field :nums
      field :is_zeng
      field :price
    end
    export do
      include_all_fields
      field :wx_user_id do
        formatted_value do
          bindings[:object].order.wx_user_id
        end
      end
      field :nums
      field :product_name

    end
  end
end
