# encoding:utf-8
class OrderBaseDetail < ActiveRecord::Base
  has_paper_trail
  self.table_name = "order_base_details"
  validates :order_no,:order_dt_id,:product_id, presence: true
  # belongs_to :order, :foreign_key => :order_id

  def get_order_state(x)
    case x
    when "unpay"
      return "未支付"
    when "closed"
      return "已关闭"
    when "unconfirmed"
      return "待确认"
    when "processing"
      return "处理中"
    when "complete"
      return "已完成"
    when "dispatch"
      return "待收货"
    when "refunded"
      return "已退款"
    else
      return "--"
    end
  end


  def get_order_pay_way(x)
    case x
    when "weixinpay"
      return "微信支付"
    when "balance"
      return "提货券支付"
    when "cent"
      return "积分支付"
    else
      return "--"
    end
  end

  rails_admin do
    navigation_label '订单基本明细'
    weight -300
    parent Order
    list do
      filters [:order_no,:product_id,:order_dt_id]
      # include_all_fields
      field :id
      field :order_no
      field :product_id
      field :product_name
      field :nums
      field :order_dt_id
      field :dt_paid_time do
        filterable true
         formatted_value do
           #bindings[:object].order.paied_time
           if !bindings[:object].order.blank?
            (bindings[:object].order.paied_at == 0 || bindings[:object].order.paied_at == nil) ? Time.at(0) : Time.at(bindings[:object].order.paied_at)
           end
         end
      end

      field :order_dt_state do
        filterable true
        formatted_value do
          if !bindings[:object].order.blank?
            bindings[:object].get_order_state(bindings[:object].order.status)
            end
          end
      end

      field :dt_pay_way do
        filterable true
        formatted_value do
          if !bindings[:object].order.blank?
            bindings[:object].get_order_pay_way(bindings[:object].order.pay_way)
          end
        end
      end
      field :send
    end
    show do
      field :id
      field :order_no
      field :product_id
      field :product_name
      field :nums
      field :order_dt_id
      field :send
    end

    edit do
      include_all_fields
      field :order_no
      field :product_id
      field :product_name
      field :nums
      field :order_dt_id
      field :send
    end

    export do
      include_all_fields
      field :wx_user_id do
        formatted_value do
          bindings[:object].order.wx_user_id
        end
      end
      field :dt_paid_time do
        filterable true
        formatted_value do
          #bindings[:object].order.paied_time
          if !bindings[:object].order.blank?
            (bindings[:object].order.paied_at == 0 || bindings[:object].order.paied_at == nil) ? Time.at(0) : Time.at(bindings[:object].order.paied_at)
          end
        end
      end

      field :order_dt_state do
        filterable true
        formatted_value do
          if !bindings[:object].order.blank?
            bindings[:object].get_order_state(bindings[:object].order.status)
          end
        end
      end

      field :dt_pay_way do
        filterable true
        formatted_value do
          if !bindings[:object].order.blank?
            bindings[:object].get_order_pay_way(bindings[:object].order.pay_way)
          end
        end
      end
    end
  end
end
