# encoding:utf-8
class BalanceOrder < ActiveRecord::Base
  has_paper_trail
  self.table_name = "balance_orders"
  belongs_to :user
  belongs_to :wx_user
  validates :order_id,:user_id,:total_price,presence: true
  #after_create :update_paied_time
  validate :update_paied_time
  belongs_to :depart_record, :foreign_key => :depart

  def update_paied_time
    wxUser = WxUser.where("id = ?", self.wx_user_id).first
    if !wxUser.blank?
      self.depart = wxUser.depart
    end
    self.paied_at = Time.now.to_i

  end
  PAY_WAY_ENUM = [
    ["Web网银连连支付","webbankllpay"],
    ["Web认证连连支付","webauthllpay"],
    ["Wap认证连连支付","wapauthllpay"],
    ["微信支付","service_wxpay"],
    ["统统微信支付","wx_sumpay"],
    ["统统Web网银支付","webbanksumpay"],
  ]

  DEPART_ENUM= [["部门1",1], ["部门2",2], ["部门3", 3], ["部门4", 4], ["部门5", 5]]

  rails_admin do
    navigation_label '资金管理'
    parent Balance
    weight -920

    list do
      filters [:order_id,:state,:wx_user,:created_at,:depart]
      field :id
      field :wx_user_id
      field :wx_user
      field :pay_way ,:enum do
        enum do
          PAY_WAY_ENUM
        end
      end
      field :paied_at do
        visible false
        formatted_value do
          (value == 0 || value == nil) ? Time.at(0) : Time.at(value)
        end
      end
      field :order_id
      field :trade_no do 
        visible false
      end
      field :total_price do
        label "总额（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :paied_price do
        label "支付总额（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :state
      field :created_at
      field :updated_at
      field :balance_bank_card_no
      field :depart do
        visible false
        filterable true
      end
      field :depart_record
      field :remark
    end

    show do
      field :order_id
      field :user
      field :wx_user
      field :pay_way ,:enum do
        enum do
          PAY_WAY_ENUM
        end
      end
      field :paied_at do
        formatted_value do
          (value == 0 || value == nil) ? Time.at(0) : Time.at(value)
        end
      end
      field :total_price do
        label "总额（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :paied_price do
        label "支付总额（元）"
        formatted_value do # used in form views
          value.to_f / 100
        end
      end
      field :balance_bank_card_no
      field :remark
      field :depart do
        visible false
        filterable true
      end
      field :depart_record
      field :created_at
    end

    edit do
      field :order_id
      field :user_id
      field :wx_user_id
      field :pay_way ,:enum do
        enum do
          PAY_WAY_ENUM
        end
      end
      # field :paied_at
      field :trade_no
      field :paied_price do
        label "支付总额（分）"
      end
      field :total_price do
        label "总额（分）"
      end
      field :state
      field :balance_bank_card_no
      #field :depart
      field :remark
    end
  end
end
