class OrderRefund < ActiveRecord::Base
    has_paper_trail
    self.table_name = "order_refunds"
    validates :order_id, presence: true
    belongs_to :wx_user, :foreign_key => :wx_user_id
    before_save :before_save

    def before_save
      order = Order.where("order_id = ?", self.order_id).first
      if !order.blank?
        self.wx_user_id = order.wx_user_id
        self.total = order.total_price
        self.refund_fee = order.paied_price
        self.transaction_id = order.trade_no
      end
    end

    rails_admin do
      navigation_label '订单退款记录'
      weight -500
      parent Order
        list do 
          filters [:id,:order_id]
          field :id
          field :order_id
          field :wx_user
          field :transaction_id
          field :total do
            label "订单金额（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :refund_fee do
            label "退款金额（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :refund_time do
            pretty_value do
              # value.to_i >0 ? Time.at(value.to_i) : ''
              (value == nil || value <=0) ? "" : Time.at(value)
            end
          end
          field :status
          field :remark
          field :created_at
        end

        show do
          field :id
          field :order_id
          field :wx_user
          field :transaction_id
          field :total
          field :total do
            label "订单金额（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :refund_fee do
            label "退款金额（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :refund_time do
            pretty_value do
              # value.to_i >0 ? Time.at(value.to_i) : ''
              (value == nil || value <=0) ? "" : Time.at(value)
            end
          end
          field :status
          field :remark
          field :created_at
          field :updated_at
        end

        edit do
          field :order_id
          field :remark
        end
    end
end