# encoding: utf-8
class FoodUnsub < ActiveRecord::Base
  self.table_name = "food_unsubs"
  validates :source,presence: true
  validate :employ_validation
  TYPE_ENUM = [["早餐",1],["中餐",2],["晚餐",3]]
  belongs_to :employ

  def employ_validation
    emp = Employ.where("id = ?", self.employ_id).first
      if !emp.blank?
        self.employ_name = emp.user_name
      end
      self.total = 0
      #self.save
  end

  rails_admin do
    navigation_label '食堂管理'
    weight -496
    list do
      filters [:source,:employ_name,:food_date]
      field :id
      field :employ_id
      field :employ_name
      field :food_date
      field :source, :enum do
        filterable true
        enum do
          TYPE_ENUM
        end
      end
      field :nums
      field :total do
        visible true
        label "金额(元)"
        formatted_value do
          value!=0 ? (value/100.0):0
        end
      end
      field :status
      field :remark
      # field :status, :enum do
      #   filterable true
      #   enum do
      #     STATUS_ENUM
      #   end
      # end
      field :created_at
      # field :updated_at
    end
    show do
      field :id
      field :employ_id
      field :employ_name
      field :food_date
      field :source, :enum do
        filterable true
        enum do
          TYPE_ENUM
        end
      end
      field :nums
      field :total do
        visible true
        label "金额(元)"
        formatted_value do
          value!=0 ? (value/100):0
        end
      end
      field :status
      field :remark
      # field :status, :enum do
      #   filterable true
      #   enum do
      #     STATUS_ENUM
      #   end
      # end
      field :created_at
    end
    edit do
      # field :employ
      field :employ_id
      field :food_date
      field :source, :enum do
        filterable true
        enum do
          TYPE_ENUM
        end
      end
    end
  end
end
