#encoding=UTF-8
require 'json'
require 'rest-client'
class Order < ActiveRecord::Base
    has_paper_trail
    self.table_name = 'orders'
    validates_presence_of :user_id,:total_price,:order_id
    #belongs_to :user, :foreign_key => :user_id
    belongs_to :wx_user, :foreign_key => :wx_user_id
    has_many :order_details
    before_save :before_save
    def before_save
        if self.express_company.length>0
            express = ExpressCompany.where("express_company = ?", self.express_company).first
            if !express.blank?
                self.express_code = express.express_code
            end
        end
    end

    STATUS_ENUM = [["未支付","unpay"],["已关闭","closed"],["待确认","unconfirmed"],["处理中","processing"],["已完成","complete"],["待收货","dispatch"],["已退款","refunded"]]
    TYPE_ENUM = [["普通",0],["秒杀",1]]
    EXP_ENUM =  []

    expressCompanys = ExpressCompany.order("id asc")
    expressCompanys.each do |exp|
        a=[exp.express_company,exp.express_company]
        EXP_ENUM.push(a)
    end

    PAY_WAY_ENUM = [

    ["微信支付","weixinpay"],
    ["代金券支付","balance"],
    ]

    SOURCE_ENUM = [["小程序","xcx"],["公众号","gzh"],["第五创系统","d5c_sys"]]

    def user_tel
      linkUser = User.where("id = ?", self.user_id).first
      if !linkUser.blank?
        if linkUser.is_black_user
            return linkUser.tel + " 【黑名单】"
        else
            return linkUser.tel
        end
      else
        return "不存在"
      end
    end

    def w_user
        if !wx_user.blank?
                return wx_user.nickname
        else
            return "--"
        end
    end


    def merchant 

        tmp = "-"
        prod = Product.where("id = ?", self.product_id).first
        if !prod.blank?

            merchant = Merchant.where("id = ?", prod.merchant_id).first
            if !merchant.blank?
                tmp = merchant.name
            end
        end
        return tmp

    end



    def total_price_format
        case self.pay_way
        when "weixinpay"
            total_price_f = self.total_price!=0 ? (self.total_price/100.0) :0
            return total_price_f.to_s #+ "元"
        when "balance"
            total_price_f = self.total_price!=0 ? (self.total_price/100.0) :0
            return total_price_f.to_s #+ "代金券"
        when "integral"
            total_price_f = self.total_price!=0 ? (self.total_price/100.0) :0
            return total_price_f.to_s #+ "代金券"
        end
    end

    def paied_price_format
        case self.pay_way
        when "weixinpay"
            paied_price_f = self.paied_price!=0 ? ((self.paied_price)/100.0) :0
            return paied_price_f.to_s # + "元"
        when "balance"
            paied_price_f = self.paied_price!=0 ? ((self.paied_price)/100.0) :0
            return paied_price_f.to_s #+ "代金券"
        when "integral"
            paied_price_f = self.paied_price!=0 ? ((self.paied_price)/100.0) :0
            return paied_price_f.to_s #+ "代金券"
        end
    end

    def coupon_price_format
        coupon_price_f = self.coupon_price!=0 ? ((self.coupon_price)/100.0) :0
        return coupon_price_f.to_s
    end


    rails_admin do 
        navigation_label '商品管理'
        weight -240

        list do 
            filters [:order_id,:status,:pay_way,:receive_time,:wx_user_id,:paied_time]
            field :id do
               visible false
            end
            field :order_id
            field :order_type, :enum do 
                filterable true
                enum do 
                    TYPE_ENUM
                end
            end
            field :user_id do
              visible false
              filterable true
            end
            field :wx_user_id do
                filterable true
            end
            field :w_user do
                # visible false
            end
            field :user_tel do
                # visible false
            end
            field :total_price do
                visible false
            end

            field :freight do
                visible true
                label "运费(元)"
                formatted_value do
                    value!=0 ? (value/100.0):0
                end
            end
            field :total_price_format do
                visible true
            end
            field :total_price_format do
                visible true
            end
            field :coupon_price_format
            field :paied_price_format do
                visible true
            end
            field :trade_no do
                visible false
            end
            field :status, :enum do
                enum do 
                    STATUS_ENUM
                end
            end

            field :pay_way, :enum do 
                enum do 
                    PAY_WAY_ENUM
                end
            end
            field :source, :enum do
                visible false
                enum do
                    SOURCE_ENUM
                end
            end
            field :paied_at do
                formatted_value do
                    (value == 0 || value == nil) ? Time.at(0) : Time.at(value)
                end
            end
            field :paied_time do
                visible false
            end
            field :contact
            field :tel 
            field :address
            field :express_company
            field :express_order_no
            field :dispatch_time
            field :receive_time do
                # visible false
            end
            field :order_remark do
                visible false
            end
            field :order_details
            field :remark
            field :created_at do
                visible false
            end
        end

        show do 
            field :id 
            field :order_id
            field :wx_user
            field :wx_user_id
            field :freight do
                visible true
                label "运费(元)"
                formatted_value do
                    value!=0 ? (value/100.0):0
                end
            end
            field :coupon_price_format
            field :paied_price_format
            field :buy_price
            field :trade_no
            field :status, :enum do
                enum do 
                    STATUS_ENUM
                end
            end
            field :pay_way, :enum do 
                enum do 
                    PAY_WAY_ENUM
                end
            end
            field :source, :enum do
                enum do
                    SOURCE_ENUM
                end
            end
            field :paied_at do
                formatted_value do
                    (value == 0 || value == nil) ? Time.at(0) : Time.at(value)
                end
            end
            field :tel 
            field :address
            field :contact
            field :express_company
            field :express_order_no
            field :remark
            field :order_remark
            field :dispatch_time
            field :receive_time
            field :order_details
            field :created_at
            field :updated_at
        end 
        
         edit do 
            field :order_id
            field :wx_user_id
            field :total_price
            field :paied_price
            field :paied_at
            field :buy_price
            field :freight
            field :trade_no
            field :status, :enum do
                enum do 
                    STATUS_ENUM
                end
            end
            field :pay_way, :enum do 
                enum do 
                    PAY_WAY_ENUM
                end
            end
            field :source, :enum do
                enum do
                    SOURCE_ENUM
                end
            end
            field :tel 
            field :address
            field :contact
            #field :express_company
            field :express_company, :enum do
                enum do
                    EXP_ENUM
                end
            end
            field :express_order_no
            field :remark
            field :order_remark
            field :dispatch_time
         end

        export do
            include_all_fields
            field :freight do
                visible true
                label "运费(元)"
                formatted_value do
                    value!=0 ? (value/100.0):0
                end
            end
            field :status, :enum do
                enum do
                    STATUS_ENUM
                end
            end
            field :pay_way, :enum do
                enum do
                    PAY_WAY_ENUM
                end
            end
            field :source, :enum do
                enum do
                    SOURCE_ENUM
                end
            end
            field :paied_at do
                formatted_value do
                    (value == 0 || value == nil) ? Time.at(0) : Time.at(value)
                end
            end
        end

    end 

end


