# encoding:utf-8
class TakeCashOrder < ActiveRecord::Base
  has_paper_trail
  self.table_name = "take_cash_orders"
  before_destroy :before_destroy_update

  belongs_to :wx_user, :foreign_key => :wx_uid
  
  STATE_ENUM = [["未审核", 0],["审核同意", 1], ["审核拒绝", 2]]
  REAL_ENUM = [["强制检验", 0],["取消检验", 1]]
  PAY_STATE_ENUM = [["未支付", 0],["已支付", 1],["提现失败", 2]]

  validates :wx_uid,:order_id,:count,presence: true

  def before_destroy_update
    tcOrder = TakeCashOrder.where("id=?",self.id).first
    if !tcOrder.nil? && tcOrder.audit_state == 1
      raise "审批通过的记录不允许删除！"
    end
  end

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

    list do
      filters [:wx_user,:order_id,:audit_state,:created_at]
      field :id
      field :wx_user
      field :wx_uid      
      field :order_id
      field :trade_no
      field :count do
        pretty_value do
          value/100.0
        end
      end
      field :fee do
        pretty_value do
          value/100.0
        end
      end
      field :pay_state , :enum do
        enum do
          PAY_STATE_ENUM
        end
      end  
=begin
      field :real_state , :enum do
        enum do
          REAL_ENUM
        end
      end
=end
      field :audit_state, :enum do
        enum do
          STATE_ENUM
        end
      end  
      field :paied_at do
        pretty_value do
          # value.to_i >0 ? Time.at(value.to_i) : ''
          (value == nil || value <=0) ? "" : Time.at(value)          
        end
      end
      #field :bank_name
      #field :bank_account
      #field :account_name
      field :expc_pay_at
      field :remark
      field :created_at
    end

    show do
      field :wx_user
      field :wx_uid      
      field :order_id
      field :trade_no
      field :count do
        pretty_value do
          value/100.0
        end
      end
      field :fee do
        pretty_value do
          value/100.0
        end
      end
      field :pay_state  , :enum do
        enum do
          PAY_STATE_ENUM
        end
      end 

      field :audit_state, :enum do
        enum do
          STATE_ENUM
        end
      end  
      field :paied_at do
        pretty_value do
          # value.to_i >0 ? Time.at(value.to_i) : ''
          (value == nil || value <=0) ? "" : Time.at(value)          
        end
      end
      # field :bank_name
      #field :bank_account
      #field :account_name
      field :expc_pay_at
      field :remark
    end

    edit do
      field :wx_uid do
        read_only do
          [1].include?(bindings[:object].audit_state)
        end
      end
      field :order_id do
        read_only do
          [1].include?(bindings[:object].audit_state)
        end
      end
      field :trade_no do
        read_only do
          [1].include?(bindings[:object].audit_state)
        end
      end
      field :count do
        read_only do
          [1].include?(bindings[:object].audit_state)
        end
      end
      field :pay_state , :enum do
        enum do
          PAY_STATE_ENUM
        end
        # read_only do
        #   [1].include?(bindings[:object].audit_state)
        # end
      end
      field :audit_state, :enum do
        enum do
          STATE_ENUM
        end
        # read_only do
        #   [1].include?(bindings[:object].audit_state)
        # end
      end  
      field :paied_at do
        label "支付时间，单位(秒)"
      end
      # field :bank_name
      #field :bank_account
      # field :account_name
      field :expc_pay_at
      field :remark
    end
    export do
      include_all_fields
      field :count do
        pretty_value do
          value/100.0
        end
      end
=begin
      field :bank_account do
        export_value do
          value != nil ? ("'"+value.to_s) : nil
        end
      end
=end
    end


    end
end
